Spaces:
Running
Running
Added async
Browse files- app/main.py +3 -3
- app/rag.py +3 -8
app/main.py
CHANGED
|
@@ -2,7 +2,7 @@ from fastapi import FastAPI
|
|
| 2 |
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from pydantic import BaseModel
|
| 5 |
-
from
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
|
@@ -24,7 +24,7 @@ class ChatInput(BaseModel):
|
|
| 24 |
session_id: str
|
| 25 |
|
| 26 |
@app.post("/chat")
|
| 27 |
-
def chat(input: ChatInput):
|
| 28 |
print("Got question:", input.question)
|
| 29 |
print("Session ID:", input.session_id)
|
| 30 |
|
|
@@ -33,5 +33,5 @@ def chat(input: ChatInput):
|
|
| 33 |
'thread_id': input.session_id
|
| 34 |
}
|
| 35 |
}
|
| 36 |
-
response = get_response(input.question, config=config)
|
| 37 |
return {"answer": response['response']}
|
|
|
|
| 2 |
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from pydantic import BaseModel
|
| 5 |
+
from rag import get_response
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
|
|
|
| 24 |
session_id: str
|
| 25 |
|
| 26 |
@app.post("/chat")
|
| 27 |
+
async def chat(input: ChatInput):
|
| 28 |
print("Got question:", input.question)
|
| 29 |
print("Session ID:", input.session_id)
|
| 30 |
|
|
|
|
| 33 |
'thread_id': input.session_id
|
| 34 |
}
|
| 35 |
}
|
| 36 |
+
response = await get_response(input.question, config=config)
|
| 37 |
return {"answer": response['response']}
|
app/rag.py
CHANGED
|
@@ -78,7 +78,6 @@ STRICT RULES YOU MUST FOLLOW:
|
|
| 78 |
1. If the Contextual Knowledge section is empty, say:
|
| 79 |
"At the moment, I cannot answer this question. Please try rephrasing it or contact us on our website."
|
| 80 |
2. Do NOT use your own general knowledge. Only use information from the Contextual Knowledge.
|
| 81 |
-
3. ONLY cite links or sources that appear exactly in the Contextual Knowledge section. Never invent or assume links.
|
| 82 |
|
| 83 |
Conversation History:
|
| 84 |
{history}
|
|
@@ -91,16 +90,12 @@ User Question:
|
|
| 91 |
|
| 92 |
Respond in the following format:
|
| 93 |
<concise answer (1–3 sentences)>
|
| 94 |
-
(line break)
|
| 95 |
-
Source: <Single most relevant link from Contextual Knowledge in the format of a Markdown link but need user to see full link>
|
| 96 |
|
| 97 |
If no answer can be found based on the Contextual Knowledge above, respond with:
|
| 98 |
"At the moment, I cannot answer this question. Please try rephrasing it or contact us on our website."
|
| 99 |
"""
|
| 100 |
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
# %%
|
| 105 |
@tool("Retrieve_FAQs")
|
| 106 |
def retrieve_faqs(query: str) -> List[dict[str,str | float]]:
|
|
@@ -119,7 +114,7 @@ def retrieve_faqs(query: str) -> List[dict[str,str | float]]:
|
|
| 119 |
docs = faq_store.similarity_search_with_score(query, k=5)
|
| 120 |
return [
|
| 121 |
{"content": doc.page_content, "source": doc.metadata.get("source", "unknown"), 'score': score}
|
| 122 |
-
for doc, score in docs if score > 0.
|
| 123 |
|
| 124 |
# %%
|
| 125 |
@tool("Retrive_Blogs")
|
|
@@ -263,13 +258,13 @@ graph.add_edge("generate_response", END)
|
|
| 263 |
app = graph.compile()
|
| 264 |
|
| 265 |
# %%
|
| 266 |
-
def get_response(query: str, config) -> dict:
|
| 267 |
session_id = config['configurable']['thread_id']
|
| 268 |
history = session_histories.get(session_id, [])
|
| 269 |
input_data = {
|
| 270 |
"input": query,
|
| 271 |
"history": history
|
| 272 |
}
|
| 273 |
-
result = app.
|
| 274 |
session_histories[session_id] = result.get("history", [])
|
| 275 |
return result
|
|
|
|
| 78 |
1. If the Contextual Knowledge section is empty, say:
|
| 79 |
"At the moment, I cannot answer this question. Please try rephrasing it or contact us on our website."
|
| 80 |
2. Do NOT use your own general knowledge. Only use information from the Contextual Knowledge.
|
|
|
|
| 81 |
|
| 82 |
Conversation History:
|
| 83 |
{history}
|
|
|
|
| 90 |
|
| 91 |
Respond in the following format:
|
| 92 |
<concise answer (1–3 sentences)>
|
|
|
|
|
|
|
| 93 |
|
| 94 |
If no answer can be found based on the Contextual Knowledge above, respond with:
|
| 95 |
"At the moment, I cannot answer this question. Please try rephrasing it or contact us on our website."
|
| 96 |
"""
|
| 97 |
|
| 98 |
|
|
|
|
|
|
|
| 99 |
# %%
|
| 100 |
@tool("Retrieve_FAQs")
|
| 101 |
def retrieve_faqs(query: str) -> List[dict[str,str | float]]:
|
|
|
|
| 114 |
docs = faq_store.similarity_search_with_score(query, k=5)
|
| 115 |
return [
|
| 116 |
{"content": doc.page_content, "source": doc.metadata.get("source", "unknown"), 'score': score}
|
| 117 |
+
for doc, score in docs if score > 0.87]
|
| 118 |
|
| 119 |
# %%
|
| 120 |
@tool("Retrive_Blogs")
|
|
|
|
| 258 |
app = graph.compile()
|
| 259 |
|
| 260 |
# %%
|
| 261 |
+
async def get_response(query: str, config) -> dict:
|
| 262 |
session_id = config['configurable']['thread_id']
|
| 263 |
history = session_histories.get(session_id, [])
|
| 264 |
input_data = {
|
| 265 |
"input": query,
|
| 266 |
"history": history
|
| 267 |
}
|
| 268 |
+
result = await app.ainvoke(input_data, config=config)
|
| 269 |
session_histories[session_id] = result.get("history", [])
|
| 270 |
return result
|