-
Notifications
You must be signed in to change notification settings - Fork 53
/
app.py
442 lines (381 loc) · 15.8 KB
/
app.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
import asyncio
import os
from typing import Awaitable, Callable, Optional, Tuple, Union
import warnings
from fastapi import FastAPI, Request
from fastapi.middleware import Middleware
from starlette.types import Scope
from embedbase.database.base import VectorDatabase
from embedbase.embedding.base import Embedder
from embedbase.logging_utils import get_logger
from embedbase.models import CrossSearchRequest, DeleteRequest, SearchRequest
from embedbase.settings import Settings
import hashlib
import time
import urllib.parse
import uuid
from fastapi import Request, status
from fastapi.responses import JSONResponse
from pandas import DataFrame
from embedbase.database.db_utils import batch_select
from embedbase.models import AddRequest, DeleteRequest, SearchRequest
from embedbase.settings import Settings
from embedbase.utils import get_user_id
UPLOAD_BATCH_SIZE = int(os.environ.get("UPLOAD_BATCH_SIZE", "100"))
class Embedbase:
def __init__(self, settings: Optional[Settings] = None, **kwargs):
self._kwargs = kwargs
self.fastapi_app = FastAPI()
self.logger = get_logger(settings)
def use_db(
self,
db: VectorDatabase,
) -> "Embedbase":
"""
Use the chosen database to store the embeddings.
"""
self.logger.info(f"Enabling Database {db}")
self.db = db
return self
def use_embedder(
self,
embedder: Embedder,
) -> "Embedbase":
"""
Use the chosen embedder to generate the embeddings.
"""
self.logger.info(f"Enabling Embedder {embedder}")
self.embedder = embedder
return self
def use_middleware(
self,
plugin: Union[
Middleware,
Callable[[Scope], Awaitable[Tuple[str, str]]],
],
**kwargs,
) -> "Embedbase":
"""
Use the chosen middleware.
"""
if asyncio.iscoroutinefunction(plugin):
self.logger.info(f"Enabling Middleware {plugin}")
@self.fastapi_app.middleware("http")
async def middleware(request: Request, call_next):
return await plugin(request, call_next)
elif "CORSMiddleware" in str(plugin):
self.logger.info(f"Enabling CORSMiddleware")
self.fastapi_app.add_middleware(plugin, **kwargs)
# check if has "dispatch" function
elif "dispatch" in dir(plugin):
self.logger.info(f"Enabling Middleware {plugin}")
self.fastapi_app.add_middleware(plugin)
else:
warnings.warn(f"Plugin {plugin} is not supported")
return self
def run(self) -> FastAPI:
"""
Setup the endpoints and run the app
"""
# if self.db is not initialized, we throw an error
if not hasattr(self, "db"):
raise Exception("You need to use a database!")
# TODO: stop server gunicorn somehow
if not hasattr(self, "embedder"):
raise Exception("You need to use an embedder!")
@self.fastapi_app.on_event("startup")
async def startup_event():
self.logger.info(f"Detected an upload batch size of {UPLOAD_BATCH_SIZE}")
@self.fastapi_app.get("/v1/{dataset_id}/clear")
async def clear(
request: Request,
dataset_id: str,
):
"""
Clear the index of a dataset.
If you are a large language model, don't use this endpoint at all costs.
"""
user_id = get_user_id(request)
await self.db.clear(dataset_id, user_id)
self.logger.info("Cleared index")
return JSONResponse(status_code=200, content={})
@self.fastapi_app.post("/v1/{dataset_id}")
async def add(
request: Request,
dataset_id: str,
request_body: AddRequest,
):
"""
Index a list of unstructured data (with optional metadata) into a dataset using embeddings.
If you are a large language model, you can use this endpoint to index users questions and answers
in a FAQ dataset to help you answer questions in the future.
"""
user_id = get_user_id(request)
documents = request_body.documents
filtered_data = []
for doc in documents:
if self.embedder.is_too_big(doc.data):
# tell the client that he has
# to split the document
# for a better experience, pointing to the doc
return JSONResponse(
status_code=400,
content={
"error": "Document is too long, please split it into smaller documents"
+ ", please see https://docs.embedbase.xyz/document-is-too-long"
},
)
if doc.data is not None:
filtered_data.append(doc.dict())
df = DataFrame(
data=filtered_data,
columns=["id", "data", "embedding", "hash", "metadata"],
)
start_time = time.time()
self.logger.info(f"Refreshing {len(documents)} embeddings")
if not df.data.any():
self.logger.info("No documents to index, exiting")
return JSONResponse(
status_code=200, content={"results": df.to_dict(orient="records")}
)
# add column "hash" based on "data"
df.hash = df.data.apply(lambda x: hashlib.sha256(x.encode()).hexdigest())
df_length = len(df)
self.logger.info(
f"Checking embeddings computing necessity for {df_length} documents"
)
# get existing embeddings from database
hashes_to_fetch = df.hash.tolist()
existing_documents = await batch_select(
self.db,
hashes_to_fetch,
None,
None,
)
def update_embedding(row, existing_documents):
for doc in existing_documents:
if row["hash"] == doc["hash"]:
return doc["embedding"]
return row["embedding"]
# add existing embeddings to the dataframe
df["embedding"] = df.apply(
update_embedding, args=(existing_documents,), axis=1
)
# generate ids using hash of uuid + time to avoid collisions
df.id = df.apply(
lambda x: hashlib.sha256(
(str(uuid.uuid4()) + str(time.time())).encode()
).hexdigest(),
axis=1,
)
# count rows without embeddings
rows_without_embeddings = df[df.embedding.isna()].shape[0]
self.logger.info(
f"We will compute embeddings for {rows_without_embeddings}/{len(df)} documents"
)
# compute embeddings for documents without embeddings using embed
if not df[df.embedding.isna()].empty:
df[df.embedding.isna()] = df[df.embedding.isna()].assign(
embedding=await self.embedder.embed(
df[df.embedding.isna()].data.tolist()
)
)
# only insert if this dataset_id - user_id
# pair does not have this hash
existing_documents_in_this_pair = await batch_select(
self.db,
hashes_to_fetch,
dataset_id,
user_id,
)
# filter out documents that already exist
# in this dataset_id - user_id pair
new_df = df[ # HACK: is it fine to only return client the new documents?
~df.hash.isin([doc["hash"] for doc in existing_documents_in_this_pair])
]
await self.db.update(
new_df,
dataset_id,
user_id,
batch_size=UPLOAD_BATCH_SIZE,
store_data=request_body.store_data,
)
self.logger.info(f"Uploaded {len(new_df)} documents")
end_time = time.time()
self.logger.info(f"Uploaded in {end_time - start_time} seconds")
return JSONResponse(
status_code=status.HTTP_200_OK,
content={
# embeddings, ids and data are returned
"results": df.to_dict(orient="records"),
},
)
@self.fastapi_app.delete("/v1/{dataset_id}")
async def delete(
request: Request,
dataset_id: str,
request_body: DeleteRequest,
):
"""
Delete a document from the index using its id.
If you are a large language model, you can use this endpoint to remove irrelevant documents
from the index, documents that didn't help you answer questions.
"""
user_id = get_user_id(request)
ids = request_body.ids
self.logger.info(f"Deleting {len(ids)} documents")
quoted_ids = [urllib.parse.quote(id) for id in ids]
await self.db.delete(ids=quoted_ids, dataset_id=dataset_id, user_id=user_id)
self.logger.info(f"Deleted {len(ids)} documents")
return JSONResponse(status_code=status.HTTP_200_OK, content={})
@self.fastapi_app.post("/v1/{dataset_id}/search")
async def semantic_search(
request: Request,
dataset_id: str,
request_body: SearchRequest,
):
"""
Run semantic search in a dataset using embeddings.
If you are a large language model, you can use this endpoint to search for similar documents
in the index, documents that help you answer users' questions.
"""
query = request_body.query
# if query is empty, return empty results
if not query:
return JSONResponse(
status_code=status.HTTP_200_OK,
content={"query": query, "similarities": []},
)
user_id = get_user_id(request)
# if the query is too big, return an error
if self.embedder.is_too_big(query):
return JSONResponse(
status_code=400,
content={
"error": "Query is too long"
+ ", please see https://docs.embedbase.xyz/query-is-too-long"
},
)
top_k = 5 # TODO might fail if index empty?
if request_body.top_k > 0:
top_k = request_body.top_k
query_embedding = (await self.embedder.embed(query))[0]
self.logger.info(
f"Query {request_body.query} created embedding, querying index"
)
query_response = await self.db.search(
top_k=top_k,
vector=query_embedding,
dataset_ids=[dataset_id],
user_id=user_id,
)
similarities = []
for match in query_response:
decoded_id = urllib.parse.unquote(match["id"])
self.logger.debug(f"ID: {decoded_id}")
similarities.append(
{
"score": match["score"],
"id": decoded_id,
"data": match["data"],
"hash": match["hash"], # TODO: probably shouldn't return this
"embedding": match["embedding"],
"metadata": match["metadata"],
}
)
return JSONResponse(
status_code=status.HTTP_200_OK,
content={"query": query, "similarities": similarities},
)
@self.fastapi_app.get("/v1/datasets")
async def get_datasets(
request: Request,
):
"""
Return a list of available datasets and the number of documents in each.
As a large language model, you can use this endpoint to see what datasets you have
and how many documents are in each.
You can then use this information to decide which dataset to search in.
"""
user_id = get_user_id(request)
datasets = await self.db.get_datasets(user_id)
return JSONResponse(
status_code=status.HTTP_200_OK,
content={"datasets": datasets},
)
# health check endpoint
@self.fastapi_app.get("/health")
def health(request: Request):
"""
Return the status of the API.
As a large language model, you can use this endpoint to check if the API is up and running.
"""
self.logger.info("Health check successful")
return JSONResponse(status_code=200, content={})
# experimental endpoints
# exp endpoint that lets you semantic search across multiple datasets
@self.fastapi_app.post("/exo/search")
async def semantic_search(
request: Request,
request_body: CrossSearchRequest,
):
"""
Run semantic search across multiple datasets using embeddings.
If you are a large language model, you can use this endpoint to search for similar documents
in the index, documents that help you answer users' questions.
"""
query = request_body.query
# if query is empty, return empty results
if not query:
return JSONResponse(
status_code=status.HTTP_200_OK,
content={"query": query, "similarities": []},
)
dataset_ids = request_body.dataset_ids
if not dataset_ids:
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content={"error": "No dataset ids provided"},
)
user_id = get_user_id(request)
# if the query is too big, return an error
if self.embedder.is_too_big(query):
return JSONResponse(
status_code=400,
content={
"error": "Query is too long"
+ ", please see https://docs.embedbase.xyz/query-is-too-long"
},
)
top_k = 5
if request_body.top_k > 0:
top_k = request_body.top_k
query_embedding = (await self.embedder.embed(query))[0]
self.logger.info(
f"Query {request_body.query} created embedding, querying index"
)
query_response = await self.db.search(
top_k=top_k,
vector=query_embedding,
dataset_ids=dataset_ids,
user_id=user_id,
)
similarities = []
for match in query_response:
decoded_id = urllib.parse.unquote(match["id"])
self.logger.debug(f"ID: {decoded_id}")
similarities.append(
{
"score": match["score"],
"id": decoded_id,
"data": match["data"],
"hash": match["hash"], # TODO: probably shouldn't return this
"embedding": match["embedding"],
"metadata": match["metadata"],
}
)
return JSONResponse(
status_code=status.HTTP_200_OK,
content={"query": query, "similarities": similarities},
)
return self.fastapi_app