langchainrb 0.19.1 → 0.19.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6c33b1ac19c1cf4a3a7de8fbf5d2568899d05662cb7db0b96321186c255ef312
4
- data.tar.gz: 96bad2ff6f9b9a9cbac26699b525a8646482a0a77131d58fed84de3bafcb074e
3
+ metadata.gz: 5567aa14daf63f120a6c2b2b2991fbd85b650b4cb4e2ac49703736dacd46197e
4
+ data.tar.gz: ee71c45590998793c343c9620565cfcfdff5cd2d715091f3ad6804d54b814444
5
5
  SHA512:
6
- metadata.gz: aef30f32cfbdba307372ab8b20a0a484d22bd72f68d614056c6abb016b16dbd5e7a51dc15fb26746fd7a88a429d6282848aace42076c41e4fb31d58be9fb27f6
7
- data.tar.gz: 3fd751a81f1121209ae2fac6bd25bb2e9d524b23dd5afe83967e10a78521e6994b7ca855e7e4343dded1117c5a7377d26cf15607f16247e9f220a27d17b91827
6
+ metadata.gz: a5d212aadfef3ca07a6c9dc517141f0e6dedb1e414b0e8bbcbc0b7abddd5e2405a8a07ab6cc10efd66ee8997c41b5c8bd82ea8755d0f8e5fea7728db94f82379
7
+ data.tar.gz: a3a5a5c62cc075e8d09c0e6b629e1ec8b118d35d98e4de107c48d779d8b35261de3ad0061e22a4be4b2aefc15bbb25c20f658fe1f0a694ecc87279cd21309ff8
data/CHANGELOG.md CHANGED
@@ -11,6 +11,12 @@
11
11
 
12
12
  ## [Unreleased]
13
13
 
