Find answers from the community

Updated 3 months ago

API Key Programmatically

hi, is it possible to programmatically set OpenAI API key while initializing llamaindex?
E
n
W
10 comments
Plain Text
    service_context = ServiceContext.from_defaults(
        llm_predictor=LLMPredictor(llm=OpenAI(model_name='text-davinci-003', temperature=0, openai_api_key='keyhere'))
    )
then you can get from your API body or etc...
is it possible in typescript implementation
Plain Text
const openaiLLM = new OpenAI({ model: "gpt-3.5-turbo", temperature: 0, apiKey: 'your_api_key' });

const serviceContext = serviceContextFromDefaults({ llm: openaiLLM });
sorry, where should I use this piece of code?
for example, in this starter code: "// example.ts
import fs from "fs/promises";
import { Document, VectorStoreIndex } from "llamaindex";

async function main() {
// Load essay from abramov.txt in Node
const essay = await fs.readFile(
"node_modules/llamaindex/examples/abramov.txt",
"utf-8",
);

// Create Document object with essay
const document = new Document({ text: essay });

// Split text and create embeddings. Store them in a VectorStoreIndex
const index = await VectorStoreIndex.fromDocuments([document]);

// Query the index
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query(
"What did the author do in college?",
);

// Output response
console.log(response.toString());
}

main();"

Where should I provide OpenAI API key?
Plain Text
  const openaiLLM = new OpenAI({ model: "gpt-3.5-turbo", temperature: 0, apiKey: 'your_api_key' });

  const serviceContext = serviceContextFromDefaults({ llm: openaiLLM });

  const index = await VectorStoreIndex.fromDocuments([document], {
     serviceContext
  });
You can set it as environment variable also.
Follow this link: https://ts.llamaindex.ai/installation#environment-variables
I am unable to test it because an error from tiktoken prevents me from able to test the the code
Add a reply
Sign up and join the conversation on Discord