Skip to content

Commit acb57d1

Browse files
added changes to handle chitchat
1 parent e7441c7 commit acb57d1

6 files changed

Lines changed: 783 additions & 67 deletions

File tree

new_architecture_v4.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,11 @@ def get_chat_context():
131131
- FOLLOW_UP: short answers to a previous assistant suggestion, e.g. assistant asked "Would you like to know about malaria prevention?" and user replies "yes" or "prevention please" -> FOLLOW_UP.
132132
- CHITCHAT: greetings, thanks, smalltalk, profanity, or explicit "stop" requests, anything not medical_question or follow up -> CHITCHAT.
133133
- MEDICAL_QUESTION: any standalone question that asks for medical facts, diagnoses, treatments, or definitions.
134+
- MEDICAL_QUESTION : any medical word or phrase in the question, even if spellings is mis-spelled or mis-phrased to a medical content
134135
135136
Examples:
137+
138+
136139
chat_history: "Assistant: Would you like to know about jaundice?"
137140
question: "yes"
138141
-> {{"label":"FOLLOW_UP","reason":"affirmation to assistant suggestion"}}
@@ -340,7 +343,9 @@ def judge_sufficiency(query, candidates, judge_llm=llm, threshold_weak=0.25):
340343
"""
341344

342345
qualified = []
343-
for c in candidates[:6]: # inspect up to 12
346+
followup_chunks=[]
347+
print("len of candidates",len(candidates))
348+
for c in candidates: # inspect up to 12
344349
snippet = f"Source: {c['meta'].get('doc_name','unknown')}\nExcerpt: {c['text']}"
345350
prompt = judge_prompt.format(query=query, context_snippet=snippet)
346351

@@ -352,16 +357,20 @@ def judge_sufficiency(query, candidates, judge_llm=llm, threshold_weak=0.25):
352357
print(obj)
353358
if obj.get("sufficient", False):
354359
qualified.append(c)
360+
else:
361+
followup_chunks.append(c)
355362
except Exception:
356363
if c["scores"]["cross"] > threshold_weak:
357364
qualified.append(c)
365+
else:
366+
followup_chunks.append(c)
358367

359-
# top 4 max for answering
360-
answer_chunks = qualified[:min(4, len(qualified))]
361-
# next 2 max for follow-up
362-
followup_chunks = qualified[4:6] if len(qualified) > 4 else candidates[len(qualified):len(qualified)+2]
363-
364-
return {"answer_chunks": answer_chunks, "followup_chunks": followup_chunks}
368+
print("BEFORE len of answer_chunks",len(qualified),"BEFORE len of followup_chunks",len(followup_chunks))
369+
if len(followup_chunks)==0:
370+
followup_chunks=qualified[-2:]
371+
qualified=qualified[:-2]
372+
print("AFTER len of answer_chunks",len(qualified),"AFTER len of followup_chunks",len(followup_chunks))
373+
return {"answer_chunks": qualified[:4], "followup_chunks": followup_chunks[:2]}
365374

366375
def synthesize_answer(query, top_candidates, context_followup, main_llm=llm):
367376
# Build context from top 3 candidates
@@ -373,6 +382,7 @@ def synthesize_answer(query, top_candidates, context_followup, main_llm=llm):
373382
sources.append(src)
374383
ctx_parts.append(f"From [{src}]:\n{c['text']}")
375384
context = "\n\n".join(ctx_parts)
385+
context_followup = "\n\n".join(context_followup)
376386
sources_str = " and ".join([Path(s).stem for s in sources]) if sources else "unknown"
377387

378388
prompt = answer_prompt.format(

0 commit comments

Comments
 (0)