payer-auth-sim / app.py
pilayar's picture
Update app.py
88d4b37 verified
from fastapi import FastAPI
from pydantic import BaseModel
import os
from openai import OpenAI
app = FastAPI()
#class PARequest(BaseModel):
# procedure_code: str
# summary: str
# dysphagia: bool
# weight_loss: bool
# ppi_weeks: int
# Make sure you add 'OPENAI_API_KEY' to your Payer Space Secrets
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
class AgentMessage(BaseModel):
message: str
@app.get("/")
def read_root():
return {"status": "Payer API is Live"}
class AgentMessage(BaseModel):
message: str
# This would ideally call an LLM (OpenAI/Mistral) to 'think' like a Payer
@app.post("/agent-chat")
async def agent_chat(payload: AgentMessage):
# DYNAMIC POLICY DATABASE (Mirroring your Provider side)
POLICIES = {
"22558": "Policy LCD-L341 (Spinal Fusion): Requires Physical Therapy (6+ weeks), Instability, or Spondylolisthesis.",
"43239": "Policy MED-772 (EGD): Requires Weight Loss, Dysphagia, or 8-week PPI failure."
}
# Step 1: Tell the Payer to identify the code first
system_instruction = (
"You are an Oracle Health Payer Adjudication Agent. "
"Your goal is to verify medical necessity based on the following policies: \n"
f"1. {POLICIES['22558']}\n"
f"2. {POLICIES['43239']}\n\n"
"INSTRUCTIONS:\n"
"- Identify which CPT code the Provider is requesting.\n"
"- ONLY apply the criteria for THAT specific code.\n"
"- If evidence for THAT code is found, reply 'APPROVED'.\n"
"- If evidence is missing, ask for the specific missing documentation for THAT policy."
)
prompt = [
{"role": "system", "content": system_instruction},
{"role": "user", "content": payload.message}
]
response = client.chat.completions.create(
model="gpt-4o", # Recommended for better logic than 3.5
messages=prompt,
temperature=0
)
return {"agent_response": response.choices[0].message.content}