Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

community : allow using apikey for PubMedAPIWrapper #27246

Merged
merged 7 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added libs/community/file::memory:?cache=shared
Binary file not shown.
6 changes: 6 additions & 0 deletions libs/community/langchain_community/utilities/pubmed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -101,6 +103,8 @@ def lazy_load(self, query: str) -> Iterator[dict]:
+ 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)
Expand Down Expand Up @@ -135,6 +139,8 @@ def retrieve_article(self, uid: str, webenv: str) -> dict:
+ "&webenv="
+ webenv
)
if self.api_key != "":
url += f"&api_key={self.api_key}"

retry = 0
while True:
Expand Down