Skip to content

Commit

Permalink
Extend Assistant constructor to support custom LLM and adapters
Browse files Browse the repository at this point in the history
Added `llm_adapter` parameter to the `Assistant` class constructor. Allows initialization of the Assistant with custom language models and adapters. Ensures more flexibility in choosing and managing LLM integrations
  • Loading branch information
qarol committed Oct 12, 2024
1 parent 8f10957 commit 11c9444
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/langchain/assistant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Assistant
# @param add_message_callback [Proc] A callback function (Proc or lambda) that is called when any message is added to the conversation
def initialize(
llm:,
llm_adapter: nil,
tools: [],
instructions: nil,
tool_choice: "auto",
Expand All @@ -50,7 +51,7 @@ def initialize(
end

@llm = llm
@llm_adapter = LLM::Adapter.build(llm)
@llm_adapter = llm_adapter || LLM::Adapter.build(llm)

# TODO: Validate that it is, indeed, a Proc or lambda
if !add_message_callback.nil? && !add_message_callback.respond_to?(:call)
Expand Down
68 changes: 68 additions & 0 deletions spec/langchain/assistant/assistant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1366,4 +1366,72 @@
end
end
end

context "when llm is custom" do
let(:llm) { Langchain::LLM::Base.new }
let(:llm_adapter_class) do
Class.new(Langchain::Assistant::LLM::Adapters::Base) do
def allowed_tool_choices
["auto"]
end

def available_tool_names(tools)
[]
end

def support_system_message?
true
end

def build_message(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil)
message_class = Class.new(Langchain::Assistant::Messages::Base) do
def initialize(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil)
@role = role
@content = content.to_s
@image_url = image_url
@tool_calls = tool_calls
@tool_call_id = tool_call_id
end
end

message_class.new(
role: role,
content: content,
image_url: image_url,
tool_calls: tool_calls,
tool_call_id: tool_call_id
)
end
end
end
let(:llm_adapter) { llm_adapter_class.new }
let(:calculator) { Langchain::Tool::Calculator.new }
let(:instructions) { "You are an expert assistant" }

subject {
described_class.new(
llm: llm,
llm_adapter: llm_adapter,
tools: [calculator],
instructions: instructions
)
}

describe ".new" do
it "initiates an assistant without error" do
expect { subject }.not_to raise_error
end
end

describe "#messages" do
it "returns list of messages" do
expect(subject.messages).to contain_exactly(
an_object_having_attributes(
role: "system",
content: "You are an expert assistant"
)
)
end
end
end
end

0 comments on commit 11c9444

Please sign in to comment.