……
class VectorDB:
def __init__(self):
self.arr = np.array([])
self.metadata = []
def insert(self, values, metadata):
values = np.array(values)
# concat
if len(self.arr) > 0:
self.arr = np.concatenate([self.arr, values])
else:
self.arr = values
self.metadata.extend(metadata)
def save(self, filename=None):
if filename is None:
filename = "embs/embs_" + settings["EMBED"].lower()
if not os.path.exists("embs"):
os.mkdir("embs")
dump_numpy_safe(self.arr, filename + ".npy")
dump_json_safe(self.metadata, filename + ".json")
def load(self, filename=None):
if filename is None:
filename = "embs/embs_" + settings["EMBED"].lower()
try:
with open(filename + ".npy", "rb") as f:
self.arr = np.load(f)
with open(filename + ".json") as f:
self.metadata = json.load(f)
if len(self.arr) != len(self.metadata):
raise Exception("length mismatch, corrupted db")
except Exception as e:
print("failed to load embs:", e)
return self
def query_nearest(self, emb, dedup=lambda t: t[1], k=10):
sims = np.dot(self.arr, emb)
topk = np.argsort(sims)[::-1]
nearest = []
keys = set()
for i in topk:
if dedup is not None:
key = dedup(self.metadata[i])
if key in keys:
continue
keys.add(key)
nearest.append((sims[i], self.metadata[i]))
if len(nearest) >= k:
break
return nearest
运行时报错:
Traceback (most recent call last):
File "C:\Users\**\AppData\Local\Programs\Python\Python311\Lib\site-packages\gradio\queueing.py", line 541, in process_events
response = await route_utils.call_process_api(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\**\AppData\Local\Programs\Python\Python311\Lib\site-packages\gradio\route_utils.py", line 276, in call_process_api
output = await app.get_blocks().process_api(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\HP\AppData\Local\Programs\Python\Python311\Lib\site-packages\gradio\blocks.py", line 1928, in process_api
result = await self.call_function(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\**\AppData\Local\Programs\Python\Python311\Lib\site-packages\gradio\blocks.py", line 1514, in call_function
prediction = await anyio.to_thread.run_sync(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\**\AppData\Local\Programs\Python\Python311\Lib\site-packages\anyio\to_thread.py", line 33, in run_sync
return await get_asynclib().run_sync_in_worker_thread(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\**\AppData\Local\Programs\Python\Python311\Lib\site-packages\anyio\_backends\_asyncio.py", line 877, in run_sync_in_worker_thread
return await future
^^^^^^^^^^^^
File "C:\Users\**\AppData\Local\Programs\Python\Python311\Lib\site-packages\anyio\_backends\_asyncio.py", line 807, in run
result = context.run(func, *args)
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\**\AppData\Local\Programs\Python\Python311\Lib\site-packages\gradio\utils.py", line 833, in wrapper
response = f(*args, **kwargs)
^^^^^^^^^^^^^^^^^^
File "F:\**.py", line 48, in querier
nearest = db.query_nearest(emb, k=topk)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "F:\**.py", line 100, in query_nearest
sims = np.dot(self.arr, emb)
^^^^^^^^^^^^^^^^^^^^^
ValueError: shapes (0,) and (3072,) not aligned: 0 (dim 0) != 3072 (dim 0)