View Source LangChain.ChatModels.ChatOpenAI (LangChain v0.1.0)

Represents the OpenAI ChatModel.

Parses and validates inputs for making a requests from the OpenAI Chat API.

Converts responses into more specialized LangChain data structures.

Summary

Functions

Calls the OpenAI API passing the ChatOpenAI struct with configuration, plus either a simple message or the list of messages to act as the prompt.

Return the params formatted for an API request.

Setup a ChatOpenAI client configuration.

Setup a ChatOpenAI client configuration and return it or raise an error if invalid.

Types

@type call_response() ::
  {:ok, LangChain.Message.t() | [LangChain.Message.t()]} | {:error, String.t()}
@type callback_data() ::
  {:ok,
   LangChain.Message.t()
   | LangChain.MessageDelta.t()
   | [LangChain.Message.t() | LangChain.MessageDelta.t()]}
  | {:error, String.t()}
@type t() :: %LangChain.ChatModels.ChatOpenAI{
  endpoint: term(),
  frequency_penalty: term(),
  model: term(),
  n: term(),
  receive_timeout: term(),
  stream: term(),
  temperature: term()
}

Functions

Link to this function

call(openai, prompt, functions \\ [], callback_fn \\ nil)

View Source

Calls the OpenAI API passing the ChatOpenAI struct with configuration, plus either a simple message or the list of messages to act as the prompt.

Optionally pass in a list of functions available to the LLM for requesting execution in response.

Optionally pass in a callback function that can be executed as data is received from the API.

NOTE: This function can be used directly, but the primary interface should be through LangChain.Chains.LLMChain. The ChatOpenAI module is more focused on translating the LangChain data structures to and from the OpenAI API.

Another benefit of using LangChain.Chains.LLMChain is that it combines the storage of messages, adding functions, adding custom context that should be passed to functions, and automatically applying LangChain.MessageDelta structs as they are are received, then converting those to the full LangChain.Message once fully complete.

Link to this function

for_api(openai, messages, functions)

View Source
@spec for_api(t(), message :: [map()], functions :: [map()]) :: %{
  required(atom()) => any()
}

Return the params formatted for an API request.

@spec new(attrs :: map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}

Setup a ChatOpenAI client configuration.

@spec new!(attrs :: map()) :: t() | no_return()

Setup a ChatOpenAI client configuration and return it or raise an error if invalid.