From 16a2ddcd58a626491f23a78f8f971c0a2bc820a9 Mon Sep 17 00:00:00 2001 From: Chill Date: Mon, 15 May 2023 15:42:24 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix=20#I7220J=20[app=5Fchatgpt]=E5=8F=AA?= =?UTF-8?q?=E5=AF=B9=E6=8C=87=E5=AE=9A=E7=94=A8=E6=88=B7=E5=BC=80=E6=94=BE?= =?UTF-8?q?=E7=AD=89=E6=A0=A1=E9=AA=8C=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=94=B9?= =?UTF-8?q?=E5=88=B0get=5Fai=5Fpre=E4=B8=AD=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app_chatgpt/models/ai_robot.py | 8 ++++++++ app_chatgpt/models/mail_channel.py | 21 +++++++++++---------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/app_chatgpt/models/ai_robot.py b/app_chatgpt/models/ai_robot.py index 883479bf..9667413f 100644 --- a/app_chatgpt/models/ai_robot.py +++ b/app_chatgpt/models/ai_robot.py @@ -117,6 +117,14 @@ GPT-3 A set of models that can understand and generate natural language if sensi is not None: _logger.error('==========敏感词:%s' % sensi['Keyword']) return _('温馨提示:您发送的内容含有敏感词,请修改内容后再向我发送。') + elif not author_id.gpt_id and answer_id.gpt_id: + user_id = answer_id.user_ids[:1] + gpt_policy = user_id.gpt_policy + gpt_wl_partners = user_id.gpt_wl_partners + is_allow = author_id.id in gpt_wl_partners.ids + if gpt_policy != 'all' and not is_allow: + # 暂时有限用户的Ai + return _('此Ai暂时未开放,请联系管理员。') else: return False diff --git a/app_chatgpt/models/mail_channel.py b/app_chatgpt/models/mail_channel.py index 8689a069..7d5fc297 100644 --- a/app_chatgpt/models/mail_channel.py +++ b/app_chatgpt/models/mail_channel.py @@ -139,16 +139,17 @@ class Channel(models.Model): partners = self.channel_partner_ids.sudo().filtered(lambda r: r.gpt_id)[:1] user_id = partners.mapped('user_ids')[:1] if user_id: - # todo: 此处理不判断,将此处逻辑迁移至 get_ai_pre, 非ai回复的直接内容注意设置为 is_ai=false - gpt_policy = user_id.gpt_policy - gpt_wl_partners = user_id.gpt_wl_partners - is_allow = message.author_id.id in gpt_wl_partners.ids - answer_id = user_id.partner_id - if gpt_policy == 'all' or (gpt_policy == 'limit' and is_allow): - ai = user_id.sudo().gpt_id - elif user_id.gpt_id and not is_allow: - # 暂时有限用户的Ai - raise UserError(_('此Ai暂时未开放,请联系管理员。')) + ai = user_id.sudo().gpt_id + # 此处理不判断,将此处逻辑迁移至 get_ai_pre, 非ai回复的直接内容注意设置为 is_ai=false + # gpt_policy = user_id.gpt_policy + # gpt_wl_partners = user_id.gpt_wl_partners + # is_allow = message.author_id.id in gpt_wl_partners.ids + # answer_id = user_id.partner_id + # if gpt_policy == 'all' or (gpt_policy == 'limit' and is_allow): + # ai = user_id.sudo().gpt_id + # elif user_id.gpt_id and not is_allow: + # # 暂时有限用户的Ai + # raise UserError(_('此Ai暂时未开放,请联系管理员。')) chatgpt_channel_id = self.env.ref('app_chatgpt.channel_chatgpt') From 2d8f4baf76def077ee9d13aeb4515fd548be001a Mon Sep 17 00:00:00 2001 From: Chill Date: Mon, 15 May 2023 16:20:13 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix=20#I6YOA1=20[app=5Fchatgpt]=20=E5=8F=91?= =?UTF-8?q?=E9=80=81prompt=E9=95=BF=E5=BA=A6=E7=9A=84=E9=99=90=E5=88=B6?= =?UTF-8?q?=EF=BC=8C=E5=9C=A8robot=E5=A2=9E=E5=8A=A0=20max=5Fsend=5Fchar?= =?UTF-8?q?=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app_chatgpt/models/ai_robot.py | 2 ++ app_chatgpt/models/mail_channel.py | 20 ++++++++++++++++---- app_chatgpt/views/ai_robot_views.xml | 2 ++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/app_chatgpt/models/ai_robot.py b/app_chatgpt/models/ai_robot.py index 9667413f..1716cec8 100644 --- a/app_chatgpt/models/ai_robot.py +++ b/app_chatgpt/models/ai_robot.py @@ -102,6 +102,8 @@ GPT-3 A set of models that can understand and generate natural language sensitive_words = fields.Text('Sensitive Words Plus', help='Sensitive word filtering. Separate keywords with a carriage return.') is_filtering = fields.Boolean('Filter Sensitive Words', default=False, help='Use base Filter in dir models/lib/sensi_words.txt') + max_send_char = fields.Integer('Max Send Char', help='Max Send Prompt Length', default=8000) + def action_disconnect(self): requests.delete('https://chatgpt.com/v1/disconnect') diff --git a/app_chatgpt/models/mail_channel.py b/app_chatgpt/models/mail_channel.py index 7d5fc297..a3ed3923 100644 --- a/app_chatgpt/models/mail_channel.py +++ b/app_chatgpt/models/mail_channel.py @@ -125,7 +125,7 @@ class Channel(models.Model): # 2个人的非私有频道不处理 pass else: - partners = self.channel_partner_ids.sudo().filtered(lambda r: r.gpt_id)[:1] + partners = self.channel_partner_ids.sudo().filtered(lambda r: r.gpt_id and r != message.author_id)[:1] user_id = partners.mapped('user_ids')[:1] elif not message.author_id.gpt_id: # 没有@时,默认第一个robot @@ -201,8 +201,19 @@ class Channel(models.Model): messages.append({"role": "user", "content": msg}) msg_len = sum(len(str(m)) for m in messages) # 接口最大接收 8430 Token - if msg_len * 2 >= 8000: - messages = [{"role": "user", "content": msg}] + if msg_len * 2 > ai.max_send_char: + messages = [] + if hasattr(channel, 'is_private') and channel.description: + messages.append({"role": "system", "content": channel.description}) + messages.append({"role": "user", "content": msg}) + msg_len = sum(len(str(m)) for m in messages) + if msg_len * 2 > ai.max_send_char: + new_msg = channel.with_user(user_id).message_post(body=_('您所发送的提示词已超长。'), message_type='comment', + subtype_xmlid='mail.mt_comment', + parent_id=message.id) + + # if msg_len * 2 >= 8000: + # messages = [{"role": "user", "content": msg}] if sync_config == 'sync': self.get_ai_response(ai, messages, channel, user_id, message) else: @@ -214,6 +225,7 @@ class Channel(models.Model): def _message_post_after_hook(self, message, msg_vals): if message.author_id.gpt_id: - if msg_vals['body'] not in [_('Response Timeout, please speak again.'), _('温馨提示:您发送的内容含有敏感词,请修改内容后再向我发送。')]: + if msg_vals['body'] not in [_('Response Timeout, please speak again.'), _('温馨提示:您发送的内容含有敏感词,请修改内容后再向我发送。'), + _('此Ai暂时未开放,请联系管理员。'), _('您所发送的提示词已超长。')]: message.is_ai = True return super(Channel, self)._message_post_after_hook(message, msg_vals) diff --git a/app_chatgpt/views/ai_robot_views.xml b/app_chatgpt/views/ai_robot_views.xml index 8c224aef..b6946bd8 100644 --- a/app_chatgpt/views/ai_robot_views.xml +++ b/app_chatgpt/views/ai_robot_views.xml @@ -12,6 +12,7 @@ + @@ -40,6 +41,7 @@ +