Files
2025-09-18 17:18:18 +08:00

115 lines
3.2 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 21,
"id": "d029ad67",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[460823023525530114, 460823023525530115]"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_milvus import BM25BuiltInFunction, Milvus\n",
"URI = \"http://10.10.10.9:19530\"\n",
"tongyiKey = \"sk-9464b2498c184982a9fe9d2c2e725ab5\"\n",
"from langchain_community.embeddings import DashScopeEmbeddings\n",
"embeddings = DashScopeEmbeddings(\n",
" model=\"text-embedding-v3\",\n",
" dashscope_api_key= tongyiKey, \n",
")\n",
"vectorstore = Milvus(\n",
" embedding_function=embeddings,\n",
" connection_args={\"uri\": URI, \"token\": \"root:Milvus\", \"db_name\": \"bbit_ai_lab\"},\n",
" collection_name=\"knowledge\",\n",
" index_params={\"index_type\": \"FLAT\", \"metric_type\": \"L2\"},\n",
" consistency_level=\"Strong\",\n",
" auto_id=True,\n",
"\n",
" primary_field = \"id\",\n",
" text_field=\"text\",\n",
" vector_field=\"vector\",\n",
" partition_key_field = \"kn_id\",\n",
" enable_dynamic_field = True,\n",
" drop_old=False, # set to True if seeking to drop the collection with that name if it exists\n",
")\n",
"\n",
"from langchain.schema import Document\n",
"\n",
"docs = [\n",
" Document(\n",
" page_content=\"这是第一条文本\",\n",
" metadata={\n",
" \"kn_id\": \"8ecd1179-4194-4b80-bc39-5addc678df4b\",\n",
" \"is_active\": True,\n",
" }\n",
" ),\n",
" Document(\n",
" page_content=\"这是第二条文本\",\n",
" metadata={\n",
" \"kn_id\": \"8ecd1179-4194-4b80-bc39-5addc678df4b\",\n",
" \"is_active\": True,\n",
" }\n",
" )\n",
"]\n",
"\n",
"vectorstore.add_documents(docs)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a480053b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"*这是第一条文本 [{'kn_id': '8ecd1179-4194-4b80-bc39-5addc678df4b', 'id': 460823023525530108, 'is_active': True}]\n",
"*这是第一条文本 [{'kn_id': '8ecd1179-4194-4b80-bc39-5addc678df4b', 'id': 460823023525530110, 'is_active': True}]\n"
]
}
],
"source": [
"results = vectorstore.similarity_search(\n",
" \"\",\n",
" k=2,\n",
" expr='kn_id == \"8ecd1179-4194-4b80-bc39-5addc678df4b\"',\n",
")\n",
"for res in results:\n",
" print(f\"*{res.page_content} [{res.metadata}]\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "lang",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.18"
}
},
"nbformat": 4,
"nbformat_minor": 5
}