Update app.py
Browse files
app.py
CHANGED
@@ -23,13 +23,19 @@ MODEL = "qwen/qwq-32b:free"
|
|
23 |
# β
FastAPI app
|
24 |
app = FastAPI()
|
25 |
|
26 |
-
# β
Context Retriever
|
27 |
-
def get_context(query, top_k=5):
|
28 |
query_vec = embed_model.encode([query])
|
29 |
D, I = index.search(np.array(query_vec), top_k)
|
30 |
-
return "\n".join([texts[i] for i in I[0]])
|
31 |
|
32 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
def chat_fn(message, history):
|
34 |
headers = {
|
35 |
"Authorization": f"Bearer {API_KEY}",
|
@@ -38,8 +44,15 @@ def chat_fn(message, history):
|
|
38 |
|
39 |
context = get_context(message)
|
40 |
|
|
|
|
|
|
|
|
|
41 |
messages = [
|
42 |
-
{
|
|
|
|
|
|
|
43 |
]
|
44 |
|
45 |
for user, assistant in history:
|
@@ -79,7 +92,7 @@ demo = gr.ChatInterface(
|
|
79 |
# β
Mount Gradio at root
|
80 |
app = gr.mount_gradio_app(app, demo, path="/")
|
81 |
|
82 |
-
# β
For local
|
83 |
if __name__ == "__main__":
|
84 |
nest_asyncio.apply()
|
85 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
23 |
# β
FastAPI app
|
24 |
app = FastAPI()
|
25 |
|
26 |
+
# β
Context Retriever with threshold
|
27 |
+
def get_context(query, top_k=5, threshold=0.3):
|
28 |
query_vec = embed_model.encode([query])
|
29 |
D, I = index.search(np.array(query_vec), top_k)
|
|
|
30 |
|
31 |
+
# Filter based on similarity distance threshold
|
32 |
+
results = [(texts[i], d) for i, d in zip(I[0], D[0]) if d >= threshold]
|
33 |
+
if not results:
|
34 |
+
return ""
|
35 |
+
|
36 |
+
return "\n".join([text for text, _ in results])
|
37 |
+
|
38 |
+
# β
Chat Function (RAG-aware + fallback)
|
39 |
def chat_fn(message, history):
|
40 |
headers = {
|
41 |
"Authorization": f"Bearer {API_KEY}",
|
|
|
44 |
|
45 |
context = get_context(message)
|
46 |
|
47 |
+
# No valid context? Respond with fallback message
|
48 |
+
if not context.strip():
|
49 |
+
return "β Sorry, I can't answer that."
|
50 |
+
|
51 |
messages = [
|
52 |
+
{
|
53 |
+
"role": "system",
|
54 |
+
"content": "You are a helpful assistant. Only use the context below to answer. If you can't find an answer in it, reply: 'Sorry, I can't answer that.'\n\nContext:\n" + context
|
55 |
+
}
|
56 |
]
|
57 |
|
58 |
for user, assistant in history:
|
|
|
92 |
# β
Mount Gradio at root
|
93 |
app = gr.mount_gradio_app(app, demo, path="/")
|
94 |
|
95 |
+
# β
For local development
|
96 |
if __name__ == "__main__":
|
97 |
nest_asyncio.apply()
|
98 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|