Skip to content

Paper Downloader

Unified paper download tool for LangGraph. Supports downloading papers from arXiv, medRxiv, bioRxiv, and PubMed through a single interface.

PaperDownloaderFactory

Factory class for creating paper downloader instances.

Source code in aiagents4pharma/talk2scholars/tools/paper_download/paper_downloader.py
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
class PaperDownloaderFactory:
    """Factory class for creating paper downloader instances."""

    # Class-level cache for configuration
    _cached_config = None
    _config_lock = None

    @classmethod
    def clear_cache(cls) -> None:
        """Clear cached configuration."""
        cls._cached_config = None

    @staticmethod
    def get_default_service() -> Literal["arxiv", "medrxiv", "biorxiv", "pubmed"]:
        """
        Get the default service from configuration.

        Returns:
            Default service name from config, fallback to 'pubmed'
        """
        config = PaperDownloaderFactory._get_unified_config()
        default_service = getattr(config.tool, "default_service", "pubmed")
        # Ensure the default service is valid and return with proper type
        if default_service == "arxiv":
            return "arxiv"
        if default_service == "medrxiv":
            return "medrxiv"
        if default_service == "biorxiv":
            return "biorxiv"
        if default_service == "pubmed":
            return "pubmed"
        logger.warning(
            "Invalid default service '%s' in config, falling back to 'pubmed'",
            default_service,
        )
        return "pubmed"

    @staticmethod
    def create(
        service: Literal["arxiv", "medrxiv", "biorxiv", "pubmed"],
    ) -> BasePaperDownloader:
        """
        Create appropriate downloader instance for the specified service.

        Args:
            service: Service name ('arxiv', 'medrxiv', 'biorxiv', 'pubmed')

        Returns:
            Configured downloader instance

        Raises:
            ValueError: If service is not supported
        """
        config = PaperDownloaderFactory._get_unified_config()
        service_config = PaperDownloaderFactory._build_service_config(config, service)

        if service == "arxiv":
            return ArxivDownloader(service_config)
        if service == "medrxiv":
            return MedrxivDownloader(service_config)
        if service == "biorxiv":
            return BiorxivDownloader(service_config)
        # service == "pubmed"
        return PubmedDownloader(service_config)

    @staticmethod
    def _get_unified_config() -> Any:
        """
        Load unified paper download configuration using Hydra with caching.
        This avoids the GlobalHydra reinitialization issue by caching the config.

        Returns:
            Unified configuration object
        """
        # Return cached config if available
        if PaperDownloaderFactory._cached_config is not None:
            return PaperDownloaderFactory._cached_config

        # Ensure lock exists and get a local reference
        lock = PaperDownloaderFactory._config_lock
        if lock is None:
            lock = threading.Lock()
            PaperDownloaderFactory._config_lock = lock

        # Thread-safe config loading with guaranteed non-None lock
        with lock:
            # Double-check pattern - another thread might have loaded it
            if PaperDownloaderFactory._cached_config is not None:
                return PaperDownloaderFactory._cached_config

            try:

                # Clear if already initialized
                if GlobalHydra().is_initialized():
                    logger.info(
                        "GlobalHydra already initialized, clearing for config load"
                    )
                    GlobalHydra.instance().clear()

                # Load configuration
                with hydra.initialize(version_base=None, config_path="../../configs"):
                    cfg = hydra.compose(
                        config_name="config", overrides=["tools/paper_download=default"]
                    )

                # Cache the configuration
                PaperDownloaderFactory._cached_config = cfg.tools.paper_download
                logger.info(
                    "Successfully loaded and cached paper download configuration"
                )

                return PaperDownloaderFactory._cached_config

            except Exception as e:
                logger.error(
                    "Failed to load unified paper download configuration: %s", e
                )
                raise RuntimeError(f"Configuration loading failed: {e}") from e

    @staticmethod
    def _build_service_config(unified_config: Any, service: str) -> Any:
        """
        Build service-specific configuration by merging common and service settings.
        Handles Hydra's OmegaConf objects properly.

        Args:
            unified_config: The unified configuration object
            service: Service name

        Returns:
            Service-specific configuration object
        """
        if (
            not hasattr(unified_config, "services")
            or service not in unified_config.services
        ):
            raise ValueError(f"Service '{service}' not found in configuration")

        # Create a simple config object that combines common and service-specific settings
        class ServiceConfig:
            """Service-specific configuration holder."""

            def get_config_dict(self):
                """Return configuration as dictionary."""
                return {k: v for k, v in self.__dict__.items() if not k.startswith("_")}

            def has_attribute(self, name: str) -> bool:
                """Check if configuration has a specific attribute."""
                return hasattr(self, name)

        config_obj = ServiceConfig()

        # Handle common config (using helper method to reduce branches)
        PaperDownloaderFactory._apply_config(
            config_obj, unified_config.common, "common"
        )

        # Handle service-specific config (using helper method to reduce branches)
        PaperDownloaderFactory._apply_config(
            config_obj, unified_config.services[service], service
        )

        return config_obj

    @staticmethod
    def _apply_config(config_obj: Any, source_config: Any, config_type: str) -> None:
        """
        Apply configuration from source to target object using multiple fallback methods.
        This preserves all the original logic but reduces branches in the main method.

        Args:
            config_obj: Target configuration object
            source_config: Source configuration to extract from
            config_type: Type description for logging
        """
        try:
            PaperDownloaderFactory._try_config_extraction(config_obj, source_config)
        except (AttributeError, TypeError, KeyError) as e:
            logger.warning("Failed to process %s config: %s", config_type, e)

    @staticmethod
    def _try_config_extraction(config_obj: Any, source_config: Any) -> None:
        """Try different methods to extract configuration data."""
        # Method 1: Try OmegaConf conversion
        if hasattr(source_config, "_content"):
            PaperDownloaderFactory._extract_from_omegaconf(config_obj, source_config)
            return

        # Method 2: Try direct attribute access
        if hasattr(source_config, "__dict__"):
            PaperDownloaderFactory._extract_from_dict(
                config_obj, source_config.__dict__
            )
            return

        # Method 3: Try items() method
        if hasattr(source_config, "items"):
            PaperDownloaderFactory._extract_from_items(config_obj, source_config)
            return

        # Method 4: Try dir() approach as fallback
        PaperDownloaderFactory._extract_from_dir(config_obj, source_config)

    @staticmethod
    def _extract_from_omegaconf(config_obj: Any, source_config: Any) -> None:
        """Extract configuration from OmegaConf object."""
        config_dict = OmegaConf.to_container(source_config, resolve=True)
        if isinstance(config_dict, dict):
            for key, value in config_dict.items():
                if isinstance(key, str):  # Type guard for key
                    setattr(config_obj, key, value)

    @staticmethod
    def _extract_from_dict(config_obj: Any, config_dict: dict) -> None:
        """Extract configuration from dictionary."""
        for key, value in config_dict.items():
            if not key.startswith("_"):
                setattr(config_obj, key, value)

    @staticmethod
    def _extract_from_items(config_obj: Any, source_config: Any) -> None:
        """Extract configuration using items() method."""
        for key, value in source_config.items():
            if isinstance(key, str):  # Type guard for key
                setattr(config_obj, key, value)

    @staticmethod
    def _extract_from_dir(config_obj: Any, source_config: Any) -> None:
        """Extract configuration using dir() approach as fallback."""
        for key in dir(source_config):
            if not key.startswith("_"):
                value = getattr(source_config, key)
                if not callable(value):
                    setattr(config_obj, key, value)

