langchainrb 0.3.9 → 0.3.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.env.example +2 -0
- data/CHANGELOG.md +9 -0
- data/Gemfile +0 -4
- data/Gemfile.lock +23 -18
- data/README.md +27 -9
- data/examples/pdf_store_and_query_with_chroma.rb +6 -1
- data/lib/dependency_helper.rb +8 -4
- data/lib/langchain.rb +4 -0
- data/lib/llm/base.rb +4 -2
- data/lib/llm/google_palm.rb +85 -0
- data/lib/llm/replicate.rb +88 -0
- data/lib/loaders/html.rb +38 -0
- data/lib/prompt/base.rb +4 -2
- data/lib/prompt/prompt_template.rb +2 -2
- data/lib/version.rb +1 -1
- metadata +59 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff7f99d961b09e827df297ddb3144821c9103fd40eabb32688ca92588a73415c
|
4
|
+
data.tar.gz: bb83eaa99055cf45cceaccb18a84e9fd4ee3ea4a93a6a0c66e04ede43e5d4bc0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40e5362520220d3ffc1b4c29c3e430b051de334c2f281d9cb7d7549a93be40b26b379dbd35d0c91ccb5010c1a495a653e31768f1b7a95bc087059d59339fd1a7
|
7
|
+
data.tar.gz: 04f24944b590ee8b577419a95718ad6796bc4cdc34d52cf05e287806912c05ba8ace22e07181a8537892124881b65a8a221e1c75d8c6245231dd5660c6b4308c
|
data/.env.example
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,14 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.3.11] - 2023-05-23
|
4
|
+
- 🗣️ LLMs
|
5
|
+
- Introducing support for Google PaLM (Pathways Language Model)
|
6
|
+
- Bug fixes and improvements
|
7
|
+
|
8
|
+
## [0.3.10] - 2023-05-19
|
9
|
+
- 🗣️ LLMs
|
10
|
+
- Introducing support for Replicate.com
|
11
|
+
|
3
12
|
## [0.3.9] - 2023-05-19
|
4
13
|
- 🚚 Loaders
|
5
14
|
- Introduce `Loaders::Docx` to parse .docx files
|
data/Gemfile
CHANGED
@@ -10,7 +10,3 @@ gem "rake", "~> 13.0"
|
|
10
10
|
gem "rspec", "~> 3.0"
|
11
11
|
|
12
12
|
gem "standardrb"
|
13
|
-
|
14
|
-
# TODO: Remove this once a new version of the gem is released
|
15
|
-
# The new version of `docx` needs to include this fix: https://github.com/ruby-docx/docx/pull/130
|
16
|
-
gem "docx", branch: "master", git: "https://github.com/ruby-docx/docx.git"
|
data/Gemfile.lock
CHANGED
@@ -1,16 +1,7 @@
|
|
1
|
-
GIT
|
2
|
-
remote: https://github.com/ruby-docx/docx.git
|
3
|
-
revision: 08b734da349d980d2d7549b907459a9e2aa5d3bb
|
4
|
-
branch: master
|
5
|
-
specs:
|
6
|
-
docx (0.7.0)
|
7
|
-
nokogiri (~> 1.13, >= 1.13.0)
|
8
|
-
rubyzip (~> 2.0)
|
9
|
-
|
10
1
|
PATH
|
11
2
|
remote: .
|
12
3
|
specs:
|
13
|
-
langchainrb (0.3.
|
4
|
+
langchainrb (0.3.11)
|
14
5
|
|
15
6
|
GEM
|
16
7
|
remote: https://rubygems.org/
|
@@ -44,12 +35,15 @@ GEM
|
|
44
35
|
dry-monads (~> 1.6)
|
45
36
|
ruby-next-core (>= 0.15.0)
|
46
37
|
coderay (1.1.3)
|
47
|
-
cohere-ruby (0.9.
|
48
|
-
faraday (
|
49
|
-
faraday_middleware (
|
38
|
+
cohere-ruby (0.9.4)
|
39
|
+
faraday (>= 1.0.0)
|
40
|
+
faraday_middleware (>= 1.0.0)
|
50
41
|
concurrent-ruby (1.2.2)
|
51
42
|
crass (1.0.6)
|
52
43
|
diff-lcs (1.5.0)
|
44
|
+
docx (0.8.0)
|
45
|
+
nokogiri (~> 1.13, >= 1.13.0)
|
46
|
+
rubyzip (~> 2.0)
|
53
47
|
dotenv (2.7.6)
|
54
48
|
dotenv-rails (2.7.6)
|
55
49
|
dotenv (= 2.7.6)
|
@@ -123,6 +117,9 @@ GEM
|
|
123
117
|
faraday-retry (1.0.3)
|
124
118
|
faraday_middleware (1.2.0)
|
125
119
|
faraday (~> 1.0)
|
120
|
+
google_palm_api (0.1.0)
|
121
|
+
faraday (>= 1.0.0)
|
122
|
+
faraday_middleware (>= 1.0.0)
|
126
123
|
google_search_results (2.0.1)
|
127
124
|
graphlient (0.6.0)
|
128
125
|
faraday (>= 1.0)
|
@@ -136,8 +133,8 @@ GEM
|
|
136
133
|
httparty (0.21.0)
|
137
134
|
mini_mime (>= 1.0.0)
|
138
135
|
multi_xml (>= 0.5.2)
|
139
|
-
hugging-face (0.3.
|
140
|
-
faraday (
|
136
|
+
hugging-face (0.3.4)
|
137
|
+
faraday (>= 1.0)
|
141
138
|
i18n (1.13.0)
|
142
139
|
concurrent-ruby (~> 1.0)
|
143
140
|
ice_nine (0.11.2)
|
@@ -203,6 +200,11 @@ GEM
|
|
203
200
|
rainbow (3.1.1)
|
204
201
|
rake (13.0.6)
|
205
202
|
regexp_parser (2.8.0)
|
203
|
+
replicate-ruby (0.2.2)
|
204
|
+
addressable
|
205
|
+
faraday (>= 1.0)
|
206
|
+
faraday-multipart
|
207
|
+
faraday-retry
|
206
208
|
rexml (3.2.5)
|
207
209
|
rspec (3.12.0)
|
208
210
|
rspec-core (~> 3.12.0)
|
@@ -276,19 +278,22 @@ PLATFORMS
|
|
276
278
|
|
277
279
|
DEPENDENCIES
|
278
280
|
chroma-db (~> 0.3.0)
|
279
|
-
cohere-ruby (~> 0.9.
|
280
|
-
docx
|
281
|
+
cohere-ruby (~> 0.9.4)
|
282
|
+
docx (~> 0.8.0)
|
281
283
|
dotenv-rails (~> 2.7.6)
|
282
284
|
eqn (~> 1.6.5)
|
285
|
+
google_palm_api (~> 0.1.0)
|
283
286
|
google_search_results (~> 2.0.0)
|
284
|
-
hugging-face (~> 0.3.
|
287
|
+
hugging-face (~> 0.3.4)
|
285
288
|
langchainrb!
|
286
289
|
milvus (~> 0.9.0)
|
290
|
+
nokogiri (~> 1.13)
|
287
291
|
pdf-reader (~> 1.4)
|
288
292
|
pinecone (~> 0.1.6)
|
289
293
|
pry-byebug (~> 3.10.0)
|
290
294
|
qdrant-ruby (~> 0.9.0)
|
291
295
|
rake (~> 13.0)
|
296
|
+
replicate-ruby (~> 0.2.2)
|
292
297
|
rspec (~> 3.0)
|
293
298
|
ruby-openai (~> 4.0.0)
|
294
299
|
standardrb
|
data/README.md
CHANGED
@@ -28,13 +28,13 @@ require "langchain"
|
|
28
28
|
|
29
29
|
#### Supported vector search databases and features:
|
30
30
|
|
31
|
-
| Database | Querying | Storage | Schema Management | Backups | Rails Integration |
|
32
|
-
| -------- |:------------------:| -------:| -----------------:| -------:| -----------------:|
|
33
|
-
| Chroma
|
34
|
-
| Milvus
|
35
|
-
| Pinecone | :white_check_mark: |
|
36
|
-
| Qdrant
|
37
|
-
| Weaviate | :white_check_mark: |
|
31
|
+
| Database | Querying | Storage | Schema Management | Backups | Rails Integration |
|
32
|
+
| -------- |:------------------:| -------:| -----------------:| -------:| -----------------:|
|
33
|
+
| [Chroma](https://trychroma.com/) | :white_check_mark: | :white_check_mark: | :white_check_mark: | WIP | WIP |
|
34
|
+
| [Milvus](https://milvus.io/) | :white_check_mark: | :white_check_mark: | :white_check_mark: | WIP | WIP |
|
35
|
+
| [Pinecone](https://www.pinecone.io/) | :white_check_mark: | :white_check_mark: | :white_check_mark: | WIP | WIP |
|
36
|
+
| [Qdrant](https://qdrant.tech/) | :white_check_mark: | :white_check_mark: | :white_check_mark: | WIP | WIP |
|
37
|
+
| [Weaviate](https://weaviate.io/) | :white_check_mark: | :white_check_mark: | :white_check_mark: | WIP | WIP |
|
38
38
|
|
39
39
|
### Using Vector Search Databases 🔍
|
40
40
|
|
@@ -76,8 +76,9 @@ client.add_texts(
|
|
76
76
|
# Store the contents of your files in your vector search database
|
77
77
|
my_pdf = Langchain.root.join("path/to/my.pdf")
|
78
78
|
my_text = Langchain.root.join("path/to/my.txt")
|
79
|
+
my_docx = Langchain.root.join("path/to/my.docx")
|
79
80
|
|
80
|
-
client.add_data(paths: [my_pdf, my_text])
|
81
|
+
client.add_data(paths: [my_pdf, my_text, my_docx])
|
81
82
|
```
|
82
83
|
```ruby
|
83
84
|
# Retrieve similar documents based on the query string passed in
|
@@ -130,6 +131,21 @@ cohere.complete(prompt: "What is the meaning of life?")
|
|
130
131
|
|
131
132
|
#### HuggingFace
|
132
133
|
Add `gem "hugging-face", "~> 0.3.2"` to your Gemfile.
|
134
|
+
```ruby
|
135
|
+
cohere = LLM::HuggingFace.new(api_key: ENV["HUGGING_FACE_API_KEY"])
|
136
|
+
```
|
137
|
+
|
138
|
+
#### Replicate
|
139
|
+
Add `gem "replicate-ruby", "~> 0.2.2"` to your Gemfile.
|
140
|
+
```ruby
|
141
|
+
cohere = LLM::Replicate.new(api_key: ENV["REPLICATE_API_KEY"])
|
142
|
+
```
|
143
|
+
|
144
|
+
#### Google PaLM (Pathways Language Model)
|
145
|
+
Add `"google_palm_api", "~> 0.1.0"` to your Gemfile.
|
146
|
+
```ruby
|
147
|
+
google_palm = LLM::GooglePalm.new(api_key: ENV["GOOGLE_PALM_API_KEY"])
|
148
|
+
```
|
133
149
|
|
134
150
|
### Using Prompts 📋
|
135
151
|
|
@@ -254,6 +270,8 @@ Need to read data from various sources? Load it up.
|
|
254
270
|
|
255
271
|
| Name | Class | Gem Requirements |
|
256
272
|
| ---- | ------------- | :--------------------------: |
|
273
|
+
| docx | Loaders::Docx | `gem "docx", "~> 0.8.0"` |
|
274
|
+
| html | Loaders::HTML | `gem "nokogiri", "~> 1.13"` |
|
257
275
|
| pdf | Loaders::PDF | `gem "pdf-reader", "~> 1.4"` |
|
258
276
|
| text | Loaders::Text | |
|
259
277
|
|
@@ -273,7 +291,7 @@ Langchain.logger.level = :info
|
|
273
291
|
|
274
292
|
1. `git clone https://github.com/andreibondarev/langchainrb.git`
|
275
293
|
2. `cp .env.example .env`, then fill out the environment variables in `.env`
|
276
|
-
3. `
|
294
|
+
3. `bundle exec rake` to ensure that the tests pass and to run standardrb
|
277
295
|
4. `bin/console` to load the gem in a REPL session. Feel free to add your own instances of LLMs, Tools, Agents, etc. and experiment with them.
|
278
296
|
|
279
297
|
## Core Contributors
|
@@ -14,10 +14,15 @@ chroma = Vectorsearch::Chroma.new(
|
|
14
14
|
# Create the default schema.
|
15
15
|
chroma.create_default_schema
|
16
16
|
|
17
|
+
# gem install these or add them to your Gemfile
|
18
|
+
# Add `gem "pdf-reader", "~> 1.4"` to your Gemfile
|
19
|
+
# Add `gem "docx", branch: "master", git: "https://github.com/ruby-docx/docx.git"` to your Gemfile
|
20
|
+
|
17
21
|
# Set up an array of PDF and TXT documents
|
18
22
|
docs = [
|
19
23
|
Langchain.root.join("/docs/document.pdf"),
|
20
|
-
Langchain.root.join("/docs/document.txt")
|
24
|
+
Langchain.root.join("/docs/document.txt"),
|
25
|
+
Langchain.root.join("/docs/document.docx")
|
21
26
|
]
|
22
27
|
|
23
28
|
# Add data to the index. Weaviate will use OpenAI to generate embeddings behind the scene.
|
data/lib/dependency_helper.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
VersionError = Class.new(ScriptError)
|
4
|
+
|
3
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`
|
4
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.
|
5
7
|
#
|
6
8
|
# @param gem_name [String] The name of the gem to load
|
7
9
|
# @return [Boolean] Whether or not the gem was loaded successfully
|
8
10
|
# @raise [LoadError] If the gem is not installed
|
9
|
-
# @raise [
|
11
|
+
# @raise [VersionError] If the gem is installed, but the version does not meet the requirements
|
10
12
|
#
|
11
13
|
def depends_on(gem_name)
|
12
14
|
gem(gem_name) # require the gem
|
@@ -14,10 +16,12 @@ def depends_on(gem_name)
|
|
14
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
|
15
17
|
|
16
18
|
gem_version = Gem.loaded_specs[gem_name].version
|
17
|
-
gem_requirement = Bundler.load.dependencies.find { |g| g.name == gem_name }
|
19
|
+
gem_requirement = Bundler.load.dependencies.find { |g| g.name == gem_name }&.requirement
|
20
|
+
|
21
|
+
raise LoadError unless gem_requirement
|
18
22
|
|
19
|
-
|
20
|
-
raise "The #{gem_name} gem is installed, but version #{gem_requirement} is required. You have #{gem_version}."
|
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}."
|
21
25
|
end
|
22
26
|
|
23
27
|
true
|
data/lib/langchain.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "logger"
|
4
|
+
require "pathname"
|
4
5
|
|
5
6
|
require_relative "./version"
|
6
7
|
require_relative "./dependency_helper"
|
@@ -34,8 +35,10 @@ end
|
|
34
35
|
module LLM
|
35
36
|
autoload :Base, "llm/base"
|
36
37
|
autoload :Cohere, "llm/cohere"
|
38
|
+
autoload :GooglePalm, "llm/google_palm"
|
37
39
|
autoload :HuggingFace, "llm/hugging_face"
|
38
40
|
autoload :OpenAI, "llm/openai"
|
41
|
+
autoload :Replicate, "llm/replicate"
|
39
42
|
end
|
40
43
|
|
41
44
|
module Prompt
|
@@ -58,6 +61,7 @@ module Loaders
|
|
58
61
|
autoload :Docx, "loaders/docx"
|
59
62
|
autoload :PDF, "loaders/pdf"
|
60
63
|
autoload :Text, "loaders/text"
|
64
|
+
autoload :HTML, "loaders/html"
|
61
65
|
end
|
62
66
|
|
63
67
|
autoload :Loader, "loader"
|
data/lib/llm/base.rb
CHANGED
@@ -7,9 +7,11 @@ module LLM
|
|
7
7
|
# Currently supported LLMs
|
8
8
|
# TODO: Add support for HuggingFace and other LLMs
|
9
9
|
LLMS = {
|
10
|
-
openai: "OpenAI",
|
11
10
|
cohere: "Cohere",
|
12
|
-
|
11
|
+
google_palm: "GooglePalm",
|
12
|
+
huggingface: "HuggingFace",
|
13
|
+
openai: "OpenAI",
|
14
|
+
replicate: "Replicate"
|
13
15
|
}.freeze
|
14
16
|
|
15
17
|
def default_dimension
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module LLM
|
4
|
+
class GooglePalm < Base
|
5
|
+
# Wrapper around the Google PaLM (Pathways Language Model) APIs.
|
6
|
+
|
7
|
+
DEFAULTS = {
|
8
|
+
temperature: 0.0,
|
9
|
+
dimension: 768 # This is what the `embedding-gecko-001` model generates
|
10
|
+
}.freeze
|
11
|
+
|
12
|
+
def initialize(api_key:)
|
13
|
+
depends_on "google_palm_api"
|
14
|
+
require "google_palm_api"
|
15
|
+
|
16
|
+
@client = ::GooglePalmApi::Client.new(api_key: api_key)
|
17
|
+
end
|
18
|
+
|
19
|
+
#
|
20
|
+
# Generate an embedding for a given text
|
21
|
+
#
|
22
|
+
# @param text [String] The text to generate an embedding for
|
23
|
+
# @return [Array] The embedding
|
24
|
+
#
|
25
|
+
def embed(text:)
|
26
|
+
response = client.embed(
|
27
|
+
text: text
|
28
|
+
)
|
29
|
+
response.dig("embedding", "value")
|
30
|
+
end
|
31
|
+
|
32
|
+
#
|
33
|
+
# Generate a completion for a given prompt
|
34
|
+
#
|
35
|
+
# @param prompt [String] The prompt to generate a completion for
|
36
|
+
# @return [String] The completion
|
37
|
+
#
|
38
|
+
def complete(prompt:, **params)
|
39
|
+
default_params = {
|
40
|
+
prompt: prompt,
|
41
|
+
temperature: DEFAULTS[:temperature]
|
42
|
+
}
|
43
|
+
|
44
|
+
if params[:stop_sequences]
|
45
|
+
default_params[:stop_sequences] = params.delete(:stop_sequences)
|
46
|
+
end
|
47
|
+
|
48
|
+
if params[:max_tokens]
|
49
|
+
default_params[:max_output_tokens] = params.delete(:max_tokens)
|
50
|
+
end
|
51
|
+
|
52
|
+
default_params.merge!(params)
|
53
|
+
|
54
|
+
response = client.generate_text(**default_params)
|
55
|
+
response.dig("candidates", 0, "output")
|
56
|
+
end
|
57
|
+
|
58
|
+
#
|
59
|
+
# Generate a chat completion for a given prompt
|
60
|
+
#
|
61
|
+
# @param prompt [String] The prompt to generate a chat completion for
|
62
|
+
# @return [String] The chat completion
|
63
|
+
#
|
64
|
+
def chat(prompt:, **params)
|
65
|
+
# TODO: Figure out how to introduce persisted conversations
|
66
|
+
default_params = {
|
67
|
+
prompt: prompt,
|
68
|
+
temperature: DEFAULTS[:temperature]
|
69
|
+
}
|
70
|
+
|
71
|
+
if params[:stop_sequences]
|
72
|
+
default_params[:stop] = params.delete(:stop_sequences)
|
73
|
+
end
|
74
|
+
|
75
|
+
if params[:max_tokens]
|
76
|
+
default_params[:max_output_tokens] = params.delete(:max_tokens)
|
77
|
+
end
|
78
|
+
|
79
|
+
default_params.merge!(params)
|
80
|
+
|
81
|
+
response = client.generate_chat_message(**default_params)
|
82
|
+
response.dig("candidates", 0, "content")
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module LLM
|
4
|
+
class Replicate < Base
|
5
|
+
# Wrapper around Replicate.com LLM provider
|
6
|
+
# Use it directly:
|
7
|
+
# replicate = LLM::Replicate.new(api_key: ENV["REPLICATE_API_KEY"])
|
8
|
+
#
|
9
|
+
# Or pass it to be instantiated by a vector search DB:
|
10
|
+
# chroma = Vectorsearch::Chroma.new(
|
11
|
+
# url: ENV["CHROMA_URL"],
|
12
|
+
# index_name: "...",
|
13
|
+
# llm: :replicate,
|
14
|
+
# llm_api_key: ENV["REPLICATE_API_KEY"],
|
15
|
+
# )
|
16
|
+
|
17
|
+
DEFAULTS = {
|
18
|
+
# TODO: Figure out how to send the temperature to the API
|
19
|
+
temperature: 0.01, # Minimum accepted value
|
20
|
+
# TODO: Design the interface to pass and use different models
|
21
|
+
completion_model_name: "replicate/vicuna-13b",
|
22
|
+
embeddings_model_name: "creatorrr/all-mpnet-base-v2",
|
23
|
+
dimension: 384
|
24
|
+
}.freeze
|
25
|
+
|
26
|
+
# Intialize the Replicate LLM
|
27
|
+
# @param api_key [String] The API key to use
|
28
|
+
def initialize(api_key:)
|
29
|
+
depends_on "replicate-ruby"
|
30
|
+
require "replicate"
|
31
|
+
|
32
|
+
::Replicate.configure do |config|
|
33
|
+
config.api_token = api_key
|
34
|
+
end
|
35
|
+
|
36
|
+
@client = ::Replicate.client
|
37
|
+
end
|
38
|
+
|
39
|
+
# Generate an embedding for a given text
|
40
|
+
# @param text [String] The text to generate an embedding for
|
41
|
+
# @return [Hash] The embedding
|
42
|
+
def embed(text:)
|
43
|
+
response = embeddings_model.predict(input: text)
|
44
|
+
|
45
|
+
until response.finished?
|
46
|
+
response.refetch
|
47
|
+
sleep(1)
|
48
|
+
end
|
49
|
+
|
50
|
+
response.output
|
51
|
+
end
|
52
|
+
|
53
|
+
# Generate a completion for a given prompt
|
54
|
+
# @param prompt [String] The prompt to generate a completion for
|
55
|
+
# @return [Hash] The completion
|
56
|
+
def complete(prompt:, **params)
|
57
|
+
response = completion_model.predict(prompt: prompt)
|
58
|
+
|
59
|
+
until response.finished?
|
60
|
+
response.refetch
|
61
|
+
sleep(1)
|
62
|
+
end
|
63
|
+
|
64
|
+
# Response comes back as an array of strings, e.g.: ["Hi", "how ", "are ", "you?"]
|
65
|
+
# The first array element is missing a space at the end, so we add it manually
|
66
|
+
response.output[0] += " "
|
67
|
+
|
68
|
+
response.output.join
|
69
|
+
end
|
70
|
+
|
71
|
+
# Cohere does not have a dedicated chat endpoint, so instead we call `complete()`
|
72
|
+
def chat(...)
|
73
|
+
complete(...)
|
74
|
+
end
|
75
|
+
|
76
|
+
alias_method :generate_embedding, :embed
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def completion_model
|
81
|
+
@completion_model ||= client.retrieve_model(DEFAULTS[:completion_model_name]).latest_version
|
82
|
+
end
|
83
|
+
|
84
|
+
def embeddings_model
|
85
|
+
@embeddings_model ||= client.retrieve_model(DEFAULTS[:embeddings_model_name]).latest_version
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/lib/loaders/html.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "open-uri"
|
4
|
+
|
5
|
+
module Loaders
|
6
|
+
class HTML < Base
|
7
|
+
# We only look for headings and paragraphs
|
8
|
+
TEXT_CONTENT_TAGS = %w[h1 h2 h3 h4 h5 h6 p]
|
9
|
+
|
10
|
+
#
|
11
|
+
# This Loader parses URL into a text.
|
12
|
+
# If you'd like to use it directly you can do so like this:
|
13
|
+
# Loaders::URL.new("https://nokogiri.org/").load
|
14
|
+
#
|
15
|
+
def initialize(url)
|
16
|
+
depends_on "nokogiri"
|
17
|
+
require "nokogiri"
|
18
|
+
|
19
|
+
@url = url
|
20
|
+
end
|
21
|
+
|
22
|
+
# Check that url is a valid URL
|
23
|
+
def loadable?
|
24
|
+
!!(@url =~ URI::DEFAULT_PARSER.make_regexp)
|
25
|
+
end
|
26
|
+
|
27
|
+
def load
|
28
|
+
return unless response.status.first == "200"
|
29
|
+
|
30
|
+
doc = Nokogiri::HTML(response.read)
|
31
|
+
doc.css(TEXT_CONTENT_TAGS.join(",")).map(&:inner_text).join("\n\n")
|
32
|
+
end
|
33
|
+
|
34
|
+
def response
|
35
|
+
@response ||= URI.parse(@url).open
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/prompt/base.rb
CHANGED
@@ -66,6 +66,8 @@ module Prompt
|
|
66
66
|
# contained within the template. Input variables are defined as text enclosed in
|
67
67
|
# curly braces (e.g. "{variable_name}").
|
68
68
|
#
|
69
|
+
# Content within two consecutive curly braces (e.g. "{{ignore_me}}) are ignored.
|
70
|
+
#
|
69
71
|
# @param template [String] The template string to extract variables from.
|
70
72
|
#
|
71
73
|
# @return [Array<String>] An array of input variable names.
|
@@ -74,9 +76,9 @@ module Prompt
|
|
74
76
|
input_variables = []
|
75
77
|
scanner = StringScanner.new(template)
|
76
78
|
|
77
|
-
while scanner.scan_until(/\{([^
|
79
|
+
while scanner.scan_until(/\{([^}]*)\}/)
|
78
80
|
variable = scanner[1].strip
|
79
|
-
input_variables << variable unless variable.empty?
|
81
|
+
input_variables << variable unless variable.empty? || variable[0] == "{"
|
80
82
|
end
|
81
83
|
|
82
84
|
input_variables
|
@@ -20,7 +20,7 @@ module Prompt
|
|
20
20
|
end
|
21
21
|
|
22
22
|
#
|
23
|
-
# Format the prompt with the inputs.
|
23
|
+
# Format the prompt with the inputs. Double {{}} replaced with single {} to adhere to f-string spec.
|
24
24
|
#
|
25
25
|
# @param kwargs [Hash] Any arguments to be passed to the prompt template.
|
26
26
|
# @return [String] A formatted string.
|
@@ -28,7 +28,7 @@ module Prompt
|
|
28
28
|
def format(**kwargs)
|
29
29
|
result = @template
|
30
30
|
kwargs.each { |key, value| result = result.gsub(/\{#{key}\}/, value.to_s) }
|
31
|
-
result
|
31
|
+
result.gsub(/{{/, "{").gsub(/}}/, "}")
|
32
32
|
end
|
33
33
|
|
34
34
|
#
|
data/lib/version.rb
CHANGED
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.3.
|
4
|
+
version: 0.3.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-05-
|
11
|
+
date: 2023-05-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dotenv-rails
|
@@ -39,47 +39,47 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 3.10.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: chroma-db
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 0.
|
47
|
+
version: 0.3.0
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: 0.
|
54
|
+
version: 0.3.0
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: cohere-ruby
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 0.
|
61
|
+
version: 0.9.4
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: 0.
|
68
|
+
version: 0.9.4
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: docx
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
75
|
+
version: 0.8.0
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
82
|
+
version: 0.8.0
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: eqn
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -94,6 +94,20 @@ dependencies:
|
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: 1.6.5
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: google_palm_api
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 0.1.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.1.0
|
97
111
|
- !ruby/object:Gem::Dependency
|
98
112
|
name: google_search_results
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -114,14 +128,14 @@ dependencies:
|
|
114
128
|
requirements:
|
115
129
|
- - "~>"
|
116
130
|
- !ruby/object:Gem::Version
|
117
|
-
version: 0.3.
|
131
|
+
version: 0.3.4
|
118
132
|
type: :development
|
119
133
|
prerelease: false
|
120
134
|
version_requirements: !ruby/object:Gem::Requirement
|
121
135
|
requirements:
|
122
136
|
- - "~>"
|
123
137
|
- !ruby/object:Gem::Version
|
124
|
-
version: 0.3.
|
138
|
+
version: 0.3.4
|
125
139
|
- !ruby/object:Gem::Dependency
|
126
140
|
name: milvus
|
127
141
|
requirement: !ruby/object:Gem::Requirement
|
@@ -136,6 +150,20 @@ dependencies:
|
|
136
150
|
- - "~>"
|
137
151
|
- !ruby/object:Gem::Version
|
138
152
|
version: 0.9.0
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: nokogiri
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - "~>"
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '1.13'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - "~>"
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '1.13'
|
139
167
|
- !ruby/object:Gem::Dependency
|
140
168
|
name: pdf-reader
|
141
169
|
requirement: !ruby/object:Gem::Requirement
|
@@ -164,6 +192,20 @@ dependencies:
|
|
164
192
|
- - "~>"
|
165
193
|
- !ruby/object:Gem::Version
|
166
194
|
version: 0.1.6
|
195
|
+
- !ruby/object:Gem::Dependency
|
196
|
+
name: replicate-ruby
|
197
|
+
requirement: !ruby/object:Gem::Requirement
|
198
|
+
requirements:
|
199
|
+
- - "~>"
|
200
|
+
- !ruby/object:Gem::Version
|
201
|
+
version: 0.2.2
|
202
|
+
type: :development
|
203
|
+
prerelease: false
|
204
|
+
version_requirements: !ruby/object:Gem::Requirement
|
205
|
+
requirements:
|
206
|
+
- - "~>"
|
207
|
+
- !ruby/object:Gem::Version
|
208
|
+
version: 0.2.2
|
167
209
|
- !ruby/object:Gem::Dependency
|
168
210
|
name: qdrant-ruby
|
169
211
|
requirement: !ruby/object:Gem::Requirement
|
@@ -248,11 +290,14 @@ files:
|
|
248
290
|
- lib/langchain.rb
|
249
291
|
- lib/llm/base.rb
|
250
292
|
- lib/llm/cohere.rb
|
293
|
+
- lib/llm/google_palm.rb
|
251
294
|
- lib/llm/hugging_face.rb
|
252
295
|
- lib/llm/openai.rb
|
296
|
+
- lib/llm/replicate.rb
|
253
297
|
- lib/loader.rb
|
254
298
|
- lib/loaders/base.rb
|
255
299
|
- lib/loaders/docx.rb
|
300
|
+
- lib/loaders/html.rb
|
256
301
|
- lib/loaders/pdf.rb
|
257
302
|
- lib/loaders/text.rb
|
258
303
|
- lib/prompt/base.rb
|