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

Translate max_tokens_to_sample for OpenAI #583

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions lib/langchain/llm/openai.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def chat(
logprobs: nil,
top_logprobs: nil,
max_tokens: nil,
max_tokens_to_sample: nil,
n: defaults[:n],
presence_penalty: nil,
response_format: nil,
Expand All @@ -129,6 +130,8 @@ def chat(
raise ArgumentError.new("model argument is required") if model.empty?
raise ArgumentError.new("'tool_choice' is only allowed when 'tools' are specified.") if tool_choice && tools.empty?

max_tokens ||= max_tokens_to_sample

parameters = {
messages: messages,
model: model
Expand Down
13 changes: 13 additions & 0 deletions spec/langchain/llm/openai_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,19 @@
}.to raise_error(ArgumentError, "'tool_choice' is only allowed when 'tools' are specified.")
end
end

context "with max_tokens_to_sample" do
let(:max_tokens_to_sample) { 50000 }
let(:parameters) { {parameters: {n: n, messages: history, model: model, temperature: temperature, max_tokens: max_tokens_to_sample}} }

it "translates max_tokens_to_sample to max_tokens" do
allow(subject.client).to receive(:chat).and_return(response)

subject.chat(messages: history, max_tokens_to_sample: max_tokens_to_sample)

expect(subject.client).to have_received(:chat).with(parameters)
end
end
end

describe "#summarize" do
Expand Down