Skip to content

Commit

Permalink
LLM param in Assistant can be a registered LLM or custom adapter
Browse files Browse the repository at this point in the history
That makes an Assistant open to very custom LLMs.
  • Loading branch information
qarol committed Nov 13, 2024
1 parent 092d5bb commit 49904df
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
12 changes: 9 additions & 3 deletions lib/langchain/assistant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Assistant

# Create a new assistant
#
# @param llm [Langchain::LLM::Base] LLM instance that the assistant will use
# @param llm [Langchain::LLM::Base, Langchain::Assistant::LLM::Adapters::Base] LLM instance that the assistant will use
# @param tools [Array<Langchain::Tool::Base>] Tools that the assistant has access to
# @param instructions [String] The system instructions
# @param tool_choice [String] Specify how tools should be selected. Options: "auto", "any", "none", or <specific function name>
Expand All @@ -37,7 +37,6 @@ 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: LLM::Adapter.build(llm),
tools: [],
instructions: nil,
tool_choice: "auto",
Expand All @@ -51,7 +50,14 @@ def initialize(
end

@llm = llm
@llm_adapter = llm_adapter
case llm
when Langchain::LLM::Base
@llm_adapter = LLM::Adapter.build(llm)
when Langchain::Assistant::LLM::Adapters::Base
@llm_adapter = llm
else
raise ArgumentError, "LLM has to be object extending Langchain::LLM::Base or Langchain::Assistant::LLM::Adapters::Base"
end

# TODO: Validate that it is, indeed, a Proc or lambda
if !add_message_callback.nil? && !add_message_callback.respond_to?(:call)
Expand Down
31 changes: 26 additions & 5 deletions spec/langchain/assistant/assistant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1561,22 +1561,43 @@ def initialize(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id

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

describe ".new" do
it "initiates an assistant without error" do
expect { subject }.not_to raise_error
it "initiates an assistant without error using adapter as LLM" do
expect {
described_class.new(
llm: llm_adapter,
tools: [calculator],
instructions: instructions
)
}.not_to raise_error
end

it "raises an error when using unknown LLM" do
expect {
described_class.new(
llm: llm,
tools: [calculator],
instructions: instructions
)
}.to raise_error
end
end

describe "#messages" do
it "returns list of messages" do
expect(subject.messages).to contain_exactly(
expect(
described_class.new(
llm: llm_adapter,
tools: [calculator],
instructions: instructions
).messages
).to contain_exactly(
an_object_having_attributes(
role: "system",
content: "You are an expert assistant"
Expand Down

0 comments on commit 49904df

Please sign in to comment.