langchainrb 0.6.10 → 0.6.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ba1fb0e3fbc05e4279fe3a698ad8fb1a25e02788991a8e6a7b27b411771096f3
4
- data.tar.gz: 309cda1c8c7a4982b22c6ad2f82c20fb12ca3bdfdc3e8c0ebeaa9687a7f71ce0
3
+ metadata.gz: 78d1726db950b4234799cdf34185110411cbc666836225a63ed9d5be6b0a5575
4
+ data.tar.gz: 0ed5595b0aae9dcaa97ee5cbc653cf3c9cb21f6057f5439ec78711ecb1cf6b46
5
5
  SHA512:
6
- metadata.gz: 3457cbad7efbc5504f4cb3b684e3837984be5d486c1ee21a718508d606dd63ccc2223dffc55f3cc9c52f5fd0a533b364407bbb4c3208515e2bab8ca2af9ea60a
7
- data.tar.gz: 1ff3cded239c286ee87d7a2f0a1cfa45c734b185e35b6e6fbc63fd2951f7c7397e8562fa145a16f545ef07b2e983f40715010e9c96653b5f2bf128325a5a7577
6
+ metadata.gz: c0313618b12b10943e8825aaabd66306e21b0a24fefe9c4593cb4f28b1a03e099781944057ce9423c98851bad7dea07e2832c4ab37d416cf743b126a26a6a308
7
+ data.tar.gz: a2910e93f883bee84288e5bd22644f17b30957a6ae9a696b3adc01dc9b32e2f546b49d46c3587856a883cd4dc1f0a677a3970d357b5fe2dfd9216ce894d06120
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- langchainrb (0.6.10)
4
+ langchainrb (0.6.11)
5
5
  baran (~> 0.1.6)
6
6
  colorize (~> 0.8.1)
7
7
  json-schema (~> 4.0.0)
@@ -226,7 +226,7 @@ GEM
226
226
  zeitwerk (~> 2.5)
227
227
  rainbow (3.1.1)
228
228
  rake (13.0.6)
229
- rb_sys (0.9.79)
229
+ rb_sys (0.9.81)
230
230
  rdiscount (2.2.7)
231
231
  regexp_parser (2.8.0)
232
232
  replicate-ruby (0.2.2)
data/README.md CHANGED
@@ -209,6 +209,10 @@ Add `gem "anthropic", "~> 0.1.0"` to your Gemfile.
209
209
  anthropic = Langchain::LLM::Anthropic.new(api_key: ENV["ANTHROPIC_API_KEY"])
210
210
  ```
211
211
 
212
+ ```ruby
213
+ anthropic.complete(prompt: "What is the meaning of life?")
214
+ ```
215
+
212
216
  ### Using Prompts 📋
213
217
 
214
218
  #### Prompt Templates
@@ -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"]
@@ -13,7 +13,8 @@ module Langchain::LLM
13
13
  class Anthropic < Base
14
14
  DEFAULTS = {
15
15
  temperature: 0.0,
16
- completion_model_name: "claude-2"
16
+ completion_model_name: "claude-2",
17
+ max_tokens_to_sample: 256
17
18
  }.freeze
18
19
 
19
20
  # TODO: Implement token length validator for Anthropic
@@ -49,7 +50,7 @@ module Langchain::LLM
49
50
  private
50
51
 
51
52
  def compose_parameters(model, params)
52
- default_params = {model: model, temperature: @defaults[:temperature]}
53
+ default_params = {model: model}.merge(@defaults.except(:completion_model_name))
53
54
 
54
55
  default_params.merge(params)
55
56
  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)
@@ -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.10"
4
+ VERSION = "0.6.11"
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.6.10
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-08-01 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