-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest.py
More file actions
65 lines (57 loc) · 1.85 KB
/
Copy pathtest.py
File metadata and controls
65 lines (57 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import chromadb
from core.memory.OllamaEmbeddingFunction import OllamaEmbeddingFunction
import json
# 初始化嵌入函数
embedding_function = OllamaEmbeddingFunction(
base_url="http://gpu.credat.com.cn",
model="nomic-embed-text:latest"
)
# 初始化Chroma客户端
client = chromadb.PersistentClient(path="./chat_memories/enhanced")
# 获取long_term集合
collection = client.get_collection(
name="user_memories_long_term",
embedding_function=embedding_function
)
# 测试查询
# query = "我提到过卤牛肉吗?"
# results = collection.query(
# query_texts=[query],
# n_results=100, # 获取前5条结果
# where_document={"$contains":"卤牛肉"},
# # where_document={
# # "$or": [
# # {"$contains": "卤牛肉"},
# # {"$contains": "防疫站"}
# # ]
# # }
# )
# results = collection.get(
# limit =100, # 获取前5条结果
# where_document={"$contains":"腰椎"},
# # where_document={
# # "$or": [
# # {"$contains": "卤牛肉"},
# # {"$contains": "防疫站"}
# # ]
# # }
# )
where_document = {"$contains": "本来就欠缺"}
results = collection.get(
limit =10,
where_document=where_document
)
# 打印结果
print("\n查询结果:")
if results["documents"] and results["documents"][0]:
for i, (doc, metadata) in enumerate(zip(results["documents"][0], results["metadatas"][0]), 1):
print(f"\n结果 {i}:")
print(f"内容: {doc}")
# 解析元数据中的context_tags并显示中文
metadata_copy = metadata.copy()
if 'context_tags' in metadata_copy:
tags = json.loads(metadata_copy['context_tags'])
metadata_copy['context_tags'] = tags # 这样会自动显示中文
print(f"元数据: {metadata_copy}")
else:
print("没有找到匹配的结果")