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 +4 -4
- data/.env.example +1 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +6 -2
- data/README.md +11 -1
- data/examples/open_ai_qdrant_function_calls.rb +0 -4
- data/lib/langchain/llm/anthropic.rb +63 -0
- data/lib/langchain/loader.rb +2 -2
- data/lib/langchain/vectorsearch/milvus.rb +47 -6
- data/lib/langchain/version.rb +1 -1
- data/lib/langchain.rb +1 -0
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 78d1726db950b4234799cdf34185110411cbc666836225a63ed9d5be6b0a5575
|
4
|
+
data.tar.gz: 0ed5595b0aae9dcaa97ee5cbc653cf3c9cb21f6057f5439ec78711ecb1cf6b46
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c0313618b12b10943e8825aaabd66306e21b0a24fefe9c4593cb4f28b1a03e099781944057ce9423c98851bad7dea07e2832c4ab37d416cf743b126a26a6a308
|
7
|
+
data.tar.gz: a2910e93f883bee84288e5bd22644f17b30957a6ae9a696b3adc01dc9b32e2f546b49d46c3587856a883cd4dc1f0a677a3970d357b5fe2dfd9216ce894d06120
|
data/.env.example
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
langchainrb (0.6.
|
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.
|
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/
|
550
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/andreibondarev/langchainrb.
|
541
551
|
|
542
552
|
## License
|
543
553
|
|
@@ -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
|
data/lib/langchain/loader.rb
CHANGED
@@ -89,9 +89,9 @@ module Langchain
|
|
89
89
|
end
|
90
90
|
|
91
91
|
def load_from_path
|
92
|
-
|
92
|
+
return File.open(@path) if File.exist?(@path)
|
93
93
|
|
94
|
-
File
|
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:,
|
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["
|
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
|
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["
|
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: "
|
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
|
data/lib/langchain/version.rb
CHANGED
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.
|
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-
|
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
|