mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-24 07:35:18 +00:00
community : allow using apikey for PubMedAPIWrapper (#27246)
**Description**: > Without an API key, any site (IP address) posting more than 3 requests per second to the E-utilities will receive an error message. By including an API key, a site can post up to 10 requests per second by default. quoted from A General Introduction to the E-utilities,NCBI : https://www.ncbi.nlm.nih.gov/books/NBK25497/ I have simply added a api_key parameter to the PubMedAPIWrapper that can be used to increase the number of requests per second from 3 to 10. **Twitter handle** : @KORmaori --------- Co-authored-by: Erick Friis <erick@langchain.dev>
This commit is contained in:
parent
eff8a54756
commit
bc5ec63d67
@ -31,6 +31,7 @@ class PubMedAPIWrapper(BaseModel):
|
|||||||
sleep_time: time to wait between retries.
|
sleep_time: time to wait between retries.
|
||||||
Default is 0.2 seconds.
|
Default is 0.2 seconds.
|
||||||
email: email address to be used for the PubMed API.
|
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:
|
parse: Any #: :meta private:
|
||||||
@ -47,6 +48,7 @@ class PubMedAPIWrapper(BaseModel):
|
|||||||
MAX_QUERY_LENGTH: int = 300
|
MAX_QUERY_LENGTH: int = 300
|
||||||
doc_content_chars_max: int = 2000
|
doc_content_chars_max: int = 2000
|
||||||
email: str = "your_email@example.com"
|
email: str = "your_email@example.com"
|
||||||
|
api_key: str = ""
|
||||||
|
|
||||||
@model_validator(mode="before")
|
@model_validator(mode="before")
|
||||||
@classmethod
|
@classmethod
|
||||||
@ -101,6 +103,8 @@ class PubMedAPIWrapper(BaseModel):
|
|||||||
+ str({urllib.parse.quote(query)})
|
+ str({urllib.parse.quote(query)})
|
||||||
+ f"&retmode=json&retmax={self.top_k_results}&usehistory=y"
|
+ 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)
|
result = urllib.request.urlopen(url)
|
||||||
text = result.read().decode("utf-8")
|
text = result.read().decode("utf-8")
|
||||||
json_text = json.loads(text)
|
json_text = json.loads(text)
|
||||||
@ -135,6 +139,8 @@ class PubMedAPIWrapper(BaseModel):
|
|||||||
+ "&webenv="
|
+ "&webenv="
|
||||||
+ webenv
|
+ webenv
|
||||||
)
|
)
|
||||||
|
if self.api_key != "":
|
||||||
|
url += f"&api_key={self.api_key}"
|
||||||
|
|
||||||
retry = 0
|
retry = 0
|
||||||
while True:
|
while True:
|
||||||
|
Loading…
Reference in New Issue
Block a user