Skip to content

Commit 58f58d2

Browse files
added final working code unoptimized
1 parent 242248b commit 58f58d2

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

new_architecture_v4.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def get_chat_context():
147147
)
148148

149149
reformulate_prompt = PromptTemplate(
150-
input_variables=["chat_history", "last_suggested", "question"],
150+
input_variables=["chat_history", "last_suggested", "question",'classifier'],
151151
template="""
152152
Return JSON only: with keys as "Rewritten" and "Correction" where correction being a dict of <original:corrected> pairs.
153153
@@ -332,10 +332,11 @@ def judge_sufficiency(query, candidates, judge_llm=llm, threshold_weak=0.25):
332332
prompt = judge_prompt.format(query=query, context_snippet=snippet)
333333

334334
resp = judge_llm.invoke([HumanMessage(content=prompt)]).content
335-
print(candidates, resp)
335+
#print(candidates, resp)
336336

337337
try:
338338
obj = json.loads(resp[resp.rfind("{"):resp.rfind("}")+1])
339+
print(obj)
339340
if obj.get("sufficient", False):
340341
qualified.append(c)
341342
except Exception:
@@ -405,19 +406,21 @@ def classify_message(chat_history, user_message):
405406
# except Exception as e:
406407
# append_debug(f"[reformulate] LLM failed: {e}")
407408
# return user_message, ""
408-
def reformulate_query(chat_history, user_message, last_suggested=""):
409+
def reformulate_query(chat_history, user_message,classify, last_suggested=""):
409410
print("here")
410411
prompt = f"""
411412
Return JSON only: {{"Rewritten":"...","Correction":{{"...":"..."}}}}
412413
413414
You are a careful medical query rewriter for a clinical assistant. Rephrase question capturing user's intent and easily searchable for a RAG application.
414-
415+
If label is chitchat -> return as it is no change or reformulation
415416
Rules:
417+
- if label is chitchat -> return as it is no change or reformulation
416418
- If user has mentioned or few words on a medical term: Rewrite it to frame a question like "Give information about <x> and cure if available"
417419
- If user's input is a FOLLOW_UP (additional questions, inputs about last answer) about a prior interaction, using chat_history rewrite into a full question.
418420
- If input is a short affirmation (e.g., "yes","sure",etc.) and last_suggested exists → rephrase follow-up question from last response into an independent standalone question.
419421
- Expand common medical abbreviations where unambiguous (IUFD -> Intrauterine fetal death). If unknown, leave unchanged.
420422
- Only correct spelling/abbreviations when it changes meaning. Report such correction in Correction.
423+
- ALso append Abbreviations in Correction
421424
- If input is chitchat or profanity, append "_chitchat" to Rewritten.
422425
- Keep rewritten concise and medically precise.
423426
@@ -438,12 +441,15 @@ def reformulate_query(chat_history, user_message, last_suggested=""):
438441
question: "any treatment?"
439442
-> Rewritten: "What are treatments for postpartum hemorrhage?", Correction: {{}}
440443
441-
Now rewrite:
444+
classify :
445+
{classify}
442446
chat_history:
443447
{chat_history}
444448
last_suggested: {last_suggested}
445449
user_message:
446450
{user_message}
451+
452+
NOW REWRITE or return as it is in case of chitchat :
447453
"""
448454
append_debug("[reformulate] sending reformulation prompt")
449455
try:
@@ -475,7 +481,7 @@ def medical_pipeline(user_message):
475481
append_debug(f"[pipeline] classifier -> {label} ({reason})")
476482

477483
# 3) reformulate (handles follow-ups, abbrs, corrections)
478-
rewritten, correction = reformulate_query(chat_history, user_message, st.session_state.last_suggested or "")
484+
rewritten, correction = reformulate_query(chat_history, user_message, st.session_state.last_suggested or "",label)
479485
append_debug(f"[pipeline] reformulated: {rewritten} | correction: {correction}")
480486

481487
if rewritten.endswith("_chitchat"):
@@ -497,14 +503,16 @@ def medical_pipeline(user_message):
497503
if followup_candidates:
498504
fc = followup_candidates[0]
499505
sec = fc["meta"].get("section") if fc.get("meta") else None
500-
followup_q = sec or (fc["text"][:100])
506+
followup_q = sec or (fc["text"])
501507
answer = synthesize_answer(rewritten, top4, followup_q)
502508
update_chat_history(user_message, answer, "answer")
503509
print("label is ",label)
504510
print("correction :",correction)
505-
print(type(correction))
506511
if label != "FOLLOW_UP":
507-
answer = f"I guess you meant {'and'.join(i for i in list(corr.keys()))}" + '/n' + answer
512+
print("he")
513+
if len(correction)!=0:
514+
print("eheh")
515+
answer = f"I guess you meant {'and'.join(i for i in list(correction.values()))}" + '\n' + answer
508516
return answer, "answer", candidates[:6]
509517
else:
510518
msg = "I apologize, but I do not have sufficient information to answer this question accurately."
@@ -524,7 +532,7 @@ def medical_pipeline(user_message):
524532
if user_input:
525533
with st.spinner("Thinking..."):
526534
reply, intent, candidates = medical_pipeline(user_input)
527-
st.session_state.history_pairs.append((user_input, reply, intent))
535+
#st.session_state.history_pairs.append((user_input, reply, intent))
528536

529537
# Render chat history (verbatim last 3 + UI)
530538
for q, a, intent in reversed(st.session_state.history_pairs):

0 commit comments

Comments
 (0)