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 +4 -4
- data/CHANGELOG.md +4 -1
- data/Gemfile.lock +1 -1
- data/README.md +41 -21
- data/lib/aoororachain/chains/retrieval_qa.rb +10 -7
- data/lib/aoororachain/llms/llama_server.rb +1 -1
- data/lib/aoororachain/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5f3616ebd00d6c06568a9b1c5d31a99147eb5f0afe48749f92e8275ee31c8272
|
4
|
+
data.tar.gz: 27711347c6c7306a16dace3cc74d931b2585fbfe953ef61166e7a51b38893dca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29096f58726afdfdc68f78f6effc57fcc8e79e645d2fccd51c87ca560d4f9ffed90c66873e9d9e69e795ec882ff6b02f12d951e5d4fd4406a9b7a11e64e56615
|
7
|
+
data.tar.gz: 7a67196e23436c158aa259cc2cb5c6e8c6e8cad6a5548ecbdfdd35cd9ca019b0d9e09378272140d9f893479be51fd48dd0f09bc956fe87a57d39345ea591b606
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
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
|

|
193
193
|
|
194
|
-
|
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: "
|
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
|
-
|
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
|
-
|
22
|
-
|
23
|
-
}
|
24
|
+
"sources" => context.map(&:metadata)
|
25
|
+
}.merge(response)
|
24
26
|
else
|
25
27
|
completion = {
|
26
|
-
response
|
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
|
17
|
+
[result.success?, result.success? ? result.success.body : result.failure.body]
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
data/lib/aoororachain/version.rb
CHANGED
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
|
+
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-
|
11
|
+
date: 2023-07-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: chroma-db
|