Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add max_tokens option for OpenAI calls #73

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/chat_models/chat_open_ai.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ defmodule LangChain.ChatModels.ChatOpenAI do
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{}
Expand All @@ -73,7 +74,8 @@ defmodule LangChain.ChatModels.ChatOpenAI do
:n,
:stream,
:receive_timeout,
:json_response
:json_response,
:max_tokens
]
@required_fields [:endpoint, :model]

Expand Down Expand Up @@ -136,6 +138,7 @@ defmodule LangChain.ChatModels.ChatOpenAI do
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
Expand Down
16 changes: 16 additions & 0 deletions test/chat_models/chat_open_ai_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ defmodule LangChain.ChatModels.ChatOpenAITest do
assert data.frequency_penalty == 0.5
assert data.response_format == %{"type" => "json_object"}
end

test "generates a map for an API call with max_tokens set" do
{:ok, openai} =
ChatOpenAI.new(%{
"model" => "gpt-3.5-turbo-0613",
"temperature" => 1,
"frequency_penalty" => 0.5,
"max_tokens" => 1234
})

data = ChatOpenAI.for_api(openai, [], [])
assert data.model == "gpt-3.5-turbo-0613"
assert data.temperature == 1
assert data.frequency_penalty == 0.5
assert data.max_tokens == 1234
end
end

describe "call/2" do
Expand Down