langchainrb 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/.env.example +2 -1
  3. data/.rubocop.yml +11 -0
  4. data/CHANGELOG.md +13 -0
  5. data/Gemfile +2 -0
  6. data/Gemfile.lock +14 -1
  7. data/README.md +42 -7
  8. data/Rakefile +5 -0
  9. data/examples/pdf_store_and_query_with_chroma.rb +1 -2
  10. data/examples/store_and_query_with_pinecone.rb +1 -2
  11. data/examples/store_and_query_with_qdrant.rb +1 -2
  12. data/examples/store_and_query_with_weaviate.rb +1 -2
  13. data/lefthook.yml +5 -0
  14. data/lib/langchain/agent/chain_of_thought_agent/chain_of_thought_agent.rb +6 -10
  15. data/lib/langchain/agent/sql_query_agent/sql_query_agent.rb +78 -0
  16. data/lib/langchain/agent/sql_query_agent/sql_query_agent_answer_prompt.json +10 -0
  17. data/lib/langchain/agent/sql_query_agent/sql_query_agent_sql_prompt.json +10 -0
  18. data/lib/langchain/dependency_helper.rb +34 -0
  19. data/lib/langchain/llm/ai21.rb +45 -0
  20. data/lib/langchain/llm/base.rb +2 -19
  21. data/lib/langchain/llm/cohere.rb +9 -0
  22. data/lib/langchain/llm/google_palm.rb +7 -0
  23. data/lib/langchain/llm/hugging_face.rb +9 -0
  24. data/lib/langchain/llm/openai.rb +33 -41
  25. data/lib/langchain/llm/replicate.rb +5 -2
  26. data/lib/langchain/processors/base.rb +2 -0
  27. data/lib/langchain/processors/xlsx.rb +27 -0
  28. data/lib/langchain/prompt/base.rb +8 -4
  29. data/lib/langchain/prompt/loading.rb +6 -1
  30. data/lib/langchain/prompt/prompt_template.rb +1 -1
  31. data/lib/langchain/tool/base.rb +4 -1
  32. data/lib/langchain/tool/calculator.rb +9 -0
  33. data/lib/langchain/tool/database.rb +45 -0
  34. data/lib/langchain/tool/ruby_code_interpreter.rb +6 -0
  35. data/lib/langchain/tool/serp_api.rb +5 -1
  36. data/lib/langchain/tool/wikipedia.rb +4 -0
  37. data/lib/langchain/vectorsearch/base.rb +8 -14
  38. data/lib/langchain/vectorsearch/chroma.rb +15 -7
  39. data/lib/langchain/vectorsearch/milvus.rb +13 -4
  40. data/lib/langchain/vectorsearch/pgvector.rb +15 -8
  41. data/lib/langchain/vectorsearch/pinecone.rb +15 -7
  42. data/lib/langchain/vectorsearch/qdrant.rb +15 -7
  43. data/lib/langchain/vectorsearch/weaviate.rb +15 -7
  44. data/lib/{version.rb → langchain/version.rb} +1 -1
  45. data/lib/langchain.rb +6 -2
  46. metadata +82 -4
  47. data/lib/dependency_helper.rb +0 -30
@@ -2,13 +2,21 @@
2
2
 
3
3
  module Langchain::Vectorsearch
4
4
  class Qdrant < Base
5
+ #
6
+ # Wrapper around Qdrant
7
+ #
8
+ # Gem requirements: gem "qdrant-ruby", "~> 0.9.0"
9
+ #
10
+ # Usage:
11
+ # qdrant = Langchain::Vectorsearch::Qdrant.new(url:, api_key:, index_name:, llm:, llm_api_key:)
12
+ #
13
+
5
14
  # Initialize the Qdrant client
6
15
  # @param url [String] The URL of the Qdrant server
7
16
  # @param api_key [String] The API key to use
8
17
  # @param index_name [String] The name of the index to use
9
- # @param llm [Symbol] The LLM to use
10
- # @param llm_api_key [String] The API key for the LLM
11
- def initialize(url:, api_key:, index_name:, llm:, llm_api_key:)
18
+ # @param llm [Object] The LLM client to use
19
+ def initialize(url:, api_key:, index_name:, llm:)
12
20
  depends_on "qdrant-ruby"
