langchainrb 0.11.0 → 0.11.2
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 +6 -0
- data/lib/langchain/assistants/assistant.rb +24 -0
- data/lib/langchain/llm/openai.rb +2 -2
- data/lib/langchain/tool/vectorsearch/vectorsearch.json +24 -0
- data/lib/langchain/tool/vectorsearch/vectorsearch.rb +36 -0
- data/lib/langchain/vectorsearch/hnswlib.rb +4 -0
- data/lib/langchain/vectorsearch/weaviate.rb +17 -1
- data/lib/langchain/version.rb +1 -1
- data/lib/langchain.rb +1 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d2eb76b006864583607672a93cd81a5c37b259a34a2760f7d686f8923b60b22
|
4
|
+
data.tar.gz: 4e307d737f9e519e68c6adb632ff863ca365907789183756fcabe7bf0a79df4f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 757e6fd5733b0365eb5b4309589f8dfab8659f451a8f31e922c7ded5ca1d974ab6d6a9b8178aa773f2b812058ea51db2f1560d18215beb4364c15b953f57648d
|
7
|
+
data.tar.gz: 8c7ffe8b7f94c32a0e1afedff943871a097811266981e5cb0e3353b66f7eebdb387565f3d2292dda665d69dac2ad29584cad14c07c90b9c2def768ec93a403ea
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.11.2]
|
4
|
+
- New `Langchain::Assistant#clear_thread!` and `Langchain::Assistant#instructions=` methods
|
5
|
+
|
6
|
+
## [0.11.1]
|
7
|
+
- Langchain::Tool::Vectorsearch that wraps Langchain::Vectorsearch::* classes. This allows the Assistant to call the tool and inject data from vector DBs.
|
8
|
+
|
3
9
|
## [0.11.0]
|
4
10
|
- Delete previously deprecated `Langchain::Agent::ReActAgent` and `Langchain::Agent::SQLQueryAgent` classes
|
5
11
|
- New `Langchain::Agent::FileSystem` tool that can read files, write to files, and list the contents of a directory
|
@@ -125,12 +125,36 @@ module Langchain
|
|
125
125
|
add_message(role: "tool", content: output, tool_call_id: tool_call_id)
|
126
126
|
end
|
127
127
|
|
128
|
+
# Delete all messages in the thread
|
129
|
+
#
|
130
|
+
# @return [Array] Empty messages array
|
131
|
+
def clear_thread!
|
132
|
+
thread.messages = []
|
133
|
+
end
|
134
|
+
|
135
|
+
# Set new instructions
|
136
|
+
#
|
137
|
+
# @param [String] New instructions that will be set as a system message
|
138
|
+
# @return [Array<Langchain::Message>] The messages in the thread
|
139
|
+
def instructions=(new_instructions)
|
140
|
+
@instructions = new_instructions
|
141
|
+
|
142
|
+
# Find message with role: "system" in thread.messages and delete it from the thread.messages array
|
143
|
+
thread.messages.delete_if(&:system?)
|
144
|
+
|
145
|
+
# Set new instructions by adding new system message
|
146
|
+
message = build_message(role: "system", content: new_instructions)
|
147
|
+
thread.messages.unshift(message)
|
148
|
+
end
|
149
|
+
|
128
150
|
private
|
129
151
|
|
130
152
|
# Call to the LLM#chat() method
|
131
153
|
#
|
132
154
|
# @return [Langchain::LLM::BaseResponse] The LLM response object
|
133
155
|
def chat_with_llm
|
156
|
+
Langchain.logger.info("Sending a call to #{llm.class}", for: self.class)
|
157
|
+
|
134
158
|
params = {messages: thread.openai_messages}
|
135
159
|
|
136
160
|
if tools.any?
|
data/lib/langchain/llm/openai.rb
CHANGED
@@ -54,7 +54,7 @@ module Langchain::LLM
|
|
54
54
|
model: defaults[:embeddings_model_name],
|
55
55
|
encoding_format: nil,
|
56
56
|
user: nil,
|
57
|
-
dimensions:
|
57
|
+
dimensions: @defaults[:dimensions]
|
58
58
|
)
|
59
59
|
raise ArgumentError.new("text argument is required") if text.empty?
|
60
60
|
raise ArgumentError.new("model argument is required") if model.empty?
|
@@ -185,7 +185,7 @@ module Langchain::LLM
|
|
185
185
|
end
|
186
186
|
|
187
187
|
def default_dimension
|
188
|
-
@defaults[:
|
188
|
+
@defaults[:dimensions] || EMBEDDING_SIZES.fetch(defaults[:embeddings_model_name])
|
189
189
|
end
|
190
190
|
|
191
191
|
private
|
@@ -0,0 +1,24 @@
|
|
1
|
+
[
|
2
|
+
{
|
3
|
+
"type": "function",
|
4
|
+
"function": {
|
5
|
+
"name": "vectorsearch-similarity_search",
|
6
|
+
"description": "Vectorsearch: Retrieves relevant document for the query",
|
7
|
+
"parameters": {
|
8
|
+
"type": "object",
|
9
|
+
"properties": {
|
10
|
+
"query": {
|
11
|
+
"type": "string",
|
12
|
+
"description": "Query to find similar documents for"
|
13
|
+
},
|
14
|
+
"k": {
|
15
|
+
"type": "integer",
|
16
|
+
"description": "Number of similar documents to retrieve",
|
17
|
+
"default": 4
|
18
|
+
}
|
19
|
+
},
|
20
|
+
"required": ["query"]
|
21
|
+
}
|
22
|
+
}
|
23
|
+
}
|
24
|
+
]
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Langchain::Tool
|
4
|
+
class Vectorsearch < Base
|
5
|
+
#
|
6
|
+
# A tool wraps vectorsearch classes
|
7
|
+
#
|
8
|
+
# Usage:
|
9
|
+
# # Initialize the LLM that will be used to generate embeddings
|
10
|
+
# ollama = Langchain::LLM::Ollama.new(url: ENV["OLLAMA_URL"]
|
11
|
+
# chroma = Langchain::Vectorsearch::Chroma.new(url: ENV["CHROMA_URL"], index_name: "my_index", llm: ollama)
|
12
|
+
#
|
13
|
+
# # This tool can now be used by the Assistant
|
14
|
+
# vectorsearch_tool = Langchain::Tool::Vectorsearch.new(vectorsearch: chroma)
|
15
|
+
#
|
16
|
+
NAME = "vectorsearch"
|
17
|
+
ANNOTATIONS_PATH = Langchain.root.join("./langchain/tool/#{NAME}/#{NAME}.json").to_path
|
18
|
+
|
19
|
+
attr_reader :vectorsearch
|
20
|
+
|
21
|
+
# Initializes the Vectorsearch tool
|
22
|
+
#
|
23
|
+
# @param vectorsearch [Langchain::Vectorsearch::Base] Vectorsearch instance to use
|
24
|
+
def initialize(vectorsearch:)
|
25
|
+
@vectorsearch = vectorsearch
|
26
|
+
end
|
27
|
+
|
28
|
+
# Executes the vector search and returns the results
|
29
|
+
#
|
30
|
+
# @param query [String] The query to search for
|
31
|
+
# @param k [Integer] The number of results to return
|
32
|
+
def similarity_search(query:, k: 4)
|
33
|
+
vectorsearch.similarity_search(query:, k: 4)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -9,7 +9,7 @@ module Langchain::Vectorsearch
|
|
9
9
|
# gem "weaviate-ruby", "~> 0.8.9"
|
10
10
|
#
|
11
11
|
# Usage:
|
12
|
-
# weaviate = Langchain::Vectorsearch::Weaviate.new(url
|
12
|
+
# weaviate = Langchain::Vectorsearch::Weaviate.new(url: ENV["WEAVIATE_URL"], api_key: ENV["WEAVIATE_API_KEY"], index_name: "Docs", llm: llm)
|
13
13
|
#
|
14
14
|
|
15
15
|
# Initialize the Weaviate adapter
|
@@ -71,6 +71,22 @@ module Langchain::Vectorsearch
|
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
74
|
+
# Deletes a list of texts in the index
|
75
|
+
# @param ids [Array] The ids of texts to delete
|
76
|
+
# @return [Hash] The response from the server
|
77
|
+
def remove_texts(ids:)
|
78
|
+
raise ArgumentError, "ids must be an array" unless ids.is_a?(Array)
|
79
|
+
|
80
|
+
client.objects.batch_delete(
|
81
|
+
class_name: index_name,
|
82
|
+
where: {
|
83
|
+
path: ["__id"],
|
84
|
+
operator: "ContainsAny",
|
85
|
+
valueTextArray: ids
|
86
|
+
}
|
87
|
+
)
|
88
|
+
end
|
89
|
+
|
74
90
|
# Create default schema
|
75
91
|
# @return [Hash] The response from the server
|
76
92
|
def create_default_schema
|
data/lib/langchain/version.rb
CHANGED
data/lib/langchain.rb
CHANGED
@@ -31,6 +31,7 @@ loader.collapse("#{__dir__}/langchain/tool/database")
|
|
31
31
|
loader.collapse("#{__dir__}/langchain/tool/file_system")
|
32
32
|
loader.collapse("#{__dir__}/langchain/tool/google_search")
|
33
33
|
loader.collapse("#{__dir__}/langchain/tool/ruby_code_interpreter")
|
34
|
+
loader.collapse("#{__dir__}/langchain/tool/vectorsearch")
|
34
35
|
loader.collapse("#{__dir__}/langchain/tool/weather")
|
35
36
|
loader.collapse("#{__dir__}/langchain/tool/wikipedia")
|
36
37
|
|
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.11.
|
4
|
+
version: 0.11.2
|
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-04-
|
11
|
+
date: 2024-04-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -776,6 +776,8 @@ files:
|
|
776
776
|
- lib/langchain/tool/google_search/google_search.rb
|
777
777
|
- lib/langchain/tool/ruby_code_interpreter/ruby_code_interpreter.json
|
778
778
|
- lib/langchain/tool/ruby_code_interpreter/ruby_code_interpreter.rb
|
779
|
+
- lib/langchain/tool/vectorsearch/vectorsearch.json
|
780
|
+
- lib/langchain/tool/vectorsearch/vectorsearch.rb
|
779
781
|
- lib/langchain/tool/weather/weather.json
|
780
782
|
- lib/langchain/tool/weather/weather.rb
|
781
783
|
- lib/langchain/tool/wikipedia/wikipedia.json
|