elna / app /agent.py
David Chu
feat: restful api endpoint
8a3374d unverified
raw
history blame
1.13 kB
import json
import re
from pathlib import Path
from google import genai
from google.genai import types
from app.tools import dailymed, literature
SYSTEM_INSTRUCTION = (Path(__file__).parent / "system_instruction.txt").read_text()
def respond(client: genai.Client, query: str) -> list[dict]:
config = types.GenerateContentConfig(
tools=[
dailymed.find_drug_set_ids,
dailymed.find_drug_instruction,
literature.search_medical_literature,
],
system_instruction=SYSTEM_INSTRUCTION,
)
resp = client.models.generate_content(
model="gemini-2.5-flash-preview-04-17",
contents=query,
config=config,
)
output = ((resp.text) or "").strip()
if output.startswith("```"):
# Extract content inside the first markdown code block (``` or ```json)
match = re.match(r"^```(?:json)?\s*([\s\S]*?)\s*```", output)
if match:
output = match.group(1).strip()
try:
return json.loads(output)
except json.decoder.JSONDecodeError as err:
print(err)
return [{"text": output}]