Skip to content

Commit

Permalink
v2.3.90
Browse files Browse the repository at this point in the history
  • Loading branch information
ashpreetbedi committed May 3, 2024
1 parent 7a5e5b4 commit a6db51c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
6 changes: 2 additions & 4 deletions cookbook/tools/pubmed.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from phi.assistant import Assistant
from phi.tools.pubmed import Pubmed

assistant = Assistant(tools=[Pubmed()], debug=True, show_tool_calls=True)
res = assistant.print_response(
assistant = Assistant(tools=[Pubmed()], debug_mode=True, show_tool_calls=True)
assistant.print_response(
"ulcerative colitis.",
markdown=True,
)

print(res)
42 changes: 26 additions & 16 deletions phi/tools/pubmed.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,27 @@
class Pubmed(Toolkit):
def __init__(
self,
search_term: Optional[str] = None,
email: str = "[email protected]",
max_results: Optional[int] = None,
email: Optional[str] = "[email protected]",
):
super().__init__(name="pubmed")
self.search_term = search_term
self.max_results = max_results
self.email = email
self.max_results: Optional[int] = max_results
self.email: str = email

self.register(self.search_pubmed)

def fetch_pubmed_ids(self, search_term: str, max_results: int, email: str) -> List[str]:
def fetch_pubmed_ids(self, query: str, max_results: int, email: str) -> List[str]:
url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi"
params = {
"db": "pubmed",
"term": search_term,
"term": query,
"retmax": max_results,
"email": email,
"usehistory": "y",
}
response = requests.get(url, params=params)
root = ElementTree.fromstring(response.content)
return [id_elem.text for id_elem in root.findall(".//Id")]
return [id_elem.text for id_elem in root.findall(".//Id") if id_elem.text is not None]

def fetch_details(self, pubmed_ids: List[str]) -> ElementTree.Element:
url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi"
Expand All @@ -54,11 +52,23 @@ def parse_details(self, xml_root: ElementTree.Element) -> List[Dict[str, Any]]:
return articles

def search_pubmed(self, query: str, max_results: int = 10) -> str:
ids = self.fetch_pubmed_ids(query, max_results, self.email)
details_root = self.fetch_details(ids)
articles = self.parse_details(details_root)
results = [
f"Published: {article['Published']}\nTitle: {article['Title']}\nSummary:\n{article['Summary']}"
for article in articles
]
return json.dumps(results)
"""Use this function to search PubMed for articles.
Args:
query (str): The search query.
max_results (int): The maximum number of results to return.
Returns:
str: A JSON string containing the search results.
"""
try:
ids = self.fetch_pubmed_ids(query, self.max_results or max_results, self.email)
details_root = self.fetch_details(ids)
articles = self.parse_details(details_root)
results = [
f"Published: {article.get('Published')}\nTitle: {article.get('Title')}\nSummary:\n{article.get('Summary')}"
for article in articles
]
return json.dumps(results)
except Exception as e:
return f"Cound not fetch articles. Error: {e}"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "phidata"
version = "2.3.89"
version = "2.3.90"
description = "Add memory, knowledge and tools to LLMs."
requires-python = ">=3.8"
readme = "README.md"
Expand Down

0 comments on commit a6db51c

Please sign in to comment.