13
21
  require "qdrant"
14
22
 
@@ -18,7 +26,7 @@ module Langchain::Vectorsearch
18
26
  )
19
27
  @index_name = index_name
20
28
 
21
- super(llm: llm, llm_api_key: llm_api_key)
29
+ super(llm: llm)
22
30
  end
23
31
 
24
32
  # Add a list of texts to the index
@@ -29,7 +37,7 @@ module Langchain::Vectorsearch
29
37
 
30
38
  Array(texts).each do |text|
31
39
  batch[:ids].push(SecureRandom.uuid)
32
- batch[:vectors].push(llm_client.embed(text: text))
40
+ batch[:vectors].push(llm.embed(text: text))
33
41
  batch[:payloads].push({content: text})
34
42
  end
35
43
 
@@ -59,7 +67,7 @@ module Langchain::Vectorsearch
59
67
  query:,
60
68
  k: 4
61
69
  )
62
- embedding = llm_client.embed(text: query)
70
+ embedding = llm.embed(text: query)
63
71
 
64
72
  similarity_search_by_vector(
65
73
  embedding: embedding,
@@ -96,7 +104,7 @@ module Langchain::Vectorsearch
96
104
 
97
105
  prompt = generate_prompt(question: question, context: context)
98
106
 
99
- llm_client.chat(prompt: prompt)
107
+ llm.chat(prompt: prompt)
100
108
  end
101
109
  end
102
110
  end
@@ -2,13 +2,21 @@
2
2
 
3
3
  module Langchain::Vectorsearch
4
4
  class Weaviate < Base
5
+ #
6
+ # Wrapper around Weaviate
7
+ #
8
+ # Gem requirements: gem "weaviate-ruby", "~> 0.8.0"
9
+ #
10
+ # Usage:
11
+ # weaviate = Langchain::Vectorsearch::Weaviate.new(url:, api_key:, index_name:, llm:, llm_api_key:)
12
+ #
13
+
5
14
  # Initialize the Weaviate adapter
6
15
  # @param url [String] The URL of the Weaviate instance
7
16
  # @param api_key [String] The API key to use
8
17
  # @param index_name [String] The name of the index to use
9
- # @param llm [Symbol] The LLM to use
10
- # @param llm_api_key [String] The API key for the LLM
11
- def initialize(url:, api_key:, index_name:, llm:, llm_api_key:)
18
+ # @param llm [Object] The LLM client to use
19
+ def initialize(url:, api_key:, index_name:, llm:)
12
20
  depends_on "weaviate-ruby"
13
21
  require "weaviate"
14
22
 
@@ -18,7 +26,7 @@ module Langchain::Vectorsearch
18
26
  )
19
27
  @index_name = index_name
20
28
 
21
- super(llm: llm, llm_api_key: llm_api_key)
29
+ super(llm: llm)
22
30
  end
23
31
 
24
32
  # Add a list of texts to the index
@@ -29,7 +37,7 @@ module Langchain::Vectorsearch
29
37
  {
30
38
  class: index_name,
31
39
  properties: {content: text},
32
- vector: llm_client.embed(text: text)
40
+ vector: llm.embed(text: text)
33
41
  }
34
42
  end
35
43
 
@@ -58,7 +66,7 @@ module Langchain::Vectorsearch
58
66
  # @param k [Integer|String] The number of results to return
59
67
  # @return [Hash] The search results
60
68
  def similarity_search(query:, k: 4)
61
- embedding = llm_client.embed(text: query)
69
+ embedding = llm.embed(text: query)
62
70
 
63
71
  similarity_search_by_vector(embedding: embedding, k: k)
64
72
  end
@@ -91,7 +99,7 @@ module Langchain::Vectorsearch
91
99
 
92
100
  prompt = generate_prompt(question: question, context: context)
93
101
 
94
- llm_client.chat(prompt: prompt)
102
+ llm.chat(prompt: prompt)
95
103
  end
96
104
  end
97
105
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Langchain
4
- VERSION = "0.4.1"
4
+ VERSION = "0.5.0"
5
5
  end
data/lib/langchain.rb CHANGED
@@ -4,8 +4,7 @@ require "logger"
4
4
  require "pathname"
5
5
  require "colorize"
6
6
 
