This repository was archived by the owner on Jul 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_postgres.py
More file actions
131 lines (109 loc) · 4.17 KB
/
Copy pathtest_postgres.py
File metadata and controls
131 lines (109 loc) · 4.17 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import openai
from rich import print
from sqlalchemy_vectorstores import PostgresDatabase, PostgresVectorStore
DB_URL = "postgresql+psycopg://postgres:postgres@127.0.0.1:5432/postgres"
OPENAI_BASE_URL = "http://192.168.8.68:9997/v1"
OPENAI_API_KEY = "E"
EMBEDDING_MODEL = "bge-large-zh-v1.5"
client = openai.Client(base_url=OPENAI_BASE_URL, api_key=OPENAI_API_KEY)
def embed_func(text: str) -> list[float]:
return client.embeddings.create(
input=text,
model=EMBEDDING_MODEL,
).data[0].embedding
db = PostgresDatabase(DB_URL, echo=False)
vs = PostgresVectorStore(db, dim=1024, embedding_func=embed_func, clear_existed=True)
query = "Alaqua Cox"
sentences1 = [
"Capri-Sun is a brand of juice concentrate–based drinks manufactured by the German company Wild and regional licensees.",
"George V was King of the United Kingdom and the British Dominions, and Emperor of India, from 6 May 1910 until his death in 1936.",
"Alaqua Cox is a Native American (Menominee) actress.",
]
sentences2 = [
"Shohei Ohtani is a Japanese professional baseball pitcher and designated hitter for the Los Angeles Dodgers of Major League Baseball.",
"Tamarindo, also commonly known as agua de tamarindo, is a non-alcoholic beverage made of tamarind, sugar, and water.",
]
def test_create():
# add sources
print("add sources")
src_id1 = vs.add_source(src="file1.pdf", tags=["a", "b"], metadata={"path": "path1"})
src_id2 = vs.add_source(src="file2.txt", tags=["c", "b"], metadata={"path": "path2"})
# add documents
print("add documents")
for s in sentences1:
vs.add_document(src_id=src_id1, content=s)
for s in sentences2:
vs.add_document(src_id=src_id2, content=s)
# search sources by url
print("search sources by url")
r = vs.search_sources(vs.db.make_filter(vs.src_table.c.src, "file1.pdf"))
print(r)
assert isinstance(r, list) and len(r) == 1
r = r[0]
assert r["id"] == src_id1
# search sources by metadata
print("search sources by metadata")
r = vs.search_sources(vs.db.make_filter(vs.src_table.c.metadata, "path2", "dict", "path"))
print(r)
assert isinstance(r, list) and len(r) == 1
r = r[0]
assert r["id"] == src_id2
r = vs.search_sources(vs.db.make_filter(vs.src_table.c.metadata, "path%", "dict", "path"))
print(r)
assert isinstance(r, list) and len(r) == 2
# search sources by tags
print("search sources by tags")
r = vs.get_sources_by_tags(tags_all=["b", "a"])
print(r)
assert len(r) == 1
assert r[0]["tags"] == ["a", "b"]
r = vs.get_sources_by_tags(tags_any=["b", "a"])
print(r)
assert len(r) == 2
# upsert source with id
print("upsert source with id")
vs.upsert_source({"id": src_id1, "metadata": {"path": "path1", "added": True}})
r = vs.get_source_by_id(src_id1)
print(r)
assert r["metadata"]["added"]
# upsert source without id
print("upsert source without id")
src_id3 = vs.upsert_source({"src": "file3.docx", "metadata": {"path": "path3", "added": True}})
r = vs.get_source_by_id(src_id3)
print(r)
assert r["metadata"]["path"] == "path3"
assert r["src"] == "file3.docx"
for s in sentences2:
vs.add_document(src_id=src_id3, content=s)
# list documents of source file
print("list documents of source file")
r = vs.get_documents_of_source(src_id3)
print(r)
assert len(r) == len(sentences2)
# delete source
print("delete source")
r = vs.delete_source(src_id3)
print(r)
r = vs.get_source_by_id(src_id3)
assert r is None
r = vs.get_documents_of_source(src_id3)
assert len(r) == 0
# search by vector
print("search by vector")
r = vs.search_by_vector(query)
print(r)
assert len(r) == 3
assert query in r[0]["content"]
# search by vector with filters
print("search by vector with filters")
filters = [
vs.db.make_filter(vs.src_table.c.src, "file1.pdf")
]
r = vs.search_by_vector(query, filters=filters)
print(r)
assert query in r[0]["content"]
# search by bm25
print("search by bm25")
r = vs.search_by_bm25(query.replace(" ", " & "))
print(r)
assert query in r[0]["content"]