asusf15 commited on
Commit
5b54a4e
·
verified ·
1 Parent(s): 19b2a18

Fix: stronger prompting to force structured reasoning output

Browse files
Files changed (1) hide show
  1. app.py +32 -16
app.py CHANGED
@@ -10,19 +10,33 @@ client = InferenceClient(token=HF_TOKEN)
10
 
11
  MODEL = "Qwen/Qwen2.5-72B-Instruct"
12
 
13
- SYSTEM_PROMPT = """You are DeepMed-R1, a medical reasoning AI trained with GRPO and multi-objective clinical rewards on AMD MI300X.
14
 
15
- For every medical question, demonstrate systematic clinical reasoning:
16
- 1. Information Analysis: Extract key demographics, symptoms, vitals, labs
17
- 2. Differential Diagnosis: Identify patterns, rank by probability, note red flags
18
- 3. Pathophysiology: Connect symptoms to disease mechanisms
19
- 4. Evidence-Based Reasoning: Apply clinical criteria, reference guidelines
20
- 5. Logical Elimination: Evaluate each option, exclude based on evidence
21
- 6. Clinical Decision: Consider risk-benefit, prioritize safety
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- Present reasoning inside <think></think> tags, then provide your final answer.
24
- For MCQ, end with \\boxed{X} where X is the correct letter.
25
- Be thorough but concise. Ground reasoning in pathophysiology and evidence."""
26
 
27
  EXAMPLES = [
28
  ["A 65-year-old male with hypertension presents with sudden 'worst headache of my life,' neck stiffness, photophobia. BP 180/100. Most likely diagnosis?\nA. Migraine\nB. Subarachnoid hemorrhage\nC. Meningitis\nD. Tension headache"],
@@ -38,15 +52,18 @@ def respond(message, history):
38
  for h in history:
39
  if h[0]: messages.append({"role": "user", "content": h[0]})
40
  if h[1]: messages.append({"role": "assistant", "content": h[1]})
41
- messages.append({"role": "user", "content": message})
 
 
 
42
 
43
  response = ""
44
  try:
45
  stream = client.chat_completion(
46
  model=MODEL,
47
  messages=messages,
48
- max_tokens=3000,
49
- temperature=0.3,
50
  top_p=0.95,
51
  stream=True,
52
  )
@@ -57,9 +74,8 @@ def respond(message, history):
57
  response += delta.content
58
  yield response
59
  except Exception as e:
60
- error_msg = str(e)
61
  if not response:
62
- yield f"⚠️ Error: {error_msg}\n\nPlease ensure HF_TOKEN is set in Space secrets."
63
  else:
64
  yield response
65
 
 
10
 
11
  MODEL = "Qwen/Qwen2.5-72B-Instruct"
12
 
13
+ SYSTEM_PROMPT = """You are DeepMed-R1, a medical reasoning AI. You MUST follow this EXACT output format for every answer:
14
 
15
+ <think>
16
+ ## Information Analysis
17
+ [Extract key patient demographics, symptoms, vitals, labs, imaging findings]
18
+
19
+ ## Differential Diagnosis
20
+ [List possible diagnoses ranked by probability]
21
+
22
+ ## Pathophysiology
23
+ [Explain the disease mechanism connecting symptoms]
24
+
25
+ ## Evidence-Based Reasoning
26
+ [Apply clinical criteria and guidelines]
27
+
28
+ ## Logical Elimination
29
+ [Evaluate each option A/B/C/D and explain why each is correct or incorrect]
30
+ </think>
31
+
32
+ ## Assessment
33
+ [Your clinical assessment in 2-3 sentences]
34
+
35
+ ## Final Answer
36
+ \\boxed{X}
37
+
38
+ IMPORTANT: You MUST write detailed reasoning inside <think></think> tags BEFORE giving the answer. Never skip the reasoning. Each section must have at least 2 sentences."""
39
 
 
 
 
40
 
41
  EXAMPLES = [
42
  ["A 65-year-old male with hypertension presents with sudden 'worst headache of my life,' neck stiffness, photophobia. BP 180/100. Most likely diagnosis?\nA. Migraine\nB. Subarachnoid hemorrhage\nC. Meningitis\nD. Tension headache"],
 
52
  for h in history:
53
  if h[0]: messages.append({"role": "user", "content": h[0]})
54
  if h[1]: messages.append({"role": "assistant", "content": h[1]})
55
+
56
+ # Add user message with reinforcement to show reasoning
57
+ user_msg = message + "\n\nShow your complete clinical reasoning step-by-step inside <think></think> tags before answering."
58
+ messages.append({"role": "user", "content": user_msg})
59
 
60
  response = ""
61
  try:
62
  stream = client.chat_completion(
63
  model=MODEL,
64
  messages=messages,
65
+ max_tokens=4000,
66
+ temperature=0.4,
67
  top_p=0.95,
68
  stream=True,
69
  )
 
74
  response += delta.content
75
  yield response
76
  except Exception as e:
 
77
  if not response:
78
+ yield f"⚠️ Error: {str(e)}"
79
  else:
80
  yield response
81