How to pass in an OpenAI API key when not using environment variables?
theMK2k opened this issue · 2 comments
theMK2k commented
Hi,
how do I pass in an OpenAI API key when not using environment variables?
I'm trying to get the "chat with pdf" example to work without the enviroment variable and it gives an error when running upsertIntoVectorIndex
.
OpenAI API key is missing. Pass it using the 'apiKey' parameter or set it as an environment variable named OPENAI_API_KEY.
snippet of my code:
async function createEmbeddings(openAiApiKey: string) {
const embeddingModel = new OpenAITextEmbeddingModel({
model: 'text-embedding-ada-002',
// TODO: openAiApiKey - where to put it?
});
// NOTE: a desperate attempt at putting the api key in...
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const settings: any = embeddingModel.settings;
settings.apiKey = openAiApiKey;
const pages = await loadPdfPages(path);
const chunks = await splitTextChunks(
splitAtToken({
maxTokensPerChunk: 256,
tokenizer: embeddingModel.tokenizer,
}),
pages,
);
// here we'll face the error
await upsertIntoVectorIndex({
vectorIndex,
embeddingModel,
objects: chunks,
getValueToEmbed: chunk => chunk.text,
});
lgrammel commented
You can use a custom api configuration:
const api = new OpenAIApiConfiguration({
apiKey: "my-api-key", // optional; default: process.env.OPENAI_API_KEY
// ...
});
const model = new OpenAIChatModel({
api,
// ...
});
See https://modelfusion.dev/integration/model-provider/openai#configuration for more options (e.g. Azure)