forked from SkywalkerDarren/chatWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.py
184 lines (147 loc) · 6.24 KB
/
storage.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import os.path
from typing import Optional
import faiss
import numpy as np
import pandas as pd
from pgvector.sqlalchemy import Vector
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import sessionmaker, declarative_base
from abc import ABC, abstractmethod
from config import Config
Base = declarative_base()
class Storage(ABC):
"""Abstract Storage class."""
# factory method
@staticmethod
def create_storage(cfg: Config, name: str) -> 'Storage':
"""Create a storage object."""
if cfg.use_postgres:
return _PostgresStorage(cfg, name)
else:
return _IndexStorage(cfg, name)
@abstractmethod
def add(self, text: str, embedding: list[float]):
"""Add a new embedding."""
pass
@abstractmethod
def add_all(self, embeddings: list[tuple[str, list[float]]]):
"""Add multiple embeddings."""
pass
@abstractmethod
def get_texts(self, embedding: list[float], limit=100) -> list[str]:
"""Get the text for the provided embedding."""
pass
@abstractmethod
def get_all_embeddings(self):
"""Get all embeddings."""
pass
@abstractmethod
def clear(self):
"""Clear the database."""
pass
@abstractmethod
def been_indexed(self) -> bool:
"""Check if the database has been indexed."""
pass
class _IndexStorage(Storage):
"""IndexStorage class."""
def __init__(self, cfg: Config, name: str):
"""Initialize the storage."""
self.texts = None
self.index: Optional[faiss.IndexIDMap] = None
self._cfg = cfg
self._name = name
self._load()
def add(self, text: str, embedding: list[float]):
"""Add a new embedding."""
array = np.array([embedding])
self.texts = pd.concat([self.texts, pd.DataFrame({'index': len(self.texts), 'text': text}, index=[0])])
self.index.add_with_ids(array, np.array([len(self.texts) - 1]))
self._save()
def add_all(self, embeddings: list[tuple[str, list[float]]]):
"""Add multiple embeddings."""
ids = np.array([len(self.texts) + i for i, _ in enumerate(embeddings)])
self.texts = pd.concat([self.texts, pd.DataFrame(
{'index': len(self.texts) + i, 'text': text} for i, (text, _) in enumerate(embeddings))])
array = np.array([emb for text, emb in embeddings])
self.index.add_with_ids(array, ids)
self._save()
def update_embedding(self, index: int, embedding: list[float]):
"""Update the embedding for the provided index."""
self.index.remove_ids(np.array([index]))
self.index.add_with_ids(np.array([embedding]), np.array([index]))
self._save()
def get_texts(self, embedding: list[float], limit=10) -> list[str]:
_, indexs = self.index.search(np.array([embedding]), limit)
return self.texts.iloc[indexs[0]].text.tolist()
def get_all_embeddings(self):
texts = self.texts.text.tolist()
embeddings = self.index.reconstruct_n(0, len(self.texts))
return list(zip(texts, embeddings))
def clear(self):
"""Clear the database."""
self._delete()
def been_indexed(self) -> bool:
return os.path.exists(os.path.join(self._cfg.index_path, f'{self._name}.csv')) and os.path.exists(
os.path.join(self._cfg.index_path, f'{self._name}.bin'))
def _save(self):
self.texts.to_csv(os.path.join(self._cfg.index_path, f'{self._name}.csv'))
faiss.write_index(self.index, os.path.join(self._cfg.index_path, f'{self._name}.bin'))
def _load(self):
if self.been_indexed():
self.texts = pd.read_csv(os.path.join(self._cfg.index_path, f'{self._name}.csv'))
self.index = faiss.read_index(os.path.join(self._cfg.index_path, f'{self._name}.bin'))
else:
self.texts = pd.DataFrame(columns=['index', 'text'])
# IDMap2 with Flat
self.index = faiss.index_factory(1536, "IDMap2,Flat", faiss.METRIC_INNER_PRODUCT)
def _delete(self):
try:
os.remove(f'{self._name}.csv')
os.remove(f'{self._name}.bin')
except FileNotFoundError:
pass
self._load()
class _PostgresStorage(Storage):
"""PostgresStorage class."""
def __init__(self, cfg: Config, name: str):
"""Initialize the storage."""
self._postgresql = cfg.postgres_url
self._engine = create_engine(self._postgresql)
Base.metadata.create_all(self._engine)
session = sessionmaker(bind=self._engine)
self._session = session()
self._name = name
def add(self, text: str, embedding: list[float]):
"""Add a new embedding."""
self._session.add(self.EmbeddingEntity(text=text, embedding=embedding, name=self._name))
self._session.commit()
def add_all(self, embeddings: list[tuple[str, list[float]]]):
"""Add multiple embeddings."""
data = [self.EmbeddingEntity(text=text, embedding=embedding, name=self._name) for text, embedding in embeddings]
self._session.add_all(data)
self._session.commit()
def get_texts(self, embedding: list[float], limit=100) -> list[str]:
"""Get the text for the provided embedding."""
result = self._session.query(self.EmbeddingEntity).order_by(
self.EmbeddingEntity.embedding.cosine_distance(embedding)).limit(limit).all()
return [s.text for s in result]
def get_all_embeddings(self):
"""Get all embeddings."""
result = self._session.query(self.EmbeddingEntity).where(self.EmbeddingEntity.name == self._name).all()
return [(s.text, s.embedding) for s in result]
def clear(self):
"""Clear the database."""
self._session.query(self.EmbeddingEntity).delete()
self._session.commit()
def been_indexed(self) -> bool:
return self._session.query(self.EmbeddingEntity).filter_by(name=self._name).first() is not None
def __del__(self):
"""Close the session."""
self._session.close()
class EmbeddingEntity(Base):
__tablename__ = 'embedding'
id = Column(Integer, primary_key=True)
name = Column(String)
text = Column(String)
embedding = Column(Vector(1536))