Files
Doctor_Patient_Summary/test_anti_hallucination.py
T

49 lines
2.6 KiB
Python

import os, urllib.request, json, sys
sys.stdout.reconfigure(encoding='utf-8')
api_key = None
with open('.env') as f:
for line in f:
if line.startswith('SARVAM_API_KEY='):
api_key = line.strip().split('=', 1)[1].strip('"\' ')
test_text = """नमस्कार या बसा काय त्रास होतोय तुम्हाला नमस्कार डॉक्टर मला गेले दोन दिवस झाले खोकला येतोय ताप आहे का हो थोडा ताप आहे ठीक आहे औषध देतो 3 दिवस घ्या"""
DIARIZE_PROMPT = """You are a STRICT verbatim medical conversation diarization assistant.
Your task is to split the exact transcribed words into Doctor and Patient speaking turns.
ABSOLUTE ANTI-HALLUCINATION RULES:
1. ZERO HALLUCINATIONS: Do NOT invent, assume, add, or alter ANY words. All output words must come directly from the transcript verbatim.
2. DOCTOR TURNS:
- Greeting and opening: "नमस्कार या बसा काय त्रास होतोय तुम्हाला"
- Diagnostic questions: "ताप आहे का", "कधीपासून", "कुठे दुखतंय", "काय खाल्ले होते"
- Advice and prescription: "ठीक आहे औषध देतो", "3 दिवस घ्या", "तपासून पाहतो", "सोनोग्राफी करू"
3. PATIENT TURNS:
- Symptoms and complaints: "मला गेले दोन दिवस झाले खोकला येतोय", "पोटात दुखत आहे"
- Answers to doctor: "हो थोडा ताप आहे", "नाही", "दोन दिवसांपासून"
4. OUTPUT FORMAT: Output ONLY "**Doctor:** " and "**Patient:** " turns with exact words from transcript."""
req = urllib.request.Request(
'https://api.sarvam.ai/v1/chat/completions',
data=json.dumps({
'model': 'sarvam-105b-conversations',
'temperature': 0.0,
'max_tokens': 1500,
'messages': [
{'role': 'system', 'content': DIARIZE_PROMPT},
{'role': 'user', 'content': f"Transcript:\n{test_text.strip()}"}
]
}).encode('utf-8'),
headers={
'Content-Type': 'application/json',
'api-subscription-key': api_key
}
)
try:
with urllib.request.urlopen(req) as resp:
res = json.loads(resp.read().decode('utf-8'))
print("Diarization Output:\n" + res['choices'][0]['message']['content'])
except Exception as e:
print("Error:", e)