diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f01fc9c3..c7e903069 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ ## [Unreleased] -## [0.19.2] - 2024-11-25 +## [0.19.2] - 2024-11-26 - [FEATURE] [https://github.com/patterns-ai-core/langchainrb/pull/884] Add `tool_execution_callback` to `Langchain::Assistant`, a callback function (proc, lambda) that is called right before a tool is executed ## [0.19.1] - 2024-11-21 diff --git a/Gemfile.lock b/Gemfile.lock index 3008f78b1..8bc496e52 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - langchainrb (1.19.2) + langchainrb (0.19.2) baran (~> 0.1.9) json-schema (~> 4) matrix diff --git a/lib/langchain/assistant.rb b/lib/langchain/assistant.rb index 5ee55a425..25411fff2 100644 --- a/lib/langchain/assistant.rb +++ b/lib/langchain/assistant.rb @@ -354,18 +354,26 @@ def chat_with_llm def run_tools(tool_calls) # Iterate over each function invocation and submit tool output tool_calls.each do |tool_call| - tool_call_id, tool_name, method_name, tool_arguments = @llm_adapter.extract_tool_call_args(tool_call: tool_call) + run_tool(tool_call) + end + end - tool_instance = tools.find do |t| - t.class.tool_name == tool_name - end or raise ArgumentError, "Tool: #{tool_name} not found in assistant.tools" + # Run the tool call + # + # @param tool_call [Hash] The tool call to run + # @return [Object] The result of the tool call + def run_tool(tool_call) + tool_call_id, tool_name, method_name, tool_arguments = @llm_adapter.extract_tool_call_args(tool_call: tool_call) - # Call the callback if set - tool_execution_callback.call(tool_call_id, tool_name, method_name, tool_arguments) if tool_execution_callback # rubocop:disable Style/SafeNavigation - output = tool_instance.send(method_name, **tool_arguments) + tool_instance = tools.find do |t| + t.class.tool_name == tool_name + end or raise ArgumentError, "Tool: #{tool_name} not found in assistant.tools" - submit_tool_output(tool_call_id: tool_call_id, output: output) - end + # Call the callback if set + tool_execution_callback.call(tool_call_id, tool_name, method_name, tool_arguments) if tool_execution_callback # rubocop:disable Style/SafeNavigation + output = tool_instance.send(method_name, **tool_arguments) + + submit_tool_output(tool_call_id: tool_call_id, output: output) end # Build a message diff --git a/lib/langchain/version.rb b/lib/langchain/version.rb index 4eef8d812..68fc10baf 100644 --- a/lib/langchain/version.rb +++ b/lib/langchain/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Langchain - VERSION = "1.19.2" + VERSION = "0.19.2" end