Spaces:
Sleeping
Sleeping
Commit ·
2d16ae8
1
Parent(s): 5520695
optional context
Browse files
app.py
CHANGED
|
@@ -79,11 +79,15 @@ def add_docs(user_id: str, docs: list[str]) -> int:
|
|
| 79 |
def build_qwen_prompt(context: str, user_question: str) -> str:
|
| 80 |
"""Return a string that follows Qwen-Chat’s template."""
|
| 81 |
load_chat() # ← make sure tokenizer is ready
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
conversation = [
|
| 83 |
{"role": "system",
|
| 84 |
"content": "You are an email assistant. Use ONLY the context provided."},
|
| 85 |
{"role": "user",
|
| 86 |
-
"content": f"
|
| 87 |
]
|
| 88 |
return tokenizer.apply_chat_template(
|
| 89 |
conversation, tokenize=False, add_generation_prompt=True
|
|
@@ -98,26 +102,28 @@ def store_doc(doc_text: str, user_id="demo"):
|
|
| 98 |
return "Nothing stored (empty input)."
|
| 99 |
return f"Stored — KB now has {len(kb[user_id]['texts'])} passage(s)."
|
| 100 |
|
| 101 |
-
def answer(question: str, user_id="demo"):
|
| 102 |
"""UI callback: retrieve, build prompt with Qwen tags, generate answer."""
|
| 103 |
try:
|
| 104 |
if not question.strip():
|
| 105 |
return "Please ask a question."
|
| 106 |
-
if not kb[user_id]["texts"]:
|
| 107 |
return "No reference passage yet. Add one first."
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
| 118 |
prompt = build_qwen_prompt(context, question)
|
| 119 |
|
| 120 |
-
# 3
|
| 121 |
load_chat()
|
| 122 |
inputs = tokenizer(prompt, return_tensors="pt").to(chat_model.device)
|
| 123 |
output = chat_model.generate(**inputs, max_new_tokens=512)
|
|
|
|
| 79 |
def build_qwen_prompt(context: str, user_question: str) -> str:
|
| 80 |
"""Return a string that follows Qwen-Chat’s template."""
|
| 81 |
load_chat() # ← make sure tokenizer is ready
|
| 82 |
+
if context:
|
| 83 |
+
context = f"Context:\n{context}\n\n"
|
| 84 |
+
else:
|
| 85 |
+
context = ""
|
| 86 |
conversation = [
|
| 87 |
{"role": "system",
|
| 88 |
"content": "You are an email assistant. Use ONLY the context provided."},
|
| 89 |
{"role": "user",
|
| 90 |
+
"content": f"{context}{user_question}"}
|
| 91 |
]
|
| 92 |
return tokenizer.apply_chat_template(
|
| 93 |
conversation, tokenize=False, add_generation_prompt=True
|
|
|
|
| 102 |
return "Nothing stored (empty input)."
|
| 103 |
return f"Stored — KB now has {len(kb[user_id]['texts'])} passage(s)."
|
| 104 |
|
| 105 |
+
def answer(question: str, user_id="demo", history=False):
|
| 106 |
"""UI callback: retrieve, build prompt with Qwen tags, generate answer."""
|
| 107 |
try:
|
| 108 |
if not question.strip():
|
| 109 |
return "Please ask a question."
|
| 110 |
+
if history and not kb[user_id]["texts"]:
|
| 111 |
return "No reference passage yet. Add one first."
|
| 112 |
|
| 113 |
+
context = None
|
| 114 |
+
# 1. Retrieve top-k similar passages
|
| 115 |
+
if history:
|
| 116 |
+
q_vec = embed(question)
|
| 117 |
+
store = kb[user_id]
|
| 118 |
+
sims = torch.matmul(store["vecs"], q_vec) # [N]
|
| 119 |
+
k = min(4, sims.numel())
|
| 120 |
+
idxs = torch.topk(sims, k=k).indices.tolist()
|
| 121 |
+
context = "\n".join(store["texts"][i] for i in idxs)
|
| 122 |
+
|
| 123 |
+
# 2. Build a Qwen-chat prompt (helper defined earlier)
|
| 124 |
prompt = build_qwen_prompt(context, question)
|
| 125 |
|
| 126 |
+
# 3. Generate and strip everything before the assistant tag
|
| 127 |
load_chat()
|
| 128 |
inputs = tokenizer(prompt, return_tensors="pt").to(chat_model.device)
|
| 129 |
output = chat_model.generate(**inputs, max_new_tokens=512)
|