|
| 1 | +import time |
| 2 | +import asyncio |
| 3 | + |
| 4 | +import postgresml as pgl |
| 5 | +import zilliz_local as zl |
| 6 | +import pinecone_local as pl |
| 7 | +import qdrant_local as ql |
| 8 | +import openai_local as al |
| 9 | +import huggingface as hf |
| 10 | +import weaviate_local as wl |
| 11 | + |
| 12 | +TRIAL_COUNT = 2 |
| 13 | + |
| 14 | +# The pairs we are testing with |
| 15 | +tests = [ |
| 16 | + { |
| 17 | + "name": "PostgresML", |
| 18 | + "vector_store": pgl, |
| 19 | + "rag+": True, |
| 20 | + "chatbot_service": al, |
| 21 | + "async": True, |
| 22 | + }, |
| 23 | + {"name": "Weaviate", "vector_store": wl, "chatbot_service": al, "rag++": True}, |
| 24 | + { |
| 25 | + "name": "Zilliz", |
| 26 | + "vector_store": zl, |
| 27 | + "embedding_service": hf, |
| 28 | + "chatbot_service": al, |
| 29 | + }, |
| 30 | + { |
| 31 | + "name": "Pinecone", |
| 32 | + "vector_store": pl, |
| 33 | + "embedding_service": hf, |
| 34 | + "chatbot_service": al, |
| 35 | + }, |
| 36 | + { |
| 37 | + "name": "Qdrant", |
| 38 | + "vector_store": ql, |
| 39 | + "embedding_service": hf, |
| 40 | + "chatbot_service": al, |
| 41 | + }, |
| 42 | +] |
| 43 | + |
| 44 | + |
| 45 | +# Our documents |
| 46 | +# We only really need to test on 2. When we search we are trying to get the first document back |
| 47 | +documents = [ |
| 48 | + {"id": "0", "metadata": {"text": "The hidden value is 1000"}}, |
| 49 | + { |
| 50 | + "id": "1", |
| 51 | + "metadata": {"text": "This is just some random text"}, |
| 52 | + }, |
| 53 | +] |
| 54 | + |
| 55 | + |
| 56 | +def maybe_do_async(func, check_dict, *args): |
| 57 | + if "async" in check_dict and check_dict["async"]: |
| 58 | + return asyncio.run(func(*args)) |
| 59 | + else: |
| 60 | + return func(*args) |
| 61 | + |
| 62 | + |
| 63 | +def do_data_upsert(name, vector_store, **kwargs): |
| 64 | + print(f"Doing Data Upsert For: {name}") |
| 65 | + if "rag++" in kwargs or "rag+" in kwargs: |
| 66 | + maybe_do_async(vector_store.upsert_data, kwargs, documents) |
| 67 | + else: |
| 68 | + texts = [d["metadata"]["text"] for d in documents] |
| 69 | + (embeddings, time_to_embed) = kwargs["embedding_service"].get_embeddings(texts) |
| 70 | + maybe_do_async(vector_store.upsert_data, kwargs, documents, embeddings) |
| 71 | + print(f"Done Doing Data Upsert For: {name}\n") |
| 72 | + |
| 73 | + |
| 74 | +def do_normal_rag_test(name, vector_store, **kwargs): |
| 75 | + print(f"Doing RAG Test For: {name}") |
| 76 | + query = "What is the hidden value?" |
| 77 | + if "rag++" in kwargs: |
| 78 | + (result, time_to_complete) = maybe_do_async( |
| 79 | + vector_store.get_llm_response, kwargs, query |
| 80 | + ) |
| 81 | + time_to_embed = 0 |
| 82 | + time_to_search = 0 |
| 83 | + elif "rag+" in kwargs: |
| 84 | + time_to_embed = 0 |
| 85 | + (context, time_to_search) = maybe_do_async( |
| 86 | + vector_store.do_search, kwargs, query |
| 87 | + ) |
| 88 | + (result, time_to_complete) = kwargs["chatbot_service"].get_llm_response( |
| 89 | + query, context |
| 90 | + ) |
| 91 | + else: |
| 92 | + (embeddings, time_to_embed) = kwargs["embedding_service"].get_embeddings( |
| 93 | + [query] |
| 94 | + ) |
| 95 | + (context, time_to_search) = vector_store.do_search(embeddings[0]) |
| 96 | + (result, time_to_complete) = kwargs["chatbot_service"].get_llm_response( |
| 97 | + query, context |
| 98 | + ) |
| 99 | + print(f"\tThe LLM Said: {result}") |
| 100 | + time_for_retrieval = time_to_embed + time_to_search |
| 101 | + total_time = time_to_embed + time_to_search + time_to_complete |
| 102 | + print(f"Done Doing RAG Test For: {name}") |
| 103 | + print(f"- Time to Embed: {time_to_embed}") |
| 104 | + print(f"- Time to Search: {time_to_search}") |
| 105 | + print(f"- Total Time for Retrieval: {time_for_retrieval}") |
| 106 | + print(f"- Time for Chatbot Completion: {time_to_complete}") |
| 107 | + print(f"- Total Time Taken: {total_time}\n") |
| 108 | + return { |
| 109 | + "time_to_embed": time_to_embed, |
| 110 | + "time_to_search": time_to_search, |
| 111 | + "time_for_retrieval": time_for_retrieval, |
| 112 | + "time_to_complete": time_to_complete, |
| 113 | + "total_time": total_time, |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + print("----------Doing Data Setup-------------------------\n") |
| 119 | + for test in tests: |
| 120 | + do_data_upsert(**test) |
| 121 | + print("\n----------Done Doing Data Setup------------------\n\n") |
| 122 | + |
| 123 | + print("----------Doing Rag Tests-------------------------\n") |
| 124 | + stats = {} |
| 125 | + for i in range(TRIAL_COUNT): |
| 126 | + for test in tests: |
| 127 | + times = do_normal_rag_test(**test) |
| 128 | + if not test["name"] in stats: |
| 129 | + stats[test["name"]] = [] |
| 130 | + stats[test["name"]].append(times) |
| 131 | + print("\n----------Done Doing Rag Tests---------------------\n") |
| 132 | + |
| 133 | + print("------------Final Results---------------------------\n") |
| 134 | + for test in tests: |
| 135 | + trials = stats[test["name"]] |
| 136 | + ( |
| 137 | + time_to_embed, |
| 138 | + time_to_search, |
| 139 | + time_for_retrieval, |
| 140 | + time_to_complete, |
| 141 | + total_time, |
| 142 | + ) = [ |
| 143 | + sum(trial[key] for trial in trials) |
| 144 | + for key in [ |
| 145 | + "time_to_embed", |
| 146 | + "time_to_search", |
| 147 | + "time_for_retrieval", |
| 148 | + "time_to_complete", |
| 149 | + "total_time", |
| 150 | + ] |
| 151 | + ] |
| 152 | + print(f'Done Doing RAG Test For: {test["name"]}') |
| 153 | + print(f"- Average Time to Embed: {(time_to_embed / TRIAL_COUNT):0.4f}") |
| 154 | + print(f"- Average Time to Search: {(time_to_search / TRIAL_COUNT):0.4f}") |
| 155 | + print( |
| 156 | + f"- Average Total Time for Retrieval: {(time_for_retrieval / TRIAL_COUNT):0.4f}" |
| 157 | + ) |
| 158 | + print( |
| 159 | + f"- Average Time for Chatbot Completion: {(time_to_complete / TRIAL_COUNT):0.4f}" |
| 160 | + ) |
| 161 | + print(f"- Average Total Time Taken: {(total_time / TRIAL_COUNT):0.4f}\n") |
0 commit comments