langchainrb 0.19.1 → 1.19.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of langchainrb might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/README.md +7 -0
- data/lib/langchain/assistant.rb +16 -5
- data/lib/langchain/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7e15dfbab8b4380ef4c71d1b596ed5f19ff55feefb01cf091903c792ab66af5f
|
4
|
+
data.tar.gz: de4d29455f1e29b34f04026868287c4464f94e45d15f6113705746b421b72fb5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 88e052d2a3a8ca936ed0c8d6d638351fdf7418e91e33756d0295039423de28fc0e25273c5e8afed94792ff464f17f883904f472464185db40242476e8530c6d4
|
7
|
+
data.tar.gz: c9cc9d7b06b1b329ca67973009e5248c688de972271cc77d8a946c00fa0bc4501f9913159bafc068ee8b3468eea47d5c3860c12bc4fbb46adbf44aedc7816531
|
data/CHANGELOG.md
CHANGED
@@ -11,6 +11,9 @@
|
|
11
11
|
|
12
12
|
## [Unreleased]
|
13
13
|
|
14
|
+
## [0.19.2] - 2024-11-25
|
15
|
+
- [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
|
16
|
+
|
14
17
|
## [0.19.1] - 2024-11-21
|
15
18
|
- [FEATURE] [https://github.com/patterns-ai-core/langchainrb/pull/858] Assistant, when using Anthropic, now also accepts image_url in the message.
|
16
19
|
- [FEATURE] [https://github.com/patterns-ai-core/langchainrb/pull/861] Clean up passing `max_tokens` to Anthropic constructor and chat method
|
data/README.md
CHANGED
@@ -536,6 +536,13 @@ Note that streaming is not currently supported for all LLMs.
|
|
536
536
|
* `tool_choice`: Specifies how tools should be selected. Default: "auto". A specific tool function name can be passed. This will force the Assistant to **always** use this function.
|
537
537
|
* `parallel_tool_calls`: Whether to make multiple parallel tool calls. Default: true
|
538
538
|
* `add_message_callback`: A callback function (proc, lambda) that is called when any message is added to the conversation (optional)
|
539
|
+
```ruby
|
540
|
+
assistant.add_message_callback = -> (message) { puts "New message: #{message}" }
|
541
|
+
```
|
542
|
+
* `tool_execution_callback`: A callback function (proc, lambda) that is called right before a tool is executed (optional)
|
543
|
+
```ruby
|
544
|
+
assistant.tool_execution_callback = -> (tool_call_id, tool_name, method_name, tool_arguments) { puts "Executing tool_call_id: #{tool_call_id}, tool_name: #{tool_name}, method_name: #{method_name}, tool_arguments: #{tool_arguments}" }
|
545
|
+
```
|
539
546
|
|
540
547
|
### Key Methods
|
541
548
|
* `add_message`: Adds a user message to the messages array
|
data/lib/langchain/assistant.rb
CHANGED
@@ -24,6 +24,7 @@ module Langchain
|
|
24
24
|
|
25
25
|
attr_accessor :tools,
|
26
26
|
:add_message_callback,
|
27
|
+
:tool_execution_callback,
|
27
28
|
:parallel_tool_calls
|
28
29
|
|
29
30
|
# Create a new assistant
|
@@ -35,6 +36,7 @@ module Langchain
|
|
35
36
|
# @param parallel_tool_calls [Boolean] Whether or not to run tools in parallel
|
36
37
|
# @param messages [Array<Langchain::Assistant::Messages::Base>] The messages
|
37
38
|
# @param add_message_callback [Proc] A callback function (Proc or lambda) that is called when any message is added to the conversation
|
39
|
+
# @param tool_execution_callback [Proc] A callback function (Proc or lambda) that is called right before a tool function is executed
|
38
40
|
def initialize(
|
39
41
|
llm:,
|
40
42
|
tools: [],
|
@@ -42,7 +44,9 @@ module Langchain
|
|
42
44
|
tool_choice: "auto",
|
43
45
|
parallel_tool_calls: true,
|
44
46
|
messages: [],
|
47
|
+
# Callbacks
|
45
48
|
add_message_callback: nil,
|
49
|
+
tool_execution_callback: nil,
|
46
50
|
&block
|
47
51
|
)
|
48
52
|
unless tools.is_a?(Array) && tools.all? { |tool| tool.class.singleton_class.included_modules.include?(Langchain::ToolDefinition) }
|
@@ -52,11 +56,8 @@ module Langchain
|
|
52
56
|
@llm = llm
|
53
57
|
@llm_adapter = LLM::Adapter.build(llm)
|
54
58
|
|
55
|
-
|
56
|
-
|
57
|
-
raise ArgumentError, "add_message_callback must be a callable object, like Proc or lambda"
|
58
|
-
end
|
59
|
-
@add_message_callback = add_message_callback
|
59
|
+
@add_message_callback = add_message_callback if validate_callback!("add_message_callback", add_message_callback)
|
60
|
+
@tool_execution_callback = tool_execution_callback if validate_callback!("tool_execution_callback", tool_execution_callback)
|
60
61
|
|
61
62
|
self.messages = messages
|
62
63
|
@tools = tools
|
@@ -359,6 +360,8 @@ module Langchain
|
|
359
360
|
t.class.tool_name == tool_name
|
360
361
|
end or raise ArgumentError, "Tool: #{tool_name} not found in assistant.tools"
|
361
362
|
|
363
|
+
# Call the callback if set
|
364
|
+
tool_execution_callback.call(tool_call_id, tool_name, method_name, tool_arguments) if tool_execution_callback # rubocop:disable Style/SafeNavigation
|
362
365
|
output = tool_instance.send(method_name, **tool_arguments)
|
363
366
|
|
364
367
|
submit_tool_output(tool_call_id: tool_call_id, output: output)
|
@@ -392,5 +395,13 @@ module Langchain
|
|
392
395
|
def available_tool_names
|
393
396
|
llm_adapter.available_tool_names(tools)
|
394
397
|
end
|
398
|
+
|
399
|
+
def validate_callback!(attr_name, callback)
|
400
|
+
if !callback.nil? && !callback.respond_to?(:call)
|
401
|
+
raise ArgumentError, "#{attr_name} must be a callable object, like Proc or lambda"
|
402
|
+
end
|
403
|
+
|
404
|
+
true
|
405
|
+
end
|
395
406
|
end
|
396
407
|
end
|
data/lib/langchain/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: langchainrb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.19.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrei Bondarev
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-11-
|
11
|
+
date: 2024-11-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: baran
|