-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
163 lines (146 loc) · 5.1 KB
/
__main__.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import time
import asyncio
import postgresml as pgl
import zilliz_local as zl
import pinecone_local as pl
import qdrant_local as ql
import openai_local as al
import huggingface as hf
import weaviate_local as wl
TRIAL_COUNT = 5
# The pairs we are testing with
tests = [
{
"name": "PostgresML",
"vector_store": pgl,
"rag+": True,
"chatbot_service": al,
"async": True,
},
{"name": "Weaviate", "vector_store": wl, "chatbot_service": al, "rag++": True},
{
"name": "Zilliz",
"vector_store": zl,
"embedding_service": hf,
"chatbot_service": al,
},
{
"name": "Pinecone",
"vector_store": pl,
"embedding_service": hf,
"chatbot_service": al,
},
{
"name": "Qdrant",
"vector_store": ql,
"embedding_service": hf,
"chatbot_service": al,
},
]
# Our documents
# We only really need to test on 2. When we search we are trying to get the first document back
documents = [
{"id": "0", "metadata": {"text": "The hidden value is 1000"}},
{
"id": "1",
"metadata": {"text": "This is just some random text"},
},
]
def maybe_do_async(func, check_dict, *args):
if "async" in check_dict and check_dict["async"]:
return asyncio.run(func(*args))
else:
return func(*args)
def do_data_upsert(name, vector_store, **kwargs):
print(f"Doing Data Upsert For: {name}")
if "rag++" in kwargs or "rag+" in kwargs:
maybe_do_async(vector_store.upsert_data, kwargs, documents)
else:
texts = [d["metadata"]["text"] for d in documents]
(embeddings, time_to_embed) = kwargs["embedding_service"].get_embeddings(texts)
maybe_do_async(vector_store.upsert_data, kwargs, documents, embeddings)
print(f"Done Doing Data Upsert For: {name}\n")
def do_normal_rag_test(name, vector_store, **kwargs):
print(f"Doing RAG Test For: {name}")
query = "What is the hidden value?"
if "rag++" in kwargs:
(result, time_to_complete) = maybe_do_async(
vector_store.get_llm_response, kwargs, query
)
time_to_embed = 0
time_to_search = 0
elif "rag+" in kwargs:
time_to_embed = 0
(context, time_to_search) = maybe_do_async(
vector_store.do_search, kwargs, query
)
(result, time_to_complete) = kwargs["chatbot_service"].get_llm_response(
query, context
)
else:
(embeddings, time_to_embed) = kwargs["embedding_service"].get_embeddings(
[query]
)
(context, time_to_search) = vector_store.do_search(embeddings[0])
(result, time_to_complete) = kwargs["chatbot_service"].get_llm_response(
query, context
)
print(f"\tThe LLM Said: {result}")
time_for_retrieval = time_to_embed + time_to_search
total_time = time_to_embed + time_to_search + time_to_complete
print(f"Done Doing RAG Test For: {name}")
print(f"- Time to Embed: {time_to_embed}")
print(f"- Time to Search: {time_to_search}")
print(f"- Total Time for Retrieval: {time_for_retrieval}")
print(f"- Time for Chatbot Completion: {time_to_complete}")
print(f"- Total Time Taken: {total_time}\n")
return {
"time_to_embed": time_to_embed,
"time_to_search": time_to_search,
"time_for_retrieval": time_for_retrieval,
"time_to_complete": time_to_complete,
"total_time": total_time,
}
if __name__ == "__main__":
print("----------Doing Data Setup-------------------------\n")
for test in tests:
do_data_upsert(**test)
print("\n----------Done Doing Data Setup------------------\n\n")
print("----------Doing Rag Tests-------------------------\n")
stats = {}
for i in range(TRIAL_COUNT):
for test in tests:
times = do_normal_rag_test(**test)
if not test["name"] in stats:
stats[test["name"]] = []
stats[test["name"]].append(times)
print("\n----------Done Doing Rag Tests---------------------\n")
print("------------Final Results---------------------------\n")
for test in tests:
trials = stats[test["name"]]
(
time_to_embed,
time_to_search,
time_for_retrieval,
time_to_complete,
total_time,
) = [
sum(trial[key] for trial in trials)
for key in [
"time_to_embed",
"time_to_search",
"time_for_retrieval",
"time_to_complete",
"total_time",
]
]
print(f'Done Doing RAG Test For: {test["name"]}')
print(f"- Average Time to Embed: {(time_to_embed / TRIAL_COUNT):0.4f}")
print(f"- Average Time to Search: {(time_to_search / TRIAL_COUNT):0.4f}")
print(
f"- Average Total Time for Retrieval: {(time_for_retrieval / TRIAL_COUNT):0.4f}"
)
print(
f"- Average Time for Chatbot Completion: {(time_to_complete / TRIAL_COUNT):0.4f}"
)
print(f"- Average Total Time Taken: {(total_time / TRIAL_COUNT):0.4f}\n")