_apply_config(config_obj, source_config, config_type) staticmethod

Apply configuration from source to target object using multiple fallback methods. This preserves all the original logic but reduces branches in the main method.

Parameters:

Name Type Description Default
config_obj Any

Target configuration object

required
source_config Any

Source configuration to extract from

required
config_type str

Type description for logging

required
Source code in aiagents4pharma/talk2scholars/tools/paper_download/paper_downloader.py
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
@staticmethod
def _apply_config(config_obj: Any, source_config: Any, config_type: str) -> None:
    """
    Apply configuration from source to target object using multiple fallback methods.
    This preserves all the original logic but reduces branches in the main method.

    Args:
        config_obj: Target configuration object
        source_config: Source configuration to extract from
        config_type: Type description for logging
    """
    try:
        PaperDownloaderFactory._try_config_extraction(config_obj, source_config)
    except (AttributeError, TypeError, KeyError) as e:
        logger.warning("Failed to process %s config: %s", config_type, e)

_build_service_config(unified_config, service) staticmethod

Build service-specific configuration by merging common and service settings. Handles Hydra's OmegaConf objects properly.

Parameters:

Name Type Description Default
unified_config Any

The unified configuration object

required
service str

Service name

required

Returns:

Type Description
Any

Service-specific configuration object

Source code in aiagents4pharma/talk2scholars/tools/paper_download/paper_downloader.py
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
@staticmethod
def _build_service_config(unified_config: Any, service: str) -> Any:
    """
    Build service-specific configuration by merging common and service settings.
    Handles Hydra's OmegaConf objects properly.

    Args:
        unified_config: The unified configuration object
        service: Service name

    Returns:
        Service-specific configuration object
    """
    if (
        not hasattr(unified_config, "services")
        or service not in unified_config.services
    ):
        raise ValueError(f"Service '{service}' not found in configuration")

    # Create a simple config object that combines common and service-specific settings
    class ServiceConfig:
        """Service-specific configuration holder."""

        def get_config_dict(self):
            """Return configuration as dictionary."""
            return {k: v for k, v in self.__dict__.items() if not k.startswith("_")}

        def has_attribute(self, name: str) -> bool:
            """Check if configuration has a specific attribute."""
            return hasattr(self, name)

    config_obj = ServiceConfig()

    # Handle common config (using helper method to reduce branches)
    PaperDownloaderFactory._apply_config(
        config_obj, unified_config.common, "common"
    )

    # Handle service-specific config (using helper method to reduce branches)
    PaperDownloaderFactory._apply_config(
        config_obj, unified_config.services[service], service
    )

    return config_obj

