Class: Langchain::Vectorsearch::Milvus
- Defined in:
- lib/langchain/vectorsearch/milvus.rb
Constant Summary
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #add_texts(texts:) ⇒ Object
-
#ask(question:, k: 4) {|String| ... } ⇒ String
Ask a question and return the answer.
-
#create_default_index ⇒ Boolean
Create the default index.
-
#create_default_schema ⇒ Hash
Create default schema.
-
#destroy_default_schema ⇒ Hash
Delete default schema.
-
#get_default_schema ⇒ Hash
Get the default schema.
-
#initialize(url:, index_name:, llm:, api_key: nil) ⇒ Milvus
constructor
Wrapper around Milvus REST APIs.
-
#load_default_schema ⇒ Boolean
Load default schema into memory.
-
#remove_texts(ids:) ⇒ Boolean
Deletes a list of texts in the index.
- #similarity_search(query:, k: 4) ⇒ Object
- #similarity_search_by_vector(embedding:, k: 4) ⇒ Object
Methods inherited from Base
#add_data, #generate_hyde_prompt, #generate_rag_prompt, #similarity_search_with_hyde, #update_texts
Methods included from DependencyHelper
Constructor Details
#initialize(url:, index_name:, llm:, api_key: nil) ⇒ Milvus
Wrapper around Milvus REST APIs.
Gem requirements:
gem "milvus", "~> 0.10.3"
Usage:
milvus = Langchain::Vectorsearch::Milvus.new(url:, index_name:, llm:, api_key:)
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/langchain/vectorsearch/milvus.rb', line 14 def initialize(url:, index_name:, llm:, api_key: nil) depends_on "milvus" @client = ::Milvus::Client.new( url: url, api_key: api_key, logger: Langchain.logger ) @index_name = index_name super(llm: llm) end |
Instance Method Details
#add_texts(texts:) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/langchain/vectorsearch/milvus.rb', line 27 def add_texts(texts:) client.entities.insert( collection_name: index_name, data: texts.map do |text| {content: text, vector: llm.(text: text).} end ) end |
#ask(question:, k: 4) {|String| ... } ⇒ String
Ask a question and return the answer
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/langchain/vectorsearch/milvus.rb', line 145 def ask(question:, k: 4, &block) search_results = similarity_search(query: question, k: k) content_data = search_results.dig("data").map { |result| result.dig("content") } context = content_data.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_index ⇒ Boolean
Create the default index
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/langchain/vectorsearch/milvus.rb', line 85 def create_default_index client.indexes.create( collection_name: index_name, index_params: [ { metricType: "L2", fieldName: "vector", indexName: "vector_idx", indexConfig: { index_type: "AUTOINDEX" } } ] ) end |
#create_default_schema ⇒ Hash
Create default schema
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/langchain/vectorsearch/milvus.rb', line 55 def create_default_schema client.collections.create( auto_id: true, collection_name: index_name, fields: [ { fieldName: "id", isPrimary: true, dataType: "Int64" }, { fieldName: "content", isPrimary: false, dataType: "VarChar", elementTypeParams: { max_length: "32768" # Largest allowed value } }, { fieldName: "vector", isPrimary: false, dataType: "FloatVector", elementTypeParams: { dim: llm.default_dimensions.to_s } } ] ) end |
#destroy_default_schema ⇒ Hash
Delete default schema
109 110 111 |
# File 'lib/langchain/vectorsearch/milvus.rb', line 109 def destroy_default_schema client.collections.drop(collection_name: index_name) end |
#get_default_schema ⇒ Hash
Get the default schema
103 104 105 |
# File 'lib/langchain/vectorsearch/milvus.rb', line 103 def get_default_schema client.collections.describe(collection_name: index_name) end |
#load_default_schema ⇒ Boolean
Load default schema into memory
115 116 117 |
# File 'lib/langchain/vectorsearch/milvus.rb', line 115 def load_default_schema client.collections.load(collection_name: index_name) end |
#remove_texts(ids:) ⇒ Boolean
Deletes a list of texts in the index
42 43 44 45 46 47 48 49 |
# File 'lib/langchain/vectorsearch/milvus.rb', line 42 def remove_texts(ids:) raise ArgumentError, "ids must be an array" unless ids.is_a?(Array) client.entities.delete( collection_name: index_name, filter: "id in #{ids}" ) end |
#similarity_search(query:, k: 4) ⇒ Object
119 120 121 122 123 124 125 126 |
# File 'lib/langchain/vectorsearch/milvus.rb', line 119 def similarity_search(query:, k: 4) = llm.(text: query). similarity_search_by_vector( embedding: , k: k ) end |
#similarity_search_by_vector(embedding:, k: 4) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/langchain/vectorsearch/milvus.rb', line 128 def similarity_search_by_vector(embedding:, k: 4) load_default_schema client.entities.search( collection_name: index_name, anns_field: "vector", data: [], limit: k, output_fields: ["content", "id", "vector"] ) end |