Cyantist8208 commited on
Commit
127e305
·
1 Parent(s): 42b7198

chat template

Browse files
Files changed (1) hide show
  1. app.py +5 -14
app.py CHANGED
@@ -116,17 +116,11 @@ def build_llm_prompt(system: str, context: list[str], user_question: str) -> str
116
  conversation.append({"role": "user", "content": user_question.strip()})
117
 
118
  # 套用 LLaMA-style prompt 格式
119
- prompt = ""
120
- for turn in conversation:
121
- role = turn["role"]
122
- content = turn["content"]
123
- if role == "system":
124
- prompt += f"<<SYS>>\n{content}\n<</SYS>>\n\n"
125
- elif role == "user":
126
- prompt += f"[INST] {content} [/INST]\n"
127
- elif role == "assistant":
128
- prompt += f"{content}\n"
129
- return prompt
130
 
131
  # ---------- 4. Gradio playground (same UI as before) --------------------------
132
  def store_doc(doc_text: str,user_id="demo",chunk_size=DEFAULT_CHUNK_SIZE,chunk_overlap=DEFAULT_CHUNK_OVERLAP):
@@ -198,7 +192,6 @@ def answer(system: str, context: str, question: str,
198
  full = tokenizer.decode(output[0], skip_special_tokens=True)
199
  reply = full.split("<|im_start|>assistant")[-1].strip()
200
  return reply
201
-
202
 
203
  except Exception as e:
204
  tb = traceback.format_exc()
@@ -215,7 +208,6 @@ def clear_kb(user_id="demo"):
215
  return f"User ID '{user_id}' not found."
216
 
217
  # ---- UI layout (feel free to tweak cosmetics) -----------------------------
218
- # ---- UI layout -----------------------------------------------------
219
  with gr.Blocks() as demo:
220
  gr.Markdown(
221
  "### Tiny-RAG playground – 1) paste a passage → **Store** · "
@@ -278,7 +270,6 @@ with gr.Blocks() as demo:
278
  outputs=answer_box
279
  )
280
 
281
-
282
  # ---------- 3. FastAPI layer --------------------------------------------------
283
  class IngestReq(BaseModel):
284
  user_id:str
 
116
  conversation.append({"role": "user", "content": user_question.strip()})
117
 
118
  # 套用 LLaMA-style prompt 格式
119
+ return tokenizer.apply_chat_template(
120
+ conversation,
121
+ tokenize=False,
122
+ add_generation_prompt=False
123
+ )
 
 
 
 
 
 
124
 
125
  # ---------- 4. Gradio playground (same UI as before) --------------------------
126
  def store_doc(doc_text: str,user_id="demo",chunk_size=DEFAULT_CHUNK_SIZE,chunk_overlap=DEFAULT_CHUNK_OVERLAP):
 
192
  full = tokenizer.decode(output[0], skip_special_tokens=True)
193
  reply = full.split("<|im_start|>assistant")[-1].strip()
194
  return reply
 
195
 
196
  except Exception as e:
197
  tb = traceback.format_exc()
 
208
  return f"User ID '{user_id}' not found."
209
 
210
  # ---- UI layout (feel free to tweak cosmetics) -----------------------------
 
211
  with gr.Blocks() as demo:
212
  gr.Markdown(
213
  "### Tiny-RAG playground – 1) paste a passage → **Store** · "
 
270
  outputs=answer_box
271
  )
272
 
 
273
  # ---------- 3. FastAPI layer --------------------------------------------------
274
  class IngestReq(BaseModel):
275
  user_id:str