forked from HexyeDEV/Telegram-Chatbot-Gpt4Free
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.py
24 lines (20 loc) · 733 Bytes
/
memory.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
import chromadb
from chromadb.db.base import UniqueConstraintError
class Memory:
def __init__(self, name):
self.name = name
self.client = chromadb.PersistentClient(
path="./persist"
)
try:
self.collection = self.client.create_collection(name)
except UniqueConstraintError:
self.collection = self.client.get_collection(name)
except Exception as e:
print(f"Error initializing Memory class: {e}")
raise
def insert(self, data, uuid):
self.collection.add(documents=[data], ids=[uuid])
def find(self, query):
q = self.collection.query(query_texts=[query], n_results=2)
return q["documents"]