Skip to content

Commit

Permalink
Improve passing max tokens to anthropic (#864)
Browse files Browse the repository at this point in the history
* Clean up passing `max_tokens` to Anthropic constructor and chat method

* changelog entry
  • Loading branch information
andreibondarev authored Oct 29, 2024
1 parent ffe0de3 commit c72736d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

## [Unreleased]
- [FEATURE] [https://github.com/patterns-ai-core/langchainrb/pull/858] Assistant, when using Anthropic, now also accepts image_url in the message.
- [FEATURE] [https://github.com/patterns-ai-core/langchainrb/pull/859] Clean up passing `max_tokens` to Anthropic constructor and chat method

## [0.19.0] - 2024-10-23
- [BREAKING] [https://github.com/patterns-ai-core/langchainrb/pull/840] Rename `chat_completion_model_name` parameter to `chat_model` in Langchain::LLM parameters.
Expand Down
12 changes: 6 additions & 6 deletions lib/langchain/llm/anthropic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class Anthropic < Base
temperature: 0.0,
completion_model: "claude-2.1",
chat_model: "claude-3-5-sonnet-20240620",
max_tokens_to_sample: 256
max_tokens: 256
}.freeze

# Initialize an Anthropic LLM instance
#
# @param api_key [String] The API key to use
# @param llm_options [Hash] Options to pass to the Anthropic client
# @param default_options [Hash] Default options to use on every call to LLM, e.g.: { temperature:, completion_model:, chat_model:, max_tokens_to_sample: }
# @param default_options [Hash] Default options to use on every call to LLM, e.g.: { temperature:, completion_model:, chat_model:, max_tokens: }
# @return [Langchain::LLM::Anthropic] Langchain::LLM::Anthropic instance
def initialize(api_key:, llm_options: {}, default_options: {})
depends_on "anthropic"
Expand All @@ -32,7 +32,7 @@ def initialize(api_key:, llm_options: {}, default_options: {})
chat_parameters.update(
model: {default: @defaults[:chat_model]},
temperature: {default: @defaults[:temperature]},
max_tokens: {default: @defaults[:max_tokens_to_sample]},
max_tokens: {default: @defaults[:max_tokens]},
metadata: {},
system: {}
)
Expand All @@ -55,7 +55,7 @@ def initialize(api_key:, llm_options: {}, default_options: {})
def complete(
prompt:,
model: @defaults[:completion_model],
max_tokens_to_sample: @defaults[:max_tokens_to_sample],
max_tokens: @defaults[:max_tokens],
stop_sequences: nil,
temperature: @defaults[:temperature],
top_p: nil,
Expand All @@ -64,12 +64,12 @@ def complete(
stream: nil
)
raise ArgumentError.new("model argument is required") if model.empty?
raise ArgumentError.new("max_tokens_to_sample argument is required") if max_tokens_to_sample.nil?
raise ArgumentError.new("max_tokens argument is required") if max_tokens.nil?

parameters = {
model: model,
prompt: prompt,
max_tokens_to_sample: max_tokens_to_sample,
max_tokens_to_sample: max_tokens,
temperature: temperature
}
parameters[:stop_sequences] = stop_sequences if stop_sequences
Expand Down
30 changes: 27 additions & 3 deletions spec/langchain/llm/anthropic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@
RSpec.describe Langchain::LLM::Anthropic do
let(:subject) { described_class.new(api_key: "123") }

describe "#initialize" do
context "when default_options are passed" do
let(:default_options) { {max_tokens: 512} }

subject { described_class.new(api_key: "123", default_options: default_options) }

it "sets the defaults options" do
expect(subject.defaults[:max_tokens]).to eq(512)
end

it "get passed to consecutive chat() call" do
subject
expect(subject.client).to receive(:messages).with(parameters: hash_including(default_options)).and_return({})
subject.chat(messages: [{role: "user", content: "Hello json!"}])
end

it "can be overridden" do
subject
expect(subject.client).to receive(:messages).with(parameters: hash_including({max_tokens: 1024})).and_return({})
subject.chat(messages: [{role: "user", content: "Hello json!"}], max_tokens: 1024)
end
end
end

describe "#complete" do
let(:completion) { "How high is the sky?" }
let(:fixture) { File.read("spec/fixtures/llm/anthropic/complete.json") }
Expand All @@ -17,7 +41,7 @@
model: described_class::DEFAULTS[:completion_model],
prompt: completion,
temperature: described_class::DEFAULTS[:temperature],
max_tokens_to_sample: described_class::DEFAULTS[:max_tokens_to_sample]
max_tokens_to_sample: described_class::DEFAULTS[:max_tokens]
})
.and_return(response)
end
Expand All @@ -40,7 +64,7 @@
model: described_class::DEFAULTS[:completion_model],
prompt: completion,
temperature: described_class::DEFAULTS[:temperature],
max_tokens_to_sample: described_class::DEFAULTS[:max_tokens_to_sample]
max_tokens_to_sample: described_class::DEFAULTS[:max_tokens]
})
.and_return(JSON.parse(fixture))
end
Expand All @@ -63,7 +87,7 @@
model: described_class::DEFAULTS[:chat_model],
messages: messages,
temperature: described_class::DEFAULTS[:temperature],
max_tokens: described_class::DEFAULTS[:max_tokens_to_sample],
max_tokens: described_class::DEFAULTS[:max_tokens],
stop_sequences: ["beep"]
})
.and_return(response)
Expand Down
8 changes: 7 additions & 1 deletion spec/langchain/llm/openai_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,15 @@

it "get passed to consecutive chat() call" do
subject
expect(subject.client).to receive(:chat).with(parameters: hash_including({response_format: {type: "json_object"}})).and_return({})
expect(subject.client).to receive(:chat).with(parameters: hash_including(default_options)).and_return({})
subject.chat(messages: [{role: "user", content: "Hello json!"}])
end

it "can be overridden" do
subject
expect(subject.client).to receive(:chat).with(parameters: hash_including({response_format: {type: "text"}})).and_return({})
subject.chat(messages: [{role: "user", content: "Hello json!"}], response_format: {type: "text"})
end
end
end

Expand Down

0 comments on commit c72736d

Please sign in to comment.