_extract_from_dict(config_obj, config_dict) staticmethod

Extract configuration from dictionary.

Source code in aiagents4pharma/talk2scholars/tools/paper_download/paper_downloader.py
264
265
266
267
268
269
@staticmethod
def _extract_from_dict(config_obj: Any, config_dict: dict) -> None:
    """Extract configuration from dictionary."""
    for key, value in config_dict.items():
        if not key.startswith("_"):
            setattr(config_obj, key, value)

_extract_from_dir(config_obj, source_config) staticmethod

Extract configuration using dir() approach as fallback.

Source code in aiagents4pharma/talk2scholars/tools/paper_download/paper_downloader.py
278
279
280
281
282
283
284
285
@staticmethod
def _extract_from_dir(config_obj: Any, source_config: Any) -> None:
    """Extract configuration using dir() approach as fallback."""
    for key in dir(source_config):
        if not key.startswith("_"):
            value = getattr(source_config, key)
            if not callable(value):
                setattr(config_obj, key, value)

_extract_from_items(config_obj, source_config) staticmethod

Extract configuration using items() method.

Source code in aiagents4pharma/talk2scholars/tools/paper_download/paper_downloader.py
271
272
273
274
275
276
@staticmethod
def _extract_from_items(config_obj: Any, source_config: Any) -> None:
    """Extract configuration using items() method."""
    for key, value in source_config.items():
        if isinstance(key, str):  # Type guard for key
            setattr(config_obj, key, value)

