David Chu commited on
feat: use semantic scholar api key
Browse files- app/config.py +1 -0
- app/tools/literature.py +8 -0
app/config.py
CHANGED
|
@@ -3,6 +3,7 @@ from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
| 3 |
|
| 4 |
class Settings(BaseSettings):
|
| 5 |
google_api_key: str
|
|
|
|
| 6 |
|
| 7 |
model_config = SettingsConfigDict(env_file=".env")
|
| 8 |
|
|
|
|
| 3 |
|
| 4 |
class Settings(BaseSettings):
|
| 5 |
google_api_key: str
|
| 6 |
+
semantic_scholar_api_key: str | None = None
|
| 7 |
|
| 8 |
model_config = SettingsConfigDict(env_file=".env")
|
| 9 |
|
app/tools/literature.py
CHANGED
|
@@ -3,6 +3,8 @@ from xml.etree import ElementTree
|
|
| 3 |
import httpx
|
| 4 |
from tenacity import retry, stop_after_attempt, wait_random_exponential
|
| 5 |
|
|
|
|
|
|
|
| 6 |
|
| 7 |
@retry(
|
| 8 |
stop=stop_after_attempt(10), wait=wait_random_exponential(multiplier=0.5, max=10)
|
|
@@ -10,6 +12,11 @@ from tenacity import retry, stop_after_attempt, wait_random_exponential
|
|
| 10 |
def search_semantic_scholar(
|
| 11 |
query: str, top_k: int = 20, min_citation_count: int = 5
|
| 12 |
) -> list[dict]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
resp = httpx.get(
|
| 14 |
"https://api.semanticscholar.org/graph/v1/paper/search",
|
| 15 |
params={
|
|
@@ -19,6 +26,7 @@ def search_semantic_scholar(
|
|
| 19 |
"fieldsOfStudy": "Medicine,Biology",
|
| 20 |
"minCitationCount": min_citation_count,
|
| 21 |
},
|
|
|
|
| 22 |
timeout=10.0,
|
| 23 |
)
|
| 24 |
resp.raise_for_status()
|
|
|
|
| 3 |
import httpx
|
| 4 |
from tenacity import retry, stop_after_attempt, wait_random_exponential
|
| 5 |
|
| 6 |
+
from app.config import settings
|
| 7 |
+
|
| 8 |
|
| 9 |
@retry(
|
| 10 |
stop=stop_after_attempt(10), wait=wait_random_exponential(multiplier=0.5, max=10)
|
|
|
|
| 12 |
def search_semantic_scholar(
|
| 13 |
query: str, top_k: int = 20, min_citation_count: int = 5
|
| 14 |
) -> list[dict]:
|
| 15 |
+
headers = {}
|
| 16 |
+
|
| 17 |
+
if api_key := settings.semantic_scholar_api_key:
|
| 18 |
+
headers["x-api-key"] = api_key
|
| 19 |
+
|
| 20 |
resp = httpx.get(
|
| 21 |
"https://api.semanticscholar.org/graph/v1/paper/search",
|
| 22 |
params={
|
|
|
|
| 26 |
"fieldsOfStudy": "Medicine,Biology",
|
| 27 |
"minCitationCount": min_citation_count,
|
| 28 |
},
|
| 29 |
+
headers=headers,
|
| 30 |
timeout=10.0,
|
| 31 |
)
|
| 32 |
resp.raise_for_status()
|