update followup
Browse files
main.py
CHANGED
@@ -152,13 +152,14 @@ class ChatRequest(BaseModel):
|
|
152 |
conversation_id: Optional[str] = Field(None, description="Unique identifier for the conversation")
|
153 |
model_id: str = Field(..., description="Identifier for the LLM model to use")
|
154 |
user_id: str = Field(..., description="Unique identifier for the user")
|
|
|
155 |
|
156 |
async def get_api_key(x_api_key: str = Header(...)) -> str:
|
157 |
if x_api_key != CHAT_AUTH_KEY:
|
158 |
raise HTTPException(status_code=403, detail="Invalid API key")
|
159 |
return x_api_key
|
160 |
|
161 |
-
async def stream_llm_request(api_key: str, llm_request: Dict[str, str]) -> AsyncGenerator[str, None]:
|
162 |
"""
|
163 |
Make a streaming request to the LLM service.
|
164 |
"""
|
@@ -166,7 +167,7 @@ async def stream_llm_request(api_key: str, llm_request: Dict[str, str]) -> Async
|
|
166 |
async with httpx.AsyncClient() as client:
|
167 |
async with client.stream(
|
168 |
"POST",
|
169 |
-
|
170 |
headers={
|
171 |
"accept": "text/event-stream",
|
172 |
"X-API-Key": api_key,
|
@@ -186,6 +187,7 @@ async def stream_llm_request(api_key: str, llm_request: Dict[str, str]) -> Async
|
|
186 |
logger.error(f"Unexpected error occurred while making LLM request: {str(e)}")
|
187 |
raise HTTPException(status_code=500, detail=f"Unexpected error occurred while making LLM request: {str(e)}")
|
188 |
|
|
|
189 |
@app.post("/chat/", response_class=StreamingResponse, tags=["Chat"])
|
190 |
async def chat(request: ChatRequest, background_tasks: BackgroundTasks, api_key: str = Depends(get_api_key)):
|
191 |
"""
|
@@ -201,23 +203,35 @@ async def chat(request: ChatRequest, background_tasks: BackgroundTasks, api_key:
|
|
201 |
|
202 |
# Create RAG prompt
|
203 |
rag_prompt = f"Based on the following context, please answer the user's question:\n\nContext:\n{context}\n\nUser's question: {request.query}\n\nAnswer:"
|
204 |
-
|
|
|
205 |
# Generate conversation_id if not provided
|
206 |
conversation_id = request.conversation_id or str(uuid.uuid4())
|
207 |
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
|
217 |
logger.info(f"Starting chat response generation for user: {request.user_id} Full request: {llm_request}")
|
218 |
async def response_generator():
|
219 |
full_response = ""
|
220 |
-
async for chunk in stream_llm_request(api_key, llm_request):
|
221 |
full_response += chunk
|
222 |
yield chunk
|
223 |
logger.info(f"Finished chat response generation for user: {request.user_id} Full response{full_response}")
|
|
|
152 |
conversation_id: Optional[str] = Field(None, description="Unique identifier for the conversation")
|
153 |
model_id: str = Field(..., description="Identifier for the LLM model to use")
|
154 |
user_id: str = Field(..., description="Unique identifier for the user")
|
155 |
+
enable_followup: bool = Field(default=False, description="Flag to enable follow-up questions")
|
156 |
|
157 |
async def get_api_key(x_api_key: str = Header(...)) -> str:
|
158 |
if x_api_key != CHAT_AUTH_KEY:
|
159 |
raise HTTPException(status_code=403, detail="Invalid API key")
|
160 |
return x_api_key
|
161 |
|
162 |
+
async def stream_llm_request(api_key: str, llm_request: Dict[str, str]), endpoint_url:str -> AsyncGenerator[str, None]:
|
163 |
"""
|
164 |
Make a streaming request to the LLM service.
|
165 |
"""
|
|
|
167 |
async with httpx.AsyncClient() as client:
|
168 |
async with client.stream(
|
169 |
"POST",
|
170 |
+
endpoint_url,
|
171 |
headers={
|
172 |
"accept": "text/event-stream",
|
173 |
"X-API-Key": api_key,
|
|
|
187 |
logger.error(f"Unexpected error occurred while making LLM request: {str(e)}")
|
188 |
raise HTTPException(status_code=500, detail=f"Unexpected error occurred while making LLM request: {str(e)}")
|
189 |
|
190 |
+
|
191 |
@app.post("/chat/", response_class=StreamingResponse, tags=["Chat"])
|
192 |
async def chat(request: ChatRequest, background_tasks: BackgroundTasks, api_key: str = Depends(get_api_key)):
|
193 |
"""
|
|
|
203 |
|
204 |
# Create RAG prompt
|
205 |
rag_prompt = f"Based on the following context, please answer the user's question:\n\nContext:\n{context}\n\nUser's question: {request.query}\n\nAnswer:"
|
206 |
+
system_prompt = "You are a helpful assistant tasked with providing answers using the context provided"
|
207 |
+
|
208 |
# Generate conversation_id if not provided
|
209 |
conversation_id = request.conversation_id or str(uuid.uuid4())
|
210 |
|
211 |
+
if request.enable_followup:
|
212 |
+
# Prepare the request for the LLM service
|
213 |
+
pass
|
214 |
+
llm_request = {
|
215 |
+
"query": rag_prompt,
|
216 |
+
"model_id": 'openai/gpt-4o-mini',
|
217 |
+
"conversation_id": conversation_id,
|
218 |
+
"user_id": request.user_id
|
219 |
+
endpoint_url = "https://pvanand-general-chat.hf.space/v2/followup-agent"
|
220 |
+
|
221 |
+
else:
|
222 |
+
llm_request = {
|
223 |
+
"prompt": rag_prompt,
|
224 |
+
"system_message": system_prompt,
|
225 |
+
"model_id": request.model_id,
|
226 |
+
"conversation_id": conversation_id,
|
227 |
+
"user_id": request.user_id
|
228 |
+
}
|
229 |
+
endpoint_url = "https://pvanand-audio-chat.hf.space/llm-agent"
|
230 |
|
231 |
logger.info(f"Starting chat response generation for user: {request.user_id} Full request: {llm_request}")
|
232 |
async def response_generator():
|
233 |
full_response = ""
|
234 |
+
async for chunk in stream_llm_request(api_key, llm_request,endpoint_url):
|
235 |
full_response += chunk
|
236 |
yield chunk
|
237 |
logger.info(f"Finished chat response generation for user: {request.user_id} Full response{full_response}")
|