Skip to content

Commit d17719c

Browse files
Refactor assistant determine tool role method (#823)
* Fix merge conflicts * Teeny adapter_spec file * Fixes * Remove `Langchain::Assistant#clear_thread!` method * CHANGELOG entry
1 parent a2b6b6a commit d17719c

File tree

10 files changed

+39
-24
lines changed

10 files changed

+39
-24
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## [Unreleased]
2+
- [BREAKING] Remove `Langchain::Assistant#clear_thread!` method
23
- [BREAKING] `Langchain::Messages::*` namespace had migrated to `Langchain::Assistant::Messages::*`
34
- [BREAKING] Modify `Langchain::LLM::AwsBedrock` constructor to pass model options via default_options: {...}
5+
- Minor improvements to the Langchain::Assistant class
46
- Added support for streaming with Anthropic
57
- Bump anthropic gem
68
- Default Langchain::LLM::Anthropic chat model is "claude-3-5-sonnet-20240620" now

lib/langchain/assistant.rb

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,8 @@ def add_message_and_run!(content: nil, image_url: nil)
165165
# @param output [String] The output of the tool
166166
# @return [Array<Langchain::Message>] The messages
167167
def submit_tool_output(tool_call_id:, output:)
168-
tool_role = determine_tool_role
169-
170168
# TODO: Validate that `tool_call_id` is valid by scanning messages and checking if this tool call ID was invoked
171-
add_message(role: tool_role, content: output, tool_call_id: tool_call_id)
169+
add_message(role: @llm_adapter.tool_role, content: output, tool_call_id: tool_call_id)
172170
end
173171

174172
# Delete all messages
@@ -179,9 +177,6 @@ def clear_messages!
179177
@messages = []
180178
end
181179

182-
# TODO: Remove in the next major release
183-
alias_method :clear_thread!, :clear_messages!
184-
185180
# Set new instructions
186181
#
187182
# @param new_instructions [String] New instructions that will be set as a system message
@@ -328,24 +323,6 @@ def execute_tools
328323
:failed
329324
end
330325

331-
# Determine the tool role based on the LLM type
332-
#
333-
# @return [String] The tool role
334-
def determine_tool_role
335-
case llm
336-
when Langchain::LLM::Anthropic
337-
Messages::AnthropicMessage::TOOL_ROLE
338-
when Langchain::LLM::GoogleGemini, Langchain::LLM::GoogleVertexAI
339-
Messages::GoogleGeminiMessage::TOOL_ROLE
340-
when Langchain::LLM::MistralAI
341-
Messages::MistralAIMessage::TOOL_ROLE
342-
when Langchain::LLM::Ollama
343-
Messages::OllamaMessage::TOOL_ROLE
344-
when Langchain::LLM::OpenAI
345-
Messages::OpenAIMessage::TOOL_ROLE
346-
end
347-
end
348-
349326
def initialize_instructions
350327
if llm.is_a?(Langchain::LLM::OpenAI) || llm.is_a?(Langchain::LLM::MistralAI)
351328
self.instructions = @instructions if @instructions

lib/langchain/assistant/llm/adapters/anthropic.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ def available_tool_names(tools)
6666
build_tools(tools).map { |tool| tool.dig(:name) }
6767
end
6868

69+
def tool_role
70+
Messages::AnthropicMessage::TOOL_ROLE
71+
end
72+
6973
private
7074

7175
def build_tool_choice(choice)

lib/langchain/assistant/llm/adapters/base.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ def extract_tool_call_args(tool_call:)
3535
def build_message(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil)
3636
raise NotImplementedError, "Subclasses must implement build_message"
3737
end
38+
39+
# Role name used to return the tool output
40+
#
41+
# @return [String] The tool role
42+
def tool_role
43+
raise NotImplementedError, "Subclasses must implement tool_role"
44+
end
3845
end
3946
end
4047
end

lib/langchain/assistant/llm/adapters/google_gemini.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ def available_tool_names(tools)
6666
build_tools(tools).map { |tool| tool.dig(:name) }
6767
end
6868

69+
def tool_role
70+
Messages::GoogleGeminiMessage::TOOL_ROLE
71+
end
72+
6973
private
7074

7175
def build_tool_config(choice)

lib/langchain/assistant/llm/adapters/mistral_ai.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ def available_tool_names(tools)
6868
build_tools(tools).map { |tool| tool.dig(:function, :name) }
6969
end
7070

71+
def tool_role
72+
Messages::MistralAIMessage::TOOL_ROLE
73+
end
74+
7175
private
7276

7377
def build_tool_choice(choice)

lib/langchain/assistant/llm/adapters/ollama.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ def allowed_tool_choices
6464
["auto", "none"]
6565
end
6666

67+
def tool_role
68+
Messages::OllamaMessage::TOOL_ROLE
69+
end
70+
6771
private
6872

6973
def build_tools(tools)

lib/langchain/assistant/llm/adapters/openai.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ def available_tool_names(tools)
6868
build_tools(tools).map { |tool| tool.dig(:function, :name) }
6969
end
7070

71+
def tool_role
72+
Messages::OpenAIMessage::TOOL_ROLE
73+
end
74+
7175
private
7276

7377
def build_tool_choice(choice)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe Langchain::Assistant::LLM::Adapter do
4+
let(:llm) { Langchain::LLM::OpenAI.new(api_key: "123") }
5+
6+
it "initialize a new OpenAI adapter" do
7+
expect(described_class.build(llm)).to be_a(Langchain::Assistant::LLM::Adapters::OpenAI)
8+
end
9+
end

spec/langchain/assistant/llm/adapters/.keep

Whitespace-only changes.

0 commit comments

Comments
 (0)