langchainrb 0.6.9 → 0.6.11

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: 4d046a7cae545da3092694fee60b0fa3caa0852492c9bf7dbbe35001d96106e4
4
- data.tar.gz: 8ed841d2359400f9a4700a49b82030fbe3dd3563545eca31b487914e8fb236f7
3
+ metadata.gz: 78d1726db950b4234799cdf34185110411cbc666836225a63ed9d5be6b0a5575
4
+ data.tar.gz: 0ed5595b0aae9dcaa97ee5cbc653cf3c9cb21f6057f5439ec78711ecb1cf6b46
5
5
  SHA512:
6
- metadata.gz: eed2f03cbdd74ef42fafd07524dc6bf5c7ac2278e5e55dd1aad5b2cd87f319a91c8719a6e9ac43ac50cda5525f3defab14cfecc749bdec81075d52b3935b23dd
7
- data.tar.gz: cd4fce88b0f9f545f6d32660879e6fa5044ec74b41dcb41bc29eafb78b307d8b2cb8204856098657d8c9707331c8629c8a707a227fe4d20a5816347f8a66991c
6
+ metadata.gz: c0313618b12b10943e8825aaabd66306e21b0a24fefe9c4593cb4f28b1a03e099781944057ce9423c98851bad7dea07e2832c4ab37d416cf743b126a26a6a308
7
+ data.tar.gz: a2910e93f883bee84288e5bd22644f17b30957a6ae9a696b3adc01dc9b32e2f546b49d46c3587856a883cd4dc1f0a677a3970d357b5fe2dfd9216ce894d06120
data/.env.example CHANGED
@@ -1,4 +1,5 @@
1
1
  AI21_API_KEY=
2
+ ANTHROPIC_API_KEY=
2
3
  CHROMA_URL=
3
4
  COHERE_API_KEY=
4
5
  GOOGLE_PALM_API_KEY=
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.6.10] - 2023-08-01
4
+ - 🗣️ LLMs
5
+ - Introducing Anthropic support
6
+
3
7
  ## [0.6.9] - 2023-07-29
4
8
 
5
9
  ## [0.6.8] - 2023-07-21
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- langchainrb (0.6.9)
4
+ langchainrb (0.6.11)
5
5
  baran (~> 0.1.6)
6
6
  colorize (~> 0.8.1)
7
7
  json-schema (~> 4.0.0)
@@ -33,6 +33,9 @@ GEM
33
33
  public_suffix (>= 2.0.2, < 6.0)
34
34
  afm (0.2.2)
35
35
  ai21 (0.2.1)
36
+ anthropic (0.1.0)
37
+ faraday (>= 1)
38
+ faraday-multipart (>= 1)
36
39
  ast (2.4.2)
37
40
  baran (0.1.7)
38
41
  builder (3.2.4)
@@ -223,7 +226,7 @@ GEM
223
226
  zeitwerk (~> 2.5)
224
227
  rainbow (3.1.1)
225
228
  rake (13.0.6)
226
- rb_sys (0.9.79)
229
+ rb_sys (0.9.81)
227
230
  rdiscount (2.2.7)
228
231
  regexp_parser (2.8.0)
229
232
  replicate-ruby (0.2.2)
@@ -318,6 +321,7 @@ PLATFORMS
318
321
 
319
322
  DEPENDENCIES
320
323
  ai21 (~> 0.2.1)
324
+ anthropic (~> 0.1.0)
321
325
  chroma-db (~> 0.3.0)
322
326
  cohere-ruby (~> 0.9.5)
323
327
  docx (~> 0.8.0)
data/README.md CHANGED
@@ -203,6 +203,16 @@ Add `gem "ai21", "~> 0.2.1"` to your Gemfile.
203
203
  ai21 = Langchain::LLM::AI21.new(api_key: ENV["AI21_API_KEY"])
