forked from KShivendu/awesome-vectordb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
67 lines (45 loc) · 1.44 KB
/
main.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
import os
from typing import Type
import cohere
from fastapi import Depends, FastAPI
from pydantic import BaseModel
from vectordb import PineconeDB, QdrantDB, RedisDB, VectorDatabase
# Initializations
app = FastAPI()
# Initialize Cohere
co = cohere.Client(os.environ["COHERE_API_KEY"])
# Define the index name
index_name = "wikipedia-embeddings"
# vector_db = None
# Define the request model
class QueryRequest(BaseModel):
query: str
# Dependency function to choose a vector database implementation
def get_vector_db() -> Type[VectorDatabase]:
# Choose either PineconeDatabase or QdrantDatabase here
vector_db_class = RedisDB # or QdrantDatabase
return vector_db_class(index_name)
@app.on_event("startup")
async def startup_event():
vector_db = get_vector_db()
vector_db.upsert()
@app.post("/ask")
async def ask(
request: QueryRequest, vector_db: VectorDatabase = Depends(get_vector_db)
) -> dict:
# Get the embeddings
query_embeds = co.embed([request.query], model="multilingual-22-12").embeddings
# Query the VectorDatabase
result = vector_db.query(query_embedding=query_embeds[0])
return {"result": result}
# @app.post("/upsert")
# async def upsert():
# vector_db = get_vector_db()
# return vector_db.upsert()
@app.get("/health")
async def health():
return {"status": "ok"}
# @app.on_event("shutdown")
# async def shutdown():
# vector_db = get_vector_db()
# # vector_db.delete_index()