aoororachain 0.1.4 → 0.1.5

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: 8fd799856d56a712154c7c6adf5c20e73e34ce7740aa70490dc84c37f7dd7905
4
- data.tar.gz: e4d4a828187141c420cecff71b38c17fdd9556b9f486a21c336ee83a1b6b20d4
3
+ metadata.gz: 5f3616ebd00d6c06568a9b1c5d31a99147eb5f0afe48749f92e8275ee31c8272
4
+ data.tar.gz: 27711347c6c7306a16dace3cc74d931b2585fbfe953ef61166e7a51b38893dca
5
5
  SHA512:
6
- metadata.gz: a157218dc07395105e4f6bda26f2a97df95d882b514a800401fb7a86596d9fe8583575395f4d16797f91800913c9b4fd639826001d5d1360c104fa0896da48e7
7
- data.tar.gz: 554a6f6c5bbac2d094a0d35fa296feb6d5bd7fc999d6579d3d7d44c8161f52acc282b098bdcc6d387a3e5bcd74d2ae29a7602ce04509aa49948308a618d4052e
6
+ metadata.gz: 29096f58726afdfdc68f78f6effc57fcc8e79e645d2fccd51c87ca560d4f9ffed90c66873e9d9e69e795ec882ff6b02f12d951e5d4fd4406a9b7a11e64e56615
7
+ data.tar.gz: 7a67196e23436c158aa259cc2cb5c6e8c6e8cad6a5548ecbdfdd35cd9ca019b0d9e09378272140d9f893479be51fd48dd0f09bc956fe87a57d39345ea591b606
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  ## [Unreleased]
2
2
 
3
- ## [0.1.0] - 2023-05-24
3
+ ## [0.1.5] - 2023-07-10
4
+ - Implements QA Retrieval with additional context.
5
+
6
+ ## [0.1.0] - 2023-06-24
4
7
 
5
8
  - Initial release
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- aoororachain (0.1.4)
4
+ aoororachain (0.1.5)
5
5
  chroma-db (~> 0.6.0)
6
6
  llm_client (~> 0.1.2)
7
7
  pdf-reader (~> 2.11)
data/README.md CHANGED
@@ -138,24 +138,24 @@ collection_name = "ruby-documentation"
138
138
  # You can define a custom Parser to clean data and maybe extract metadata.
139
139
  # Here is the code of RubyDocParser that does exactly that.
140
140
  class RubyDocParser
141
- def self.parse(text)
142
- name_match = text.match(/Name (\w+)/)
143
- constant_match = text.match(/Constant (\w+)/)
144
-
145
- object_match = text.match(/Object (\w+)/)
146
- method_match = text.match(/Method ([\w\[\]\+\=\-\*\%\/]+)/)
147
-
148
- metadata = {}
149
- metadata[:name] = name_match[1] if name_match
150
- metadata[:constant] = constant_match[1] if constant_match
151
- metadata[:object] = object_match[1] if object_match
152
- metadata[:method] = method_match[1] if method_match
153
- metadata[:lang] = :ruby
154
- metadata[:version] = "3.2"
155
-
156
- text.gsub!(/\s+/, " ").strip!
157
- [text, metadata]
158
- end
141
+ def self.parse(text)
142
+ name_match = text.match(/Name (\w+)/)
143
+ constant_match = text.match(/Constant (\w+)/)
144
+
145
+ object_match = text.match(/Object (\w+)/)
146
+ method_match = text.match(/Method ([\w\[\]\+\=\-\*\%\/]+)/)
147
+
148
+ metadata = {}
149
+ metadata[:name] = name_match[1] if name_match
150
+ metadata[:constant] = constant_match[1] if constant_match
151
+ metadata[:object] = object_match[1] if object_match
152
+ metadata[:method] = method_match[1] if method_match
153
+ metadata[:lang] = :ruby
154
+ metadata[:version] = "3.2"
155
+
156
+ text.gsub!(/\s+/, " ").strip!
157
+ [text, metadata]
158
+ end
159
159
  end
160
160
 
161
161
  # A DirectoryLoader points to a path and sets the glob for the files you want to load. 
@@ -170,7 +170,7 @@ text_splitter = Aoororachain::RecursiveTextSplitter.new(size: 512, overlap: 0)
170
170
 
171
171
  texts = []
172
172
  files.each do |file|
173
- texts.concat(text_splitter.split_documents(file))
173
+ texts.concat(text_splitter.split_documents(file))
174
174
  end
175
175
 
176
176
  # The final step is to create and store the embeddings.
@@ -191,7 +191,7 @@ vector_database.from_documents(texts, index: collection_name)
191
191
  With embedding loaded in the database, you can use a tool like Chroma UI -**not yet released** - to query documents.
