Skip to content

Commit

Permalink
Appends the system parameter to Anthropic chat if instructions are sp…
Browse files Browse the repository at this point in the history
…ecified, regardless of whether or not tools are being used. (#695)

Co-authored-by: Andrei Bondarev <andrei@sourcelabs.io>
  • Loading branch information
victorivanov and andreibondarev authored Jul 5, 2024
1 parent bfa4e1d commit f82fa45
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/langchain/assistants/assistant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,21 +258,25 @@ def chat_with_llm

params = {messages: thread.array_of_message_hashes}

if tools.any?
if llm.is_a?(Langchain::LLM::OpenAI)
if llm.is_a?(Langchain::LLM::OpenAI)
if tools.any?
params[:tools] = tools.map(&:to_openai_tools).flatten
params[:tool_choice] = "auto"
elsif llm.is_a?(Langchain::LLM::Anthropic)
end
elsif llm.is_a?(Langchain::LLM::Anthropic)
if tools.any?
params[:tools] = tools.map(&:to_anthropic_tools).flatten
params[:system] = instructions if instructions
params[:tool_choice] = {type: "auto"}
elsif [Langchain::LLM::GoogleGemini, Langchain::LLM::GoogleVertexAI].include?(llm.class)
end
params[:system] = instructions if instructions
elsif [Langchain::LLM::GoogleGemini, Langchain::LLM::GoogleVertexAI].include?(llm.class)
if tools.any?
params[:tools] = tools.map(&:to_google_gemini_tools).flatten
params[:system] = instructions if instructions
params[:tool_choice] = "auto"
end
# TODO: Not sure that tool_choice should always be "auto"; Maybe we can let the user toggle it.
end
# TODO: Not sure that tool_choice should always be "auto"; Maybe we can let the user toggle it.

llm.chat(**params)
end
Expand Down
32 changes: 32 additions & 0 deletions spec/langchain/assistants/assistant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,27 @@
}
end

context "when not using tools" do
subject {
described_class.new(
llm: llm,
thread: thread,
instructions: instructions
)
}

it "adds a system param to chat when instructions are given" do
expect(subject.llm).to receive(:chat)
.with(
hash_including(
system: instructions
)
).and_return(Langchain::LLM::AnthropicResponse.new(raw_anthropic_response))
subject.add_message content: "Please calculate 2+2"
subject.run
end
end

context "when auto_tool_execution is false" do
before do
allow(subject.llm).to receive(:chat)
Expand All @@ -456,6 +477,17 @@
expect(subject.thread.messages.last.role).to eq("assistant")
expect(subject.thread.messages.last.tool_calls).to eq([raw_anthropic_response["content"].first])
end

it "adds a system param to chat when instructions are given" do
expect(subject.llm).to receive(:chat)
.with(
hash_including(
system: instructions
)
).and_return(Langchain::LLM::AnthropicResponse.new(raw_anthropic_response))
subject.add_message content: "Please calculate 2+2"
subject.run
end
end

context "when auto_tool_execution is true" do
Expand Down

0 comments on commit f82fa45

Please sign in to comment.