7
- require_relative "./version"
8
- require_relative "./dependency_helper"
7
+ require_relative "./langchain/version"
9
8
 
10
9
  module Langchain
11
10
  class << self
@@ -20,10 +19,12 @@ module Langchain
20
19
 
21
20
  autoload :Loader, "langchain/loader"
22
21
  autoload :Data, "langchain/data"
22
+ autoload :DependencyHelper, "langchain/dependency_helper"
23
23
 
24
24
  module Agent
25
25
  autoload :Base, "langchain/agent/base"
26
26
  autoload :ChainOfThoughtAgent, "langchain/agent/chain_of_thought_agent/chain_of_thought_agent.rb"
27
+ autoload :SQLQueryAgent, "langchain/agent/sql_query_agent/sql_query_agent.rb"
27
28
  end
28
29
 
29
30
  module Tool
@@ -32,6 +33,7 @@ module Langchain
32
33
  autoload :RubyCodeInterpreter, "langchain/tool/ruby_code_interpreter"
33
34
  autoload :SerpApi, "langchain/tool/serp_api"
34
35
  autoload :Wikipedia, "langchain/tool/wikipedia"
36
+ autoload :Database, "langchain/tool/database"
35
37
  end
36
38
 
37
39
  module Processors
@@ -43,6 +45,7 @@ module Langchain
43
45
  autoload :JSONL, "langchain/processors/jsonl"
44
46
  autoload :PDF, "langchain/processors/pdf"
45
47
  autoload :Text, "langchain/processors/text"
48
+ autoload :Xlsx, "langchain/processors/xlsx"
46
49
  end
47
50
 
48
51
  module Utils
@@ -60,6 +63,7 @@ module Langchain
60
63
  end
61
64
 
62
65
  module LLM
66
+ autoload :AI21, "langchain/llm/ai21"
63
67
  autoload :Base, "langchain/llm/base"
64
68
  autoload :Cohere, "langchain/llm/cohere"
65
69
  autoload :GooglePalm, "langchain/llm/google_palm"
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.1
4
+ version: 0.5.0
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-02 00:00:00.000000000 Z
11
+ date: 2023-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tiktoken_ruby
@@ -66,6 +66,48 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 3.10.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: yard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rdiscount
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: ai21
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.2.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.2.0
69
111
  - !ruby/object:Gem::Dependency
70
112
  name: chroma-db
71
113
  requirement: !ruby/object:Gem::Requirement
@@ -276,6 +318,20 @@ dependencies:
276
318
  - - "~>"
277
319
  - !ruby/object:Gem::Version
278
320
  version: 0.9.0
321
+ - !ruby/object:Gem::Dependency
322
+ name: roo
323
+ requirement: !ruby/object:Gem::Requirement
324
+ requirements:
325
+ - - "~>"
326
+ - !ruby/object:Gem::Version
327
+ version: 2.10.0
328
+ type: :development
329
+ prerelease: false
330
+ version_requirements: !ruby/object:Gem::Requirement
331
+ requirements:
332
+ - - "~>"
333
+ - !ruby/object:Gem::Version
334
+ version: 2.10.0
279
335
  - !ruby/object:Gem::Dependency
280
336
  name: ruby-openai
281
337
  requirement: !ruby/object:Gem::Requirement
@@ -304,6 +360,20 @@ dependencies:
304
360
  - - "~>"
305
361
  - !ruby/object:Gem::Version
306
362
  version: 1.0.4
363
+ - !ruby/object:Gem::Dependency
364
+ name: sequel
365
+ requirement: !ruby/object:Gem::Requirement
366
+ requirements:
367
+ - - "~>"
368
+ - !ruby/object:Gem::Version
369
+ version: 5.68.0
370
+ type: :development
371
+ prerelease: false
372
+ version_requirements: !ruby/object:Gem::Requirement
373
+ requirements:
374
+ - - "~>"
375
+ - !ruby/object:Gem::Version
376
+ version: 5.68.0
307
377
  - !ruby/object:Gem::Dependency
308
378
  name: weaviate-ruby
309
379
  requirement: !ruby/object:Gem::Requirement
@@ -341,6 +411,7 @@ extra_rdoc_files: []
341
411
  files:
342
412
  - ".env.example"