_extract_from_omegaconf(config_obj, source_config) staticmethod

Extract configuration from OmegaConf object.

Source code in aiagents4pharma/talk2scholars/tools/paper_download/paper_downloader.py
255
256
257
258
259
260
261
262
@staticmethod
def _extract_from_omegaconf(config_obj: Any, source_config: Any) -> None:
    """Extract configuration from OmegaConf object."""
    config_dict = OmegaConf.to_container(source_config, resolve=True)
    if isinstance(config_dict, dict):
        for key, value in config_dict.items():
            if isinstance(key, str):  # Type guard for key
                setattr(config_obj, key, value)

_get_unified_config() staticmethod

Load unified paper download configuration using Hydra with caching. This avoids the GlobalHydra reinitialization issue by caching the config.

Returns:

Type Description
Any

Unified configuration object

Source code in aiagents4pharma/talk2scholars/tools/paper_download/paper_downloader.py
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
@staticmethod
def _get_unified_config() -> Any:
    """
    Load unified paper download configuration using Hydra with caching.
    This avoids the GlobalHydra reinitialization issue by caching the config.

    Returns:
        Unified configuration object
    """
    # Return cached config if available
    if PaperDownloaderFactory._cached_config is not None:
        return PaperDownloaderFactory._cached_config

    # Ensure lock exists and get a local reference
    lock = PaperDownloaderFactory._config_lock
    if lock is None:
        lock = threading.Lock()
        PaperDownloaderFactory._config_lock = lock

    # Thread-safe config loading with guaranteed non-None lock
    with lock:
        # Double-check pattern - another thread might have loaded it
        if PaperDownloaderFactory._cached_config is not None:
            return PaperDownloaderFactory._cached_config

        try:

            # Clear if already initialized
            if GlobalHydra().is_initialized():
                logger.info(
                    "GlobalHydra already initialized, clearing for config load"
                )
                GlobalHydra.instance().clear()

            # Load configuration
            with hydra.initialize(version_base=None, config_path="../../configs"):
                cfg = hydra.compose(
                    config_name="config", overrides=["tools/paper_download=default"]
                )

            # Cache the configuration
            PaperDownloaderFactory._cached_config = cfg.tools.paper_download
            logger.info(
                "Successfully loaded and cached paper download configuration"
            )

            return PaperDownloaderFactory._cached_config

        except Exception as e:
            logger.error(
                "Failed to load unified paper download configuration: %s", e
            )
            raise RuntimeError(f"Configuration loading failed: {e}") from e

_try_config_extraction(config_obj, source_config) staticmethod

Try different methods to extract configuration data.

Source code in aiagents4pharma/talk2scholars/tools/paper_download/paper_downloader.py
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
@staticmethod
def _try_config_extraction(config_obj: Any, source_config: Any) -> None:
    """Try different methods to extract configuration data."""
    # Method 1: Try OmegaConf conversion
    if hasattr(source_config, "_content"):
        PaperDownloaderFactory._extract_from_omegaconf(config_obj, source_config)
        return

    # Method 2: Try direct attribute access
    if hasattr(source_config, "__dict__"):
        PaperDownloaderFactory._extract_from_dict(
            config_obj, source_config.__dict__
        )
        return

    # Method 3: Try items() method
    if hasattr(source_config, "items"):
        PaperDownloaderFactory._extract_from_items(config_obj, source_config)
        return

    # Method 4: Try dir() approach as fallback
    PaperDownloaderFactory._extract_from_dir(config_obj, source_config)

clear_cache() classmethod

Clear cached configuration.

Source code in aiagents4pharma/talk2scholars/tools/paper_download/paper_downloader.py
59
60
61
62
@classmethod
def clear_cache(cls) -> None:
    """Clear cached configuration."""
    cls._cached_config = None

create(service) staticmethod

