Skip to content

OpenAI

When using OpenAi models, llm-exe will make POST requests to https://api.openai.com/v1/chat/completions. All models are supported if you pass openai.chat.v1 as the first argument, and then specify a model in the options.

Basic Usage

OpenAi Chat

ts
const llm = useLlm("openai.chat.v1", {
  model: "gpt-4o", // specify a model
});

OpenAi Chat By Model

ts
const llm = useLlm("openai.gpt-4o", {
  // other options,
  // no model needed, using gpt-4o
});

NOTE

You can use the following models using this shorthand:

  • openai.gpt-4o
  • openai.gpt-4o-mini

Authentication

To authenticate, you need to provide an OpenAi API Key. You can provide the API key various ways, depending on your use case.

  1. Pass in as execute options using openAIApiKey
  2. Pass in as setup options using openAIApiKey
  3. Use a default key by setting an environment variable of OPEN_AI_API_KEY

Generally you pass the LLM instance off to an LLM Executor and call that. However, it is possible to interact with the LLM object directly, if you wanted.

ts
// given array of chat messages, calls chat completion
await llm.chat([]);

// given string prompt, calls completion
await llm.completion("");

TIP

Note: The OpenAILlm checks to make sure you are using the correct prompt type when using chat vs completion, and will throw an error if you try to use the wrong prompt type with the wrong model.

OpenAi-Specific Options

In addition to the generic options, the following options are OpenAi-specific and can be passed in when creating a llm function.

OptionTypeDefaultDescription
modelstringgpt-4o-miniThe model to use. Can be any valid chat model. See OpenAI Docs
openAIApiKeystringundefinedAPI key for OpenAi. See authentication
temperaturenumberundefinedMaps to temperature.*
maxTokensnumberundefinedMaps to max_tokens. See OpenAI Docs
topPnumberundefinedMaps to top_p. See OpenAI Docs
nnumberundefinedMaps to n. See OpenAI Docs
streambooleanundefinedSee OpenAI Docs. Note: Not supported yet.
stop?undefinedMaps to stop. See OpenAI Docs
presencePenaltynumberundefinedMaps to presence_penalty. See OpenAI Docs
frequencyPenaltynumberundefinedMaps to frequency_penalty. See OpenAI Docs
logitBiasobjectundefinedMaps to logit_bias. See OpenAI Docs
userstringundefinedMaps to user. See OpenAI Docs

* OpenAI Docs: link