3.2 KiB
3.2 KiB
In [21]:
from langchain_milvus import BM25BuiltInFunction, Milvus
URI = "http://10.10.10.9:19530"
tongyiKey = "sk-9464b2498c184982a9fe9d2c2e725ab5"
from langchain_community.embeddings import DashScopeEmbeddings
embeddings = DashScopeEmbeddings(
model="text-embedding-v3",
dashscope_api_key= tongyiKey,
)
vectorstore = Milvus(
embedding_function=embeddings,
connection_args={"uri": URI, "token": "root:Milvus", "db_name": "bbit_ai_lab"},
collection_name="knowledge",
index_params={"index_type": "FLAT", "metric_type": "L2"},
consistency_level="Strong",
auto_id=True,
primary_field = "id",
text_field="text",
vector_field="vector",
partition_key_field = "kn_id",
enable_dynamic_field = True,
drop_old=False, # set to True if seeking to drop the collection with that name if it exists
)
from langchain.schema import Document
docs = [
Document(
page_content="这是第一条文本",
metadata={
"kn_id": "8ecd1179-4194-4b80-bc39-5addc678df4b",
"is_active": True,
}
),
Document(
page_content="这是第二条文本",
metadata={
"kn_id": "8ecd1179-4194-4b80-bc39-5addc678df4b",
"is_active": True,
}
)
]
vectorstore.add_documents(docs)
Out [21]:
[460823023525530114, 460823023525530115]
In [ ]:
results = vectorstore.similarity_search(
"",
k=2,
expr='kn_id == "8ecd1179-4194-4b80-bc39-5addc678df4b"',
)
for res in results:
print(f"*{res.page_content} [{res.metadata}]")*这是第一条文本 [{'kn_id': '8ecd1179-4194-4b80-bc39-5addc678df4b', 'id': 460823023525530108, 'is_active': True}]
*这是第一条文本 [{'kn_id': '8ecd1179-4194-4b80-bc39-5addc678df4b', 'id': 460823023525530110, 'is_active': True}]