Create appropriate downloader instance for the specified service.

Parameters:

Name Type Description Default
service Literal['arxiv', 'medrxiv', 'biorxiv', 'pubmed']

Service name ('arxiv', 'medrxiv', 'biorxiv', 'pubmed')

required

Returns:

Type Description
BasePaperDownloader

Configured downloader instance

Raises:

Type Description
ValueError

If service is not supported

Source code in aiagents4pharma/talk2scholars/tools/paper_download/paper_downloader.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
@staticmethod
def create(
    service: Literal["arxiv", "medrxiv", "biorxiv", "pubmed"],
) -> BasePaperDownloader:
    """
    Create appropriate downloader instance for the specified service.

    Args:
        service: Service name ('arxiv', 'medrxiv', 'biorxiv', 'pubmed')

    Returns:
        Configured downloader instance

    Raises:
        ValueError: If service is not supported
    """
    config = PaperDownloaderFactory._get_unified_config()
    service_config = PaperDownloaderFactory._build_service_config(config, service)

    if service == "arxiv":
        return ArxivDownloader(service_config)
    if service == "medrxiv":
        return MedrxivDownloader(service_config)
    if service == "biorxiv":
        return BiorxivDownloader(service_config)
    # service == "pubmed"
    return PubmedDownloader(service_config)

get_default_service() staticmethod

Get the default service from configuration.

Returns:

Type Description
Literal['arxiv', 'medrxiv', 'biorxiv', 'pubmed']

Default service name from config, fallback to 'pubmed'

Source code in aiagents4pharma/talk2scholars/tools/paper_download/paper_downloader.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
@staticmethod
def get_default_service() -> Literal["arxiv", "medrxiv", "biorxiv", "pubmed"]:
    """
    Get the default service from configuration.

    Returns:
        Default service name from config, fallback to 'pubmed'
    """
    config = PaperDownloaderFactory._get_unified_config()
    default_service = getattr(config.tool, "default_service", "pubmed")
    # Ensure the default service is valid and return with proper type
    if default_service == "arxiv":
        return "arxiv"
    if default_service == "medrxiv":
        return "medrxiv"
    if default_service == "biorxiv":
        return "biorxiv"
    if default_service == "pubmed":
        return "pubmed"
    logger.warning(
        "Invalid default service '%s' in config, falling back to 'pubmed'",
        default_service,
    )
    return "pubmed"

UnifiedPaperDownloadInput

Bases: BaseModel

Input schema for the unified paper download tool.

Source code in aiagents4pharma/talk2scholars/tools/paper_download/paper_downloader.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class UnifiedPaperDownloadInput(BaseModel):
    """Input schema for the unified paper download tool."""

    service: Optional[Literal["arxiv", "medrxiv", "biorxiv", "pubmed"]] = Field(
        default=None,
        description=(
            "Paper service to download from: 'arxiv', 'medrxiv', 'biorxiv', or 'pubmed'. "
            "If not specified, uses the configured default service."
        ),
    )
    identifiers: List[str] = Field(
        description=(
            "List of paper identifiers. Format depends on service:\n"
            "- arxiv: arXiv IDs (e.g., ['1234.5678', '2301.12345'])\n"
            "- medrxiv: DOIs (e.g., ['10.1101/2020.09.09.20191205'])\n"
            "- biorxiv: DOIs (e.g., ['10.1101/2020.09.09.20191205'])\n"
            "- pubmed: PMIDs (e.g., ['12345678', '87654321'])"
        )
    )
    tool_call_id: Annotated[str, InjectedToolCallId]

_download_papers_impl(service, identifiers, tool_call_id)

Internal implementation function that contains the actual download logic. This is called by both the decorated tool and the convenience functions.

