langchainrb 0.4.0 → 0.4.1

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: ce8728ec2208577809174e154642db161121cb9dd49e0ec5d190d080e68b1d78
4
- data.tar.gz: bb0e0ccc4558ca849549f495a4adfacc5f7851c786869974afdaef29f0cde3ca
3
+ metadata.gz: 100c9989f1a6787168064f6d810f009c6ded8129949f0040a7c1ed05ebff9d20
4
+ data.tar.gz: 0cf01f053841e9a45353d72b62df9a29c0daf7268fb69aff794aae830f7db448
5
5
  SHA512:
6
- metadata.gz: b2b4c27e31d730563aeca70a0aa3c4cf129e69773e34f397ba057faa8298a4368c1b9f66f925188f867f1feb47b4e07f77df702fa7c6cb76ad1e1a8464b895f6
7
- data.tar.gz: 55dd3fbc21e2cdf9bd84afcd6bb4de0f72c960dec0c6b1d2efff1f9492b3d5c7399f2d14c323597045e64eafb6f2f20992348d640317c64721fb0556f8a64126
6
+ metadata.gz: becea5f089e7455e8dd9437cb3647c921eebb3155a9685697190e0e757d31ec8d9f4572faa3e299911b44b371c097bd9b5edf3da35772562027d1b12cd743b9e
7
+ data.tar.gz: 44cfe6f8cb2056d6a0a5c619ded7828a9cdad0e5f4e8876242adad2f13251ea1c3dbb64c9feffea25b61e91f163245954c1f272fadb9f4f3a2a06cadd64bd9a0
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.4.1] - 2023-06-02
4
+ - Beautiful colored log messages
5
+ - 🛠️ Tools
6
+ - Introducing `Langchain::Tool::RubyCodeInterpreter`, a tool executes sandboxed Ruby code
7
+
3
8
  ## [0.4.0] - 2023-06-01
4
9
  - [BREAKING] Everything is namespaced under `Langchain::` now
5
10
  - Pgvector similarity search uses the cosine distance by default now
data/Gemfile.lock CHANGED
@@ -1,7 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- langchainrb (0.4.0)
4
+ langchainrb (0.4.1)
5
+ colorize (~> 0.8.1)
5
6
  tiktoken_ruby (~> 0.0.5)
6
7
 
7
8
  GEM
@@ -32,6 +33,7 @@ GEM
32
33
  ast (2.4.2)
33
34
  builder (3.2.4)
34
35
  byebug (11.1.3)
36
+ childprocess (4.1.0)
35
37
  chroma-db (0.3.0)
36
38
  dry-monads (~> 1.6)
37
39
  ruby-next-core (>= 0.15.0)
@@ -39,6 +41,7 @@ GEM
39
41
  cohere-ruby (0.9.4)
40
42
  faraday (>= 1.0.0)
41
43
  faraday_middleware (>= 1.0.0)
44
+ colorize (0.8.1)
42
45
  concurrent-ruby (1.2.2)
43
46
  crass (1.0.6)
44
47
  diff-lcs (1.5.0)
@@ -250,6 +253,8 @@ GEM
250
253
  ruby-rc4 (0.1.5)
251
254
  ruby2_keywords (0.0.5)
252
255
  rubyzip (2.3.2)
256
+ safe_ruby (1.0.4)
257
+ childprocess (>= 0.3.9)
253
258
  standard (1.28.2)
254
259
  language_server-protocol (~> 3.17.0.2)
255
260
  lint_roller (~> 1.0)
@@ -313,6 +318,7 @@ DEPENDENCIES
313
318
  replicate-ruby (~> 0.2.2)
314
319
  rspec (~> 3.0)
315
320
  ruby-openai (~> 4.0.0)
321
+ safe_ruby (~> 1.0.4)
316
322
  standardrb
317
323
  weaviate-ruby (~> 0.8.0)
318
324
  wikipedia-client (~> 1.17.0)
