defmodule LangChain.ChatModels.ChatOpenAI do @moduledoc """ Represents the [OpenAI ChatModel](https://platform.openai.com/docs/api-reference/chat/create). Parses and validates inputs for making a requests from the OpenAI Chat API. Converts responses into more specialized `LangChain` data structures. - https://github.com/openai/openai-cookbook/blob/main/examples/How_to_call_functions_with_chat_models.ipynb """ use Ecto.Schema require Logger import Ecto.Changeset import LangChain.Utils.ApiOverride alias __MODULE__ alias LangChain.Config alias LangChain.ChatModels.ChatModel alias LangChain.Message alias LangChain.LangChainError alias LangChain.ForOpenAIApi alias LangChain.Utils alias LangChain.MessageDelta @behaviour ChatModel # NOTE: As of gpt-4 and gpt-3.5, only one function_call is issued at a time # even when multiple requests could be issued based on the prompt. # allow up to 1 minute for response. @receive_timeout 60_000 @primary_key false embedded_schema do field :endpoint, :string, default: "https://api.openai.com/v1/chat/completions" # field :model, :string, default: "gpt-4" field :model, :string, default: "gpt-3.5-turbo" # API key for OpenAI. If not set, will use global api key. Allows for usage # of a different API key per-call if desired. For instance, allowing a # customer to provide their own. field :api_key, :string # What sampling temperature to use, between 0 and 2. Higher values like 0.8 # will make the output more random, while lower values like 0.2 will make it # more focused and deterministic. field :temperature, :float, default: 1.0 # Number between -2.0 and 2.0. Positive values penalize new tokens based on # their existing frequency in the text so far, decreasing the model's # likelihood to repeat the same line verbatim. field :frequency_penalty, :float, default: 0.0 # Duration in seconds for the response to be received. When streaming a very # lengthy response, a longer time limit may be required. However, when it # goes on too long by itself, it tends to hallucinate more. field :receive_timeout, :integer, default: @receive_timeout # Seed for more deterministic output. Helpful for testing. # https://platform.openai.com/docs/guides/text-generation/reproducible-outputs field :seed, :integer # How many chat completion choices to generate for each input message. field :n, :integer, default: 1 field :json_response, :boolean, default: false field :stream, :boolean, default: false field :max_tokens, :integer, default: nil end @type t :: %ChatOpenAI{} @create_fields [ :endpoint, :model, :temperature, :frequency_penalty, :api_key, :seed, :n, :stream, :receive_timeout, :json_response, :max_tokens ] @required_fields [:endpoint, :model] @spec get_api_key(t()) :: String.t() defp get_api_key(%ChatOpenAI{api_key: api_key}) do # if no API key is set default to `""` which will raise a OpenAI API error api_key || Config.resolve(:openai_key, "") end @spec get_org_id() :: String.t() | nil defp get_org_id() do Config.resolve(:openai_org_id) end @doc """ Setup a ChatOpenAI client configuration. """ @spec new(attrs :: map()) :: {:ok, t} | {:error, Ecto.Changeset.t()} def new(%{} = attrs \\ %{}) do %ChatOpenAI{} |> cast(attrs, @create_fields) |> common_validation() |> apply_action(:insert) end @doc """ Setup a ChatOpenAI client configuration and return it or raise an error if invalid. """ @spec new!(attrs :: map()) :: t() | no_return() def new!(attrs \\ %{}) do case new(attrs) do {:ok, chain} -> chain {:error, changeset} -> raise LangChainError, changeset end end defp common_validation(changeset) do changeset |> validate_required(@required_fields) |> validate_number(:temperature, greater_than_or_equal_to: 0, less_than_or_equal_to: 2) |> validate_number(:frequency_penalty, greater_than_or_equal_to: -2, less_than_or_equal_to: 2) |> validate_number(:n, greater_than_or_equal_to: 1) |> validate_number(:receive_timeout, greater_than_or_equal_to: 0) end @doc """ Return the params formatted for an API request. """ @spec for_api(t, message :: [map()], functions :: [map()]) :: %{atom() => any()} def for_api(%ChatOpenAI{} = openai, messages, functions) do %{ model: openai.model, temperature: openai.temperature, frequency_penalty: openai.frequency_penalty, n: openai.n, stream: openai.stream, messages: Enum.map(messages, &ForOpenAIApi.for_api/1), response_format: set_response_format(openai) } |> Utils.conditionally_add_to_map(:max_tokens, openai.max_tokens) |> Utils.conditionally_add_to_map(:seed, openai.seed) |> Utils.conditionally_add_to_map(:functions, get_functions_for_api(functions)) end defp get_functions_for_api(nil), do: [] defp get_functions_for_api(functions) do Enum.map(functions, &ForOpenAIApi.for_api/1) end defp set_response_format(%ChatOpenAI{json_response: true}), do: %{"type" => "json_object"} defp set_response_format(%ChatOpenAI{json_response: false}), do: %{"type" => "text"} @doc """ 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. """ @impl ChatModel def call(openai, prompt, functions \\ [], callback_fn \\ nil) def call(%ChatOpenAI{} = openai, prompt, functions, callback_fn) when is_binary(prompt) do messages = [ Message.new_system!(), Message.new_user!(prompt) ] call(openai, messages, functions, callback_fn) end def call(%ChatOpenAI{} = openai, messages, functions, callback_fn) when is_list(messages) do if override_api_return?() do Logger.warning("Found override API response. Will not make live API call.") case get_api_override() do {:ok, {:ok, data} = response} -> # fire callback for fake responses too Utils.fire_callback(openai, data, callback_fn) response # fake error response {:ok, {:error, _reason} = response} -> response _other -> raise LangChainError, "An unexpected fake API response was set. Should be an `{:ok, value}`" end else try do # make base api request and perform high-level success/failure checks case do_api_request(openai, messages, functions, callback_fn) do {:error, reason} -> {:error, reason} parsed_data -> {:ok, parsed_data} end rescue err in LangChainError -> {:error, err.message} end end end # Make the API request from the OpenAI server. # # The result of the function is: # # - `result` - where `result` is a data-structure like a list or map. # - `{:error, reason}` - Where reason is a string explanation of what went wrong. # # If a callback_fn is provided, it will fire with each # When `stream: true` is # If `stream: false`, the completed message is returned. # # If `stream: true`, the `callback_fn` is executed for the returned MessageDelta # responses. # # Executes the callback function passing the response only parsed to the data # structures. # Retries the request up to 3 times on transient errors with a 1 second delay @doc false @spec do_api_request(t(), [Message.t()], [Function.t()], (any() -> any())) :: list() | struct() | {:error, String.t()} def do_api_request(openai, messages, functions, callback_fn, retry_count \\ 3) def do_api_request(_openai, _messages, _functions, _callback_fn, 0) do raise LangChainError, "Retries exceeded. Connection failed." end def do_api_request( %ChatOpenAI{stream: false} = openai, messages, functions, callback_fn, retry_count ) do req = Req.new( url: openai.endpoint, json: for_api(openai, messages, functions), auth: {:bearer, get_api_key(openai)}, receive_timeout: openai.receive_timeout, retry: :transient, max_retries: 3, retry_delay: fn attempt -> 300 * attempt end ) req |> maybe_add_org_id_header() |> Req.post() # parse the body and return it as parsed structs |> case do {:ok, %Req.Response{body: data}} -> case do_process_response(data) do {:error, reason} -> {:error, reason} result -> Utils.fire_callback(openai, result, callback_fn) result end {:error, %Mint.TransportError{reason: :timeout}} -> {:error, "Request timed out"} {:error, %Mint.TransportError{reason: :closed}} -> # Force a retry by making a recursive call decrementing the counter Logger.debug(fn -> "Mint connection closed: retry count = #{inspect(retry_count)}" end) do_api_request(openai, messages, functions, callback_fn, retry_count - 1) other -> Logger.error("Unexpected and unhandled API response! #{inspect(other)}") other end end def do_api_request( %ChatOpenAI{stream: true} = openai, messages, functions, callback_fn, retry_count ) do Req.new( url: openai.endpoint, json: for_api(openai, messages, functions), auth: {:bearer, get_api_key(openai)}, receive_timeout: openai.receive_timeout ) |> maybe_add_org_id_header() |> Req.post(into: Utils.handle_stream_fn(openai, &do_process_response/1, callback_fn)) |> case do {:ok, %Req.Response{body: data}} -> data {:error, %LangChainError{message: reason}} -> {:error, reason} {:error, %Mint.TransportError{reason: :timeout}} -> {:error, "Request timed out"} {:error, %Mint.TransportError{reason: :closed}} -> # Force a retry by making a recursive call decrementing the counter Logger.debug(fn -> "Mint connection closed: retry count = #{inspect(retry_count)}" end) do_api_request(openai, messages, functions, callback_fn, retry_count - 1) other -> Logger.error( "Unhandled and unexpected response from streamed post call. #{inspect(other)}" ) {:error, "Unexpected response"} end end # Parse a new message response @doc false @spec do_process_response(data :: %{String.t() => any()} | {:error, any()}) :: Message.t() | [Message.t()] | MessageDelta.t() | [MessageDelta.t()] | {:error, String.t()} def do_process_response(%{"choices" => choices}) when is_list(choices) do # process each response individually. Return a list of all processed choices for choice <- choices do do_process_response(choice) end end def do_process_response( %{ "finish_reason" => "function_call", "message" => %{"function_call" => %{"arguments" => raw_args, "name" => name}} } = data ) do case Message.new(%{ "role" => "assistant", "function_name" => name, "arguments" => raw_args, "complete" => true, "index" => data["index"] }) do {:ok, message} -> message {:error, changeset} -> {:error, Utils.changeset_error_to_string(changeset)} end end def do_process_response( %{"delta" => delta_body, "finish_reason" => finish, "index" => index} = _msg ) do status = case finish do nil -> :incomplete "stop" -> :complete "length" -> :length "function_call" -> :complete other -> Logger.warning("Unsupported finish_reason in delta message. Reason: #{inspect(other)}") nil end function_name = case delta_body do %{"function_call" => %{"name" => name}} -> name _other -> nil end arguments = case delta_body do %{"function_call" => %{"arguments" => args}} when is_binary(args) -> args _other -> nil end # more explicitly interpret the role. We treat a "function_call" as a a role # while OpenAI addresses it as an "assistant". Technically, they are correct # that the assistant is issuing the function_call. role = case delta_body do %{"role" => role} -> role _other -> "unknown" end data = delta_body |> Map.put("role", role) |> Map.put("index", index) |> Map.put("status", status) |> Map.put("function_name", function_name) |> Map.put("arguments", arguments) case MessageDelta.new(data) do {:ok, message} -> message {:error, changeset} -> {:error, Utils.changeset_error_to_string(changeset)} end end def do_process_response(%{ "finish_reason" => finish_reason, "message" => message, "index" => index }) do status = case finish_reason do "stop" -> :complete "length" -> :length other -> Logger.warning("Unsupported finish_reason in message. Reason: #{inspect(other)}") nil end case Message.new(Map.merge(message, %{"status" => status, "index" => index})) do {:ok, message} -> message {:error, changeset} -> {:error, Utils.changeset_error_to_string(changeset)} end end def do_process_response(%{"error" => %{"message" => reason}}) do Logger.error("Received error from API: #{inspect(reason)}") {:error, reason} end def do_process_response({:error, %Jason.DecodeError{} = response}) do error_message = "Received invalid JSON: #{inspect(response)}" Logger.error(error_message) {:error, error_message} end def do_process_response(other) do Logger.error("Trying to process an unexpected response. #{inspect(other)}") {:error, "Unexpected response"} end defp maybe_add_org_id_header(%Req.Request{} = req) do org_id = get_org_id() if org_id do Req.Request.put_header(req, "OpenAI-Organization", org_id) else req end end end