204
204
  ```
205
205
 
206
+ #### Anthropic
207
+ Add `gem "anthropic", "~> 0.1.0"` to your Gemfile.
208
+ ```ruby
209
+ anthropic = Langchain::LLM::Anthropic.new(api_key: ENV["ANTHROPIC_API_KEY"])
210
+ ```
211
+
212
+ ```ruby
213
+ anthropic.complete(prompt: "What is the meaning of life?")
214
+ ```
215
+
206
216
  ### Using Prompts 📋
207
217
 
208
218
  #### Prompt Templates
@@ -537,7 +547,7 @@ Join us in the [Langchain.rb](https://discord.gg/WDARp7J2n8) Discord server.
537
547
 
538
548
  ## Contributing
539
549
 
540
- Bug reports and pull requests are welcome on GitHub at https://github.com/andreibondarev/langchain.
550
+ Bug reports and pull requests are welcome on GitHub at https://github.com/andreibondarev/langchainrb.
541
551
 
542
552
  ## License
543
553
 
@@ -11,10 +11,6 @@ functions = [
11
11
  controller_name: {
12
12
  type: :string,
13
13
  description: "the controller name, e.g. users_controller"
14
- },
15
- unit: {
16
- type: "string",
17
- enum: %w[celsius fahrenheit]
18
14
  }
19
15
  },
20
16
  required: ["controller_name"]
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Langchain::LLM
4
+ #
5
+ # Wrapper around Anthropic APIs.
6
+ #
7
+ # Gem requirements:
8
+ # gem "anthropic", "~> 0.1.0"
9
+ #
10
+ # Usage:
11
+ # anthorpic = Langchain::LLM::Anthropic.new(api_key:)
12
+ #
13
+ class Anthropic < Base
14
+ DEFAULTS = {
15
+ temperature: 0.0,
16
+ completion_model_name: "claude-2",
17
+ max_tokens_to_sample: 256
18
+ }.freeze
19
+
20
+ # TODO: Implement token length validator for Anthropic
21
+ # LENGTH_VALIDATOR = Langchain::Utils::TokenLength::AnthropicValidator
22
+
23
+ def initialize(api_key:, llm_options: {}, default_options: {})
24
+ depends_on "anthropic"
25
+ require "anthropic"
26
+
27
+ @client = ::Anthropic::Client.new(access_token: api_key, **llm_options)
28
+ @defaults = DEFAULTS.merge(default_options)
29
+ end
30
+
31
+ #
32
+ # Generate a completion for a given prompt
33
+ #
34
+ # @param prompt [String] The prompt to generate a completion for
35
+ # @param params [Hash] extra parameters passed to Anthropic::Client#complete
36
+ # @return [String] The completion
37
+ #
38
+ def complete(prompt:, **params)
39
+ parameters = compose_parameters @defaults[:completion_model_name], params
40
+
41
+ parameters[:prompt] = prompt
42
+
43
+ # TODO: Implement token length validator for Anthropic
44
+ # parameters[:max_tokens_to_sample] = validate_max_tokens(prompt, parameters[:completion_model_name])
45
+
46
+ response = client.complete(parameters: parameters)
47
+ response.dig("completion")
48
+ end
49
+
50
+ private
51
+
52
+ def compose_parameters(model, params)
53
+ default_params = {model: model}.merge(@defaults.except(:completion_model_name))
54
+
55
+ default_params.merge(params)
56
+ end
57
+
58
+ # TODO: Implement token length validator for Anthropic
59
+ # def validate_max_tokens(messages, model)
60
+ # LENGTH_VALIDATOR.validate_max_tokens!(messages, model)
61
+ # end
62
+ end
63
+ end
@@ -89,9 +89,9 @@ module Langchain
89
89
  end
90
90
 
91
91
  def load_from_path
92
- raise FileNotFound unless File.exist?(@path)
92
+ return File.open(@path) if File.exist?(@path)
93
93
 
94
- File.open(@path)
94
+ raise FileNotFound, "File #{@path} does not exist"
95
95
  end
96
96
 
97
97
  def load_from_directory(&block)
@@ -8,7 +8,7 @@ module Langchain::Vectorsearch
8
8
  # Gem requirements: gem "milvus", "~> 0.9.0"
9
9
  #
10
10
  # Usage:
11
- # milvus = Langchain::Vectorsearch::Milvus.new(url:, index_name:, llm:, llm_api_key:)
11
+ # milvus = Langchain::Vectorsearch::Milvus.new(url:, index_name:, llm:, api_key:)
12
12
  #
13
13
 
14
14
  def initialize(url:, index_name:, llm:, api_key: nil)
@@ -32,7 +32,7 @@ module Langchain::Vectorsearch
32
32
  field: Array(texts)
33
33
  }, {
34
34
  field_name: "vectors",
35
- type: ::Milvus::DATA_TYPES["binary_vector"],
35
+ type: ::Milvus::DATA_TYPES["float_vector"],
36
36
  field: Array(texts).map { |text| llm.embed(text: text) }
37
37
  }
38
38
  ]
@@ -47,7 +47,7 @@ module Langchain::Vectorsearch
47
47
  client.collections.create(
48
48
  auto_id: true,
49
49
  collection_name: index_name,
50
- description: "Default schema created by Vectorsearch",
50
+ description: "Default schema created by langchain.rb",
51
51
  fields: [
52
52
  {
53
53
  name: "id",
@@ -66,7 +66,7 @@ module Langchain::Vectorsearch
66
66
  ]
67
67
  }, {
68
68
  name: "vectors",
69
- data_type: ::Milvus::DATA_TYPES["binary_vector"],
69
+ data_type: ::Milvus::DATA_TYPES["float_vector"],
70
70
  is_primary_key: false,
71
71
  type_params: [
72
72
  {
@@ -79,6 +79,20 @@ module Langchain::Vectorsearch
79
79
  )
80
80
  end
81
81
 
82
+ # Create the default index
83
+ # @return [Boolean] The response from the server
84
+ def create_default_index
85
+ client.indices.create(
86
+ collection_name: "Documents",
87
+ field_name: "vectors",
88
+ extra_params: [
89
+ {key: "metric_type", value: "L2"},
90
+ {key: "index_type", value: "IVF_FLAT"},
91
+ {key: "params", value: "{\"nlist\":1024}"}
92
+ ]
93
+ )
94
+ end
95
+
82
96
  # Get the default schema
83
97
  # @return [Hash] The response from the server
84
98
  def get_default_schema
@@ -91,6 +105,12 @@ module Langchain::Vectorsearch
91
105
  client.collections.delete(collection_name: index_name)
92
106
  end
93
107
 
108
+ # Load default schema into memory
109
+ # @return [Boolean] The response from the server
110
+ def load_default_schema
111
+ client.collections.load(collection_name: index_name)
112
+ end
113
+
94
114
  def similarity_search(query:, k: 4)
95
115
  embedding = llm.embed(text: query)
96
116
 
@@ -101,15 +121,36 @@ module Langchain::Vectorsearch
101
121
  end
102
122
 
103
123
  def similarity_search_by_vector(embedding:, k: 4)
124
+ load_default_schema
125
+
104
126
  client.search(
105
127
  collection_name: index_name,
128
+ output_fields: ["id", "content", "vectors"],
106
129
  top_k: k.to_s,
107
130
  vectors: [embedding],
108
131
  dsl_type: 1,
109
132
  params: "{\"nprobe\": 10}",
110
- anns_field: "content",
111
- metric_type: "L2"
133
+ anns_field: "vectors",
134
+ metric_type: "L2",
135
+ vector_type: ::Milvus::DATA_TYPES["float_vector"]
112
136
  )
113
137
  end
138
+
139
+ # Ask a question and return the answer
140
+ # @param question [String] The question to ask
141
+ # @yield [String] Stream responses back one String at a time
142
+ # @return [String] The answer to the question
143
+ def ask(question:, &block)
144
+ search_results = similarity_search(query: question)
145
+
146
+ content_field = search_results.dig("results", "fields_data").select { |field| field.dig("field_name") == "content" }
147
+ content_data = content_field.first.dig("Field", "Scalars", "Data", "StringData", "data")
148
+
149
+ context = content_data.join("\n---\n")
150
+
151
+ prompt = generate_prompt(question: question, context: context)
152
+
153
+ llm.chat(prompt: prompt, &block)
154
+ end
114
155
  end
115
156
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Langchain
4
- VERSION = "0.6.9"
4
+ VERSION = "0.6.11"
5
5
  end
data/lib/langchain.rb CHANGED
@@ -131,6 +131,7 @@ module Langchain
131
131
 
132
132
  module LLM
133
133
  autoload :AI21, "langchain/llm/ai21"
134
+ autoload :Anthropic, "langchain/llm/anthropic"
134
135
  autoload :Base, "langchain/llm/base"
135
136
  autoload :Cohere, "langchain/llm/cohere"
136
137
  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.6.9
4
+ version: 0.6.11
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-07-29 00:00:00.000000000 Z
11
+ date: 2023-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: baran
@@ -136,6 +136,20 @@ dependencies:
136
136
  - - "~>"
137
137
  - !ruby/object:Gem::Version
138
138
  version: 0.2.1
139
+ - !ruby/object:Gem::Dependency
140
+ name: anthropic
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 0.1.0
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 0.1.0
139
153
  - !ruby/object:Gem::Dependency
140
154
  name: chroma-db
141
155
  requirement: !ruby/object:Gem::Requirement
@@ -518,6 +532,7 @@ files:
518
532
  - lib/langchain/data.rb
519
533
  - lib/langchain/dependency_helper.rb
520
534
  - lib/langchain/llm/ai21.rb
535
+ - lib/langchain/llm/anthropic.rb
521
536
  - lib/langchain/llm/base.rb
522
537
  - lib/langchain/llm/cohere.rb
523
538
  - lib/langchain/llm/google_palm.rb