add timeout

This commit is contained in:
ivan deng
2023-03-03 19:14:02 +08:00
parent 6a3c1ff6f5
commit 3d69fd85c4
2 changed files with 5 additions and 4 deletions

View File

@@ -4,7 +4,7 @@
<record id="config_openapi_context_timeout" model="ir.config_parameter"> <record id="config_openapi_context_timeout" model="ir.config_parameter">
<field name="key">app_chatgpt.openapi_context_timeout</field> <field name="key">app_chatgpt.openapi_context_timeout</field>
<field name="value">600</field> <field name="value">300</field>
</record> </record>
</data> </data>

View File

@@ -17,6 +17,7 @@ class Channel(models.Model):
@api.model @api.model
def get_openai(self, api_key, ai_model, data, user="Odoo"): def get_openai(self, api_key, ai_model, data, user="Odoo"):
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"} headers = {"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"}
R_TIMEOUT = 3
if ai_model == 'dall-e2': if ai_model == 'dall-e2':
# todo: 处理 图像引擎,主要是返回参数到聊天中 # todo: 处理 图像引擎,主要是返回参数到聊天中
@@ -40,7 +41,7 @@ class Channel(models.Model):
"user": user, "user": user,
"stop": ["Human:", "AI:"] "stop": ["Human:", "AI:"]
} }
response = requests.post("https://api.openai.com/v1/chat/completions", data=json.dumps(pdata), headers=headers) response = requests.post("https://api.openai.com/v1/chat/completions", data=json.dumps(pdata), headers=headers, timeout=R_TIMEOUT)
res = response.json() res = response.json()
if 'choices' in res: if 'choices' in res:
# for rec in res: # for rec in res:
@@ -59,7 +60,7 @@ class Channel(models.Model):
"user": user, "user": user,
"stop": ["Human:", "AI:"] "stop": ["Human:", "AI:"]
} }
response = requests.post("https://api.openai.com/v1/completions", data=json.dumps(pdata), headers=headers) response = requests.post("https://api.openai.com/v1/completions", data=json.dumps(pdata), headers=headers, timeout=R_TIMEOUT)
res = response.json() res = response.json()
if 'choices' in res: if 'choices' in res:
res = '\n'.join([x['text'] for x in res['choices']]) res = '\n'.join([x['text'] for x in res['choices']])
@@ -192,7 +193,7 @@ class Channel(models.Model):
res = res.replace('\n', '<br/>') res = res.replace('\n', '<br/>')
# print('res:',res) # print('res:',res)
# print('channel:',channel) # print('channel:',channel)
channel.with_user(user_id).message_post(body=res, message_type='comment',subtype_xmlid='mail.mt_comment',parent_id=message.id) channel.with_user(user_id).message_post(body=res, message_type='comment',subtype_xmlid='mail.mt_comment', parent_id=message.id)
# channel.with_user(user_chatgpt).message_post(body=res, message_type='notification', subtype_xmlid='mail.mt_comment') # channel.with_user(user_chatgpt).message_post(body=res, message_type='notification', subtype_xmlid='mail.mt_comment')
# channel.sudo().message_post( # channel.sudo().message_post(
# body=res, # body=res,