paperminertoolkit 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- paperminertoolkit/__init__.py +10 -0
- paperminertoolkit/_version.py +11 -0
- paperminertoolkit/cli.py +1053 -0
- paperminertoolkit/corpus/__init__.py +1 -0
- paperminertoolkit/corpus/database.py +1542 -0
- paperminertoolkit/corpus/documents.py +313 -0
- paperminertoolkit/corpus/filtering.py +1390 -0
- paperminertoolkit/corpus/metadata.py +532 -0
- paperminertoolkit/extraction/__init__.py +1 -0
- paperminertoolkit/extraction/compression.py +486 -0
- paperminertoolkit/extraction/extract.py +573 -0
- paperminertoolkit/extraction/models.py +784 -0
- paperminertoolkit/extraction/recipes.py +241 -0
- paperminertoolkit/extraction/scrape.py +737 -0
- paperminertoolkit/extraction/store.py +244 -0
- paperminertoolkit/extraction/tokenizer.py +313 -0
- paperminertoolkit/providers/__init__.py +1 -0
- paperminertoolkit/providers/arxiv.py +760 -0
- paperminertoolkit/providers/base.py +514 -0
- paperminertoolkit/providers/biorxiv.py +561 -0
- paperminertoolkit/providers/chemrxiv.py +1115 -0
- paperminertoolkit/providers/core.py +423 -0
- paperminertoolkit/providers/crossref.py +649 -0
- paperminertoolkit/providers/elsevier.py +455 -0
- paperminertoolkit/providers/medrxiv.py +548 -0
- paperminertoolkit/providers/openalex.py +471 -0
- paperminertoolkit/providers/pubmed.py +1272 -0
- paperminertoolkit/providers/registry.py +521 -0
- paperminertoolkit/providers/rxiv.py +936 -0
- paperminertoolkit/providers/unpaywall.py +162 -0
- paperminertoolkit/resources/recipes.json +1670 -0
- paperminertoolkit/settings.py +661 -0
- paperminertoolkit/workflows/__init__.py +1 -0
- paperminertoolkit/workflows/download.py +1748 -0
- paperminertoolkit/workflows/enrichment.py +1906 -0
- paperminertoolkit/workflows/imports.py +121 -0
- paperminertoolkit/workflows/search.py +877 -0
- paperminertoolkit/workflows/topics.py +2837 -0
- paperminertoolkit/workflows/utilities.py +73 -0
- paperminertoolkit-1.0.0.dist-info/METADATA +126 -0
- paperminertoolkit-1.0.0.dist-info/RECORD +45 -0
- paperminertoolkit-1.0.0.dist-info/WHEEL +5 -0
- paperminertoolkit-1.0.0.dist-info/entry_points.txt +2 -0
- paperminertoolkit-1.0.0.dist-info/licenses/LICENSE +21 -0
- paperminertoolkit-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,661 @@
|
|
|
1
|
+
"""Load, save, and update PaperMinerToolkit API and model configuration.
|
|
2
|
+
|
|
3
|
+
Settings are read from the user config file and environment variables. This
|
|
4
|
+
module also provides interactive command helpers for storing API keys and model
|
|
5
|
+
profiles used by search, download, and extraction workflows.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from collections.abc import Mapping
|
|
11
|
+
import json
|
|
12
|
+
import openai
|
|
13
|
+
import os
|
|
14
|
+
import requests
|
|
15
|
+
from copy import deepcopy
|
|
16
|
+
from typing import Any, Literal
|
|
17
|
+
|
|
18
|
+
SETTINGS_FILE = os.path.join(os.path.expanduser('~'), '.config', '.paperminertoolkitrc.json')
|
|
19
|
+
DEFAULT_MODEL = 'gpt-5.4-mini'
|
|
20
|
+
DEFAULT_TEMPERATURE = 0
|
|
21
|
+
DEFAULT_TOP_P = 1
|
|
22
|
+
DEFAULT_INPUT_TOKEN_LIMIT = 32000
|
|
23
|
+
DEFAULT_MODEL_PROFILE = {
|
|
24
|
+
'provider': 'openai',
|
|
25
|
+
'model': DEFAULT_MODEL,
|
|
26
|
+
'base_url': None,
|
|
27
|
+
'api_key': None,
|
|
28
|
+
'capabilities': ['text'],
|
|
29
|
+
'temperature': DEFAULT_TEMPERATURE,
|
|
30
|
+
'top_p': DEFAULT_TOP_P,
|
|
31
|
+
'input_token_limit': DEFAULT_INPUT_TOKEN_LIMIT,
|
|
32
|
+
}
|
|
33
|
+
DEFAULT_SETTINGS = {
|
|
34
|
+
'model_profiles': {
|
|
35
|
+
'text': DEFAULT_MODEL_PROFILE.copy(),
|
|
36
|
+
'vision': {
|
|
37
|
+
**DEFAULT_MODEL_PROFILE,
|
|
38
|
+
'capabilities': ['text', 'vision'],
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _float_setting(value: float | int | str | None, default: float) -> float:
|
|
45
|
+
"""Normalize an optional floating-point setting.
|
|
46
|
+
|
|
47
|
+
Parameters
|
|
48
|
+
----------
|
|
49
|
+
value : float | int | str | None
|
|
50
|
+
Configured value. ``None`` and an empty string select the default.
|
|
51
|
+
default : float
|
|
52
|
+
Value returned when the configured value is missing.
|
|
53
|
+
|
|
54
|
+
Returns
|
|
55
|
+
-------
|
|
56
|
+
float
|
|
57
|
+
The default or the configured value converted to ``float``.
|
|
58
|
+
|
|
59
|
+
Raises
|
|
60
|
+
------
|
|
61
|
+
ValueError
|
|
62
|
+
If a non-empty value cannot be converted to ``float``.
|
|
63
|
+
"""
|
|
64
|
+
if value is None or value == '':
|
|
65
|
+
return default
|
|
66
|
+
return float(value)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _capabilities(value: str | list[str] | None, default: list[str] | None = None) -> list[str]:
|
|
70
|
+
"""Normalize a model capability setting.
|
|
71
|
+
|
|
72
|
+
Parameters
|
|
73
|
+
----------
|
|
74
|
+
value : str | list[str] | None
|
|
75
|
+
Comma-separated capabilities, an existing list, or a missing value.
|
|
76
|
+
default : list[str] | None, optional
|
|
77
|
+
Capabilities used when ``value`` is missing. Text-only capability is
|
|
78
|
+
used when both inputs are missing.
|
|
79
|
+
|
|
80
|
+
Returns
|
|
81
|
+
-------
|
|
82
|
+
list[str]
|
|
83
|
+
Normalized capability names.
|
|
84
|
+
"""
|
|
85
|
+
if value is None or value == '':
|
|
86
|
+
return default or ['text']
|
|
87
|
+
if isinstance(value, str):
|
|
88
|
+
return [cap.strip() for cap in value.split(',') if cap.strip()]
|
|
89
|
+
return value
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _merge_profile(
|
|
93
|
+
default: Mapping[str, Any],
|
|
94
|
+
configured: Mapping[str, Any] | None,
|
|
95
|
+
) -> dict[str, Any]:
|
|
96
|
+
"""Merge and normalize a configured model profile.
|
|
97
|
+
|
|
98
|
+
Parameters
|
|
99
|
+
----------
|
|
100
|
+
default : Mapping[str, Any]
|
|
101
|
+
Default model profile values.
|
|
102
|
+
configured : Mapping[str, Any] | None
|
|
103
|
+
Stored or environment-derived overrides.
|
|
104
|
+
|
|
105
|
+
Returns
|
|
106
|
+
-------
|
|
107
|
+
dict[str, Any]
|
|
108
|
+
Merged profile with normalized capabilities, sampling values, and
|
|
109
|
+
input token limit.
|
|
110
|
+
"""
|
|
111
|
+
profile = default.copy()
|
|
112
|
+
profile.update(configured or {})
|
|
113
|
+
profile['capabilities'] = _capabilities(profile.get('capabilities'), default.get('capabilities'))
|
|
114
|
+
profile['temperature'] = _float_setting(profile.get('temperature'), DEFAULT_TEMPERATURE)
|
|
115
|
+
profile['top_p'] = _float_setting(profile.get('top_p'), DEFAULT_TOP_P)
|
|
116
|
+
input_token_limit = profile.get('input_token_limit')
|
|
117
|
+
if input_token_limit is None or input_token_limit == '':
|
|
118
|
+
input_token_limit = DEFAULT_INPUT_TOKEN_LIMIT
|
|
119
|
+
profile['input_token_limit'] = int(input_token_limit)
|
|
120
|
+
return profile
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _env_profile(prefix: str) -> dict[str, str]:
|
|
124
|
+
"""Collect model profile overrides from environment variables.
|
|
125
|
+
|
|
126
|
+
Parameters
|
|
127
|
+
----------
|
|
128
|
+
prefix : str
|
|
129
|
+
Environment variable prefix for the profile.
|
|
130
|
+
|
|
131
|
+
Returns
|
|
132
|
+
-------
|
|
133
|
+
dict[str, str]
|
|
134
|
+
Defined, non-empty environment overrides keyed by profile field.
|
|
135
|
+
"""
|
|
136
|
+
env = {
|
|
137
|
+
'provider': os.environ.get(f'{prefix}PROVIDER'),
|
|
138
|
+
'model': os.environ.get(f'{prefix}NAME'),
|
|
139
|
+
'base_url': os.environ.get(f'{prefix}BASE_URL'),
|
|
140
|
+
'api_key': os.environ.get(f'{prefix}API_KEY'),
|
|
141
|
+
'capabilities': os.environ.get(f'{prefix}CAPABILITIES'),
|
|
142
|
+
'temperature': os.environ.get(f'{prefix}TEMPERATURE'),
|
|
143
|
+
'top_p': os.environ.get(f'{prefix}TOP_P'),
|
|
144
|
+
'input_token_limit': os.environ.get(f'{prefix}INPUT_TOKEN_LIMIT'),
|
|
145
|
+
}
|
|
146
|
+
return {key: value for key, value in env.items() if value is not None and value != ''}
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def load_settings() -> dict[str, Any]:
|
|
150
|
+
"""Load effective PaperMinerToolkit settings.
|
|
151
|
+
|
|
152
|
+
Values from the user config file are merged with package defaults, then
|
|
153
|
+
overridden by supported environment variables.
|
|
154
|
+
|
|
155
|
+
Returns
|
|
156
|
+
-------
|
|
157
|
+
dict[str, Any]
|
|
158
|
+
Effective credentials and normalized text and vision model profiles.
|
|
159
|
+
|
|
160
|
+
Raises
|
|
161
|
+
------
|
|
162
|
+
RuntimeError
|
|
163
|
+
If the settings file exists but cannot be read or decoded.
|
|
164
|
+
ValueError
|
|
165
|
+
If a configured numeric profile value is invalid.
|
|
166
|
+
"""
|
|
167
|
+
try:
|
|
168
|
+
with open(SETTINGS_FILE, mode='r', encoding='utf-8') as json_file:
|
|
169
|
+
settings = json.load(json_file)
|
|
170
|
+
except FileNotFoundError:
|
|
171
|
+
settings = {}
|
|
172
|
+
except Exception as e:
|
|
173
|
+
raise RuntimeError(f'Error loading {SETTINGS_FILE}: {e}.') from e
|
|
174
|
+
|
|
175
|
+
merged = deepcopy(DEFAULT_SETTINGS)
|
|
176
|
+
for key in ['elsevier_api_key', 'core_api_key', 'unpaywall_email', 'openalex_api_key', 'openai_api_key',
|
|
177
|
+
'anthropic_api_key', 'crossref_email', 'ncbi_api_key', 'ncbi_email']:
|
|
178
|
+
if key in settings:
|
|
179
|
+
merged[key] = settings[key]
|
|
180
|
+
|
|
181
|
+
configured_profiles = settings.get('model_profiles', {})
|
|
182
|
+
for profile_name, default_profile in DEFAULT_SETTINGS['model_profiles'].items():
|
|
183
|
+
merged['model_profiles'][profile_name] = _merge_profile(default_profile, configured_profiles.get(profile_name))
|
|
184
|
+
|
|
185
|
+
elsevier_api_key = os.environ.get('ELSEVIER_API_KEY')
|
|
186
|
+
if elsevier_api_key:
|
|
187
|
+
merged['elsevier_api_key'] = elsevier_api_key
|
|
188
|
+
openai_api_key = os.environ.get('OPENAI_API_KEY')
|
|
189
|
+
if openai_api_key:
|
|
190
|
+
merged['openai_api_key'] = openai_api_key
|
|
191
|
+
anthropic_api_key = os.environ.get('ANTHROPIC_API_KEY')
|
|
192
|
+
if anthropic_api_key:
|
|
193
|
+
merged['anthropic_api_key'] = anthropic_api_key
|
|
194
|
+
core_api_key = os.environ.get('CORE_API_KEY')
|
|
195
|
+
if core_api_key:
|
|
196
|
+
merged['core_api_key'] = core_api_key
|
|
197
|
+
unpaywall_email = os.environ.get('UNPAYWALL_EMAIL')
|
|
198
|
+
if unpaywall_email:
|
|
199
|
+
merged['unpaywall_email'] = unpaywall_email
|
|
200
|
+
openalex_api_key = os.environ.get('OPENALEX_API_KEY')
|
|
201
|
+
if openalex_api_key:
|
|
202
|
+
merged['openalex_api_key'] = openalex_api_key
|
|
203
|
+
crossref_email = os.environ.get('CROSSREF_EMAIL')
|
|
204
|
+
if crossref_email:
|
|
205
|
+
merged['crossref_email'] = crossref_email
|
|
206
|
+
ncbi_api_key = os.environ.get('NCBI_API_KEY')
|
|
207
|
+
if ncbi_api_key:
|
|
208
|
+
merged['ncbi_api_key'] = ncbi_api_key
|
|
209
|
+
ncbi_email = os.environ.get('NCBI_EMAIL')
|
|
210
|
+
if ncbi_email:
|
|
211
|
+
merged['ncbi_email'] = ncbi_email
|
|
212
|
+
|
|
213
|
+
text_env = _env_profile('PAPERMINERTOOLKIT_MODEL_')
|
|
214
|
+
if text_env:
|
|
215
|
+
current = merged['model_profiles']['text'].copy()
|
|
216
|
+
current.update(text_env)
|
|
217
|
+
merged['model_profiles']['text'] = _merge_profile(DEFAULT_SETTINGS['model_profiles']['text'], current)
|
|
218
|
+
|
|
219
|
+
vision_env = _env_profile('PAPERMINERTOOLKIT_VISION_MODEL_')
|
|
220
|
+
if vision_env:
|
|
221
|
+
current = merged['model_profiles']['vision'].copy()
|
|
222
|
+
current.update(vision_env)
|
|
223
|
+
merged['model_profiles']['vision'] = _merge_profile(DEFAULT_SETTINGS['model_profiles']['vision'], current)
|
|
224
|
+
return merged
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def _save_settings(settings: dict[str, Any]) -> None:
|
|
228
|
+
"""Persist PaperMinerToolkit settings as JSON.
|
|
229
|
+
|
|
230
|
+
Parameters
|
|
231
|
+
----------
|
|
232
|
+
settings : dict[str, Any]
|
|
233
|
+
Settings to write to the user config file.
|
|
234
|
+
"""
|
|
235
|
+
os.makedirs(os.path.dirname(SETTINGS_FILE), exist_ok=True)
|
|
236
|
+
with open(SETTINGS_FILE, mode='w', encoding='utf-8') as json_file:
|
|
237
|
+
json.dump(settings, json_file, indent=2)
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def get_model_profile(profile: str) -> dict[str, Any]:
|
|
241
|
+
"""Load an effective model profile by name.
|
|
242
|
+
|
|
243
|
+
Parameters
|
|
244
|
+
----------
|
|
245
|
+
profile : str
|
|
246
|
+
Profile name, normally ``text`` or ``vision``.
|
|
247
|
+
|
|
248
|
+
Returns
|
|
249
|
+
-------
|
|
250
|
+
dict[str, Any]
|
|
251
|
+
Normalized model profile.
|
|
252
|
+
|
|
253
|
+
Raises
|
|
254
|
+
------
|
|
255
|
+
KeyError
|
|
256
|
+
If the named profile does not exist.
|
|
257
|
+
"""
|
|
258
|
+
settings = load_settings()
|
|
259
|
+
try:
|
|
260
|
+
return settings['model_profiles'][profile]
|
|
261
|
+
except KeyError as e:
|
|
262
|
+
raise KeyError(f'Model profile "{profile}" does not exist.') from e
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def infer_model_capabilities(profile: str, model: str) -> list[str]:
|
|
266
|
+
"""Infer model capabilities from the profile and model name.
|
|
267
|
+
|
|
268
|
+
Parameters
|
|
269
|
+
----------
|
|
270
|
+
profile : str
|
|
271
|
+
Model profile name.
|
|
272
|
+
model : str
|
|
273
|
+
Provider model identifier.
|
|
274
|
+
|
|
275
|
+
Returns
|
|
276
|
+
-------
|
|
277
|
+
list[str]
|
|
278
|
+
Text-only capability, or text and vision capabilities for a vision
|
|
279
|
+
profile or vision-like model name.
|
|
280
|
+
"""
|
|
281
|
+
model_lower = (model or '').lower()
|
|
282
|
+
vision_markers = ['vl',
|
|
283
|
+
'vision',
|
|
284
|
+
'omni',
|
|
285
|
+
'llava',
|
|
286
|
+
'pixtral',
|
|
287
|
+
'molmo',
|
|
288
|
+
'internvl',
|
|
289
|
+
'qwen2.5-vl',
|
|
290
|
+
'qwen2-vl',
|
|
291
|
+
'qwen3-vl']
|
|
292
|
+
if profile == 'vision' or any(marker in model_lower for marker in vision_markers):
|
|
293
|
+
return ['text', 'vision']
|
|
294
|
+
return ['text']
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def set_model_profile(
|
|
298
|
+
profile: str,
|
|
299
|
+
provider: str,
|
|
300
|
+
model: str,
|
|
301
|
+
base_url: str | None = None,
|
|
302
|
+
api_key: str | None = None,
|
|
303
|
+
capabilities: str | list[str] | None = None,
|
|
304
|
+
temperature: float = DEFAULT_TEMPERATURE,
|
|
305
|
+
top_p: float = DEFAULT_TOP_P,
|
|
306
|
+
input_token_limit: int = DEFAULT_INPUT_TOKEN_LIMIT,
|
|
307
|
+
) -> None:
|
|
308
|
+
"""Store a model profile for extraction runs.
|
|
309
|
+
|
|
310
|
+
Parameters
|
|
311
|
+
----------
|
|
312
|
+
profile : str
|
|
313
|
+
Profile name to create or replace.
|
|
314
|
+
provider : str
|
|
315
|
+
Model provider name.
|
|
316
|
+
model : str
|
|
317
|
+
Provider model identifier.
|
|
318
|
+
base_url : str | None, optional
|
|
319
|
+
Custom provider API base URL.
|
|
320
|
+
api_key : str | None, optional
|
|
321
|
+
Profile-specific provider credential.
|
|
322
|
+
capabilities : str | list[str] | None, optional
|
|
323
|
+
Explicit capabilities. Capabilities are inferred when omitted.
|
|
324
|
+
temperature : float, default=DEFAULT_TEMPERATURE
|
|
325
|
+
Sampling temperature.
|
|
326
|
+
top_p : float, default=DEFAULT_TOP_P
|
|
327
|
+
Nucleus sampling probability mass.
|
|
328
|
+
input_token_limit : int, default=DEFAULT_INPUT_TOKEN_LIMIT
|
|
329
|
+
Maximum model input tokens before chunking.
|
|
330
|
+
"""
|
|
331
|
+
settings = load_settings()
|
|
332
|
+
capabilities = _capabilities(capabilities, infer_model_capabilities(profile, model))
|
|
333
|
+
settings.setdefault('model_profiles', {})[profile] = {
|
|
334
|
+
'provider': provider,
|
|
335
|
+
'model': model,
|
|
336
|
+
'base_url': base_url,
|
|
337
|
+
'api_key': api_key,
|
|
338
|
+
'capabilities': capabilities,
|
|
339
|
+
'temperature': float(temperature),
|
|
340
|
+
'top_p': float(top_p),
|
|
341
|
+
'input_token_limit': int(input_token_limit),
|
|
342
|
+
}
|
|
343
|
+
_save_settings(settings)
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
def _check_openai_api_key(api_key: str) -> bool:
|
|
347
|
+
"""Validate an OpenAI API key against the models API.
|
|
348
|
+
|
|
349
|
+
Parameters
|
|
350
|
+
----------
|
|
351
|
+
api_key : str
|
|
352
|
+
OpenAI API key to validate.
|
|
353
|
+
|
|
354
|
+
Returns
|
|
355
|
+
-------
|
|
356
|
+
bool
|
|
357
|
+
``False`` for an authentication failure; ``True`` after a successful
|
|
358
|
+
model listing.
|
|
359
|
+
"""
|
|
360
|
+
client = openai.OpenAI(api_key=api_key)
|
|
361
|
+
try:
|
|
362
|
+
client.models.list()
|
|
363
|
+
except openai.AuthenticationError:
|
|
364
|
+
return False
|
|
365
|
+
else:
|
|
366
|
+
return True
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
def _mask_secret(value: object) -> str:
|
|
370
|
+
"""Mask a configured secret for display.
|
|
371
|
+
|
|
372
|
+
Parameters
|
|
373
|
+
----------
|
|
374
|
+
value : object
|
|
375
|
+
Secret value, or a false value when no secret is configured.
|
|
376
|
+
|
|
377
|
+
Returns
|
|
378
|
+
-------
|
|
379
|
+
str
|
|
380
|
+
``not set``, a fully masked short value, or the first and last four
|
|
381
|
+
characters of a longer value.
|
|
382
|
+
"""
|
|
383
|
+
if not value:
|
|
384
|
+
return 'not set'
|
|
385
|
+
value = str(value)
|
|
386
|
+
if len(value) <= 8:
|
|
387
|
+
return '********'
|
|
388
|
+
return f'{value[:4]}...{value[-4:]}'
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
def _show_current_setting(
|
|
392
|
+
settings: Mapping[str, Any],
|
|
393
|
+
key: str,
|
|
394
|
+
label: str,
|
|
395
|
+
secret: bool = True,
|
|
396
|
+
) -> None:
|
|
397
|
+
"""Print a configured value before prompting for its replacement.
|
|
398
|
+
|
|
399
|
+
Parameters
|
|
400
|
+
----------
|
|
401
|
+
settings : Mapping[str, Any]
|
|
402
|
+
Settings containing the value to display.
|
|
403
|
+
key : str
|
|
404
|
+
Settings key to read.
|
|
405
|
+
label : str
|
|
406
|
+
Human-readable label included in the prompt.
|
|
407
|
+
secret : bool, default=True
|
|
408
|
+
Whether to mask the configured value.
|
|
409
|
+
"""
|
|
410
|
+
value = settings.get(key)
|
|
411
|
+
if secret:
|
|
412
|
+
value = _mask_secret(value)
|
|
413
|
+
elif not value:
|
|
414
|
+
value = 'not set'
|
|
415
|
+
print(f'Current {label}: {value}')
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
def update_openai_key(settings: dict[str, Any] | Literal[True] = True) -> None:
|
|
419
|
+
"""Prompt for, validate, and save an OpenAI API key.
|
|
420
|
+
|
|
421
|
+
Parameters
|
|
422
|
+
----------
|
|
423
|
+
settings : dict[str, Any] or Literal[True], default=True
|
|
424
|
+
Settings mapping to update. A true value loads the current settings.
|
|
425
|
+
|
|
426
|
+
Raises
|
|
427
|
+
------
|
|
428
|
+
ValueError
|
|
429
|
+
If the entered key fails OpenAI authentication.
|
|
430
|
+
"""
|
|
431
|
+
if settings:
|
|
432
|
+
settings = load_settings()
|
|
433
|
+
_show_current_setting(settings, 'openai_api_key', 'OpenAI API key')
|
|
434
|
+
api_key = input('Enter OpenAI API key: ')
|
|
435
|
+
if _check_openai_api_key(api_key):
|
|
436
|
+
settings['openai_api_key'] = api_key
|
|
437
|
+
_save_settings(settings)
|
|
438
|
+
else:
|
|
439
|
+
raise ValueError('OpenAI API key is invalid.')
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
def update_anthropic_key(settings: dict[str, Any] | Literal[True] = True) -> None:
|
|
443
|
+
"""Prompt for and save an Anthropic API key.
|
|
444
|
+
|
|
445
|
+
Parameters
|
|
446
|
+
----------
|
|
447
|
+
settings : dict[str, Any] or Literal[True], default=True
|
|
448
|
+
Settings mapping to update. A true value loads the current settings.
|
|
449
|
+
"""
|
|
450
|
+
if settings:
|
|
451
|
+
settings = load_settings()
|
|
452
|
+
_show_current_setting(settings, 'anthropic_api_key', 'Anthropic API key')
|
|
453
|
+
api_key = input('Enter Anthropic API key: ')
|
|
454
|
+
settings['anthropic_api_key'] = api_key
|
|
455
|
+
_save_settings(settings)
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
def _check_elsevier_api_key(api_key: str) -> bool:
|
|
459
|
+
"""Validate an Elsevier API key with a minimal Scopus search.
|
|
460
|
+
|
|
461
|
+
Parameters
|
|
462
|
+
----------
|
|
463
|
+
api_key : str
|
|
464
|
+
Elsevier API key to validate.
|
|
465
|
+
|
|
466
|
+
Returns
|
|
467
|
+
-------
|
|
468
|
+
bool
|
|
469
|
+
``True`` when the request succeeds, otherwise ``False``.
|
|
470
|
+
"""
|
|
471
|
+
from paperminertoolkit.providers.elsevier import check_api_key
|
|
472
|
+
return check_api_key(api_key)
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
def update_elsevier_key(settings: dict[str, Any] | Literal[True] = True) -> None:
|
|
476
|
+
"""Prompt for, validate, and save an Elsevier API key.
|
|
477
|
+
|
|
478
|
+
Parameters
|
|
479
|
+
----------
|
|
480
|
+
settings : dict[str, Any] or Literal[True], default=True
|
|
481
|
+
Settings mapping to update. A true value loads the current settings.
|
|
482
|
+
|
|
483
|
+
Raises
|
|
484
|
+
------
|
|
485
|
+
ValueError
|
|
486
|
+
If the entered key fails Elsevier validation.
|
|
487
|
+
"""
|
|
488
|
+
if settings:
|
|
489
|
+
settings = load_settings()
|
|
490
|
+
_show_current_setting(settings, 'elsevier_api_key', 'Elsevier API key')
|
|
491
|
+
api_key = input('Enter Elsevier API key: ')
|
|
492
|
+
if _check_elsevier_api_key(api_key):
|
|
493
|
+
settings['elsevier_api_key'] = api_key
|
|
494
|
+
_save_settings(settings)
|
|
495
|
+
else:
|
|
496
|
+
raise ValueError('Elsevier API key is invalid.')
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
def update_core_key(settings: dict[str, Any] | Literal[True] = True) -> None:
|
|
500
|
+
"""Prompt for and save a CORE API key.
|
|
501
|
+
|
|
502
|
+
Parameters
|
|
503
|
+
----------
|
|
504
|
+
settings : dict[str, Any] or Literal[True], default=True
|
|
505
|
+
Settings mapping to update. A true value loads the current settings.
|
|
506
|
+
"""
|
|
507
|
+
if settings:
|
|
508
|
+
settings = load_settings()
|
|
509
|
+
_show_current_setting(settings, 'core_api_key', 'CORE API key')
|
|
510
|
+
api_key = input('Enter CORE API key: ')
|
|
511
|
+
settings['core_api_key'] = api_key
|
|
512
|
+
_save_settings(settings)
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
def update_unpaywall_email(settings: dict[str, Any] | Literal[True] = True) -> None:
|
|
516
|
+
"""Prompt for and save the email address sent to Unpaywall.
|
|
517
|
+
|
|
518
|
+
Parameters
|
|
519
|
+
----------
|
|
520
|
+
settings : dict[str, Any] or Literal[True], default=True
|
|
521
|
+
Settings mapping to update. A true value loads the current settings.
|
|
522
|
+
|
|
523
|
+
Raises
|
|
524
|
+
------
|
|
525
|
+
ValueError
|
|
526
|
+
If the entered value does not contain an ``@`` character.
|
|
527
|
+
"""
|
|
528
|
+
if settings:
|
|
529
|
+
settings = load_settings()
|
|
530
|
+
_show_current_setting(settings, 'unpaywall_email', 'Unpaywall email', secret=False)
|
|
531
|
+
email = input('Enter Unpaywall email: ').strip()
|
|
532
|
+
if '@' not in email:
|
|
533
|
+
raise ValueError('Unpaywall email must be a valid email address.')
|
|
534
|
+
settings['unpaywall_email'] = email
|
|
535
|
+
_save_settings(settings)
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
def update_crossref_email(settings: dict[str, Any] | Literal[True] = True) -> None:
|
|
539
|
+
"""Prompt for and save the contact email sent to Crossref.
|
|
540
|
+
|
|
541
|
+
Crossref asks that automated clients identify themselves with a contact
|
|
542
|
+
address. The same address is reused as the OpenAlex ``mailto`` parameter.
|
|
543
|
+
|
|
544
|
+
Parameters
|
|
545
|
+
----------
|
|
546
|
+
settings : dict[str, Any] or Literal[True], default=True
|
|
547
|
+
Settings mapping to update. A true value loads the current settings.
|
|
548
|
+
|
|
549
|
+
Raises
|
|
550
|
+
------
|
|
551
|
+
ValueError
|
|
552
|
+
If the entered value does not contain an ``@`` character.
|
|
553
|
+
"""
|
|
554
|
+
if settings:
|
|
555
|
+
settings = load_settings()
|
|
556
|
+
_show_current_setting(settings, 'crossref_email', 'Crossref email', secret=False)
|
|
557
|
+
email = input('Enter Crossref email: ').strip()
|
|
558
|
+
if '@' not in email:
|
|
559
|
+
raise ValueError('Crossref email must be a valid email address.')
|
|
560
|
+
settings['crossref_email'] = email
|
|
561
|
+
_save_settings(settings)
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
def update_ncbi_key(settings: dict[str, Any] | Literal[True] = True) -> None:
|
|
565
|
+
"""Prompt for and save an NCBI E-utilities API key.
|
|
566
|
+
|
|
567
|
+
The key is optional. PubMed and PMC serve unauthenticated clients at three
|
|
568
|
+
requests per second; a key raises that ceiling to ten.
|
|
569
|
+
|
|
570
|
+
Parameters
|
|
571
|
+
----------
|
|
572
|
+
settings : dict[str, Any] or Literal[True], default=True
|
|
573
|
+
Settings mapping to update. A true value loads the current settings.
|
|
574
|
+
"""
|
|
575
|
+
if settings:
|
|
576
|
+
settings = load_settings()
|
|
577
|
+
_show_current_setting(settings, 'ncbi_api_key', 'NCBI API key')
|
|
578
|
+
api_key = input('Enter NCBI API key: ')
|
|
579
|
+
settings['ncbi_api_key'] = api_key
|
|
580
|
+
_save_settings(settings)
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
def update_ncbi_email(settings: dict[str, Any] | Literal[True] = True) -> None:
|
|
584
|
+
"""Prompt for and save the contact email sent to NCBI E-utilities.
|
|
585
|
+
|
|
586
|
+
NCBI asks that automated clients identify themselves so it can warn a
|
|
587
|
+
contact before blocking an address. PaperMinerToolkit falls back to the Crossref
|
|
588
|
+
address when no NCBI-specific address is configured.
|
|
589
|
+
|
|
590
|
+
Parameters
|
|
591
|
+
----------
|
|
592
|
+
settings : dict[str, Any] or Literal[True], default=True
|
|
593
|
+
Settings mapping to update. A true value loads the current settings.
|
|
594
|
+
|
|
595
|
+
Raises
|
|
596
|
+
------
|
|
597
|
+
ValueError
|
|
598
|
+
If the entered value does not contain an ``@`` character.
|
|
599
|
+
"""
|
|
600
|
+
if settings:
|
|
601
|
+
settings = load_settings()
|
|
602
|
+
_show_current_setting(settings, 'ncbi_email', 'NCBI email', secret=False)
|
|
603
|
+
email = input('Enter NCBI email: ').strip()
|
|
604
|
+
if '@' not in email:
|
|
605
|
+
raise ValueError('NCBI email must be a valid email address.')
|
|
606
|
+
settings['ncbi_email'] = email
|
|
607
|
+
_save_settings(settings)
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
def _check_openalex_api_key(api_key: str) -> bool:
|
|
611
|
+
"""Check whether OpenAlex explicitly rejects an API key.
|
|
612
|
+
|
|
613
|
+
Parameters
|
|
614
|
+
----------
|
|
615
|
+
api_key : str
|
|
616
|
+
OpenAlex API key to validate.
|
|
617
|
+
|
|
618
|
+
Returns
|
|
619
|
+
-------
|
|
620
|
+
bool
|
|
621
|
+
``False`` only for an HTTP 401 response. Other statuses and connection
|
|
622
|
+
failures return ``True`` so transient outages do not block saving a
|
|
623
|
+
key.
|
|
624
|
+
"""
|
|
625
|
+
from paperminertoolkit.providers import openalex
|
|
626
|
+
|
|
627
|
+
try:
|
|
628
|
+
response = requests.get(openalex.RATE_LIMIT_URL,
|
|
629
|
+
params={'api_key': api_key},
|
|
630
|
+
headers=openalex.request_headers(),
|
|
631
|
+
timeout=30)
|
|
632
|
+
except requests.RequestException:
|
|
633
|
+
return True
|
|
634
|
+
return response.status_code != 401
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
def update_openalex_key(settings: dict[str, Any] | Literal[True] = True) -> None:
|
|
638
|
+
"""Prompt for, validate, and save an OpenAlex API key.
|
|
639
|
+
|
|
640
|
+
OpenAlex permits unauthenticated requests with a smaller daily credit
|
|
641
|
+
budget; a free API key increases that budget.
|
|
642
|
+
|
|
643
|
+
Parameters
|
|
644
|
+
----------
|
|
645
|
+
settings : dict[str, Any] or Literal[True], default=True
|
|
646
|
+
Settings mapping to update. A true value loads the current settings.
|
|
647
|
+
|
|
648
|
+
Raises
|
|
649
|
+
------
|
|
650
|
+
ValueError
|
|
651
|
+
If OpenAlex explicitly rejects the entered key.
|
|
652
|
+
"""
|
|
653
|
+
if settings:
|
|
654
|
+
settings = load_settings()
|
|
655
|
+
_show_current_setting(settings, 'openalex_api_key', 'OpenAlex API key')
|
|
656
|
+
api_key = input('Enter OpenAlex API key: ').strip()
|
|
657
|
+
if _check_openalex_api_key(api_key):
|
|
658
|
+
settings['openalex_api_key'] = api_key
|
|
659
|
+
_save_settings(settings)
|
|
660
|
+
else:
|
|
661
|
+
raise ValueError('OpenAlex API key is invalid.')
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Corpus discovery, acquisition, enrichment, and analysis workflows."""
|