Skip to content

Commit

Permalink
Anthropic tool use is always accompanied with text content. We should…
Browse files Browse the repository at this point in the history
… be sending it back as well. (#869)

* Anthropic tool use is always accompanied with text content. We should be sending it back as well.

* changelog entry
  • Loading branch information
andreibondarev authored Nov 11, 2024
1 parent 195e4ef commit 2406166
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- [FEATURE] [https://github.com/patterns-ai-core/langchainrb/pull/849] Langchain::Assistant now works with AWS Bedrock-hosted Anthropic models
- [OPTIM] [https://github.com/patterns-ai-core/langchainrb/pull/867] Refactor `GoogleGeminiMessage#to_hash` and `OpenAIMessage#to_hash` methods.
- [OPTIM] [https://github.com/patterns-ai-core/langchainrb/pull/849] Simplify Langchain::LLM::AwsBedrock class
- [BUGFIX] [https://github.com/patterns-ai-core/langchainrb/pull/869] AnthropicMessage now correctly handles tool calls with content.

## [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
11 changes: 6 additions & 5 deletions lib/langchain/assistant/messages/anthropic_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ def to_hash
def assistant_hash
{
role: "assistant",
content: if tool_calls.any?
tool_calls
else
content
end
content: [
{
type: "text",
text: content
}
].concat(tool_calls)
}
end

Expand Down
16 changes: 14 additions & 2 deletions spec/langchain/assistant/assistant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,10 @@
"output_tokens" => 55
},
"content" => [
{
"type" => "text",
"text" => "Sure, let's calculate it!"
},
{
"type" => "tool_use",
"id" => "toolu_014eSx9oBA5DMe8gZqaqcJ3H",
Expand Down Expand Up @@ -1159,7 +1163,7 @@
subject.run(auto_tool_execution: false)

expect(subject.messages.last.role).to eq("assistant")
expect(subject.messages.last.tool_calls).to eq([raw_anthropic_response["content"].first])
expect(subject.messages.last.tool_calls).to eq([raw_anthropic_response["content"].last])
end

it "adds a system param to chat when instructions are given" do
Expand Down Expand Up @@ -1193,6 +1197,10 @@
messages: [
{role: "user", content: [{text: "Please calculate 2+2", type: "text"}]},
{role: "assistant", content: [
{
type: "text",
text: "Sure, let's calculate it!"
},
{
"type" => "tool_use",
"id" => "toolu_014eSx9oBA5DMe8gZqaqcJ3H",
Expand All @@ -1215,7 +1223,11 @@
).and_return("4.0")

subject.add_message(role: "user", content: "Please calculate 2+2")
subject.add_message(role: "assistant", tool_calls: raw_anthropic_response["content"])
subject.add_message(
role: "assistant",
content: raw_anthropic_response["content"].first["text"],
tool_calls: [raw_anthropic_response["content"].last]
)

subject.run(auto_tool_execution: true)

Expand Down
12 changes: 11 additions & 1 deletion spec/langchain/assistant/messages/anthropic_message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@
expect(message).to receive(:assistant_hash).and_call_original
expect(message.to_hash).to eq(
role: role,
content: "Hello, how can I help you?"
content: [
{
type: "text",
text: "Hello, how can I help you?"
}
]
)
end

it "returns assistant_hash with tool_calls" do
message = described_class.new(
role: role,
content: "Okay, let's fetch the news",
tool_calls: [
{
"type" => "tool_use",
Expand All @@ -37,6 +43,10 @@
expect(message.to_hash).to eq(
role: role,
content: [
{
type: "text",
text: "Okay, let's fetch the news"
},
{
"type" => "tool_use",
"id" => "toolu_01UEciZACvRZ6S4rqAwD1syH",
Expand Down

0 comments on commit 2406166

Please sign in to comment.