langchain-chat-with-milvus/query_from_user.py
2023-11-13 20:23:15 +08:00

62 lines
1.3 KiB
Python

import json
from pymilvus import (
connections,
utility,
FieldSchema,
CollectionSchema,
DataType,
Collection,
)
MILVUS_HOST = "127.0.0.1"
MILVUS_PORT = "19530"
connections.connect("default", host=MILVUS_HOST, port=MILVUS_PORT)
collection = Collection("todos")
f = open("question_vec.json", "r").read()
vector_data = json.loads(f)
search_param = {
"data": [vector_data],
"anns_field": "vector",
"param": {"metric_type": "L2", "ef": 250},
"limit": 10,
"expr": "user_id == 2",
"output_fields": ["todo_id", "title", "source", "todo_description", "language", "text", "user_id"],
}
res = collection.search(**search_param)
# search data
# json_strings = [
# '{"page_content": "I love MLflow.", "metadata": {"source": "/path/to/mlflow.txt"}}',
# '{"page_content": "I love langchain.", "metadata": {"source": "/path/to/langchain.txt"}}',
# '{"page_content": "I love AI.", "metadata": {"source": "/path/to/ai.txt"}}',
# ]
json_string = []
# get all of the text
for i in range(len(res[0])):
data = []
data.append({"page_content": res[0][i].get("text")})
data.append({"metadata": {"source": res[0][i].get("source")}})
json_string.append(data)
print(json_string)
#
# print(res[0][0].get("text"))
#
# print("metadata")
# print(res[0][0].get("source"))
# get all