192
192
  ![chroma-ui](https://github.com/mariochavez/aoororachain/assets/59967/d65dea13-c6ef-452a-9774-8cf3b47c048f)
193
193
 
194
- But it is more useful to query with Aoororachain.
194
+ Now you can query your embeddings with Aoororachain.
195
195
 
196
196
  ```ruby
197
197
  # Define a retriever for the Vector database.
@@ -236,9 +236,29 @@ chain = Aoororachain::Chains::RetrievalQA.new(llm, retriever)
236
236
  # Create a template for the LLM. Aoororachain does not include any templates because these are model specific. The following template is for the Vicuna model.
237
237
  template = "A conversation between a human and an AI assistant. The assistant responds to a question using the context. Context: ===%{context}===. Question: %{prompt}"
238
238
 
239
- response = chain.complete(prompt: "how can I use the Data class to define a new class?", prompt_template: template)
239
+ response = chain.complete(prompt: "given the following array [[1,3], [2,4]], how can I get a flatten and sorted array?", prompt_template: template)
240
+ ```
241
+
242
+ _response_ is a Hash with two keys: _response_ and _sources_.
243
+
244
+ ```ruby
245
+ pp response
246
+ {:response=>
247
+ "User: Assistant: Assistant: To flatten the nested arrays in an array and sort it, you can use Ruby's built-in `sort` method along with the `flatten` method. Here is an example of how to do this for the given array [[1, 3], [2, 4]]:\n" +
248
+ "```ruby\n" +
249
+ "array = [[1, 3], [2, 4]]\n" +
250
+ "sorted_and_flattened_array = array.sort { |a, b| a[0] <=> b[0] }.flat_map(&:to_a)\n" +
251
+ "# Output: [1, 2, 3, 4]\n" +
252
+ "```\n",
253
+ :sources=>
254
+ [{"source"=>"./ruby-docs/hash-flatten.txt", "object"=>"Hash", "method"=>"flatten", "lang"=>"ruby", "version"=>"3.2"},
255
+ {"source"=>"./ruby-docs/array-flatten.txt", "object"=>"Array", "method"=>"flatten", "lang"=>"ruby", "version"=>"3.2"},
256
+ {"source"=>"./ruby-docs/array-flatten.txt", "object"=>"Array", "method"=>"flatten", "lang"=>"ruby", "version"=>"3.2"},
257
+ {"source"=>"./ruby-docs/array-flatten2.txt", "object"=>"Array", "method"=>"flatten", "lang"=>"ruby", "version"=>"3.2"}]}
240
258
  ```
241
259
 
260
+ Where _response_ is tge generated response from the LLM and _sources_ is the list of text chunks that were sent to the LLM as context.
261
+
242
262
  ## Contributing
243
263
 
244
264
  Bug reports and pull requests are welcome on GitHub at https://github.com/mariochavez/aoororachain. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/mariochavez/aoororachain/blob/main/CODE_OF_CONDUCT.md).
@@ -9,22 +9,25 @@ module Aoororachain
9
9
  @type = type
10
10
  end
11
11
 
12
- def complete(prompt:, prompt_template:)
12
+ def complete(prompt:, prompt_template:, additional_context: "")
13
13
  context = @retriever.search(prompt)
14
14
 
15
- stuff_prompt = prompt_template % {context: context.map(&:document).join(" ").tr("\n", " "), prompt:}
15
+ mapped_context = context.map(&:document)
16
+ mapped_context << additional_context if !additional_context.nil? || additional_context != ""
17
+
18
+ stuff_prompt = prompt_template % {context: mapped_context.join(" ").tr("\n", " "), prompt:}
16
19
 
17
20
  success, response = @llm.complete(prompt: stuff_prompt)
18
21
 
19
22
  if success
20
23
  completion = {
21
- response: response,
22
- sources: context.map(&:metadata)
23
- }
24
+ "sources" => context.map(&:metadata)
25
+ }.merge(response)
24
26
  else
25
27
  completion = {
26
- response: "Sorry we had a problem with the LLM",
27
- sources: []
28
+ "response" => "Sorry we had a problem with the LLM",
29
+ "sources" => [],
30
+ "model" => ""
28
31
  }
29
32
  Aoororachain::Util.log_error("Failed to complete", message: response)
30
33
  end
@@ -14,7 +14,7 @@ module Aoororachain
14
14
  def complete(prompt:)
15
15
  result = LlmClient.completion(prompt)
16
16
 
17
- [result.success?, result.success? ? result.success.body["response"].gsub(/Usuario:.*Asistente:/, "") : result.failure.body]
17
+ [result.success?, result.success? ? result.success.body : result.failure.body]
18
18
  end
19
19
  end
20
20
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Aoororachain
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.5"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aoororachain
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Alberto Chávez
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-04 00:00:00.000000000 Z
11
+ date: 2023-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chroma-db