70 lines
2.1 KiB
Python
70 lines
2.1 KiB
Python
import json
|
|
from app.config import ANTHROPIC_API_KEY, ANTHROPIC_MODEL
|
|
|
|
|
|
class ClaudeClient:
|
|
|
|
def __init__(self):
|
|
self.api_key = ANTHROPIC_API_KEY
|
|
self.model = ANTHROPIC_MODEL
|
|
|
|
async def validate_extraction(
|
|
self,
|
|
patient_context: dict,
|
|
specialty_config: dict,
|
|
conversation_history: list,
|
|
extracted_data: dict,
|
|
) -> dict:
|
|
if not self.api_key or self.api_key == "your_claude_key":
|
|
return {
|
|
"valid": True,
|
|
"missing": [],
|
|
"recommendation": "continue",
|
|
"hallucinations_detected": False,
|
|
}
|
|
|
|
try:
|
|
from anthropic import AsyncAnthropic
|
|
client = AsyncAnthropic(api_key=self.api_key)
|
|
|
|
prompt = f"""
|
|
You are a senior clinical auditor and AI quality control specialist.
|
|
Your task is to validate whether the extracted patient data accurately reflects the actual patient conversation without hallucination or omitted critical criteria.
|
|
|
|
PATIENT CONTEXT:
|
|
{json.dumps(patient_context, indent=2)}
|
|
|
|
SPECIALTY REQUIREMENTS:
|
|
{json.dumps(specialty_config, indent=2)}
|
|
|
|
CONVERSATION HISTORY:
|
|
{json.dumps(conversation_history, indent=2)}
|
|
|
|
PROPOSED EXTRACTED STATE:
|
|
{json.dumps(extracted_data, indent=2)}
|
|
|
|
Return ONLY a JSON response:
|
|
{{
|
|
"valid": true/false,
|
|
"missing": ["list of critical missing focus items for this specialty, if any"],
|
|
"recommendation": "continue or ask_severity or ask_duration etc.",
|
|
"hallucinations_detected": false
|
|
}}
|
|
"""
|
|
response = await client.messages.create(
|
|
model=self.model,
|
|
max_tokens=1024,
|
|
system="You are a clinical AI quality controller. Output only valid JSON.",
|
|
messages=[{"role": "user", "content": prompt}],
|
|
)
|
|
content = response.content[0].text
|
|
return json.loads(content)
|
|
except Exception as e:
|
|
print(f"[Claude Validation Error]: {e}")
|
|
return {
|
|
"valid": True,
|
|
"missing": [],
|
|
"recommendation": "continue",
|
|
"hallucinations_detected": False,
|
|
}
|