update app_chatgpt

This commit is contained in:
Chill
2023-02-22 12:24:48 +08:00
parent 4a7d48a726
commit c4c0ac4557

View File

@@ -82,17 +82,19 @@ class Channel(models.Model):
# print(f'rdata:{rdata}') # print(f'rdata:{rdata}')
chatgpt_channel_id = self.env.ref('app_chatgpt.channel_chatgpt') chatgpt_channel_id = self.env.ref('app_chatgpt.channel_chatgpt')
user_chatgpt = self.env.ref("app_chatgpt.user_chatgpt") user_chatgpt = False
partner_chatgpt = self.env.ref("app_chatgpt.partner_chatgpt") partner_chatgpt = self.env['res.partner']
chatgpt_name = ''
author_id = msg_vals.get('author_id') author_id = msg_vals.get('author_id')
# print('author_id:',author_id) # print('author_id:',author_id)
gpt_id = self.env['gpt.robot'] gpt_id = self.env['gpt.robot']
# todo: 应该先确定 gpt_id才处理对话黑名单。 黑名单是指是否允许与 gpt对话不是是否允许绑定 gpt # 应该先确定 gpt_id才处理对话黑名单。 黑名单是指是否允许与 gpt对话不是是否允许绑定 gpt
# partner_ids = @ ids
partner_ids = list(msg_vals.get('partner_ids')) partner_ids = list(msg_vals.get('partner_ids'))
if partner_ids: if partner_ids:
partners = self.env['res.partner'].search([('id', 'in', partner_ids)]) partners = self.env['res.partner'].search([('id', 'in', partner_ids)])
# user_id = user has binded gpt robot
user_id = partners.mapped('user_ids').filtered(lambda r: r.gpt_id)[:1] user_id = partners.mapped('user_ids').filtered(lambda r: r.gpt_id)[:1]
if user_id: if user_id:
gpt_policy = user_id.gpt_policy gpt_policy = user_id.gpt_policy
@@ -104,14 +106,14 @@ class Channel(models.Model):
gpt_id = user_id.gpt_id gpt_id = user_id.gpt_id
# print('partner_chatgpt.id:',partner_chatgpt.id) # print('partner_chatgpt.id:',partner_chatgpt.id)
chatgpt_name = str(partner_chatgpt.name or '') + ', '
# print('chatgpt_name:', chatgpt_name)
prompt = msg_vals.get('body') prompt = msg_vals.get('body')
# print('prompt:', prompt) # print('prompt:', prompt)
# print('-----') # print('-----')
if not prompt: if not prompt:
return rdata return rdata
api_key = self.env['ir.config_parameter'].sudo().get_param('app_chatgpt.openapi_api_key') # api_key = self.env['ir.config_parameter'].sudo().get_param('app_chatgpt.openapi_api_key')
api_key = ''
if gpt_id: if gpt_id:
api_key = gpt_id.openapi_api_key api_key = gpt_id.openapi_api_key
try: try:
@@ -120,49 +122,44 @@ class Channel(models.Model):
openapi_context_timeout = 600 openapi_context_timeout = 600
openai.api_key = api_key openai.api_key = api_key
Partner = self.env['res.partner']
partner_name = '' partner_name = ''
if author_id:
partner_id = Partner.browse(author_id)
if partner_id:
user_id = partner_id.user_ids[:1]
if user_id.gpt_id:
return rdata
partner_name = partner_id.name
# print(msg_vals) # print(msg_vals)
# print(msg_vals.get('record_name', '')) # print(msg_vals.get('record_name', ''))
# print('self.channel_type :',self.channel_type) # print('self.channel_type :',self.channel_type)
if author_id != partner_chatgpt.id and (chatgpt_name in msg_vals.get('record_name', '') or 'ChatGPT,' in msg_vals.get('record_name', '') ) and self.channel_type == 'chat': if partner_chatgpt:
_logger.info(f'私聊:author_id:{author_id},partner_chatgpt.id:{partner_chatgpt.id}') chatgpt_name = str(partner_chatgpt.name or '') + ', '
try: # print('chatgpt_name:', chatgpt_name)
channel = self.env[msg_vals.get('model')].browse(msg_vals.get('res_id')) if author_id != partner_chatgpt.id and (chatgpt_name in msg_vals.get('record_name', '') or 'ChatGPT,' in msg_vals.get('record_name', '') ) and self.channel_type == 'chat':
prompt = self.get_openai_context(channel.id, partner_chatgpt.id, prompt,openapi_context_timeout) _logger.info(f'私聊:author_id:{author_id},partner_chatgpt.id:{partner_chatgpt.id}')
print(prompt) try:
# res = self.get_chatgpt_answer(prompt,partner_name) channel = self.env[msg_vals.get('model')].browse(msg_vals.get('res_id'))
res = self.get_openai(api_key, prompt, partner_name) prompt = self.get_openai_context(channel.id, partner_chatgpt.id, prompt,openapi_context_timeout)
# print('res:',res) print(prompt)
# print('channel:',channel) # res = self.get_chatgpt_answer(prompt,partner_name)
channel.with_user(user_chatgpt).message_post(body=res, message_type='comment',subtype_xmlid='mail.mt_comment',parent_id=message.id) res = self.get_openai(api_key, prompt, partner_name)
# channel.with_user(user_chatgpt).message_post(body=res, message_type='notification', subtype_xmlid='mail.mt_comment') # print('res:',res)
# channel.sudo().message_post( # print('channel:',channel)
# body=res, channel.with_user(user_chatgpt).message_post(body=res, message_type='comment',subtype_xmlid='mail.mt_comment',parent_id=message.id)
# author_id=partner_chatgpt.id, # channel.with_user(user_chatgpt).message_post(body=res, message_type='notification', subtype_xmlid='mail.mt_comment')
# message_type="comment", # channel.sudo().message_post(
# subtype_xmlid="mail.mt_comment", # body=res,
# ) # author_id=partner_chatgpt.id,
# self.with_user(user_chatgpt).message_post(body=res, message_type='comment', subtype_xmlid='mail.mt_comment') # message_type="comment",
except Exception as e: # subtype_xmlid="mail.mt_comment",
raise UserError(_(e)) # )
# self.with_user(user_chatgpt).message_post(body=res, message_type='comment', subtype_xmlid='mail.mt_comment')
except Exception as e:
raise UserError(_(e))
elif author_id != partner_chatgpt.id and msg_vals.get('model', '') == 'mail.channel' and msg_vals.get('res_id', 0) == chatgpt_channel_id.id: elif author_id != partner_chatgpt.id and msg_vals.get('model', '') == 'mail.channel' and msg_vals.get('res_id', 0) == chatgpt_channel_id.id:
_logger.info(f'频道群聊:author_id:{author_id},partner_chatgpt.id:{partner_chatgpt.id}') _logger.info(f'频道群聊:author_id:{author_id},partner_chatgpt.id:{partner_chatgpt.id}')
try: try:
prompt = self.get_openai_context(chatgpt_channel_id.id, partner_chatgpt.id, prompt,openapi_context_timeout) prompt = self.get_openai_context(chatgpt_channel_id.id, partner_chatgpt.id, prompt,openapi_context_timeout)
# print(prompt) # print(prompt)
# res = self.get_chatgpt_answer(prompt, partner_name) # res = self.get_chatgpt_answer(prompt, partner_name)
res = self.get_openai(api_key, prompt, partner_name) res = self.get_openai(api_key, prompt, partner_name)
chatgpt_channel_id.with_user(user_chatgpt).message_post(body=res, message_type='comment', subtype_xmlid='mail.mt_comment',parent_id=message.id) chatgpt_channel_id.with_user(user_chatgpt).message_post(body=res, message_type='comment', subtype_xmlid='mail.mt_comment',parent_id=message.id)
except Exception as e: except Exception as e:
raise UserError(_(e)) raise UserError(_(e))
return rdata return rdata