sxandie commited on
Commit
702d7e4
·
1 Parent(s): 61e0238

fix: resolve model role confusion by using chat completion API, and fix emergency card rendering

Browse files
Files changed (2) hide show
  1. app.py +1 -1
  2. src/llm.py +25 -15
app.py CHANGED
@@ -1613,7 +1613,7 @@ with gr.Blocks(css="assets/custom.css", title="Trailhead — Tactical Trail Comp
1613
  ]
1614
  )
1615
  with gr.Column(scale=1):
1616
- gr.HTML(EMERGENCY_CARD)
1617
  with gr.Accordion("🔍 First-Aid Manual Quick Search", open=False):
1618
  rag_query = gr.Textbox(placeholder="What symptoms or injury do you want to query?", label="Query Symptoms")
1619
  rag_search_btn = gr.Button("Search manual", variant="primary")
 
1613
  ]
1614
  )
1615
  with gr.Column(scale=1):
1616
+ gr.Markdown(EMERGENCY_CARD)
1617
  with gr.Accordion("🔍 First-Aid Manual Quick Search", open=False):
1618
  rag_query = gr.Textbox(placeholder="What symptoms or injury do you want to query?", label="Query Symptoms")
1619
  rag_search_btn = gr.Button("Search manual", variant="primary")
src/llm.py CHANGED
@@ -481,19 +481,31 @@ def generate_llama_cpp(prompt, system="", image_path=None, audio_path=None, hist
481
  if image_path:
482
  prompt = f"[📸 Image uploaded] {prompt}"
483
 
484
- formatted_prompt = f"<|im_start|>system\n{system}<|im_end|>\n"
 
 
 
 
485
  if history:
 
486
  for msg in history:
487
  role = msg.get("role", "user")
488
  content = msg.get("content", "")
489
- formatted_prompt += f"<|im_start|>{role}\n{content}<|im_end|>\n"
490
- formatted_prompt += f"<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n"
491
-
492
- print(f"\n--- [llama.cpp INPUT PROMPT] ---\n{formatted_prompt}\n--------------------------------")
 
 
 
 
 
 
 
493
  print("--- [llama.cpp STREAMING RESPONSE] ---")
494
  try:
495
- response = model(
496
- formatted_prompt,
497
  max_tokens=512,
498
  temperature=0.3,
499
  top_p=0.9,
@@ -521,16 +533,14 @@ def generate_llama_cpp(prompt, system="", image_path=None, audio_path=None, hist
521
  yield voice_prefix
522
 
523
  if first_chunk:
524
- text = first_chunk['choices'][0]['text']
525
- cleaned = text.replace("<|im_end|>", "")
526
- print(cleaned, end="", flush=True)
527
- yield cleaned
528
 
529
  for chunk in response_iter:
530
- text = chunk['choices'][0]['text']
531
- cleaned = text.replace("<|im_end|>", "")
532
- print(cleaned, end="", flush=True)
533
- yield cleaned
534
  print("\n--------------------------------------")
535
  except Exception as e:
536
  print(f"[llm.py] Error running llama.cpp: {e}. Falling back to mock.")
 
481
  if image_path:
482
  prompt = f"[📸 Image uploaded] {prompt}"
483
 
484
+ messages = []
485
+ combined_prompt = prompt
486
+ if system:
487
+ combined_prompt = f"System Instructions:\n{system}\n\nUser Query: {prompt}"
488
+
489
  if history:
490
+ first_msg_updated = False
491
  for msg in history:
492
  role = msg.get("role", "user")
493
  content = msg.get("content", "")
494
+ if role == "system":
495
+ continue
496
+ if not first_msg_updated and role == "user":
497
+ content = f"System Instructions:\n{system}\n\nUser Query: {content}"
498
+ first_msg_updated = True
499
+ messages.append({"role": role, "content": content})
500
+ messages.append({"role": "user", "content": prompt})
501
+ else:
502
+ messages.append({"role": "user", "content": combined_prompt})
503
+
504
+ print(f"\n--- [llama.cpp INPUT MESSAGES] ---\n{messages}\n--------------------------------")
505
  print("--- [llama.cpp STREAMING RESPONSE] ---")
506
  try:
507
+ response = model.create_chat_completion(
508
+ messages=messages,
509
  max_tokens=512,
510
  temperature=0.3,
511
  top_p=0.9,
 
533
  yield voice_prefix
534
 
535
  if first_chunk:
536
+ text = first_chunk['choices'][0]['delta'].get('content', '')
537
+ print(text, end="", flush=True)
538
+ yield text
 
539
 
540
  for chunk in response_iter:
541
+ text = chunk['choices'][0]['delta'].get('content', '')
542
+ print(text, end="", flush=True)
543
+ yield text
 
544
  print("\n--------------------------------------")
545
  except Exception as e:
546
  print(f"[llm.py] Error running llama.cpp: {e}. Falling back to mock.")