Source code in aiagents4pharma/talk2scholars/tools/paper_download/paper_downloader.py
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
def _download_papers_impl(
    service: Optional[Literal["arxiv", "medrxiv", "biorxiv", "pubmed"]],
    identifiers: List[str],
    tool_call_id: str,
) -> Command[Any]:
    """
    Internal implementation function that contains the actual download logic.
    This is called by both the decorated tool and the convenience functions.
    """
    # Resolve default service if not specified
    if service is None:
        service = PaperDownloaderFactory.get_default_service()
        logger.info("No service specified, using configured default: %s", service)
    logger.info(
        "Starting unified paper download for service '%s' with %d identifiers: %s",
        service,
        len(identifiers),
        identifiers,
    )

    try:
        # Step 1: Create appropriate downloader using factory
        downloader = PaperDownloaderFactory.create(service)
        logger.info("Created %s downloader successfully", downloader.get_service_name())

        # Step 2: Process all identifiers
        article_data = downloader.process_identifiers(identifiers)

        # Step 3: Build summary for user
        content = downloader.build_summary(article_data)

        # Step 4: Log results summary
        total_papers = len(article_data)
        successful_downloads = sum(
            1
            for paper in article_data.values()
            if paper.get("access_type") == "open_access_downloaded"
        )
        logger.info(
            "Download complete for %s: %d papers processed, %d PDFs downloaded",
            service,
            total_papers,
            successful_downloads,
        )

        # Step 5: Return command with results
        return Command(
            update={
                "article_data": article_data,
                "messages": [
                    ToolMessage(
                        content=content,
                        tool_call_id=tool_call_id,
                        artifact=article_data,
                    )
                ],
            }
        )

    except ValueError as e:
        # Handle service/configuration errors
        error_msg = f"Service error for '{service}': {str(e)}"
        logger.error(error_msg)

        return Command(
            update={
                "article_data": {},
                "messages": [
                    ToolMessage(
                        content=f"Error: {error_msg}",
                        tool_call_id=tool_call_id,
                        artifact={},
                    )
                ],
            }
        )

    except Exception as e:  # pylint: disable=broad-exception-caught
        # Handle unexpected errors
        error_msg = f"Unexpected error during paper download: {str(e)}"
        logger.error(error_msg, exc_info=True)

        return Command(
            update={
                "article_data": {},
                "messages": [
                    ToolMessage(
                        content=f"Error: {error_msg}",
                        tool_call_id=tool_call_id,
                        artifact={},
                    )
                ],
            }
        )

download_arxiv_papers(arxiv_ids, tool_call_id)

Convenience function for downloading arXiv papers (explicitly uses arXiv service).

Source code in aiagents4pharma/talk2scholars/tools/paper_download/paper_downloader.py
336
337
338
339
340
def download_arxiv_papers(
    arxiv_ids: List[str], tool_call_id: Annotated[str, InjectedToolCallId]
) -> Command[Any]:
    """Convenience function for downloading arXiv papers (explicitly uses arXiv service)."""
    return _download_papers_impl("arxiv", arxiv_ids, tool_call_id)

download_biorxiv_papers(dois, tool_call_id)

Convenience function for downloading bioRxiv papers (explicitly uses bioRxiv service).

Source code in aiagents4pharma/talk2scholars/tools/paper_download/paper_downloader.py
350
351
352
353
354
def download_biorxiv_papers(
    dois: List[str], tool_call_id: Annotated[str, InjectedToolCallId]
) -> Command[Any]:
    """Convenience function for downloading bioRxiv papers (explicitly uses bioRxiv service)."""
    return _download_papers_impl("biorxiv", dois, tool_call_id)

download_medrxiv_papers(dois, tool_call_id)

Convenience function for downloading medRxiv papers (explicitly uses medRxiv service).

Source code in aiagents4pharma/talk2scholars/tools/paper_download/paper_downloader.py
343
344
345
346
347
def download_medrxiv_papers(
    dois: List[str], tool_call_id: Annotated[str, InjectedToolCallId]
) -> Command[Any]:
    """Convenience function for downloading medRxiv papers (explicitly uses medRxiv service)."""
    return _download_papers_impl("medrxiv", dois, tool_call_id)

