vercel / modelfusion

The TypeScript library for building AI applications.
https://modelfusion.dev
MIT License
1.06k stars 76 forks source link

How to pass in an OpenAI API key when not using environment variables? #149

Closed theMK2k closed 9 months ago

theMK2k commented 9 months ago

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 9 months ago

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)

theMK2k commented 9 months ago

Thanks @lgrammel :)