From c4c0ac4557fc0997385aac661f80879c0426f4b0 Mon Sep 17 00:00:00 2001 From: Chill Date: Wed, 22 Feb 2023 12:24:48 +0800 Subject: [PATCH] update app_chatgpt --- app_chatgpt/models/mail_channel.py | 89 +++++++++++++++--------------- 1 file changed, 43 insertions(+), 46 deletions(-) diff --git a/app_chatgpt/models/mail_channel.py b/app_chatgpt/models/mail_channel.py index 5def8390..c11c933a 100644 --- a/app_chatgpt/models/mail_channel.py +++ b/app_chatgpt/models/mail_channel.py @@ -82,17 +82,19 @@ class Channel(models.Model): # print(f'rdata:{rdata}') chatgpt_channel_id = self.env.ref('app_chatgpt.channel_chatgpt') - user_chatgpt = self.env.ref("app_chatgpt.user_chatgpt") - partner_chatgpt = self.env.ref("app_chatgpt.partner_chatgpt") - + user_chatgpt = False + partner_chatgpt = self.env['res.partner'] + chatgpt_name = '' author_id = msg_vals.get('author_id') # print('author_id:',author_id) 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')) 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 @@ -104,14 +106,14 @@ class Channel(models.Model): gpt_id = user_id.gpt_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') # print('prompt:', prompt) # print('-----') if not prompt: 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: api_key = gpt_id.openapi_api_key try: @@ -120,49 +122,44 @@ class Channel(models.Model): openapi_context_timeout = 600 openai.api_key = api_key - Partner = self.env['res.partner'] 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.get('record_name', '')) # 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': - _logger.info(f'私聊:author_id:{author_id},partner_chatgpt.id:{partner_chatgpt.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) - 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_chatgpt).message_post(body=res, message_type='notification', subtype_xmlid='mail.mt_comment') - # channel.sudo().message_post( - # body=res, - # author_id=partner_chatgpt.id, - # message_type="comment", - # subtype_xmlid="mail.mt_comment", - # ) - # self.with_user(user_chatgpt).message_post(body=res, message_type='comment', subtype_xmlid='mail.mt_comment') - except Exception as e: - raise UserError(_(e)) + if partner_chatgpt: + chatgpt_name = str(partner_chatgpt.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}') + 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) + 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_chatgpt).message_post(body=res, message_type='notification', subtype_xmlid='mail.mt_comment') + # channel.sudo().message_post( + # body=res, + # author_id=partner_chatgpt.id, + # message_type="comment", + # subtype_xmlid="mail.mt_comment", + # ) + # 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: - _logger.info(f'频道群聊:author_id:{author_id},partner_chatgpt.id:{partner_chatgpt.id}') - try: - prompt = self.get_openai_context(chatgpt_channel_id.id, partner_chatgpt.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) - 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}') + try: + prompt = self.get_openai_context(chatgpt_channel_id.id, partner_chatgpt.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) + except Exception as e: + raise UserError(_(e)) return rdata