14
+ ## [0.19.3] - 2025-01-13
15
+ - [BUGFIX] [https://github.com/patterns-ai-core/langchainrb/pull/900] Empty text content should not be set when content is nil when using AnthropicMessage
16
+
17
+ ## [0.19.2] - 2024-11-26
18
+ - [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
19
+
14
20
  ## [0.19.1] - 2024-11-21
15
21
  - [FEATURE] [https://github.com/patterns-ai-core/langchainrb/pull/858] Assistant, when using Anthropic, now also accepts image_url in the message.
16
22
  - [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
@@ -53,14 +53,14 @@ module Langchain
53
53
  #
54
54
  # @return [Hash] The message as an Anthropic API-compatible hash, with the role as "assistant"
55
55
  def assistant_hash
56
+ content_array = []
57
+ if content && !content.empty?
58
+ content_array << {type: "text", text: content}
59
+ end
60
+
56
61
  {
57
62
  role: "assistant",
58
- content: [
59
- {
60
- type: "text",
61
- text: content
62
- }
63
- ].concat(tool_calls)
63
+ content: content_array.concat(tool_calls)
64
64
  }
65
65
  end
66
66
 
@@ -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
- # TODO: Validate that it is, indeed, a Proc or lambda
56
- if !add_message_callback.nil? && !add_message_callback.respond_to?(:call)
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
@@ -353,16 +354,26 @@ module Langchain
353
354
  def run_tools(tool_calls)
354
355
  # Iterate over each function invocation and submit tool output
355
356
  tool_calls.each do |tool_call|
356
- tool_call_id, tool_name, method_name, tool_arguments = @llm_adapter.extract_tool_call_args(tool_call: tool_call)
357
+ run_tool(tool_call)
358
+ end
359
+ end
357
360
 
358
- tool_instance = tools.find do |t|
359
- t.class.tool_name == tool_name
360
- end or raise ArgumentError, "Tool: #{tool_name} not found in assistant.tools"
361
+ # Run the tool call
362
+ #
363
+ # @param tool_call [Hash] The tool call to run
364
+ # @return [Object] The result of the tool call
365
+ def run_tool(tool_call)
366
+ tool_call_id, tool_name, method_name, tool_arguments = @llm_adapter.extract_tool_call_args(tool_call: tool_call)
361
367
 
362
- output = tool_instance.send(method_name, **tool_arguments)
368
+ tool_instance = tools.find do |t|
369
+ t.class.tool_name == tool_name
370
+ end or raise ArgumentError, "Tool: #{tool_name} not found in assistant.tools"
363
371
 
364
- submit_tool_output(tool_call_id: tool_call_id, output: output)
365
- end
372
+ # Call the callback if set
373
+ tool_execution_callback.call(tool_call_id, tool_name, method_name, tool_arguments) if tool_execution_callback # rubocop:disable Style/SafeNavigation
374
+ output = tool_instance.send(method_name, **tool_arguments)
375
+
376
+ submit_tool_output(tool_call_id: tool_call_id, output: output)
366
377
  end
367
378
 
368
379
  # Build a message
@@ -392,5 +403,13 @@ module Langchain
392
403
  def available_tool_names
393
404
  llm_adapter.available_tool_names(tools)
394
405
  end
406
+
407
+ def validate_callback!(attr_name, callback)
408
+ if !callback.nil? && !callback.respond_to?(:call)
409
+ raise ArgumentError, "#{attr_name} must be a callable object, like Proc or lambda"
410
+ end
411
+
412
+ true
413
+ end
395
414
  end
396
415
  end
@@ -12,9 +12,9 @@ module Langchain::LLM
12
12
 
13
13
  DEFAULTS = {
14
14
  temperature: 0.0,
15
- completion_model: "llama3.1",
16
- embedding_model: "llama3.1",
17
- chat_model: "llama3.1",
15
+ completion_model: "llama3.2",
16
+ embedding_model: "llama3.2",
17
+ chat_model: "llama3.2",
18
18
  options: {}
19
19
  }.freeze
20
20
 
@@ -24,6 +24,7 @@ module Langchain::LLM
24
24
  llama2: 4_096,
25
25
  llama3: 4_096,
26
26
  "llama3.1": 4_096,
27
+ "llama3.2": 4_096,
27
28
  llava: 4_096,
28
29
  mistral: 4_096,
29
30
  "mistral-openorca": 4_096,
@@ -5,7 +5,7 @@ module Langchain::Tool
5
5
  # Connects to a SQL database, executes SQL queries, and outputs DB schema for Agents to use
6
6
  #
7
7
  # Gem requirements:
8
- # gem "sequel", "~> 5.68.0"
8
+ # gem "sequel", "~> 5.87.0"
9
9
  #
10
10
  # Usage:
11
11
  # database = Langchain::Tool::Database.new(connection_string: "postgres://user:password@localhost:5432/db_name")
@@ -115,6 +115,7 @@ module Langchain::Tool
115
115
  else
116
116
  primary_key_columns << column[0]
117
117
  end
118
+ schema << " COMMENT '#{column[1][:comment]}'" if column[1][:comment]
118
119
  schema << ",\n" unless column == db.schema(table).last && primary_key_column_count == 1
119
120
  end
120
121
  if primary_key_column_count > 1
@@ -116,7 +116,8 @@ module Langchain::Vectorsearch
116
116
  count = collection.count
117
117
  n_results = [count, k].min
118
118
 
119
- collection.query(query_embeddings: [embedding], results: n_results)
119
+ # workaround mentioned here: https://github.com/mariochavez/chroma/issues/29
120
+ collection.query(query_embeddings: [embedding], results: n_results, where: nil, where_document: nil)
120
121
  end
121
122
 
122
123
  # Ask a question and return the answer
@@ -6,7 +6,7 @@ module Langchain::Vectorsearch
6
6
  # The PostgreSQL vector search adapter
7
7
  #
8
8
  # Gem requirements:
9
- # gem "sequel", "~> 5.68.0"
9
+ # gem "sequel", "~> 5.87.0"
10
10
  # gem "pgvector", "~> 0.2"
11
11
  #
12
12
  # Usage:
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Langchain
4
- VERSION = "0.19.1"
4
+ VERSION = "0.19.3"
5
5
  end
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: 0.19.1
4
+ version: 0.19.3
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-21 00:00:00.000000000 Z
11
+ date: 2025-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: baran
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.1.9
27
+ - !ruby/object:Gem::Dependency
28
+ name: csv
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: json-schema
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -86,14 +100,14 @@ dependencies:
86
100
  requirements:
87
101
  - - "~>"
88
102
  - !ruby/object:Gem::Version
89
- version: 2.7.6
103
+ version: 3.1.6
90
104
  type: :development
91
105
  prerelease: false
92
106
  version_requirements: !ruby/object:Gem::Requirement
93
107
  requirements:
94
108
  - - "~>"
95
109
  - !ruby/object:Gem::Version
96
- version: 2.7.6
110
+ version: 3.1.6
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: pry-byebug
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -576,14 +590,14 @@ dependencies:
576
590
  requirements:
577
591
  - - "~>"
578
592
  - !ruby/object:Gem::Version
579
- version: 5.68.0
593
+ version: 5.87.0
580
594
  type: :development
581
595
  prerelease: false
582
596
  version_requirements: !ruby/object:Gem::Requirement
583
597
  requirements:
584
598
  - - "~>"
585
599
  - !ruby/object:Gem::Version
586
- version: 5.68.0
600
+ version: 5.87.0
587
601
  - !ruby/object:Gem::Dependency
588
602
  name: weaviate-ruby
589
603
  requirement: !ruby/object:Gem::Requirement
@@ -776,7 +790,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
776
790
  - !ruby/object:Gem::Version
777
791
  version: '0'
778
792
  requirements: []
779
- rubygems_version: 3.5.20
793
+ rubygems_version: 3.5.3
780
794
  signing_key:
781
795
  specification_version: 4
782
796
  summary: Build LLM-backed Ruby applications with Ruby's Langchain.rb