rxn4chemistry / biocatalysis-model

RXN for biochemical reactions
MIT License
61 stars 14 forks source link

'Too many requests' at api #4

Closed jachermocilla closed 1 year ago

jachermocilla commented 1 year ago

Hello. I tried the example using an API key but I get this error. May I know if the API endpoint is still supported. Thanks. image

drugilsberg commented 1 year ago

Hi @jachermocilla, the API is supporting this, but you most likely are hitting the rate limits. Can you share the code you are using?

jachermocilla commented 1 year ago

Hi @drugilsberg . Here's the code. It's basically the same as the one in the README.md. I commented a few lines. API key is not the actual shown here. Thanks.

api_key = 'apk-xxxxx'
from rxn4chemistry import RXN4ChemistryWrapper

rxn4chemistry_wrapper = RXN4ChemistryWrapper(api_key=api_key)

# NOTE: you can create a project or set an esiting one using:
# rxn4chemistry_wrapper.set_project('PROJECT_ID')
rxn4chemistry_wrapper.create_project('test_wrapper')
print(rxn4chemistry_wrapper.project_id)

response = rxn4chemistry_wrapper.predict_automatic_retrosynthesis(
    'OC1C(O)C=C(Br)C=C1', ai_model='enzymatic-2021-04-16'
)
#results = rxn4chemistry_wrapper.get_predict_automatic_retrosynthesis_results(
#    response['prediction_id']
#)

#print(results['status'])
print(response)

# NOTE: upon 'SUCCESS' you can inspect the predicted retrosynthetic paths.
#print(results['retrosynthetic_paths'][0])
drugilsberg commented 1 year ago

de. It's basically the same as the one in the README.md. I commented a few lines. API key is not the actual shown here. Thanks.

Thanks a lot for sharing this and sorry for the lack clarity, it's sufficient to put a couple of sleep here and there as the RXN API allow a maximum of 5 requests per minute and at least a second between consecutive calls. Here a suggested modification:

import time
api_key = 'apk-xxxxx'
from rxn4chemistry import RXN4ChemistryWrapper

rxn4chemistry_wrapper = RXN4ChemistryWrapper(api_key=api_key)

# NOTE: you can create a project or set an esiting one using:
# rxn4chemistry_wrapper.set_project('PROJECT_ID')
rxn4chemistry_wrapper.create_project('test_wrapper')
time.sleep(2)
print(rxn4chemistry_wrapper.project_id)

response = rxn4chemistry_wrapper.predict_automatic_retrosynthesis(
    'OC1C(O)C=C(Br)C=C1', ai_model='enzymatic-2021-04-16'
)
time.sleep(30) # retros on average takes 2 minutes so here a sleep of 30 seconds is reasonable
#results = rxn4chemistry_wrapper.get_predict_automatic_retrosynthesis_results(
#    response['prediction_id']
#)

#print(results['status'])
print(response)

# NOTE: upon 'SUCCESS' you can inspect the predicted retrosynthetic paths.
#print(results['retrosynthetic_paths'][0])
jachermocilla commented 1 year ago

Great!It's working now. Thanks for your help. Will close the issue now.

drugilsberg commented 1 year ago

Thanks to you, will also update the snippet.