Designing an Intelligent Medical Workflow with Gemini
Learn how to create an automated medical evidence gathering agent using Gemini.
Overview
In this tutorial, we devise how to orchestrate a fully functional, tool-using medical prior-authorization agent powered by Gemini. We walk through each component step by step, from securely configuring the model to building realistic external tools and finally constructing an intelligent agent loop that reasons, acts, and responds entirely through structured JSON. As we progress, we see how the system thinks, retrieves evidence, and interacts with simulated medical systems to complete a complex workflow. Check out the FULL CODES here.
Setting Up the Environment
We set up our environment and automatically detect the best available Gemini model. We securely configure the API key and let the system choose the most capable model without hardcoding anything. This ensures that we start with a clean, flexible, and reliable foundation.
!pip install -q -U google-generative-ai
import google.generativeai as genai
from google.colab import userdata
import os
import getpass
import json
import time
try:
GOOGLE_API_KEY = userdata.get('GOOGLE_API_KEY')
except:
print("Please enter your Google API Key:")
GOOGLE_API_KEY = getpass.getpass("API Key: ")
genai.configure(api_key=GOOGLE_API_KEY)
available_models = [m.name for m in genai.list_models()]
target_model = ""
if 'models/gemini-1.5-flash' in available_models:
target_model = 'gemini-1.5-flash'
elif 'models/gemini-1.5-flash-001' in available_models:
target_model = 'gemini-1.5-flash-001'
elif 'models/gemini-pro' in available_models:
target_model = 'gemini-pro'
else:
for m in available_models:
if 'generateContent' in genai.get_model(m).supported_generation_methods:
target_model = m
break
if not target_model:
raise ValueError("No text generation models found for this API key.")
print(f"Selected Model: {target_model}")
model = genai.GenerativeModel(target_model) Defining Medical Tools
We define the medical tools that our agent can use during the workflow. We simulate an EHR search and a prior-authorization submission system so the agent has real actions to perform. By doing this, we ground the agent’s reasoning in tool-enabled interactions rather than plain text generation.
class MedicalTools:
def __init__(self):
self.ehr_docs = [
"Patient: John Doe | DOB: 1980-05-12",
"Visit 2023-01-10: Diagnosed with Type 2 Diabetes. Prescribed Metformin.",
"Visit 2023-04-15: Patient reports severe GI distress with Metformin. Discontinued.",
"Visit 2023-04-20: BMI recorded at 32.5. A1C is 8.4%.",
"Visit 2023-05-01: Doctor recommends starting Ozempic (Semaglutide)."
]
def search_ehr(self, query):
results = [doc for doc in self.ehr_docs if any(q.lower() in doc.lower() for q in query.split())]
if not results:
return "No records found."
return "\n".join(results)
def submit_prior_auth(self, drug_name, justification):
justification_lower = justification.lower()
if "metformin" in justification_lower and ("discontinued" in justification_lower or "intolerance" in justification_lower):
if "bmi" in justification_lower and "32" in justification_lower:
return "SUCCESS: Authorization Approved. Auth ID: #998877"
return "DENIED: Policy requires proof of (1) Metformin failure and (2) BMI > 30." Initialization of the Agent
We initialize the agent and provide its full system prompt. We define the rules, the JSON response format, and the expectation that the agent must think before acting. This gives us a controlled structure for building a safe and traceable agent loop.
def execute_tool(self, action_name, action_input):
if action_name == "search_ehr":
return self.tools.search_ehr(action_input)
elif action_name == "submit_prior_auth":
if isinstance(action_input, str):
return "Error: submit_prior_auth requires a dictionary."
return self.tools.submit_prior_auth(**action_input)
else:
return "Error: Unknown tool."
def run(self, objective):
self.history.append(f"User: {objective}")
for i in range(self.max_steps):
prompt = self.system_prompt + "\n\nHistory:\n" + "\n".join(self.history) + "\n\nNext JSON:"
try:
response = self.model.generate_content(prompt)
agent_decision = json.loads(response.text.strip())
except Exception as e:
continue
tool_result = self.execute_tool(agent_decision['action'], agent_decision['action_input'])
self.history.append(f"Assistant: {response.text}")
self.history.append(f"System: {tool_result}")
if "SUCCESS" in str(tool_result):
break Running the Agent
We instantiate the tools and agent, then run the entire system end-to-end. We see the complete workflow unfold as the agent navigates through medical history, validates evidence, and attempts prior authorization.
tools_instance = MedicalTools()
ageant = AgenticSystem(model, tools_instance)
ageant.run("Please get prior authorization for Ozempic for patient John Doe.") Final Thoughts
The framework enables us to design real-world agentic behaviors that go beyond simple text responses. It allows the agent to plan, consult tools, gather evidence, and ultimately complete a structured insurance authorization task, entirely through autonomous reasoning.
Сменить язык
Читать эту статью на русском