-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
132 lines (108 loc) · 3.5 KB
/
server.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
import asyncio
import json
import traceback
import uuid
from typing import Literal, TypedDict
from fastapi import FastAPI, HTTPException, Response
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from model.faiss import Node
from model.rag import GuidedRagTopicAndNodes, IterativeRagResult
from services.rag import guided_rag, iterative_rag, naive_rag
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods (GET, POST, etc.)
allow_headers=["*"], # Allows all headers
)
class IterativeRagRequest(BaseModel):
question: str
max_steps: int
top_K: int
class IterativeRagResponse(IterativeRagResult):
id: str
type: Literal["iterative"]
LLM_LOCK = asyncio.Lock()
@app.post("/iterative_rag")
async def iterative_rag_handler(request: IterativeRagRequest):
await LLM_LOCK.acquire()
try:
response: IterativeRagResponse = {
"id": str(uuid.uuid4()),
"type": "iterative",
**iterative_rag(request.question, request.max_steps, request.top_K),
}
except:
print(traceback.format_exc())
LLM_LOCK.release()
raise HTTPException(
status_code=500,
detail="Internal Server Error. Please try another question or adjusting parameters (they may be set to high).",
)
LLM_LOCK.release()
return Response(json.dumps(response), media_type="application/json")
class GuidedRagRequest(BaseModel):
question: str
max_topics: int
top_K_per_topic: int
class GuidedRagResponse(TypedDict):
id: str
answer: str
question: str
topics_and_nodes: list[GuidedRagTopicAndNodes]
type: Literal["guided"]
@app.post("/guided_rag")
async def guided_rag_handler(request: GuidedRagRequest):
await LLM_LOCK.acquire()
try:
answer, topics_and_nodes = guided_rag(
request.question, request.max_topics, request.top_K_per_topic
)
except:
print(traceback.format_exc())
LLM_LOCK.release()
raise HTTPException(
status_code=500,
detail="Internal Server Error. Please try another question or adjusting parameters (they may be set to high).",
)
LLM_LOCK.release()
response: GuidedRagResponse = {
"answer": answer,
"question": request.question,
"topics_and_nodes": topics_and_nodes,
"id": str(uuid.uuid4()),
"type": "guided",
}
return Response(json.dumps(response), media_type="application/json")
class NaiveRagRequest(BaseModel):
question: str
top_K: int
class NaiveRagResponse(TypedDict):
answer: str
question: str
sources: list[Node]
id: str
type: Literal["naive"]
@app.post("/naive_rag")
async def naive_rag_handler(request: NaiveRagRequest):
await LLM_LOCK.acquire()
try:
answer, nodes = naive_rag(request.question, request.top_K)
except:
print(traceback.format_exc())
LLM_LOCK.release()
raise HTTPException(
status_code=500,
detail="Internal Server Error. Please try another question or adjusting parameters (they may be set to high).",
)
response: NaiveRagResponse = {
"answer": answer,
"question": request.question,
"sources": nodes,
"id": str(uuid.uuid4()),
"type": "naive",
}
LLM_LOCK.release()
return Response(json.dumps(response), media_type="application/json")