langchainrb 0.13.5 → 0.15.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -0
- data/README.md +2 -17
- data/lib/langchain/assistants/assistant.rb +207 -92
- data/lib/langchain/assistants/messages/ollama_message.rb +74 -0
- data/lib/langchain/assistants/thread.rb +8 -1
- data/lib/langchain/contextual_logger.rb +2 -2
- data/lib/langchain/llm/ai21.rb +0 -4
- data/lib/langchain/llm/anthropic.rb +15 -6
- data/lib/langchain/llm/azure.rb +3 -3
- data/lib/langchain/llm/base.rb +1 -0
- data/lib/langchain/llm/cohere.rb +0 -2
- data/lib/langchain/llm/google_gemini.rb +1 -1
- data/lib/langchain/llm/google_palm.rb +1 -4
- data/lib/langchain/llm/ollama.rb +24 -18
- data/lib/langchain/llm/openai.rb +1 -1
- data/lib/langchain/llm/response/google_gemini_response.rb +1 -1
- data/lib/langchain/llm/response/ollama_response.rb +5 -1
- 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/weather.rb → weather.rb} +21 -17
- data/lib/langchain/tool/{wikipedia/wikipedia.rb → wikipedia.rb} +17 -13
- data/lib/langchain/tool_definition.rb +212 -0
- data/lib/langchain/utils/hash_transformer.rb +9 -17
- 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 +3 -3
- 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
- metadata +16 -45
- 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/wikipedia/wikipedia.json +0 -19
- data/lib/langchain/utils/token_length/ai21_validator.rb +0 -41
- data/lib/langchain/utils/token_length/base_validator.rb +0 -42
- data/lib/langchain/utils/token_length/cohere_validator.rb +0 -49
- data/lib/langchain/utils/token_length/google_palm_validator.rb +0 -57
- data/lib/langchain/utils/token_length/openai_validator.rb +0 -138
- data/lib/langchain/utils/token_length/token_limit_exceeded.rb +0 -17
@@ -1,19 +1,23 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Langchain::Tool
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
4
|
+
#
|
5
|
+
# Tool that adds the capability to search using the Wikipedia API
|
6
|
+
#
|
7
|
+
# Gem requirements:
|
8
|
+
# gem "wikipedia-client", "~> 1.17.0"
|
9
|
+
#
|
10
|
+
# Usage:
|
11
|
+
# wikipedia = Langchain::Tool::Wikipedia.new
|
12
|
+
# wikipedia.execute(input: "The Roman Empire")
|
13
|
+
#
|
14
|
+
class Wikipedia
|
15
|
+
extend Langchain::ToolDefinition
|
16
|
+
include Langchain::DependencyHelper
|
17
|
+
|
18
|
+
define_function :execute, description: "Executes Wikipedia API search and returns the answer" do
|
19
|
+
property :input, type: "string", description: "Search query", required: true
|
20
|
+
end
|
17
21
|
|
18
22
|
# Initializes the Wikipedia tool
|
19
23
|
def initialize
|
@@ -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
|
@@ -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
|
@@ -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
|
@@ -140,7 +140,7 @@ module Langchain::Vectorsearch
|
|
140
140
|
|
141
141
|
client.search(
|
142
142
|
collection_name: index_name,
|
143
|
-
output_fields: ["id", "content", "vectors"
|
143
|
+
output_fields: ["id", "content"], # Add "vectors" if need to have full vectors returned.
|
144
144
|
top_k: k.to_s,
|
145
145
|
vectors: [embedding],
|
146
146
|
dsl_type: 1,
|
@@ -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
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.0
|
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-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: baran
|
@@ -212,14 +212,14 @@ dependencies:
|
|
212
212
|
requirements:
|
213
213
|
- - "~>"
|
214
214
|
- !ruby/object:Gem::Version
|
215
|
-
version: '0.
|
215
|
+
version: '0.3'
|
216
216
|
type: :development
|
217
217
|
prerelease: false
|
218
218
|
version_requirements: !ruby/object:Gem::Requirement
|
219
219
|
requirements:
|
220
220
|
- - "~>"
|
221
221
|
- !ruby/object:Gem::Version
|
222
|
-
version: '0.
|
222
|
+
version: '0.3'
|
223
223
|
- !ruby/object:Gem::Dependency
|
224
224
|
name: aws-sdk-bedrockruntime
|
225
225
|
requirement: !ruby/object:Gem::Requirement
|
@@ -682,20 +682,6 @@ dependencies:
|
|
682
682
|
- - "~>"
|
683
683
|
- !ruby/object:Gem::Version
|
684
684
|
version: 0.1.0
|
685
|
-
- !ruby/object:Gem::Dependency
|
686
|
-
name: tiktoken_ruby
|
687
|
-
requirement: !ruby/object:Gem::Requirement
|
688
|
-
requirements:
|
689
|
-
- - "~>"
|
690
|
-
- !ruby/object:Gem::Version
|
691
|
-
version: 0.0.9
|
692
|
-
type: :development
|
693
|
-
prerelease: false
|
694
|
-
version_requirements: !ruby/object:Gem::Requirement
|
695
|
-
requirements:
|
696
|
-
- - "~>"
|
697
|
-
- !ruby/object:Gem::Version
|
698
|
-
version: 0.0.9
|
699
685
|
description: Build LLM-backed Ruby applications with Ruby's Langchain.rb
|
700
686
|
email:
|
701
687
|
- andrei.bondarev13@gmail.com
|
@@ -711,6 +697,7 @@ files:
|
|
711
697
|
- lib/langchain/assistants/messages/anthropic_message.rb
|
712
698
|
- lib/langchain/assistants/messages/base.rb
|
713
699
|
- lib/langchain/assistants/messages/google_gemini_message.rb
|
700
|
+
- lib/langchain/assistants/messages/ollama_message.rb
|
714
701
|
- lib/langchain/assistants/messages/openai_message.rb
|
715
702
|
- lib/langchain/assistants/thread.rb
|
716
703
|
- lib/langchain/chunk.rb
|
@@ -787,35 +774,19 @@ files:
|
|
787
774
|
- lib/langchain/prompt/few_shot_prompt_template.rb
|
788
775
|
- lib/langchain/prompt/loading.rb
|
789
776
|
- lib/langchain/prompt/prompt_template.rb
|
790
|
-
- lib/langchain/tool/
|
791
|
-
- lib/langchain/tool/
|
792
|
-
- lib/langchain/tool/
|
793
|
-
- lib/langchain/tool/
|
794
|
-
- lib/langchain/tool/
|
795
|
-
- lib/langchain/tool/
|
796
|
-
- lib/langchain/tool/
|
797
|
-
- lib/langchain/tool/
|
798
|
-
- lib/langchain/tool/
|
799
|
-
- lib/langchain/tool/
|
800
|
-
- lib/langchain/
|
801
|
-
- lib/langchain/tool/ruby_code_interpreter/ruby_code_interpreter.json
|
802
|
-
- lib/langchain/tool/ruby_code_interpreter/ruby_code_interpreter.rb
|
803
|
-
- lib/langchain/tool/tavily/tavily.json
|
804
|
-
- lib/langchain/tool/tavily/tavily.rb
|
805
|
-
- lib/langchain/tool/vectorsearch/vectorsearch.json
|
806
|
-
- lib/langchain/tool/vectorsearch/vectorsearch.rb
|
807
|
-
- lib/langchain/tool/weather/weather.json
|
808
|
-
- lib/langchain/tool/weather/weather.rb
|
809
|
-
- lib/langchain/tool/wikipedia/wikipedia.json
|
810
|
-
- lib/langchain/tool/wikipedia/wikipedia.rb
|
777
|
+
- lib/langchain/tool/calculator.rb
|
778
|
+
- lib/langchain/tool/database.rb
|
779
|
+
- lib/langchain/tool/file_system.rb
|
780
|
+
- lib/langchain/tool/google_search.rb
|
781
|
+
- lib/langchain/tool/news_retriever.rb
|
782
|
+
- lib/langchain/tool/ruby_code_interpreter.rb
|
783
|
+
- lib/langchain/tool/tavily.rb
|
784
|
+
- lib/langchain/tool/vectorsearch.rb
|
785
|
+
- lib/langchain/tool/weather.rb
|
786
|
+
- lib/langchain/tool/wikipedia.rb
|
787
|
+
- lib/langchain/tool_definition.rb
|
811
788
|
- lib/langchain/utils/cosine_similarity.rb
|
812
789
|
- lib/langchain/utils/hash_transformer.rb
|
813
|
-
- lib/langchain/utils/token_length/ai21_validator.rb
|
814
|
-
- lib/langchain/utils/token_length/base_validator.rb
|
815
|
-
- lib/langchain/utils/token_length/cohere_validator.rb
|
816
|
-
- lib/langchain/utils/token_length/google_palm_validator.rb
|
817
|
-
- lib/langchain/utils/token_length/openai_validator.rb
|
818
|
-
- lib/langchain/utils/token_length/token_limit_exceeded.rb
|
819
790
|
- lib/langchain/vectorsearch/base.rb
|
820
791
|
- lib/langchain/vectorsearch/chroma.rb
|
821
792
|
- lib/langchain/vectorsearch/elasticsearch.rb
|