langchainrb 0.14.0 → 0.15.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -2
- data/lib/langchain/assistants/assistant.rb +175 -131
- data/lib/langchain/assistants/messages/ollama_message.rb +9 -21
- data/lib/langchain/contextual_logger.rb +11 -5
- data/lib/langchain/evals/ragas/faithfulness.rb +5 -1
- data/lib/langchain/llm/google_gemini.rb +1 -1
- data/lib/langchain/llm/ollama.rb +23 -17
- data/lib/langchain/llm/openai.rb +1 -1
- data/lib/langchain/llm/response/ollama_response.rb +1 -15
- data/lib/langchain/llm/unified_parameters.rb +2 -2
- data/lib/langchain/tool/calculator.rb +38 -0
- data/lib/langchain/tool/{database/database.rb → database.rb} +24 -12
- data/lib/langchain/tool/file_system.rb +44 -0
- data/lib/langchain/tool/{google_search/google_search.rb → google_search.rb} +17 -23
- data/lib/langchain/tool/{news_retriever/news_retriever.rb → news_retriever.rb} +41 -14
- data/lib/langchain/tool/ruby_code_interpreter.rb +41 -0
- data/lib/langchain/tool/{tavily/tavily.rb → tavily.rb} +24 -10
- data/lib/langchain/tool/vectorsearch.rb +40 -0
- data/lib/langchain/tool/weather.rb +109 -0
- data/lib/langchain/tool/{wikipedia/wikipedia.rb → wikipedia.rb} +17 -13
- data/lib/langchain/tool_definition.rb +212 -0
- data/lib/langchain/utils/colorizer.rb +19 -0
- data/lib/langchain/utils/hash_transformer.rb +9 -17
- data/lib/langchain/utils/to_boolean.rb +27 -0
- data/lib/langchain/vectorsearch/chroma.rb +2 -2
- data/lib/langchain/vectorsearch/elasticsearch.rb +2 -2
- data/lib/langchain/vectorsearch/epsilla.rb +3 -3
- data/lib/langchain/vectorsearch/milvus.rb +2 -2
- data/lib/langchain/vectorsearch/pgvector.rb +2 -2
- data/lib/langchain/vectorsearch/pinecone.rb +2 -2
- data/lib/langchain/vectorsearch/qdrant.rb +2 -2
- data/lib/langchain/vectorsearch/weaviate.rb +4 -4
- data/lib/langchain/version.rb +1 -1
- data/lib/langchain.rb +1 -2
- metadata +18 -54
- data/lib/langchain/tool/base.rb +0 -107
- data/lib/langchain/tool/calculator/calculator.json +0 -19
- data/lib/langchain/tool/calculator/calculator.rb +0 -34
- data/lib/langchain/tool/database/database.json +0 -46
- data/lib/langchain/tool/file_system/file_system.json +0 -57
- data/lib/langchain/tool/file_system/file_system.rb +0 -32
- data/lib/langchain/tool/google_search/google_search.json +0 -19
- data/lib/langchain/tool/news_retriever/news_retriever.json +0 -122
- data/lib/langchain/tool/ruby_code_interpreter/ruby_code_interpreter.json +0 -19
- data/lib/langchain/tool/ruby_code_interpreter/ruby_code_interpreter.rb +0 -37
- data/lib/langchain/tool/tavily/tavily.json +0 -54
- data/lib/langchain/tool/vectorsearch/vectorsearch.json +0 -24
- data/lib/langchain/tool/vectorsearch/vectorsearch.rb +0 -36
- data/lib/langchain/tool/weather/weather.json +0 -19
- data/lib/langchain/tool/weather/weather.rb +0 -55
- data/lib/langchain/tool/wikipedia/wikipedia.json +0 -19
@@ -0,0 +1,212 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "json"
|
4
|
+
|
5
|
+
#
|
6
|
+
# Extends a class to be used as a tool in the assistant.
|
7
|
+
# A tool is a collection of functions (methods) used to perform specific tasks.
|
8
|
+
#
|
9
|
+
# == Usage
|
10
|
+
#
|
11
|
+
# 1. Extend your class with {Langchain::ToolDefinition}
|
12
|
+
# 2. Use {#define_function} to define each function of the tool
|
13
|
+
#
|
14
|
+
# == Key Concepts
|
15
|
+
#
|
16
|
+
# - {#define_function}: Defines a new function (method) for the tool
|
17
|
+
# - {ParameterBuilder#property}: Defines properties for the function parameters
|
18
|
+
# - {ParameterBuilder#item}: Alias for {ParameterBuilder#property}, used for array items
|
19
|
+
#
|
20
|
+
# These methods support various data types and nested structures, allowing for flexible and expressive tool definitions.
|
21
|
+
#
|
22
|
+
# @example Defining a tool with various property types and configurations
|
23
|
+
# define_function :sample_function, description: "Demonstrates various property types and configurations" do
|
24
|
+
# property :string_prop, type: "string", description: "A simple string property"
|
25
|
+
# property :number_prop, type: "number", description: "A number property"
|
26
|
+
# property :integer_prop, type: "integer", description: "An integer property"
|
27
|
+
# property :boolean_prop, type: "boolean", description: "A boolean property"
|
28
|
+
# property :enum_prop, type: "string", description: "An enum property", enum: ["option1", "option2", "option3"]
|
29
|
+
# property :required_prop, type: "string", description: "A required property", required: true
|
30
|
+
# property :array_prop, type: "array", description: "An array property" do
|
31
|
+
# item type: "string", description: "Array item"
|
32
|
+
# end
|
33
|
+
# property :object_prop, type: "object", description: "An object property" do
|
34
|
+
# property :nested_string, type: "string", description: "Nested string property"
|
35
|
+
# property :nested_number, type: "number", description: "Nested number property"
|
36
|
+
# end
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
module Langchain::ToolDefinition
|
40
|
+
# Defines a function for the tool
|
41
|
+
#
|
42
|
+
# @param method_name [Symbol] Name of the method to define
|
43
|
+
# @param description [String] Description of the function
|
44
|
+
# @yield Block that defines the parameters for the function
|
45
|
+
def define_function(method_name, description:, &)
|
46
|
+
function_schemas.add_function(method_name:, description:, &)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Returns the FunctionSchemas instance for this tool
|
50
|
+
#
|
51
|
+
# @return [FunctionSchemas] The FunctionSchemas instance
|
52
|
+
def function_schemas
|
53
|
+
@function_schemas ||= FunctionSchemas.new(tool_name)
|
54
|
+
end
|
55
|
+
|
56
|
+
# Returns the snake_case version of the class name as the tool's name
|
57
|
+
#
|
58
|
+
# @return [String] The snake_case version of the class name
|
59
|
+
def tool_name
|
60
|
+
@tool_name ||= name
|
61
|
+
.gsub("::", "_")
|
62
|
+
.gsub(/(?<=[A-Z])(?=[A-Z][a-z])|(?<=[a-z\d])(?=[A-Z])/, "_")
|
63
|
+
.downcase
|
64
|
+
end
|
65
|
+
|
66
|
+
# Manages schemas for functions
|
67
|
+
class FunctionSchemas
|
68
|
+
def initialize(tool_name)
|
69
|
+
@schemas = {}
|
70
|
+
@tool_name = tool_name
|
71
|
+
end
|
72
|
+
|
73
|
+
# Adds a function to the schemas
|
74
|
+
#
|
75
|
+
# @param method_name [Symbol] Name of the method to add
|
76
|
+
# @param description [String] Description of the function
|
77
|
+
# @yield Block that defines the parameters for the function
|
78
|
+
# @raise [ArgumentError] If a block is defined and no parameters are specified for the function
|
79
|
+
def add_function(method_name:, description:, &)
|
80
|
+
name = "#{@tool_name}__#{method_name}"
|
81
|
+
|
82
|
+
if block_given?
|
83
|
+
parameters = ParameterBuilder.new(parent_type: "object").build(&)
|
84
|
+
|
85
|
+
if parameters[:properties].empty?
|
86
|
+
raise ArgumentError, "Function parameters must have at least one property defined within it, if a block is provided"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
@schemas[method_name] = {
|
91
|
+
type: "function",
|
92
|
+
function: {name:, description:, parameters:}.compact
|
93
|
+
}
|
94
|
+
end
|
95
|
+
|
96
|
+
# Converts schemas to OpenAI-compatible format
|
97
|
+
#
|
98
|
+
# @return [String] JSON string of schemas in OpenAI format
|
99
|
+
def to_openai_format
|
100
|
+
@schemas.values
|
101
|
+
end
|
102
|
+
|
103
|
+
# Converts schemas to Anthropic-compatible format
|
104
|
+
#
|
105
|
+
# @return [String] JSON string of schemas in Anthropic format
|
106
|
+
def to_anthropic_format
|
107
|
+
@schemas.values.map do |schema|
|
108
|
+
schema[:function].transform_keys(parameters: :input_schema)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
# Converts schemas to Google Gemini-compatible format
|
113
|
+
#
|
114
|
+
# @return [String] JSON string of schemas in Google Gemini format
|
115
|
+
def to_google_gemini_format
|
116
|
+
@schemas.values.map { |schema| schema[:function] }
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
# Builds parameter schemas for functions
|
121
|
+
class ParameterBuilder
|
122
|
+
VALID_TYPES = %w[object array string number integer boolean].freeze
|
123
|
+
|
124
|
+
def initialize(parent_type:)
|
125
|
+
@schema = (parent_type == "object") ? {type: "object", properties: {}, required: []} : {}
|
126
|
+
@parent_type = parent_type
|
127
|
+
end
|
128
|
+
|
129
|
+
# Builds the parameter schema
|
130
|
+
#
|
131
|
+
# @yield Block that defines the properties of the schema
|
132
|
+
# @return [Hash] The built schema
|
133
|
+
def build(&)
|
134
|
+
instance_eval(&)
|
135
|
+
@schema
|
136
|
+
end
|
137
|
+
|
138
|
+
# Defines a property in the schema
|
139
|
+
#
|
140
|
+
# @param name [Symbol] Name of the property (required only for a parent of type object)
|
141
|
+
# @param type [String] Type of the property
|
142
|
+
# @param description [String] Description of the property
|
143
|
+
# @param enum [Array] Array of allowed values
|
144
|
+
# @param required [Boolean] Whether the property is required
|
145
|
+
# @yield [Block] Block for nested properties (only for object and array types)
|
146
|
+
# @raise [ArgumentError] If any parameter is invalid
|
147
|
+
def property(name = nil, type:, description: nil, enum: nil, required: false, &)
|
148
|
+
validate_parameters(name:, type:, enum:, required:)
|
149
|
+
|
150
|
+
prop = {type:, description:, enum:}.compact
|
151
|
+
|
152
|
+
if block_given?
|
153
|
+
nested_schema = ParameterBuilder.new(parent_type: type).build(&)
|
154
|
+
|
155
|
+
case type
|
156
|
+
when "object"
|
157
|
+
if nested_schema[:properties].empty?
|
158
|
+
raise ArgumentError, "Object properties must have at least one property defined within it"
|
159
|
+
end
|
160
|
+
prop = nested_schema
|
161
|
+
when "array"
|
162
|
+
if nested_schema.empty?
|
163
|
+
raise ArgumentError, "Array properties must have at least one item defined within it"
|
164
|
+
end
|
165
|
+
prop[:items] = nested_schema
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
if @parent_type == "object"
|
170
|
+
@schema[:properties][name] = prop
|
171
|
+
@schema[:required] << name.to_s if required
|
172
|
+
else
|
173
|
+
@schema = prop
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
# Alias for property method, used for defining array items
|
178
|
+
alias_method :item, :property
|
179
|
+
|
180
|
+
private
|
181
|
+
|
182
|
+
# Validates the parameters for a property
|
183
|
+
#
|
184
|
+
# @param name [Symbol] Name of the property
|
185
|
+
# @param type [String] Type of the property
|
186
|
+
# @param enum [Array] Array of allowed values
|
187
|
+
# @param required [Boolean] Whether the property is required
|
188
|
+
# @raise [ArgumentError] If any parameter is invalid
|
189
|
+
def validate_parameters(name:, type:, enum:, required:)
|
190
|
+
if @parent_type == "object"
|
191
|
+
if name.nil?
|
192
|
+
raise ArgumentError, "Name must be provided for properties of an object"
|
193
|
+
end
|
194
|
+
unless name.is_a?(Symbol)
|
195
|
+
raise ArgumentError, "Invalid name '#{name}'. Name must be a symbol"
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
unless VALID_TYPES.include?(type)
|
200
|
+
raise ArgumentError, "Invalid type '#{type}'. Valid types are: #{VALID_TYPES.join(", ")}"
|
201
|
+
end
|
202
|
+
|
203
|
+
unless enum.nil? || enum.is_a?(Array)
|
204
|
+
raise ArgumentError, "Invalid enum '#{enum}'. Enum must be nil or an array"
|
205
|
+
end
|
206
|
+
|
207
|
+
unless [true, false].include?(required)
|
208
|
+
raise ArgumentError, "Invalid required '#{required}'. Required must be a boolean"
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Langchain
|
4
|
+
module Utils
|
5
|
+
class Colorizer
|
6
|
+
def self.colorize(line, options)
|
7
|
+
decorated_line = Rainbow(line)
|
8
|
+
options.each_pair.each do |modifier, value|
|
9
|
+
decorated_line = if modifier == :mode
|
10
|
+
decorated_line.public_send(value)
|
11
|
+
else
|
12
|
+
decorated_line.public_send(modifier, value)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
decorated_line
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -1,24 +1,16 @@
|
|
1
1
|
module Langchain
|
2
2
|
module Utils
|
3
3
|
class HashTransformer
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
def self.deep_transform_keys(hash, &block)
|
11
|
-
case hash
|
12
|
-
when Hash
|
13
|
-
hash.each_with_object({}) do |(key, value), result|
|
14
|
-
new_key = block.call(key)
|
15
|
-
result[new_key] = deep_transform_keys(value, &block)
|
4
|
+
def self.symbolize_keys(hash)
|
5
|
+
hash.map do |k, v|
|
6
|
+
new_key = begin
|
7
|
+
k.to_sym
|
8
|
+
rescue
|
9
|
+
k
|
16
10
|
end
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
hash
|
21
|
-
end
|
11
|
+
new_value = v.is_a?(Hash) ? symbolize_keys(v) : v
|
12
|
+
[new_key, new_value]
|
13
|
+
end.to_h
|
22
14
|
end
|
23
15
|
end
|
24
16
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Langchain
|
4
|
+
module Utils
|
5
|
+
class ToBoolean
|
6
|
+
TRUTHABLE_STRINGS = %w[1 true t yes y]
|
7
|
+
private_constant :TRUTHABLE_STRINGS
|
8
|
+
|
9
|
+
def to_bool(value)
|
10
|
+
case value
|
11
|
+
when String
|
12
|
+
TRUTHABLE_STRINGS.include?(value.downcase)
|
13
|
+
when Integer
|
14
|
+
value == 1
|
15
|
+
when TrueClass
|
16
|
+
true
|
17
|
+
when FalseClass
|
18
|
+
false
|
19
|
+
when Symbol
|
20
|
+
to_bool(value.to_s)
|
21
|
+
else
|
22
|
+
false
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -124,7 +124,7 @@ module Langchain::Vectorsearch
|
|
124
124
|
# @param k [Integer] The number of results to have in context
|
125
125
|
# @yield [String] Stream responses back one String at a time
|
126
126
|
# @return [String] The answer to the question
|
127
|
-
def ask(question:, k: 4, &)
|
127
|
+
def ask(question:, k: 4, &block)
|
128
128
|
search_results = similarity_search(query: question, k: k)
|
129
129
|
|
130
130
|
context = search_results.map do |result|
|
@@ -136,7 +136,7 @@ module Langchain::Vectorsearch
|
|
136
136
|
prompt = generate_rag_prompt(question: question, context: context)
|
137
137
|
|
138
138
|
messages = [{role: "user", content: prompt}]
|
139
|
-
response = llm.chat(messages: messages, &)
|
139
|
+
response = llm.chat(messages: messages, &block)
|
140
140
|
|
141
141
|
response.context = context
|
142
142
|
response
|
@@ -143,7 +143,7 @@ module Langchain::Vectorsearch
|
|
143
143
|
# @param k [Integer] The number of results to have in context
|
144
144
|
# @yield [String] Stream responses back one String at a time
|
145
145
|
# @return [String] The answer to the question
|
146
|
-
def ask(question:, k: 4, &)
|
146
|
+
def ask(question:, k: 4, &block)
|
147
147
|
search_results = similarity_search(query: question, k: k)
|
148
148
|
|
149
149
|
context = search_results.map do |result|
|
@@ -153,7 +153,7 @@ module Langchain::Vectorsearch
|
|
153
153
|
prompt = generate_rag_prompt(question: question, context: context)
|
154
154
|
|
155
155
|
messages = [{role: "user", content: prompt}]
|
156
|
-
response = llm.chat(messages: messages, &)
|
156
|
+
response = llm.chat(messages: messages, &block)
|
157
157
|
|
158
158
|
response.context = context
|
159
159
|
response
|
@@ -96,7 +96,7 @@ module Langchain::Vectorsearch
|
|
96
96
|
|
97
97
|
status_code, response = @client.database.insert(@table_name, data)
|
98
98
|
raise "Failed to insert texts: #{response}" if status_code != 200
|
99
|
-
response
|
99
|
+
JSON.parse(response)
|
100
100
|
end
|
101
101
|
|
102
102
|
# Search for similar texts
|
@@ -129,7 +129,7 @@ module Langchain::Vectorsearch
|
|
129
129
|
# @param k [Integer] The number of results to have in context
|
130
130
|
# @yield [String] Stream responses back one String at a time
|
131
131
|
# @return [String] The answer to the question
|
132
|
-
def ask(question:, k: 4, &)
|
132
|
+
def ask(question:, k: 4, &block)
|
133
133
|
search_results = similarity_search(query: question, k: k)
|
134
134
|
|
135
135
|
context = search_results.map do |result|
|
@@ -140,7 +140,7 @@ module Langchain::Vectorsearch
|
|
140
140
|
prompt = generate_rag_prompt(question: question, context: context)
|
141
141
|
|
142
142
|
messages = [{role: "user", content: prompt}]
|
143
|
-
response = llm.chat(messages: messages, &)
|
143
|
+
response = llm.chat(messages: messages, &block)
|
144
144
|
|
145
145
|
response.context = context
|
146
146
|
response
|
@@ -156,7 +156,7 @@ module Langchain::Vectorsearch
|
|
156
156
|
# @param k [Integer] The number of results to have in context
|
157
157
|
# @yield [String] Stream responses back one String at a time
|
158
158
|
# @return [String] The answer to the question
|
159
|
-
def ask(question:, k: 4, &)
|
159
|
+
def ask(question:, k: 4, &block)
|
160
160
|
search_results = similarity_search(query: question, k: k)
|
161
161
|
|
162
162
|
content_field = search_results.dig("results", "fields_data").select { |field| field.dig("field_name") == "content" }
|
@@ -167,7 +167,7 @@ module Langchain::Vectorsearch
|
|
167
167
|
prompt = generate_rag_prompt(question: question, context: context)
|
168
168
|
|
169
169
|
messages = [{role: "user", content: prompt}]
|
170
|
-
response = llm.chat(messages: messages, &)
|
170
|
+
response = llm.chat(messages: messages, &block)
|
171
171
|
|
172
172
|
response.context = context
|
173
173
|
response
|
@@ -146,7 +146,7 @@ module Langchain::Vectorsearch
|
|
146
146
|
# @param k [Integer] The number of results to have in context
|
147
147
|
# @yield [String] Stream responses back one String at a time
|
148
148
|
# @return [String] The answer to the question
|
149
|
-
def ask(question:, k: 4, &)
|
149
|
+
def ask(question:, k: 4, &block)
|
150
150
|
search_results = similarity_search(query: question, k: k)
|
151
151
|
|
152
152
|
context = search_results.map do |result|
|
@@ -157,7 +157,7 @@ module Langchain::Vectorsearch
|
|
157
157
|
prompt = generate_rag_prompt(question: question, context: context)
|
158
158
|
|
159
159
|
messages = [{role: "user", content: prompt}]
|
160
|
-
response = llm.chat(messages: messages, &)
|
160
|
+
response = llm.chat(messages: messages, &block)
|
161
161
|
|
162
162
|
response.context = context
|
163
163
|
response
|
@@ -171,7 +171,7 @@ module Langchain::Vectorsearch
|
|
171
171
|
# @param filter [String] The filter to use
|
172
172
|
# @yield [String] Stream responses back one String at a time
|
173
173
|
# @return [String] The answer to the question
|
174
|
-
def ask(question:, namespace: "", filter: nil, k: 4, &)
|
174
|
+
def ask(question:, namespace: "", filter: nil, k: 4, &block)
|
175
175
|
search_results = similarity_search(query: question, namespace: namespace, filter: filter, k: k)
|
176
176
|
|
177
177
|
context = search_results.map do |result|
|
@@ -182,7 +182,7 @@ module Langchain::Vectorsearch
|
|
182
182
|
prompt = generate_rag_prompt(question: question, context: context)
|
183
183
|
|
184
184
|
messages = [{role: "user", content: prompt}]
|
185
|
-
response = llm.chat(messages: messages, &)
|
185
|
+
response = llm.chat(messages: messages, &block)
|
186
186
|
|
187
187
|
response.context = context
|
188
188
|
response
|
@@ -137,7 +137,7 @@ module Langchain::Vectorsearch
|
|
137
137
|
# @param k [Integer] The number of results to have in context
|
138
138
|
# @yield [String] Stream responses back one String at a time
|
139
139
|
# @return [String] The answer to the question
|
140
|
-
def ask(question:, k: 4, &)
|
140
|
+
def ask(question:, k: 4, &block)
|
141
141
|
search_results = similarity_search(query: question, k: k)
|
142
142
|
|
143
143
|
context = search_results.map do |result|
|
@@ -148,7 +148,7 @@ module Langchain::Vectorsearch
|
|
148
148
|
prompt = generate_rag_prompt(question: question, context: context)
|
149
149
|
|
150
150
|
messages = [{role: "user", content: prompt}]
|
151
|
-
response = llm.chat(messages: messages, &)
|
151
|
+
response = llm.chat(messages: messages, &block)
|
152
152
|
|
153
153
|
response.context = context
|
154
154
|
response
|
@@ -6,7 +6,7 @@ module Langchain::Vectorsearch
|
|
6
6
|
# Wrapper around Weaviate
|
7
7
|
#
|
8
8
|
# Gem requirements:
|
9
|
-
# gem "weaviate-ruby", "~> 0.
|
9
|
+
# gem "weaviate-ruby", "~> 0.9.0"
|
10
10
|
#
|
11
11
|
# Usage:
|
12
12
|
# weaviate = Langchain::Vectorsearch::Weaviate.new(url: ENV["WEAVIATE_URL"], api_key: ENV["WEAVIATE_API_KEY"], index_name: "Docs", llm: llm)
|
@@ -17,7 +17,7 @@ module Langchain::Vectorsearch
|
|
17
17
|
# @param api_key [String] The API key to use
|
18
18
|
# @param index_name [String] The capitalized name of the index to use
|
19
19
|
# @param llm [Object] The LLM client to use
|
20
|
-
def initialize(url:,
|
20
|
+
def initialize(url:, index_name:, llm:, api_key: nil)
|
21
21
|
depends_on "weaviate-ruby", req: "weaviate"
|
22
22
|
|
23
23
|
@client = ::Weaviate::Client.new(
|
@@ -143,7 +143,7 @@ module Langchain::Vectorsearch
|
|
143
143
|
# @param k [Integer] The number of results to have in context
|
144
144
|
# @yield [String] Stream responses back one String at a time
|
145
145
|
# @return [Hash] The answer
|
146
|
-
def ask(question:, k: 4, &)
|
146
|
+
def ask(question:, k: 4, &block)
|
147
147
|
search_results = similarity_search(query: question, k: k)
|
148
148
|
|
149
149
|
context = search_results.map do |result|
|
@@ -154,7 +154,7 @@ module Langchain::Vectorsearch
|
|
154
154
|
prompt = generate_rag_prompt(question: question, context: context)
|
155
155
|
|
156
156
|
messages = [{role: "user", content: prompt}]
|
157
|
-
response = llm.chat(messages: messages, &)
|
157
|
+
response = llm.chat(messages: messages, &block)
|
158
158
|
|
159
159
|
response.context = context
|
160
160
|
response
|
data/lib/langchain/version.rb
CHANGED
data/lib/langchain.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.
|
4
|
+
version: 0.15.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrei Bondarev
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: baran
|
@@ -25,19 +25,19 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 0.1.9
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: rainbow
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 3.1.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 3.1.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: json-schema
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,20 +80,6 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: 0.3.0
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: to_bool
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
86
|
-
requirements:
|
87
|
-
- - "~>"
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: 2.0.0
|
90
|
-
type: :runtime
|
91
|
-
prerelease: false
|
92
|
-
version_requirements: !ruby/object:Gem::Requirement
|
93
|
-
requirements:
|
94
|
-
- - "~>"
|
95
|
-
- !ruby/object:Gem::Version
|
96
|
-
version: 2.0.0
|
97
83
|
- !ruby/object:Gem::Dependency
|
98
84
|
name: matrix
|
99
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -472,20 +458,6 @@ dependencies:
|
|
472
458
|
- - ">="
|
473
459
|
- !ruby/object:Gem::Version
|
474
460
|
version: '0'
|
475
|
-
- !ruby/object:Gem::Dependency
|
476
|
-
name: open-weather-ruby-client
|
477
|
-
requirement: !ruby/object:Gem::Requirement
|
478
|
-
requirements:
|
479
|
-
- - "~>"
|
480
|
-
- !ruby/object:Gem::Version
|
481
|
-
version: 0.4.0
|
482
|
-
type: :development
|
483
|
-
prerelease: false
|
484
|
-
version_requirements: !ruby/object:Gem::Requirement
|
485
|
-
requirements:
|
486
|
-
- - "~>"
|
487
|
-
- !ruby/object:Gem::Version
|
488
|
-
version: 0.4.0
|
489
461
|
- !ruby/object:Gem::Dependency
|
490
462
|
name: pg
|
491
463
|
requirement: !ruby/object:Gem::Requirement
|
@@ -774,29 +746,21 @@ files:
|
|
774
746
|
- lib/langchain/prompt/few_shot_prompt_template.rb
|
775
747
|
- lib/langchain/prompt/loading.rb
|
776
748
|
- lib/langchain/prompt/prompt_template.rb
|
777
|
-
- lib/langchain/tool/
|
778
|
-
- lib/langchain/tool/
|
779
|
-
- lib/langchain/tool/
|
780
|
-
- lib/langchain/tool/
|
781
|
-
- lib/langchain/tool/
|
782
|
-
- lib/langchain/tool/
|
783
|
-
- lib/langchain/tool/
|
784
|
-
- lib/langchain/tool/
|
785
|
-
- lib/langchain/tool/
|
786
|
-
- lib/langchain/tool/
|
787
|
-
- lib/langchain/
|
788
|
-
- lib/langchain/
|
789
|
-
- lib/langchain/tool/ruby_code_interpreter/ruby_code_interpreter.rb
|
790
|
-
- lib/langchain/tool/tavily/tavily.json
|
791
|
-
- lib/langchain/tool/tavily/tavily.rb
|
792
|
-
- lib/langchain/tool/vectorsearch/vectorsearch.json
|
793
|
-
- lib/langchain/tool/vectorsearch/vectorsearch.rb
|
794
|
-
- lib/langchain/tool/weather/weather.json
|
795
|
-
- lib/langchain/tool/weather/weather.rb
|
796
|
-
- lib/langchain/tool/wikipedia/wikipedia.json
|
797
|
-
- lib/langchain/tool/wikipedia/wikipedia.rb
|
749
|
+
- lib/langchain/tool/calculator.rb
|
750
|
+
- lib/langchain/tool/database.rb
|
751
|
+
- lib/langchain/tool/file_system.rb
|
752
|
+
- lib/langchain/tool/google_search.rb
|
753
|
+
- lib/langchain/tool/news_retriever.rb
|
754
|
+
- lib/langchain/tool/ruby_code_interpreter.rb
|
755
|
+
- lib/langchain/tool/tavily.rb
|
756
|
+
- lib/langchain/tool/vectorsearch.rb
|
757
|
+
- lib/langchain/tool/weather.rb
|
758
|
+
- lib/langchain/tool/wikipedia.rb
|
759
|
+
- lib/langchain/tool_definition.rb
|
760
|
+
- lib/langchain/utils/colorizer.rb
|
798
761
|
- lib/langchain/utils/cosine_similarity.rb
|
799
762
|
- lib/langchain/utils/hash_transformer.rb
|
763
|
+
- lib/langchain/utils/to_boolean.rb
|
800
764
|
- lib/langchain/vectorsearch/base.rb
|
801
765
|
- lib/langchain/vectorsearch/chroma.rb
|
802
766
|
- lib/langchain/vectorsearch/elasticsearch.rb
|