Update api.py

Hi,

When the generate_text() is called by the API, there is no error catch in case of failure. 
This change allows to see the error message on the answer cell of the web app instead of a blank.

Useful when openai account is exceeding the quota or other types of error.

MA
pull/81/head
Marc Trevoux 12 months ago committed by GitHub
parent 32d9f880a8
commit 185460d279
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -110,16 +110,19 @@ def load_recommender(path, start_page=1):
def generate_text(openAI_key, prompt, engine="text-davinci-003"):
openai.api_key = openAI_key
completions = openai.Completion.create(
engine=engine,
prompt=prompt,
max_tokens=512,
n=1,
stop=None,
temperature=0.7,
)
message = completions.choices[0].text
return message
try:
completions = openai.Completion.create(
engine=engine,
prompt=prompt,
max_tokens=512,
n=1,
stop=None,
temperature=0.7,
)
message = completions.choices[0].text
except Exception as e:
message = f'API Error: {str(e)}'
return message
def generate_answer(question, openAI_key):

Loading…
Cancel
Save