David Chu
commited on
doc: improve code comments
Browse files- app/agent.py +9 -2
- app/tools/dailymed.py +1 -1
- app/tools/literature.py +4 -3
- main.py +0 -6
app/agent.py
CHANGED
@@ -7,7 +7,14 @@ from google.genai import types
|
|
7 |
|
8 |
from app.tools import dailymed, literature
|
9 |
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
|
13 |
def respond(client: genai.Client, query: str) -> list[dict]:
|
@@ -22,7 +29,7 @@ def respond(client: genai.Client, query: str) -> list[dict]:
|
|
22 |
resp = client.models.generate_content(
|
23 |
model="gemini-2.5-flash-preview-04-17",
|
24 |
contents=query,
|
25 |
-
config=
|
26 |
)
|
27 |
|
28 |
output = ((resp.text) or "").strip()
|
|
|
7 |
|
8 |
from app.tools import dailymed, literature
|
9 |
|
10 |
+
CONFIG = types.GenerateContentConfig(
|
11 |
+
tools=[
|
12 |
+
dailymed.find_drug_set_ids,
|
13 |
+
dailymed.find_drug_instruction,
|
14 |
+
literature.search_medical_literature,
|
15 |
+
],
|
16 |
+
system_instruction=(Path(__file__).parent / "system_instruction.txt").read_text(),
|
17 |
+
)
|
18 |
|
19 |
|
20 |
def respond(client: genai.Client, query: str) -> list[dict]:
|
|
|
29 |
resp = client.models.generate_content(
|
30 |
model="gemini-2.5-flash-preview-04-17",
|
31 |
contents=query,
|
32 |
+
config=CONFIG,
|
33 |
)
|
34 |
|
35 |
output = ((resp.text) or "").strip()
|
app/tools/dailymed.py
CHANGED
@@ -30,7 +30,7 @@ def find_drug_instruction(set_id: str) -> str:
|
|
30 |
"""Get the instruction of a drug from the FDA database.
|
31 |
|
32 |
The instruction includes dosage, contradictions, adverse
|
33 |
-
reactions,
|
34 |
|
35 |
Args:
|
36 |
set_id: Set ID of the drug to look up.
|
|
|
30 |
"""Get the instruction of a drug from the FDA database.
|
31 |
|
32 |
The instruction includes dosage, contradictions, adverse
|
33 |
+
reactions, drug interactions, etc.
|
34 |
|
35 |
Args:
|
36 |
set_id: Set ID of the drug to look up.
|
app/tools/literature.py
CHANGED
@@ -25,9 +25,6 @@ def search_semantic_scholar(
|
|
25 |
|
26 |
@retry(stop=stop_after_attempt(5), wait=wait_random_exponential(multiplier=0.5, max=10))
|
27 |
def get_pubmed_abstracts(pmids: list[int]) -> dict[str, dict]:
|
28 |
-
"""
|
29 |
-
Referenced `pymed` library for parsing the xml.
|
30 |
-
"""
|
31 |
resp = httpx.get(
|
32 |
"https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi",
|
33 |
params={"db": "pubmed", "id": pmids, "retmode": "xml"},
|
@@ -41,6 +38,8 @@ def get_pubmed_abstracts(pmids: list[int]) -> dict[str, dict]:
|
|
41 |
pmid = article.findtext(
|
42 |
".//PubmedData/ArticleIdList/ArticleId[@IdType='pubmed']", default=""
|
43 |
)
|
|
|
|
|
44 |
for text in article.findall(".//AbstractText"):
|
45 |
if label := text.attrib.get("Label"):
|
46 |
abstract += f"## {label}\n\n"
|
@@ -93,6 +92,8 @@ def search_medical_literature(query: str) -> str:
|
|
93 |
if pubmed_abstract := pubmed_abstracts.get(
|
94 |
publication["externalIds"].get("PubMed")
|
95 |
):
|
|
|
|
|
96 |
publication["abstract"] = pubmed_abstract
|
97 |
|
98 |
outputs.append(format_publication(publication))
|
|
|
25 |
|
26 |
@retry(stop=stop_after_attempt(5), wait=wait_random_exponential(multiplier=0.5, max=10))
|
27 |
def get_pubmed_abstracts(pmids: list[int]) -> dict[str, dict]:
|
|
|
|
|
|
|
28 |
resp = httpx.get(
|
29 |
"https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi",
|
30 |
params={"db": "pubmed", "id": pmids, "retmode": "xml"},
|
|
|
38 |
pmid = article.findtext(
|
39 |
".//PubmedData/ArticleIdList/ArticleId[@IdType='pubmed']", default=""
|
40 |
)
|
41 |
+
# The abstract is sometimes divided into multiple
|
42 |
+
# sections. Concatenate into one Markdown text.
|
43 |
for text in article.findall(".//AbstractText"):
|
44 |
if label := text.attrib.get("Label"):
|
45 |
abstract += f"## {label}\n\n"
|
|
|
92 |
if pubmed_abstract := pubmed_abstracts.get(
|
93 |
publication["externalIds"].get("PubMed")
|
94 |
):
|
95 |
+
# Abstracts on PubMed are more complete than the
|
96 |
+
# ones returned from Semantic Scholar.
|
97 |
publication["abstract"] = pubmed_abstract
|
98 |
|
99 |
outputs.append(format_publication(publication))
|
main.py
CHANGED
@@ -1,11 +1,5 @@
|
|
1 |
-
import json
|
2 |
-
import os
|
3 |
-
import re
|
4 |
-
from pathlib import Path
|
5 |
-
|
6 |
import streamlit as st
|
7 |
from google import genai
|
8 |
-
from google.genai import types
|
9 |
|
10 |
from app import agent, config
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from google import genai
|
|
|
3 |
|
4 |
from app import agent, config
|
5 |
|