download_papers(service, identifiers, tool_call_id)

Universal paper download tool supporting multiple academic paper services.

Downloads paper metadata and PDFs from arXiv, medRxiv, bioRxiv, or PubMed and stores them in temporary files for further processing. The downloaded PDFs can be accessed using the temp_file_path in the returned metadata.

Parameters:

Name Type Description Default
service Optional[Literal['arxiv', 'medrxiv', 'biorxiv', 'pubmed']]

Paper service to download from (optional, uses configured default if not specified) - 'arxiv': For arXiv preprints (requires arXiv IDs) - 'medrxiv': For medRxiv preprints (requires DOIs) - 'biorxiv': For bioRxiv preprints (requires DOIs) - 'pubmed': For PubMed papers (requires PMIDs)

required
identifiers List[str]

List of paper identifiers in the format expected by the service

required

Returns:

Type Description
Command[Any]

Command with article_data containing paper metadata and local file paths

Examples:

Download from arXiv

download_papers("arxiv", ["1234.5678", "2301.12345"])

Download from medRxiv

download_papers("medrxiv", ["10.1101/2020.09.09.20191205"])

Download from bioRxiv

download_papers("biorxiv", ["10.1101/2020.09.09.20191205"])

Download from PubMed

download_papers("pubmed", ["12345678", "87654321"])

Use default service (configured in default.yaml)

download_papers(None, ["12345678", "87654321"])

Source code in aiagents4pharma/talk2scholars/tools/paper_download/paper_downloader.py
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
@tool(
    args_schema=UnifiedPaperDownloadInput,
    parse_docstring=True,
)
def download_papers(
    service: Optional[Literal["arxiv", "medrxiv", "biorxiv", "pubmed"]],
    identifiers: List[str],
    tool_call_id: Annotated[str, InjectedToolCallId],
) -> Command[Any]:
    """
    Universal paper download tool supporting multiple academic paper services.

    Downloads paper metadata and PDFs from arXiv, medRxiv, bioRxiv, or PubMed and stores them
    in temporary files for further processing. The downloaded PDFs can be accessed
    using the temp_file_path in the returned metadata.

    Args:
        service: Paper service to download from (optional, uses configured default if not specified)
            - 'arxiv': For arXiv preprints (requires arXiv IDs)
            - 'medrxiv': For medRxiv preprints (requires DOIs)
            - 'biorxiv': For bioRxiv preprints (requires DOIs)
            - 'pubmed': For PubMed papers (requires PMIDs)
        identifiers: List of paper identifiers in the format expected by the service

    Returns:
        Command with article_data containing paper metadata and local file paths

    Examples:
        # Download from arXiv
        download_papers("arxiv", ["1234.5678", "2301.12345"])

        # Download from medRxiv
        download_papers("medrxiv", ["10.1101/2020.09.09.20191205"])

        # Download from bioRxiv
        download_papers("biorxiv", ["10.1101/2020.09.09.20191205"])

        # Download from PubMed
        download_papers("pubmed", ["12345678", "87654321"])

        # Use default service (configured in default.yaml)
        download_papers(None, ["12345678", "87654321"])
    """
    return _download_papers_impl(service, identifiers, tool_call_id)

download_pubmed_papers(pmids, tool_call_id)

Convenience function for downloading PubMed papers (explicitly uses PubMed service).

Source code in aiagents4pharma/talk2scholars/tools/paper_download/paper_downloader.py
357
358
359
360
361
def download_pubmed_papers(
    pmids: List[str], tool_call_id: Annotated[str, InjectedToolCallId]
) -> Command[Any]:
    """Convenience function for downloading PubMed papers (explicitly uses PubMed service)."""
    return _download_papers_impl("pubmed", pmids, tool_call_id)