-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.py
More file actions
41 lines (35 loc) · 1.15 KB
/
api.py
File metadata and controls
41 lines (35 loc) · 1.15 KB
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
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from embedding.rag import RAG
def create_app(model_name):
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
# Initialize RAG with specified model
rag = RAG()
rag.load_model(model_name)
rag_callback = rag.get_rag_callback()
search_callback = rag.get_search_callback()
@app.get("/rag/")
def rag_endpoint(question: str):
try:
answer = rag_callback(question)
return {"answer": answer}
except Exception as e:
return {"error": str(e)}
@app.get("/search/")
def search_endpoint(query: str):
try:
answer = search_callback(query)
return {"results": answer}
except Exception as e:
return {"error": str(e)}
@app.get("/")
def read_root():
return {"message": f"RAG API running with model: {model_name}"}
return app