Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor assistant determine tool role method #823

Merged
merged 5 commits into from
Oct 11, 2024
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## [Unreleased]
- [BREAKING] Remove `Langchain::Assistant#clear_thread!` method
- [BREAKING] `Langchain::Messages::*` namespace had migrated to `Langchain::Assistant::Messages::*`
- [BREAKING] Modify `Langchain::LLM::AwsBedrock` constructor to pass model options via default_options: {...}
- Minor improvements to the Langchain::Assistant class
- Added support for streaming with Anthropic
- Bump anthropic gem
- Default Langchain::LLM::Anthropic chat model is "claude-3-5-sonnet-20240620" now
Expand Down
25 changes: 1 addition & 24 deletions lib/langchain/assistant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,8 @@ def add_message_and_run!(content: nil, image_url: nil)
# @param output [String] The output of the tool
# @return [Array<Langchain::Message>] The messages
def submit_tool_output(tool_call_id:, output:)
tool_role = determine_tool_role

# TODO: Validate that `tool_call_id` is valid by scanning messages and checking if this tool call ID was invoked
add_message(role: tool_role, content: output, tool_call_id: tool_call_id)
add_message(role: @llm_adapter.tool_role, content: output, tool_call_id: tool_call_id)
end

# Delete all messages
Expand All @@ -179,9 +177,6 @@ def clear_messages!
@messages = []
end

# TODO: Remove in the next major release
alias_method :clear_thread!, :clear_messages!

# Set new instructions
#
# @param new_instructions [String] New instructions that will be set as a system message
Expand Down Expand Up @@ -328,24 +323,6 @@ def execute_tools
:failed
end

# Determine the tool role based on the LLM type
#
# @return [String] The tool role
def determine_tool_role
case llm
when Langchain::LLM::Anthropic
Messages::AnthropicMessage::TOOL_ROLE
when Langchain::LLM::GoogleGemini, Langchain::LLM::GoogleVertexAI
Messages::GoogleGeminiMessage::TOOL_ROLE
when Langchain::LLM::MistralAI
Messages::MistralAIMessage::TOOL_ROLE
when Langchain::LLM::Ollama
Messages::OllamaMessage::TOOL_ROLE
when Langchain::LLM::OpenAI
Messages::OpenAIMessage::TOOL_ROLE
end
end

def initialize_instructions
if llm.is_a?(Langchain::LLM::OpenAI) || llm.is_a?(Langchain::LLM::MistralAI)
self.instructions = @instructions if @instructions
Expand Down
4 changes: 4 additions & 0 deletions lib/langchain/assistant/llm/adapters/anthropic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ def available_tool_names(tools)
build_tools(tools).map { |tool| tool.dig(:name) }
end

def tool_role
Messages::AnthropicMessage::TOOL_ROLE
end

private

def build_tool_choice(choice)
Expand Down
7 changes: 7 additions & 0 deletions lib/langchain/assistant/llm/adapters/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ def extract_tool_call_args(tool_call:)
def build_message(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil)
raise NotImplementedError, "Subclasses must implement build_message"
end

# Role name used to return the tool output
#
# @return [String] The tool role
def tool_role
raise NotImplementedError, "Subclasses must implement tool_role"
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/langchain/assistant/llm/adapters/google_gemini.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ def available_tool_names(tools)
build_tools(tools).map { |tool| tool.dig(:name) }
end

def tool_role
Messages::GoogleGeminiMessage::TOOL_ROLE
end

private

def build_tool_config(choice)
Expand Down
4 changes: 4 additions & 0 deletions lib/langchain/assistant/llm/adapters/mistral_ai.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ def available_tool_names(tools)
build_tools(tools).map { |tool| tool.dig(:function, :name) }
end

def tool_role
Messages::MistralAIMessage::TOOL_ROLE
end

private

def build_tool_choice(choice)
Expand Down
4 changes: 4 additions & 0 deletions lib/langchain/assistant/llm/adapters/ollama.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ def allowed_tool_choices
["auto", "none"]
end

def tool_role
Messages::OllamaMessage::TOOL_ROLE
end

private

def build_tools(tools)
Expand Down
4 changes: 4 additions & 0 deletions lib/langchain/assistant/llm/adapters/openai.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ def available_tool_names(tools)
build_tools(tools).map { |tool| tool.dig(:function, :name) }
end

def tool_role
Messages::OpenAIMessage::TOOL_ROLE
end

private

def build_tool_choice(choice)
Expand Down
9 changes: 9 additions & 0 deletions spec/langchain/assistant/llm/adapter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

RSpec.describe Langchain::Assistant::LLM::Adapter do
let(:llm) { Langchain::LLM::OpenAI.new(api_key: "123") }

it "initialize a new OpenAI adapter" do
expect(described_class.build(llm)).to be_a(Langchain::Assistant::LLM::Adapters::OpenAI)
end
end
Empty file.