Skip to content

The agent engineering platform.

community : allow using apikey for PubMedAPIWrapper (#27246)

peterdhpbc5ec63d6758
diff --git a/libs/community/langchain_community/utilities/pubmed.py b/libs/community/langchain_community/utilities/pubmed.py
index e3b23cfa0a..20185d0c28 100644
--- a/libs/community/langchain_community/utilities/pubmed.py
+++ b/libs/community/langchain_community/utilities/pubmed.py
@@ -31,6 +31,7 @@ class PubMedAPIWrapper(BaseModel):
sleep_time: time to wait between retries.
Default is 0.2 seconds.
email: email address to be used for the PubMed API.
+ api_key: API key to be used for the PubMed API.
"""
parse: Any #: :meta private:
@@ -47,6 +48,7 @@ class PubMedAPIWrapper(BaseModel):
MAX_QUERY_LENGTH: int = 300
doc_content_chars_max: int = 2000
email: str = "[email protected]"
+ api_key: str = ""
@model_validator(mode="before")
@classmethod
@@ -101,6 +103,8 @@ class PubMedAPIWrapper(BaseModel):
+ str({urllib.parse.quote(query)})
+ f"&retmode=json&retmax={self.top_k_results}&usehistory=y"
)
+ if self.api_key != "":
+ url += f"&api_key={self.api_key}"
result = urllib.request.urlopen(url)
text = result.read().decode("utf-8")
json_text = json.loads(text)
@@ -135,6 +139,8 @@ class PubMedAPIWrapper(BaseModel):
+ "&webenv="
+ webenv
)
+ if self.api_key != "":
+ url += f"&api_key={self.api_key}"
retry = 0
while True: