Milvus(
self,
embedding_function: Optional[Union[EmbeddingType| Name | Type |
|---|---|
| embedding_function | Optional[Union[EmbeddingType, List[EmbeddingType]]] |
| collection_name | str |
| collection_description | str |
| collection_properties | Optional[dict[str, Any]] |
| connection_args | Optional[dict[str, Any]] |
| consistency_level | str |
| index_params | Optional[Union[dict, List[dict]]] |
| search_params | Optional[Union[dict, List[dict]]] |
| drop_old | Optional[bool] |
| auto_id | bool |
| primary_field | str |
| text_field | str |
| vector_field | Union[str, List[str]] |
| enable_dynamic_field | bool |
| metadata_field | Optional[str] |
| partition_key_field | Optional[str] |
| num_partitions | Optional[int] |
| partition_names | Optional[list] |
| replica_number | int |
| timeout | Optional[float] |
| num_shards | Optional[int] |
| vector_schema | Optional[Union[dict[str, Any], List[dict[str, Any]]]] |
| metadata_schema | Optional[dict[str, Any]] |
| builtin_function | Optional[Union[BaseMilvusBuiltInFunction, List[BaseMilvusBuiltInFunction]]] |
Perform a search on a query string and return results with score.
For more information about the search parameters, take a look at the pymilvus documentation found here: https://milvus.io/api-reference/pymilvus/v2.5.x/ORM/Collection/search.md
Perform a search on an embedding and return results with score.
For more information about the search parameters, take a look at the pymilvus documentation found here: https://milvus.io/api-reference/pymilvus/v2.5.x/ORM/Collection/search.md
Delete by vector ID or boolean expression. Refer to Milvus documentation for notes and examples of expressions.
Perform an async search on a query string and return results with score.
For more information about the search parameters, take a look at the pymilvus documentation found here: https://milvus.io/api-reference/pymilvus/v2.5.x/ORM/Collection/search.md
Perform an async search on an embedding and return results with score.
For more information about the search parameters, take a look at the pymilvus documentation found here: https://milvus.io/api-reference/pymilvus/v2.5.x/ORM/Collection/search.md
Async delete by vector ID or boolean expression. Refer to Milvus documentation for notes and examples of expressions.
Milvus vector store integration.
Setup:
Install langchain_milvus package:
pip install -qU langchain_milvus
Key init args — indexing params: collection_name: str Name of the collection. collection_description: str Description of the collection. embedding_function: Union[Embeddings, BaseSparseEmbedding] Embedding function to use.
Key init args — client params: connection_args: Optional[dict] Connection arguments.
Instantiate:
from langchain_milvus import Milvus
from langchain_openai import OpenAIEmbeddings
URI = "./milvus_example.db"
vector_store = Milvus(
embedding_function=OpenAIEmbeddings(),
connection_args={"uri": URI},
)
Add Documents:
from langchain_core.documents import Document
document_1 = Document(page_content="foo", metadata={"baz": "bar"})
document_2 = Document(page_content="thud", metadata={"baz": "baz"})
document_3 = Document(page_content="i will be deleted :(", metadata={"baz": "qux"})
documents = [document_1, document_2, document_3]
ids = ["1", "2", "3"]
vector_store.add_documents(documents=documents, ids=ids)
Delete Documents:
vector_store.delete(ids=["3"])
Search:
results = vector_store.similarity_search(query="thud",k=1)
for doc in results:
print(f"* {doc.page_content} [{doc.metadata}]")
* thud [{'baz': 'baz', 'pk': '2'}]
Search with score:
results = vector_store.similarity_search_with_score(query="qux",k=1)
for doc, score in results:
print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")
* [SIM=0.335463] foo [{'baz': 'bar', 'pk': '1'}]
Async:
# add documents
# await vector_store.aadd_documents(documents=documents, ids=ids)
# delete documents
# await vector_store.adelete(ids=["3"])
# search
# results = vector_store.asimilarity_search(query="thud",k=1)
# search with score
results = await vector_store.asimilarity_search_with_score(query="qux",k=1)
for doc,score in results:
print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")
* [SIM=0.335463] foo [{'baz': 'bar', 'pk': '1'}]
Use as Retriever:
retriever = vector_store.as_retriever(
search_type="mmr",
search_kwargs={"k": 1, "fetch_k": 2, "lambda_mult": 0.5},
)
retriever.invoke("thud")
[Document(metadata={'baz': 'baz', 'pk': '2'}, page_content='thud')]