343
413
  - ".rspec"
414
+ - ".rubocop.yml"
344
415
  - CHANGELOG.md
345
416
  - Gemfile
346
417
  - Gemfile.lock
@@ -353,12 +424,17 @@ files:
353
424
  - examples/store_and_query_with_pinecone.rb
354
425
  - examples/store_and_query_with_qdrant.rb
355
426
  - examples/store_and_query_with_weaviate.rb
356
- - lib/dependency_helper.rb
427
+ - lefthook.yml
357
428
  - lib/langchain.rb
358
429
  - lib/langchain/agent/base.rb
359
430
  - lib/langchain/agent/chain_of_thought_agent/chain_of_thought_agent.rb
360
431
  - lib/langchain/agent/chain_of_thought_agent/chain_of_thought_agent_prompt.json
432
+ - lib/langchain/agent/sql_query_agent/sql_query_agent.rb
433
+ - lib/langchain/agent/sql_query_agent/sql_query_agent_answer_prompt.json
434
+ - lib/langchain/agent/sql_query_agent/sql_query_agent_sql_prompt.json
361
435
  - lib/langchain/data.rb
436
+ - lib/langchain/dependency_helper.rb
437
+ - lib/langchain/llm/ai21.rb
362
438
  - lib/langchain/llm/base.rb
363
439
  - lib/langchain/llm/cohere.rb
364
440
  - lib/langchain/llm/google_palm.rb
@@ -375,12 +451,14 @@ files:
375
451
  - lib/langchain/processors/jsonl.rb
376
452
  - lib/langchain/processors/pdf.rb
377
453
  - lib/langchain/processors/text.rb
454
+ - lib/langchain/processors/xlsx.rb
378
455
  - lib/langchain/prompt/base.rb
379
456
  - lib/langchain/prompt/few_shot_prompt_template.rb
380
457
  - lib/langchain/prompt/loading.rb
381
458
  - lib/langchain/prompt/prompt_template.rb
382
459
  - lib/langchain/tool/base.rb
383
460
  - lib/langchain/tool/calculator.rb
461
+ - lib/langchain/tool/database.rb
384
462
  - lib/langchain/tool/ruby_code_interpreter.rb
385
463
  - lib/langchain/tool/serp_api.rb
386
464
  - lib/langchain/tool/wikipedia.rb
@@ -392,8 +470,8 @@ files:
392
470
  - lib/langchain/vectorsearch/pinecone.rb
393
471
  - lib/langchain/vectorsearch/qdrant.rb
394
472
  - lib/langchain/vectorsearch/weaviate.rb
473
+ - lib/langchain/version.rb
395
474
  - lib/langchainrb.rb
396
- - lib/version.rb
397
475
  - sig/langchain.rbs
398
476
  homepage: https://rubygems.org/gems/langchainrb
399
477
  licenses:
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- VersionError = Class.new(ScriptError)
4
-
5
- # This method requires and loads the given gem, and then checks to see if the version of the gem meets the requirements listed in `langchain.gemspec`
6
- # This solution was built to avoid auto-loading every single gem in the Gemfile when the developer will mostly likely be only using a few of them.
7
- #
8
- # @param gem_name [String] The name of the gem to load
9
- # @return [Boolean] Whether or not the gem was loaded successfully
10
- # @raise [LoadError] If the gem is not installed
11
- # @raise [VersionError] If the gem is installed, but the version does not meet the requirements
12
- #
13
- def depends_on(gem_name)
14
- gem(gem_name) # require the gem
15
-
16
- return(true) unless defined?(Bundler) # If we're in a non-bundler environment, we're no longer able to determine if we'll meet requirements
17
-
18
- gem_version = Gem.loaded_specs[gem_name].version
19
- gem_requirement = Bundler.load.dependencies.find { |g| g.name == gem_name }&.requirement
20
-
21
- raise LoadError unless gem_requirement
22
-
23
- unless gem_requirement.satisfied_by?(gem_version)
24
- raise VersionError, "The #{gem_name} gem is installed, but version #{gem_requirement} is required. You have #{gem_version}."
25
- end
26
-
27
- true
28
- rescue LoadError
29
- raise LoadError, "Could not load #{gem_name}. Please ensure that the #{gem_name} gem is installed."
30
- end