Skip to content
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
21 changes: 18 additions & 3 deletions lib/langchain/assistant/messages/anthropic_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class AnthropicMessage < Base
ROLES = [
"assistant",
"user",
"tool_result"
"tool_result",
"system"
].freeze

TOOL_ROLE = "tool_result"
Expand Down Expand Up @@ -42,6 +43,8 @@ def initialize(
def to_hash
if assistant?
assistant_hash
elsif system?
system_hash
elsif tool?
tool_hash
elsif user?
Expand All @@ -64,6 +67,16 @@ def assistant_hash
}
end

# Convert the message to an Anthropic API-compatible hash
#
# @return [Hash] The message as an Anthropic API-compatible hash, with the role as "system"
def system_hash
{
role: "system",
content: build_content_array
}
end

# Convert the message to an Anthropic API-compatible hash
#
# @return [Hash] The message as an Anthropic API-compatible hash, with the role as "user"
Expand Down Expand Up @@ -125,9 +138,11 @@ def tool?
role == "tool_result"
end

# Anthropic does not implement system prompts
# Check if the message is a system message
#
# @return [Boolean] true/false whether this message is a system message
def system?
false
role == "system"
end

# Check if the message came from an LLM
Expand Down
6 changes: 5 additions & 1 deletion spec/lib/langchain/assistant/assistant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,10 @@
before do
allow(subject.llm).to receive(:chat)
.with(
messages: [{role: "user", content: [{text: "Please calculate 2+2", type: "text"}]}],
messages: [
{role: "system", content: [{text: instructions, type: "text"}]},
{role: "user", content: [{text: "Please calculate 2+2", type: "text"}]}
],
tools: calculator.class.function_schemas.to_anthropic_format,
tool_choice: {disable_parallel_tool_use: false, type: "auto"},
system: instructions
Expand Down Expand Up @@ -1268,6 +1271,7 @@
allow(subject.llm).to receive(:chat)
.with(
messages: [
{role: "system", content: [{text: instructions, type: "text"}]},
{role: "user", content: [{text: "Please calculate 2+2", type: "text"}]},
{role: "assistant", content: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

describe "#support_system_message?" do
it "returns true" do
expect(subject.support_system_message?).to eq(false)
expect(subject.support_system_message?).to eq(true)
end
end

Expand Down
38 changes: 38 additions & 0 deletions spec/lib/langchain/assistant/messages/anthropic_message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,32 @@
end
end

context "when role is system" do
let(:role) { "system" }

it "returns system_hash" do
message = described_class.new(role: role, content: "You are a helpful assistant.")
expect(message).to receive(:system_hash).and_call_original
expect(message.to_hash).to eq(
role: role,
content: [
{
type: "text",
text: "You are a helpful assistant."
}
]
)
end

it "returns system_hash without content" do
message = described_class.new(role: role, content: nil)
expect(message.to_hash).to eq(
role: role,
content: []
)
end
end

context "when role is tool_result" do
let(:message) { described_class.new(role: "tool_result", content: "4.0", tool_call_id: "toolu_014eSx9oBA5DMe8gZqaqcJ3H") }

Expand Down Expand Up @@ -153,4 +179,16 @@
end
end
end

describe "#system?" do
it "returns true when role is system" do
message = described_class.new(role: "system", content: "You are a helpful assistant.")
expect(message.system?).to be true
end

it "returns false when role is not system" do
message = described_class.new(role: "user", content: "Hello")
expect(message.system?).to be false
end
end
end