🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
fead: Add pubmed in toolkits (#1806)
Rishabh3d7d6924ed04
diff --git a/camel/toolkits/__init__.py b/camel/toolkits/__init__.pyindex b7f5ee859..c892954d5 100644--- a/camel/toolkits/__init__.py+++ b/camel/toolkits/__init__.py@@ -58,6 +58,7 @@ from .mcp_toolkit import MCPToolkitfrom .web_toolkit import WebToolkitfrom .file_write_toolkit import FileWriteToolkitfrom .terminal_toolkit import TerminalToolkit+from .pubmed_toolkit import PubMedToolkit__all__ = [@@ -104,4 +105,5 @@ __all__ = ['WebToolkit','FileWriteToolkit','TerminalToolkit',+ 'PubMedToolkit',]diff --git a/camel/toolkits/pubmed_toolkit.py b/camel/toolkits/pubmed_toolkit.pynew file mode 100644index 000000000..e3bd7d3be--- /dev/null+++ b/camel/toolkits/pubmed_toolkit.py@@ -0,0 +1,346 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========++from typing import Any, Dict, List, Optional, Union, cast++import requests++from camel.logger import get_logger+from camel.toolkits import BaseToolkit, FunctionTool++logger = get_logger(__name__)+++class PubMedToolkit(BaseToolkit):+ r"""A toolkit for interacting with PubMed's E-utilities API to access+ MEDLINE data.++ This toolkit provides functionality to search and retrieve papers from the+ PubMed database, including abstracts, citations, and other metadata.++ Args:+ timeout (Optional[float]): The timeout for API requests in seconds.+ (default: :obj:`None`)+ """++ BASE_URL = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils"++ def __init__(self, timeout: Optional[float] = None) -> None:+ r"""Initializes the PubMedToolkit."""+ super().__init__(timeout=timeout)++ def _make_request(+ self,+ endpoint: str,+ params: Dict[str, Union[str, int]],+ retries: int = 3,+ ) -> Optional[Dict[str, Any]]:+ r"""Makes a request to the PubMed/MEDLINE API with error handling and+ retries.++ Args:+ endpoint (str): The API endpoint to call.+ params (Dict[str, Union[str, int]]): Query parameters.+ retries (int, optional): Number of retry attempts.+ (default: :obj:`3`)++ Returns:+ Optional[Dict[str, Any]]: JSON response if successful, else None.+ """+ url = f"{self.BASE_URL}/{endpoint}"+ request_params = cast(Dict[str, Union[str, int]], params)++ for attempt in range(retries):+ try:+ response = requests.get(+ url, params=request_params, timeout=self.timeout+ )+ response.raise_for_status()++ if not response.text:+ logger.warning(+ f"Empty response from PubMed API: {endpoint}"+ )+ return None++ return response.json()+ except requests.RequestException as e:+ if attempt == retries - 1:+ logger.error(f"Failed to fetch data from PubMed: {e!s}")+ return None+ logger.warning(f"Request attempt {attempt + 1} failed: {e!s}")+ except ValueError as e:+ logger.error(f"Failed to parse JSON response: {e!s}")+ return None+ return None++ def search_papers(+ self,+ query: str,+ max_results: int = 10,+ sort: str = "relevance",+ date_range: Optional[Dict[str, str]] = None,+ publication_type: Optional[List[str]] = None,+ ) -> List[Dict[str, str]]:+ r"""Search for biomedical papers in MEDLINE via PubMed with advanced+ filtering options.++ Args:+ query (str): The search query string.+ max_results (int, optional): Maximum number of results to return.+ (default: :obj:`10`)+ sort (str, optional): Sort order - 'relevance' or 'date'.+ (default: :obj:`"relevance"`)+ date_range (Optional[Dict[str, str]], optional): Date range filter+ with 'from' and 'to' dates in YYYY/MM/DD format.+ (default: :obj:`None`)+ publication_type (Optional[List[str]], optional): Filter by+ publication types (e.g., ["Journal Article", "Review"]).+ (default: :obj:`None`)++ Returns:+ List[Dict[str, str]]: List of papers with their metadata.+ """+ # Build query with filters+ filtered_query = query+ if publication_type:+ type_filter = " OR ".join(+ [f'"{pt}"[Publication Type]' for pt in publication_type]+ )+ filtered_query = f"({query}) AND ({type_filter})"+ if date_range:+ date_filter = (+ f"{date_range.get('from', '')}:"+ f"{date_range.get('to', '')}[Date - Publication]"+ )+ filtered_query = f"({filtered_query}) AND ({date_filter})"++ # Search for paper IDs+ search_params: Dict[str, Union[str, int]] = {+ "db": "pubmed",+ "term": filtered_query,+ "retmax": max_results,+ "sort": "relevance" if sort == "relevance" else "pub+date",+ "retmode": "json",+ }++ search_data = self._make_request("esearch.fcgi", search_params)+ if not search_data or "esearchresult" not in search_data:+ logger.error("Failed to retrieve search results")+ return []++ paper_ids = search_data["esearchresult"].get("idlist", [])+ if not paper_ids:+ return []++ # Fetch details for papers+ results = []+ for paper_id in paper_ids:+ paper_details = self.get_paper_details(paper_id)+ if paper_details:+ results.append(paper_details)++ return results++ def get_paper_details(+ self,+ paper_id: Union[str, int],+ include_references: bool = False,+ ) -> Optional[Dict[str, Any]]:+ r"""Get detailed information about a specific biomedical paper from+ MEDLINE/PubMed.++ Args:+ paper_id (Union[str, int]): PubMed ID of the paper.+ include_references (bool, optional): Whether to include referenced+ papers. (default: :obj:`False`)++ Returns:+ Optional[Dict[str, Any]]: Paper details including title, authors,+ abstract, etc., or None if retrieval fails.+ """+ # Fetch summary+ summary_params: Dict[str, Union[str, int]] = {+ "db": "pubmed",+ "id": str(paper_id),+ "retmode": "json",+ }+ summary_data = self._make_request("esummary.fcgi", summary_params)++ if not summary_data or "result" not in summary_data:+ logger.error(+ f"Failed to retrieve paper details for ID: {paper_id}"+ )+ return None++ paper_data = summary_data["result"][str(paper_id)]++ # Handle authors - they come as a list of dicts with 'name' key+ authors = paper_data.get("authors", [])+ author_names = []+ for author in authors:+ if isinstance(author, dict) and "name" in author:+ author_names.append(author["name"])+ elif isinstance(author, str):+ author_names.append(author)++ # Get abstract+ abstract = self.get_abstract(paper_id)++ # Get references if requested+ references = []+ if include_references:+ ref_params: Dict[str, Union[str, int]] = {+ "db": "pubmed",+ "id": str(paper_id),+ "linkname": "pubmed_pubmed_refs",+ "retmode": "json",+ }+ ref_data = self._make_request("elink.fcgi", ref_params)+ if ref_data and "linksets" in ref_data:+ try:+ references = ref_data["linksets"][0]["linksetdbs"][0][+ "links"+ ]+ except (KeyError, IndexError):+ logger.warning(+ f"No references found for paper ID: {paper_id}"+ )++ return cast(+ Dict[str, Any],+ {+ "id": str(paper_id),+ "title": paper_data.get("title", ""),+ "authors": ", ".join(author_names),+ "journal": paper_data.get("source", ""),+ "pub_date": paper_data.get("pubdate", ""),+ "abstract": abstract,+ "doi": paper_data.get("elocationid", ""),+ "keywords": paper_data.get("keywords", []),+ "mesh_terms": paper_data.get("mesh", []),+ "publication_types": paper_data.get("pubtype", []),+ "references": references if include_references else None,+ },+ )++ def get_abstract(self, paper_id: Union[str, int]) -> str:+ r"""Get the abstract of a specific biomedical paper from MEDLINE/+ PubMed.++ Args:+ paper_id (Union[str, int]): PubMed ID of the paper.++ Returns:+ str: The abstract text.+ """+ params: Dict[str, Union[str, int]] = {+ "db": "pubmed",+ "id": str(paper_id),+ "rettype": "abstract",+ "retmode": "text",+ }++ try:+ response = requests.get(+ f"{self.BASE_URL}/efetch.fcgi", params=params+ )+ response.raise_for_status()+ return response.text.strip()+ except requests.exceptions.RequestException as e:+ logger.error(+ f"Failed to retrieve abstract for ID {paper_id}: {e!s}"+ )+ return ""++ def get_citation_count(self, paper_id: Union[str, int]) -> int:+ r"""Get the number of citations for a biomedical paper in MEDLINE/+ PubMed.++ Args:+ paper_id (Union[str, int]): PubMed ID of the paper.++ Returns:+ int: Number of citations, or 0 if retrieval fails.+ """+ params: Dict[str, Union[str, int]] = {+ "db": "pubmed",+ "id": str(paper_id),+ "linkname": "pubmed_pubmed_citedin",+ "retmode": "json",+ }++ data = self._make_request("elink.fcgi", params)+ if not data or "linksets" not in data:+ return 0++ try:+ return len(data["linksets"][0]["linksetdbs"][0]["links"])+ except (KeyError, IndexError):+ return 0++ def get_related_papers(+ self,+ paper_id: Union[str, int],+ max_results: int = 10,+ ) -> List[Dict[str, Any]]:+ r"""Get biomedical papers related to a specific paper in MEDLINE/+ PubMed.++ Args:+ paper_id (Union[str, int]): PubMed ID of the paper.+ max_results (int, optional): Maximum number of results to return.+ (default: :obj:`10`)++ Returns:+ List[Dict[str, Any]]: List of related papers with their metadata.+ """+ params: Dict[str, Union[str, int]] = {+ "db": "pubmed",+ "id": str(paper_id),+ "linkname": "pubmed_pubmed",+ "retmode": "json",+ }++ data = self._make_request("elink.fcgi", params)+ if not data or "linksets" not in data:+ return []++ try:+ related_ids = data["linksets"][0]["linksetdbs"][0]["links"][+ :max_results+ ]+ related_papers: List[Dict[str, Any]] = []++ for pid in related_ids:+ if paper := self.get_paper_details(pid):+ related_papers.append(paper)++ return related_papers+ except (KeyError, IndexError):+ return []++ def get_tools(self) -> List[FunctionTool]:+ r"""Returns a list of tools provided by the PubMed toolkit.++ Returns:+ List[FunctionTool]: List of available tools.+ """+ return [+ FunctionTool(self.search_papers),+ FunctionTool(self.get_paper_details),+ FunctionTool(self.get_abstract),+ FunctionTool(self.get_citation_count),+ FunctionTool(self.get_related_papers),+ ]diff --git a/examples/toolkits/pubmed_toolkit.py b/examples/toolkits/pubmed_toolkit.pynew file mode 100644index 000000000..ff731fd3c--- /dev/null+++ b/examples/toolkits/pubmed_toolkit.py@@ -0,0 +1,283 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========++from camel.agents import ChatAgent+from camel.configs.openai_config import ChatGPTConfig+from camel.models import ModelFactory+from camel.toolkits import PubMedToolkit+from camel.types import ModelPlatformType, ModelType++# Initialize PubMed toolkit and get tools+tools = PubMedToolkit().get_tools()++# Set up model configuration+model_config_dict = ChatGPTConfig(+ temperature=0.0,+).as_dict()++model = ModelFactory.create(+ model_platform=ModelPlatformType.DEFAULT,+ model_type=ModelType.DEFAULT,+ model_config_dict=model_config_dict,+)++# Create chat agent+system_msg = (+ "You are a research assistant specialized in medical literature. "+ "Help researchers find and analyze scientific papers from PubMed."+)+camel_agent = ChatAgent(+ system_message=system_msg,+ model=model,+ tools=tools,+)+camel_agent.reset()++# Example 1: Search for recent papers about mRNA vaccine technology+print("\nExample 1: Search for recent papers about mRNA vaccine technology")+print("=" * 80)++usr_msg = (+ "Find recent review papers about mRNA vaccine technology published "+ "in 2024, with a focus on therapeutic applications and clinical trials. "+ "Limit to 3 papers."+)++response = camel_agent.step(usr_msg)+print(str(response.info['tool_calls'])[:2000])++"""+===============================================================================+ToolCallingRecord(+ tool_name='search_papers',+ args={+ 'query': 'mRNA vaccine tech therapeutic applications trials',+ 'max_results': 10,+ 'sort': 'date',+ 'date_range': {'from': '2024/01/01', 'to': '2024/12/31'},+ 'publication_type': ['Review'],+ },+ result=[+ {+ 'id': '39601789',+ 'title': 'Example Title',+ 'authors': 'First Author, Second Author',+ 'journal': 'Example Journal',+ 'pub_date': '2025 Jan 6',+ 'abstract': 'Abstract of the paper',+===============================================================================+"""+++# Example 2: Get detailed information about a specific paper+print("\nExample 2: Get detailed paper information")+print("=" * 80)++usr_msg = (+ "Get detailed information about PubMed ID 39601789 "+ "(a key paper about mRNA vaccine technology)."+)+camel_agent.reset()+response = camel_agent.step(usr_msg)+print(str(response.info['tool_calls'])[:2000])++"""+===============================================================================+[ToolCallingRecord(+ tool_name='get_paper_details',+ args={'paper_id': 37840631, 'include_references': True},+ result={+ 'id': '37840631',+ 'title': 'Chinese guideline for lipid management (2023):+ a new guideline rich in domestic elements for+ controlling dyslipidemia.',+ 'authors': 'Li JJ',+ 'journal': 'J Geriatr Cardiol',+ 'pub_date': '2023 Sep 28',+ 'abstract': '1. J Geriatr Cardiol.+ 2023 Sep 28;20(9):618-620.+ doi: 10.26599/1671-5411.2023.09.007.+ Chinese guideline for lipid management (2023):+ a new guideline rich in domestic elements for+ controlling dyslipidemia.Li JJ(1).\Author information:+ (1)Division of Cardio-Metabolic Center,+ State Key Laboratory of Cardiovascular+ Disease, Fu Wai Hospital, National Center+ for Cardiovascular Disease, Chinese Academy+ of Medical Sciences, Peking Union Medical College,+ Beijing, China.DOI: 10.26599/1671-5411.2023.09.007+ PMCID: PMC10568543\nPMID: 37840631',+ 'doi': 'doi: 10.26599/1671-5411.2023.09.007',+ 'keywords': [],+ 'mesh_terms': [],+ 'publication_types': ['Journal Article'],+ 'references': ['35729555', '34734202', '34404993',+ '31172370', '30586774', '30526649',+ '29434622', '20350253']+ },+ tool_call_id='call_k8s7oFcRvDBKuEKvk48uoWXZ'+)]+===============================================================================+"""++# Example 3: Find related papers and citation metrics+print("\nExample 3: Find related papers and citation metrics")+print("=" * 80)++usr_msg = (+ "Find papers related to PubMed ID 39601789 (limit to 3 papers) and "+ "show its citation count."+)+camel_agent.reset()+response = camel_agent.step(usr_msg)+print(str(response.info['tool_calls'])[:2000])++"""+===============================================================================+[ToolCallingRecord(+ tool_name='get_related_papers',+ args={'paper_id': 37840631, 'max_results': 5},+ result=[+ {'id': '37840631',+ 'title': 'Chinese guideline for lipid management (2023):+ a new guideline rich in domestic elements for+ controlling dyslipidemia.',+ 'authors': 'Li JJ',+ 'journal': 'J Geriatr Cardiol',+ 'pub_date': '2023 Sep 28',+ 'abstract': (+ '1. J Geriatr Cardiol. 2023 Sep 28;20(9):618-620. doi: '+ '10.26599/1671-5411.2023.09.007.'+ 'Chinese guideline for lipid management (2023): a new guideline'+ 'rich in domestic elements for controlling dyslipidemia.'+ 'Li JJ(1).Author information:(1)Division of Cardio-Metabolic '+ 'Center, State Key Laboratory of Cardiovascular Disease, Fu Wai '+ 'Hospital, National Center for Cardiovascular Disease, Chinese '+ 'Academy of Medical Sciences, Peking Union Medical College, '+ 'Beijing, China.DOI: 10.26599/1671-5411.2023.09.007'+ 'PMCID: PMC10568543 PMID: 37840631'+ ),+ 'doi': 'doi: 10.26599/1671-5411.2023.09.007',+ 'keywords': [],+ 'mesh_terms': [],+ 'publication_types': ['Journal Article'],+ 'references': None},+ {'id': '22801311',+ 'title': (+ '[Short-term impact of modified blood-lipid reports on physicians'+ 'lipid lowering drug prescribing behavior and knowledge '+ 'improvement on dyslipidemia].'+ ),+ 'authors': 'Li JH, Jiang H, Sun XH, Li CC, Ke YN, Yan SK, Wu YF',+ 'journal': 'Zhonghua Xin Xue Guan Bing Za Zhi',+ 'pub_date': '2012 Apr',+ 'abstract': (+ '1. Zhonghua Xin Xue Guan Bing Za Zhi. 2012 Apr;40(4):318-22.'+ '[Short-term impact modified blood-lipid reports on physicians'+ 'lipid lowering drug prescribing behavior and knowledge '+ 'improvement on dyslipidemia].Article in Chinese]'+ 'Li JH(1), Jiang H, Sun XH, Li CC, Ke YN, Yan SK, Wu YF.'+ 'Author information:(1)Department of Cardiology, China-Japan'+===============================================================================+"""++# Example 4: Advanced search with multiple filters+print("\nExample 4: Advanced search with multiple filters")+print("=" * 80)++usr_msg = (+ "Find clinical trial papers about mRNA-based cancer vaccines published "+ "between 2023/01/01 and 2024/03/01, focusing on phase III trials. "+ "Limit to 3 papers."+)+camel_agent.reset()+response = camel_agent.step(usr_msg)+print(str(response.info['tool_calls'])[:2000])++"""+===============================================================================+[ToolCallingRecord(+ tool_name='search_papers',+ args={+ 'query': 'mRNA cancer vaccine phase III clinical trial',+ 'max_results': 10,+ 'sort': 'date',+ 'date_range': {'from': '2023/01/01', 'to': '2024/03/01'},+ 'publication_type': ['Clinical Trial']+ },+ result=[+ {+ 'id': '37820782',+ 'title': 'Stochastic interventional approach to assessing immune '+ 'correlates of protection: Application to the COVE '+ 'RNA-1273 vaccine trial.',+ 'authors': (+ 'Hejazi NS, Shen X, Carpp LN, Benkeser D, Follmann D,+ Janes HE, Baden LR, El Sahly HM, Deng W, Zhou H,+ Leav B, Montefiori DC, 'Gilbert PB'+ ),+ 'journal': 'Int J Infect Dis',+ 'pub_date': '2023 Dec',+ 'abstract': Abstract of the paper+===============================================================================+"""++# Example 5: Get abstract and analyze citations+print("\nExample 5: Get abstract and analyze citations")+print("=" * 80)++usr_msg = (+ "Get the abstract of PubMed ID 39601789 and find out how many times "+ "it has been cited."+)+camel_agent.reset()+response = camel_agent.step(usr_msg)+print(str(response.info['tool_calls'])[:2000])++"""+===============================================================================+[+ ToolCallingRecord(+ tool_name='get_abstract',+ args={'paper_id': 37840631},+ result='''+ 1. J Geriatr Cardiol. 2023 Sep 28;20(9):618-620. doi:+ 10.26599/1671-5411.2023.09.007.++ Chinese guideline for lipid management (2023):a new guideline+ rich in domestic elements for controlling dyslipidemia.++ Li JJ(1).++ Author information:+ (1)Division of Cardio-Metabolic Center, State Key Laboratory+ of Cardiovascular Disease, Fu Wai Hospital, National Center+ for Cardiovascular Disease, Chinese Academy of Medical Sciences,+ Peking Union Medical College, Beijing, China.++ DOI: 10.26599/1671-5411.2023.09.007+ PMCID: PMC10568543+ PMID: 37840631+ ''',+ tool_call_id='call_AFG6jLkdvWidaVGrj9UblTci'+ ),+ ToolCallingRecord(+ tool_name='get_citation_count',+ args={'paper_id': 37840631},+ result=0,+ tool_call_id='call_ZM3p59gtYmeR9DPdONNHV4Qw'+ )+]+===============================================================================+"""diff --git a/test/toolkits/test_pubmed_toolkit.py b/test/toolkits/test_pubmed_toolkit.pynew file mode 100644index 000000000..39e93e26c--- /dev/null+++ b/test/toolkits/test_pubmed_toolkit.py@@ -0,0 +1,283 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========++from unittest.mock import MagicMock, patch++import pytest+import requests++from camel.toolkits import PubMedToolkit+++def test_init():+ toolkit = PubMedToolkit()+ assert toolkit.BASE_URL == "https://eutils.ncbi.nlm.nih.gov/entrez/eutils"+ assert toolkit.timeout is None++ toolkit_with_timeout = PubMedToolkit(timeout=30)+ assert toolkit_with_timeout.timeout == 30+++def test_get_tools():+ toolkit = PubMedToolkit()+ tools = toolkit.get_tools()++ assert isinstance(tools, list)+ assert len(tools) == 5++ # Extract function names from FunctionTool objects+ tool_functions = {tool.func.__name__ for tool in tools}+ expected_functions = {+ "search_papers",+ "get_paper_details",+ "get_abstract",+ "get_citation_count",+ "get_related_papers",+ }+ assert tool_functions == expected_functions+++@patch('requests.get')+def test_search_papers_success(mock_get):+ # Mock the search response+ mock_search_response = MagicMock()+ mock_search_response.json.return_value = {+ "esearchresult": {"idlist": ["12345", "67890"]}+ }+ mock_search_response.text = (+ '{"esearchresult": {"idlist": ["12345", "67890"]}}'+ )++ # Mock the summary responses for each paper ID+ mock_summary_response1 = MagicMock()+ mock_summary_response1.json.return_value = {+ "result": {+ "12345": {+ "title": "Test Paper 1",+ "authors": [{"name": "Author 1"}, {"name": "Author 2"}],+ "source": "Test Journal",+ "pubdate": "2024",+ }+ }+ }+ mock_summary_response1.text = (+ '{"result": {"12345": {"title": "Test Paper 1"}}}'+ )++ mock_summary_response2 = MagicMock()+ mock_summary_response2.json.return_value = {+ "result": {+ "67890": {+ "title": "Test Paper 2",+ "authors": [{"name": "Author 3"}],+ "source": "Another Journal",+ "pubdate": "2023",+ }+ }+ }+ mock_summary_response2.text = (+ '{"result": {"67890": {"title": "Test Paper 2"}}}'+ )++ # Mock the abstract responses+ mock_abstract_response1 = MagicMock()+ mock_abstract_response1.text = "Test abstract 1"++ mock_abstract_response2 = MagicMock()+ mock_abstract_response2.text = "Test abstract 2"++ # Set up the side effect sequence+ mock_get.side_effect = [+ mock_search_response, # Search for papers+ mock_summary_response1, # Get details for paper 12345+ mock_abstract_response1, # Get abstract for paper 12345+ mock_summary_response2, # Get details for paper 67890+ mock_abstract_response2, # Get abstract for paper 67890+ ]++ toolkit = PubMedToolkit()+ results = toolkit.search_papers(+ query="test query",+ max_results=2,+ date_range={"from": "2023/01/01", "to": "2024/12/31"},+ publication_type=["Journal Article"],+ )++ assert len(results) == 2+ assert results[0]["title"] == "Test Paper 1"+ assert results[1]["title"] == "Test Paper 2"++ # Verify the search query construction+ first_call_args = mock_get.call_args_list[0][1]['params']+ assert "test query" in first_call_args['term']+ assert '"Journal Article"[Publication Type]' in first_call_args['term']+ assert (+ "2023/01/01:2024/12/31[Date - Publication]" in first_call_args['term']+ )+++@patch('requests.get')+def test_search_papers_error(mock_get):+ mock_get.side_effect = requests.RequestException("API Error")++ toolkit = PubMedToolkit()+ results = toolkit.search_papers("test query")++ assert results == []+++@patch('requests.get')+def test_get_paper_details_success(mock_get):+ # Mock the summary response+ mock_summary_response = MagicMock()+ mock_summary_response.json.return_value = {+ "result": {+ "12345": {+ "title": "Test Paper",+ "authors": [{"name": "Author 1"}, {"name": "Author 2"}],+ "source": "Test Journal",+ "pubdate": "2024",+ "mesh": ["Term 1", "Term 2"],+ "keywords": ["keyword1", "keyword2"],+ "pubtype": ["Journal Article"],+ }+ }+ }+ mock_summary_response.text = (+ '{"result": {"12345": {"title": "Test Paper"}}}'+ )++ # Mock the abstract response+ mock_abstract_response = MagicMock()+ mock_abstract_response.text = "Test abstract"++ mock_get.side_effect = [mock_summary_response, mock_abstract_response]++ toolkit = PubMedToolkit()+ result = toolkit.get_paper_details("12345")++ assert result["title"] == "Test Paper"+ assert result["authors"] == "Author 1, Author 2"+ assert result["abstract"] == "Test abstract"+ assert result["mesh_terms"] == ["Term 1", "Term 2"]+ assert result["keywords"] == ["keyword1", "keyword2"]+ assert result["publication_types"] == ["Journal Article"]+++@patch('requests.get')+def test_get_paper_details_with_references(mock_get):+ # Mock responses+ mock_summary_response = MagicMock()+ mock_summary_response.json.return_value = {+ "result": {+ "12345": {+ "title": "Test Paper",+ "authors": [{"name": "Author 1"}],+ "source": "Test Journal",+ "pubdate": "2024",+ }+ }+ }+ mock_summary_response.text = (+ '{"result": {"12345": {"title": "Test Paper"}}}'+ )++ mock_abstract_response = MagicMock()+ mock_abstract_response.text = "Test abstract"++ mock_ref_response = MagicMock()+ mock_ref_response.json.return_value = {+ "linksets": [{"linksetdbs": [{"links": ["67890", "11111"]}]}]+ }+ mock_ref_response.text = (+ '{"linksets": [{"linksetdbs": [{"links": ["67890", "11111"]}]}]}'+ )++ mock_get.side_effect = [+ mock_summary_response,+ mock_abstract_response,+ mock_ref_response,+ ]++ toolkit = PubMedToolkit()+ result = toolkit.get_paper_details("12345", include_references=True)++ assert result["references"] == ["67890", "11111"]+++@patch('requests.get')+def test_get_citation_count(mock_get):+ mock_response = MagicMock()+ mock_response.json.return_value = {+ "linksets": [{"linksetdbs": [{"links": ["1", "2", "3"]}]}]+ }+ mock_response.text = (+ '{"linksets": [{"linksetdbs": [{"links": ["1", "2", "3"]}]}]}'+ )++ mock_get.return_value = mock_response++ toolkit = PubMedToolkit()+ count = toolkit.get_citation_count("12345")++ assert count == 3+++@patch('requests.get')+def test_get_related_papers(mock_get):+ # Mock the related papers response+ mock_related_response = MagicMock()+ mock_related_response.json.return_value = {+ "linksets": [{"linksetdbs": [{"links": ["12345"]}]}]+ }+ mock_related_response.text = (+ '{"linksets": [{"linksetdbs": [{"links": ["12345"]}]}]}'+ )++ # Mock the paper details response+ mock_details_response = MagicMock()+ mock_details_response.json.return_value = {+ "result": {+ "12345": {+ "title": "Related Paper",+ "authors": [{"name": "Author 1"}],+ "source": "Test Journal",+ "pubdate": "2024",+ }+ }+ }+ mock_details_response.text = (+ '{"result": {"12345": {"title": "Related Paper"}}}'+ )++ # Mock the abstract response+ mock_abstract_response = MagicMock()+ mock_abstract_response.text = "Test abstract"++ mock_get.side_effect = [+ mock_related_response,+ mock_details_response,+ mock_abstract_response,+ ]++ toolkit = PubMedToolkit()+ results = toolkit.get_related_papers("67890", max_results=1)++ assert len(results) == 1+ assert results[0]["title"] == "Related Paper"+++def test_invalid_timeout():+ with pytest.raises(ValueError):+ PubMedToolkit(timeout=-1)