pubmed.py
Overview
The pubmed.py file implements a component designed to search and retrieve scientific articles from the PubMed database, a comprehensive and freely accessible repository of biomedical literature. This component is part of a larger system (likely the InfiniFlow platform) and provides functionality to query PubMed using user-specified keywords, fetch metadata and abstracts for relevant articles, and format the results for downstream consumption.
Key features include:
Parameterization of search queries with validation.
Integration with the NCBI Entrez API via Biopython to perform searches and fetch article details.
Handling of XML data returned by PubMed, including extraction of titles, URLs, and abstracts.
Robust error handling with retry logic and graceful failure reporting.
Compliance with user-configured timeouts and retry policies.
Classes and Their Details
PubMedParam
Description:
Defines and validates parameters required for the PubMed search component. It inherits from ToolParamBase and encapsulates metadata and input form definitions that facilitate integration with UI or API layers.
Attributes:
meta: ToolMeta
Metadata describing the component, including its name, description, and expected parameters.top_n: int(default: 12)
Maximum number of PubMed records to retrieve per query.email: str(default:"[email protected]")
Contact email required by NCBI Entrez API for identification.
Methods:
init(self)
Initializes default values and metadata.check(self)
Validates thattop_nis a positive integer. Raises an error if invalid.get_input_form(self) -> dict[str, dict]
Returns a dictionary describing the input fields expected by the component.
Used for UI generation or API schema validation.
Usage Example:
param = PubMedParam()
param.top_n = 10
param.email = "[email protected]"
param.check() # Validates parameters
input_form = param.get_input_form()
print(input_form)
PubMed
Description:
Main component class that performs PubMed queries and data retrieval. It inherits from ToolBase and Python’s ABC to provide a structured interface. The class executes searches, fetches article details, and processes XML responses to extract relevant information such as titles, URLs, and abstracts.
Class Attributes:
component_name: str="PubMed"
Identifier for this component.
Methods:
_invoke(self, **kwargs) -> str
Core method that performs the PubMed search and fetch operation.Parameters:
kwargs(dict): Accepts"query"(string) as the search keywords.
Returns:
str- Formalized content string containing the retrieved articles or an error message.
Behavior:
Checks if the
"query"argument is present; if absent, sets empty output and returns immediately.Sets the Entrez email from parameters.
Uses Entrez API to search PubMed for article IDs matching the query, limited by
top_n.Fetches detailed article information in XML format.
Cleans XML of
<b>,<i>tags for consistent parsing.Extracts article titles, URLs, and abstracts from XML nodes.
Populates the output buffer with formatted content.
Implements retry logic with delays on exceptions, capturing the last error.
If all retries fail, sets an error output and returns an error message.
thoughts(self) -> str
Provides a textual explanation of the component’s purpose in context of the current query.Returns:
str- A short descriptive string indicating the search focus.
Implementation Details:
Decorated with
@timeoutto enforce execution time limits (default 12 seconds or from environment).Uses Biopython's
Entrezmodule for API communication.XML parsing is done via Python's
xml.etree.ElementTreeafter removing problematic HTML-style tags using regex.Retrieves abstracts carefully, checking for presence to avoid errors.
Constructs URLs pointing to PubMed article landing pages.
Usage Example:
pubmed_tool = PubMed()
result = pubmed_tool._invoke(query="cancer immunotherapy")
print(result)
Important Implementation Details and Algorithms
Retry Mechanism:
The_invokemethod retries the Entrez API calls according to_param.max_retries+1times. Between retries, it logs exceptions and sleeps for_param.delay_after_errorseconds, improving robustness against transient network or API issues.XML Cleaning:
PubMed's XML responses sometimes include<b>and<i>tags that interfere with XML parsing. These tags are stripped out via regex before parsing to avoid errors.Data Extraction:
Uses XPath-like access viaElementTreeto find article titles, PMIDs, and abstracts. Handles missing abstracts gracefully by substituting placeholder text.Timeout Enforcement:
The decorator@timeoutensures that the component does not exceed configured execution time, preventing hanging or long-running queries that could degrade system performance.
Interaction with Other System Parts
Base Classes:
Inherits fromToolBaseandToolParamBase(likely part of the InfiniFlow framework), which provide standardized input/output handling, configuration, logging, and lifecycle management.Agent and API Utilities:
Usestimeoutdecorator fromapi.utils.api_utilsand base classes fromagent.tools.base, indicating this component integrates into an agent-based tooling system that manages execution and API exposure.External Dependencies:
Relies on Biopython'sEntrezmodule to communicate with the NCBI Entrez API, a key external system for biomedical literature retrieval.Configuration:
Uses environment variables (e.g.,COMPONENT_EXEC_TIMEOUT) to tune runtime behavior, allowing deployment-time flexibility.
Mermaid Diagram: Class Structure of pubmed.py
classDiagram
class PubMedParam {
+meta: ToolMeta
+top_n: int
+email: str
+__init__()
+check()
+get_input_form() dict
}
class PubMed {
+component_name: str
+_invoke(kwargs) str
+thoughts() str
}
PubMedParam <|-- PubMedParam : inherits ToolParamBase
PubMed <|-- PubMed : inherits ToolBase, ABC
Summary
The pubmed.py module provides a robust and configurable component to query the PubMed literature database within the InfiniFlow platform. It abstracts the complexity of interacting with the Entrez API, processes XML responses, and exposes a clean interface for retrieving biomedical article metadata and abstracts based on user queries. Its design emphasizes reliability, configurability, and integration readiness, making it a critical building block for biomedical knowledge retrieval workflows.