update app_chatgpt

This commit is contained in:
Chill
2023-02-22 15:26:33 +08:00
parent c4c0ac4557
commit 735e41d059
2 changed files with 37 additions and 27 deletions

View File

@@ -80,31 +80,41 @@ class Channel(models.Model):
def _notify_thread(self, message, msg_vals=False, **kwargs):
rdata = super(Channel, self)._notify_thread(message, msg_vals=msg_vals, **kwargs)
# print(f'rdata:{rdata}')
chatgpt_channel_id = self.env.ref('app_chatgpt.channel_chatgpt')
user_chatgpt = False
partner_chatgpt = self.env['res.partner']
chatgpt_name = ''
to_partner_id = self.env['res.partner']
user_id = self.env['res.users']
author_id = msg_vals.get('author_id')
# print('author_id:',author_id)
gpt_id = self.env['gpt.robot']
# 应该先确定 gpt_id才处理对话黑名单。 黑名单是指是否允许与 gpt对话不是是否允许绑定 gpt
# partner_ids = @ ids
partner_ids = list(msg_vals.get('partner_ids'))
if 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]
channel_type = self.channel_type
if channel_type == 'chat':
channel_partner_ids = self.channel_partner_ids
to_partner_id = channel_partner_ids - message.author_id
user_id = to_partner_id.mapped('user_ids').filtered(lambda r: r.gpt_id)[:1]
if user_id:
gpt_policy = user_id.gpt_policy
gpt_wl_users = user_id.gpt_wl_users
is_allow = message.create_uid.id in gpt_wl_users.ids
if gpt_policy == 'all' or (gpt_policy == 'limit' and is_allow):
user_chatgpt = user_id
partner_chatgpt = user_id.partner_id
gpt_id = user_id.gpt_id
elif channel_type in ['group', 'channel']:
# partner_ids = @ ids
partner_ids = list(msg_vals.get('partner_ids'))
if 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]
if user_id:
gpt_policy = user_id.gpt_policy
gpt_wl_users = user_id.gpt_wl_users
is_allow = message.create_uid.id in gpt_wl_users.ids
to_partner_id = user_id.partner_id
if gpt_policy == 'all' or (gpt_policy == 'limit' and is_allow):
gpt_id = user_id.gpt_id
chatgpt_channel_id = self.env.ref('app_chatgpt.channel_chatgpt')
# print('author_id:',author_id)
# print('partner_chatgpt.id:',partner_chatgpt.id)
prompt = msg_vals.get('body')
@@ -126,20 +136,21 @@ class Channel(models.Model):
# print(msg_vals)
# print(msg_vals.get('record_name', ''))
# print('self.channel_type :',self.channel_type)
if partner_chatgpt:
chatgpt_name = str(partner_chatgpt.name or '') + ', '
if gpt_id:
chatgpt_name = str(gpt_id.name or '') + ', '
# print('chatgpt_name:', chatgpt_name)
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':
_logger.info(f'私聊:author_id:{author_id},partner_chatgpt.id:{partner_chatgpt.id}')
# if author_id != to_partner_id.id and (chatgpt_name in msg_vals.get('record_name', '') or 'ChatGPT' in msg_vals.get('record_name', '') ) and self.channel_type == 'chat':
if author_id != to_partner_id.id and self.channel_type == 'chat':
_logger.info(f'私聊:author_id:{author_id},partner_chatgpt.id:{to_partner_id.id}')
try:
channel = self.env[msg_vals.get('model')].browse(msg_vals.get('res_id'))
prompt = self.get_openai_context(channel.id, partner_chatgpt.id, prompt,openapi_context_timeout)
prompt = self.get_openai_context(channel.id, to_partner_id.id, prompt,openapi_context_timeout)
print(prompt)
# res = self.get_chatgpt_answer(prompt,partner_name)
res = self.get_openai(api_key, prompt, partner_name)
# print('res:',res)
# print('channel:',channel)
channel.with_user(user_chatgpt).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.sudo().message_post(
# body=res,
@@ -151,14 +162,14 @@ class Channel(models.Model):
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:
_logger.info(f'频道群聊:author_id:{author_id},partner_chatgpt.id:{partner_chatgpt.id}')
elif author_id != to_partner_id.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:{to_partner_id.id}')
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, to_partner_id.id, prompt,openapi_context_timeout)
# print(prompt)
# res = self.get_chatgpt_answer(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_id).message_post(body=res, message_type='comment', subtype_xmlid='mail.mt_comment',parent_id=message.id)
except Exception as e:
raise UserError(_(e))