data/README.md CHANGED
@@ -260,10 +260,10 @@ agent.run(question: "How many full soccer fields would be needed to cover the di
260
260
  | Name | Description | ENV Requirements | Gem Requirements |
261
261
  | ------------ | :------------------------------------------------: | :-----------------------------------------------------------: | :---------------------------------------: |
262
262
  | "calculator" | Useful for getting the result of a math expression | | `gem "eqn", "~> 1.6.5"` |
263
- | "search" | A wrapper around Google Search | `ENV["SERPAPI_API_KEY"]` (https://serpapi.com/manage-api-key) | `gem "google_search_results", "~> 2.0.0"` | |
263
+ | "ruby_code_interpreter" | Interprets Ruby expressions | | `gem "safe_ruby", "~> 1.0.4"` |
264
+ | "search" | A wrapper around Google Search | `ENV["SERPAPI_API_KEY"]` (https://serpapi.com/manage-api-key) | `gem "google_search_results", "~> 2.0.0"` |
264
265
  | "wikipedia" | Calls Wikipedia API to retrieve the summary | | `gem "wikipedia-client", "~> 1.17.0"` |
265
266
 
266
-
267
267
  #### Loaders 🚚
268
268
 
269
269
  Need to read data from various sources? Load it up.
@@ -42,7 +42,7 @@ module Langchain::Agent
42
42
  )
43
43
 
44
44
  loop do
45
- Langchain.logger.info("Agent: Passing the prompt to the #{llm} LLM")
45
+ Langchain.logger.info("[#{self.class.name}]".red + ": Sending the prompt to the #{llm} LLM")
46
46
  response = llm_client.complete(
47
47
  prompt: prompt,
48
48
  stop_sequences: ["Observation:"],
@@ -59,12 +59,11 @@ module Langchain::Agent
59
59
  # Find the input to the action in the "Action Input: [action_input]" format
60
60
  action_input = response.match(/Action Input: "?(.*)"?/)&.send(:[], -1)
61
61
 
62
- Langchain.logger.info("Agent: Using the \"#{action}\" Tool with \"#{action_input}\"")
63
-
64
62
  # Retrieve the Tool::[ToolName] class and call `execute`` with action_input as the input
65
- result = Langchain::Tool
66
- .const_get(Langchain::Tool::Base::TOOLS[action.strip])
67
- .execute(input: action_input)
63
+ tool = Langchain::Tool.const_get(Langchain::Tool::Base::TOOLS[action.strip])
64
+ Langchain.logger.info("[#{self.class.name}]".red + ": Invoking \"#{tool}\" Tool with \"#{action_input}\"")
65
+
66
+ result = tool.execute(input: action_input)
68
67
 
69
68
  # Append the Observation to the prompt
70
69
  prompt += if prompt.end_with?("Observation:")
@@ -18,6 +18,8 @@ module Langchain::Tool
18
18
  # @param input [String] math expression
19
19
  # @return [String] Answer
20
20
  def execute(input:)
21
+ Langchain.logger.info("[#{self.class.name}]".light_blue + ": Executing \"#{input}\"")
22
+
21
23
  Eqn::Calculator.calc(input)
22
24
  rescue Eqn::ParseError, Eqn::NoVariableValueError
23
25
  # Sometimes the input is not a pure math expression, e.g: "12F in Celsius"
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Langchain::Tool
4
+ class RubyCodeInterpreter < Base
5
+ description <<~DESC
6
+ A Ruby code interpreter. Use this to execute ruby expressions. Input should be a valid ruby expression. If you want to see the output of the tool, make sure to return a value.
7
+ DESC
8
+
9
+ def initialize(timeout: 30)
10
+ @timeout = timeout
11
+ depends_on "safe_ruby"
12
+ require "safe_ruby"
13
+ end
14
+
15
+ # @param input [String] ruby code expression
16
+ # @return [String] Answer
17
+ def execute(input:)
18
+ Langchain.logger.info("[#{self.class.name}]".light_blue + ": Executing \"#{input}\"")
19
+
20
+ safe_eval(input)
21
+ end
22
+
23
+ def safe_eval(code)
24
+ SafeRuby.eval(code, timeout: @timeout)
25
+ end
26
+ end
27
+ end
@@ -33,6 +33,8 @@ module Langchain::Tool
33
33
  # TODO: Glance at all of the fields that langchain Python looks through: https://github.com/hwchase17/langchain/blob/v0.0.166/langchain/utilities/serpapi.py#L128-L156
34
34
  # We may need to do the same thing here.
35
35
  def execute(input:)
36
+ Langchain.logger.info("[#{self.class.name}]".light_blue + ": Executing \"#{input}\"")
37
+
36
38
  hash_results = execute_search(input: input)
37
39
 
38
40
  hash_results.dig(:answer_box, :answer) ||
@@ -22,6 +22,8 @@ module Langchain::Tool
22
22
  # @param input [String] search query
23
23
  # @return [String] Answer
24
24
  def execute(input:)
25
+ Langchain.logger.info("[#{self.class.name}]".light_blue + ": Executing \"#{input}\"")
26
+
25
27
  page = ::Wikipedia.find(input)
26
28
  # It would be nice to figure out a way to provide page.content but the LLM token limit is an issue
27
29
  page.summary
data/lib/langchain.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "logger"
4
4
  require "pathname"
5
+ require "colorize"
5
6
 
6
7
  require_relative "./version"
7
8
  require_relative "./dependency_helper"
@@ -13,7 +14,7 @@ module Langchain
13
14
  attr_reader :root
14
15
  end
15
16
 
16
- @logger ||= ::Logger.new($stdout, level: :warn, formatter: ->(severity, datetime, progname, msg) { "[LangChain.rb] #{msg}\n" })
17
+ @logger ||= ::Logger.new($stdout, level: :warn, formatter: ->(severity, datetime, progname, msg) { "[LangChain.rb]".yellow + " #{msg}\n" })
17
18
 
18
19
  @root = Pathname.new(__dir__)
19
20
 
@@ -28,6 +29,7 @@ module Langchain
28
29
  module Tool
29
30
  autoload :Base, "langchain/tool/base"
30
31
  autoload :Calculator, "langchain/tool/calculator"
32
+ autoload :RubyCodeInterpreter, "langchain/tool/ruby_code_interpreter"
31
33
  autoload :SerpApi, "langchain/tool/serp_api"
32
34
  autoload :Wikipedia, "langchain/tool/wikipedia"
33
35
  end
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Langchain
4
- VERSION = "0.4.0"
4
+ VERSION = "0.4.1"
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.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Bondarev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-06-01 00:00:00.000000000 Z
11
+ date: 2023-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tiktoken_ruby
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.0.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: colorize
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.8.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.8.1
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: dotenv-rails
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -276,6 +290,20 @@ dependencies:
276
290
  - - "~>"
277
291
  - !ruby/object:Gem::Version
278
292
  version: 4.0.0
293
+ - !ruby/object:Gem::Dependency
294
+ name: safe_ruby
295
+ requirement: !ruby/object:Gem::Requirement
296
+ requirements:
297
+ - - "~>"
298
+ - !ruby/object:Gem::Version
299
+ version: 1.0.4
300
+ type: :development
301
+ prerelease: false
302
+ version_requirements: !ruby/object:Gem::Requirement
303
+ requirements:
304
+ - - "~>"
305
+ - !ruby/object:Gem::Version
306
+ version: 1.0.4
279
307
  - !ruby/object:Gem::Dependency
280
308
  name: weaviate-ruby
281
309
  requirement: !ruby/object:Gem::Requirement
@@ -353,6 +381,7 @@ files:
353
381
  - lib/langchain/prompt/prompt_template.rb
354
382
  - lib/langchain/tool/base.rb
355
383
  - lib/langchain/tool/calculator.rb
384
+ - lib/langchain/tool/ruby_code_interpreter.rb
356
385
  - lib/langchain/tool/serp_api.rb
357
386
  - lib/langchain/tool/wikipedia.rb
358
387
  - lib/langchain/utils/token_length_validator.rb