File size: 1,129 Bytes
8a3374d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
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}]
|