Class: Langchain::Vectorsearch::Weaviate
- Defined in:
- lib/langchain/vectorsearch/weaviate.rb
Constant Summary
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#add_texts(texts:, ids: []) ⇒ Hash
Add a list of texts to the index.
-
#ask(question:, k: 4) {|String| ... } ⇒ Hash
Ask a question and return the answer.
-
#create_default_schema ⇒ Hash
Create default schema.
-
#destroy_default_schema ⇒ Boolean
Delete the index.
-
#get_default_schema ⇒ Hash
Get default schema.
-
#initialize(url:, index_name:, llm:, api_key: nil) ⇒ Weaviate
constructor
Initialize the Weaviate adapter.
-
#remove_texts(ids:) ⇒ Hash
Deletes a list of texts in the index.
-
#similarity_search(query:, k: 4) ⇒ Hash
Return documents similar to the query.
-
#similarity_search_by_vector(embedding:, k: 4) ⇒ Hash
Return documents similar to the vector.
-
#update_texts(texts:, ids:) ⇒ Hash
Update a list of texts in the index.
Methods inherited from Base
#add_data, #generate_hyde_prompt, #generate_rag_prompt, #similarity_search_with_hyde
Methods included from DependencyHelper
Constructor Details
#initialize(url:, index_name:, llm:, api_key: nil) ⇒ Weaviate
Initialize the Weaviate adapter
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/langchain/vectorsearch/weaviate.rb', line 20 def initialize(url:, index_name:, llm:, api_key: nil) depends_on "weaviate-ruby", req: "weaviate" @client = ::Weaviate::Client.new( url: url, api_key: api_key, logger: Langchain.logger ) # Weaviate requires the class name to be Capitalized: https://weaviate.io/developers/weaviate/configuration/schema-configuration#create-a-class # TODO: Capitalize index_name @index_name = index_name super(llm: llm) end |
Instance Method Details
#add_texts(texts:, ids: []) ⇒ Hash
Add a list of texts to the index
39 40 41 42 43 |
# File 'lib/langchain/vectorsearch/weaviate.rb', line 39 def add_texts(texts:, ids: []) client.objects.batch_create( objects: weaviate_objects(texts, ids) ) end |
#ask(question:, k: 4) {|String| ... } ⇒ Hash
Ask a question and return the answer
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/langchain/vectorsearch/weaviate.rb', line 147 def ask(question:, k: 4, &block) search_results = similarity_search(query: question, k: k) context = search_results.map do |result| result.dig("content").to_s end context = context.join("\n---\n") prompt = generate_rag_prompt(question: question, context: context) = [{role: "user", content: prompt}] response = llm.chat(messages: , &block) response.context = context response end |
#create_default_schema ⇒ Hash
Create default schema
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/langchain/vectorsearch/weaviate.rb', line 93 def create_default_schema client.schema.create( class_name: index_name, vectorizer: "none", properties: [ # __id to be used a pointer to the original document {dataType: ["string"], name: "__id"}, # '_id' is a reserved property name (single underscore) {dataType: ["text"], name: "content"} ] ) end |
#destroy_default_schema ⇒ Boolean
Delete the index
113 114 115 |
# File 'lib/langchain/vectorsearch/weaviate.rb', line 113 def destroy_default_schema client.schema.delete(class_name: index_name) end |
#get_default_schema ⇒ Hash
Get default schema
107 108 109 |
# File 'lib/langchain/vectorsearch/weaviate.rb', line 107 def get_default_schema client.schema.get(class_name: index_name) end |
#remove_texts(ids:) ⇒ Hash
Deletes a list of texts in the index
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/langchain/vectorsearch/weaviate.rb', line 78 def remove_texts(ids:) raise ArgumentError, "ids must be an array" unless ids.is_a?(Array) client.objects.batch_delete( class_name: index_name, where: { path: ["__id"], operator: "ContainsAny", valueTextArray: ids } ) end |
#similarity_search(query:, k: 4) ⇒ Hash
Return documents similar to the query
121 122 123 124 125 |
# File 'lib/langchain/vectorsearch/weaviate.rb', line 121 def similarity_search(query:, k: 4) = llm.(text: query). similarity_search_by_vector(embedding: , k: k) end |
#similarity_search_by_vector(embedding:, k: 4) ⇒ Hash
Return documents similar to the vector
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/langchain/vectorsearch/weaviate.rb', line 131 def similarity_search_by_vector(embedding:, k: 4) near_vector = "{ vector: #{} }" client.query.get( class_name: index_name, near_vector: near_vector, limit: k.to_s, fields: "__id content _additional { id }" ) end |
#update_texts(texts:, ids:) ⇒ Hash
Update a list of texts in the index
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/langchain/vectorsearch/weaviate.rb', line 48 def update_texts(texts:, ids:) uuids = [] # Retrieve the UUIDs of the objects to update Array(texts).map.with_index do |text, i| record = client.query.get( class_name: index_name, fields: "_additional { id }", where: "{ path: [\"__id\"], operator: Equal, valueString: \"#{ids[i]}\" }" ) uuids.push record[0].dig("_additional", "id") end # Update the objects texts.map.with_index do |text, i| client.objects.update( class_name: index_name, id: uuids[i], properties: { __id: ids[i].to_s, content: text }, vector: llm.(text: text). ) end end |