Skip to content

Commit ec4f021

Browse files
committed
Extend Assistant constructor to support custom LLM and adapters
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
1 parent 8f10957 commit ec4f021

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

lib/langchain/assistant.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Assistant
3737
# @param add_message_callback [Proc] A callback function (Proc or lambda) that is called when any message is added to the conversation
3838
def initialize(
3939
llm:,
40+
llm_adapter: LLM::Adapter.build(llm),
4041
tools: [],
4142
instructions: nil,
4243
tool_choice: "auto",
@@ -50,7 +51,7 @@ def initialize(
5051
end
5152

5253
@llm = llm
53-
@llm_adapter = LLM::Adapter.build(llm)
54+
@llm_adapter = llm_adapter
5455

5556
# TODO: Validate that it is, indeed, a Proc or lambda
5657
if !add_message_callback.nil? && !add_message_callback.respond_to?(:call)

spec/langchain/assistant/assistant_spec.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,4 +1366,72 @@
13661366
end
13671367
end
13681368
end
1369+
1370+
context "when llm is custom" do
1371+
let(:llm) { Langchain::LLM::Base.new }
1372+
let(:llm_adapter_class) do
1373+
Class.new(Langchain::Assistant::LLM::Adapters::Base) do
1374+
def allowed_tool_choices
1375+
["auto"]
1376+
end
1377+
1378+
def available_tool_names(tools)
1379+
[]
1380+
end
1381+
1382+
def support_system_message?
1383+
true
1384+
end
1385+
1386+
def build_message(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil)
1387+
message_class = Class.new(Langchain::Assistant::Messages::Base) do
1388+
def initialize(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil)
1389+
@role = role
1390+
@content = content.to_s
1391+
@image_url = image_url
1392+
@tool_calls = tool_calls
1393+
@tool_call_id = tool_call_id
1394+
end
1395+
end
1396+
1397+
message_class.new(
1398+
role: role,
1399+
content: content,
1400+
image_url: image_url,
1401+
tool_calls: tool_calls,
1402+
tool_call_id: tool_call_id
1403+
)
1404+
end
1405+
end
1406+
end
1407+
let(:llm_adapter) { llm_adapter_class.new }
1408+
let(:calculator) { Langchain::Tool::Calculator.new }
1409+
let(:instructions) { "You are an expert assistant" }
1410+
1411+
subject {
1412+
described_class.new(
1413+
llm: llm,
1414+
llm_adapter: llm_adapter,
1415+
tools: [calculator],
1416+
instructions: instructions
1417+
)
1418+
}
1419+
1420+
describe ".new" do
1421+
it "initiates an assistant without error" do
1422+
expect { subject }.not_to raise_error
1423+
end
1424+
end
1425+
1426+
describe "#messages" do
1427+
it "returns list of messages" do
1428+
expect(subject.messages).to contain_exactly(
1429+
an_object_having_attributes(
1430+
role: "system",
1431+
content: "You are an expert assistant"
1432+
)
1433+
)
1434+
end
1435+
end
1436+
end
13691437
end

0 commit comments

Comments
 (0)