From c004ea5e8258381d94d1c16c58c3164c65ec176e Mon Sep 17 00:00:00 2001 From: Chill Date: Tue, 18 Apr 2023 13:50:23 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix=20#I6WMVK=20[app=5Fchatgpt]=20ChatGPT?= =?UTF-8?q?=20=E4=BD=BF=E7=94=A8=E6=83=85=E5=86=B5=EF=BC=8C=E5=AF=B9?= =?UTF-8?q?=E8=AF=9D=E6=9D=83=E9=99=90=E4=B8=BA=E6=89=80=E6=9C=89=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=97=B6=EF=BC=8C=E4=B9=9F=E8=A6=81=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app_chatgpt/models/ai_robot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app_chatgpt/models/ai_robot.py b/app_chatgpt/models/ai_robot.py index 421e6ba6..3586293e 100644 --- a/app_chatgpt/models/ai_robot.py +++ b/app_chatgpt/models/ai_robot.py @@ -141,7 +141,7 @@ GPT-3 A set of models that can understand and generate natural language content = json.loads(json.dumps(res['choices'][0]['message']['content'])) data = content.replace(' .', '.').strip() answer_user = answer_id.mapped('user_ids')[:1] - if answer_user.gpt_policy == 'limit' and usage: + if usage: prompt_tokens = usage['prompt_tokens'] completion_tokens = usage['completion_tokens'] total_tokens = usage['total_tokens'] @@ -151,6 +151,7 @@ GPT-3 A set of models that can understand and generate natural language if not ai_use: ai_use.create({ 'name': author_id.id, + 'ai_user_id': answer_user.id, 'human_prompt_tokens': prompt_tokens, 'ai_completion_tokens': completion_tokens, 'tokens_total': total_tokens, From 02ba6e7cbaee45912d81bbd4815859faf6859493 Mon Sep 17 00:00:00 2001 From: Chill Date: Tue, 18 Apr 2023 14:11:44 +0800 Subject: [PATCH 2/3] fix --- app_chatgpt/models/mail_channel.py | 1 - 1 file changed, 1 deletion(-) diff --git a/app_chatgpt/models/mail_channel.py b/app_chatgpt/models/mail_channel.py index 6edb9190..194016d9 100644 --- a/app_chatgpt/models/mail_channel.py +++ b/app_chatgpt/models/mail_channel.py @@ -173,7 +173,6 @@ class Channel(models.Model): messages.append({"role": "system", "content": channel.description}) try: - messages = [] c_history = self.get_openai_context(channel.id, author_id, answer_id, openapi_context_timeout, chat_count) if c_history: messages += c_history From 723ff14f6b2b6daa95ef2815ecd7ea8e26d9d257 Mon Sep 17 00:00:00 2001 From: Chill Date: Wed, 19 Apr 2023 18:50:48 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix=20#I6WUDK=20app=5Fchatgpt=E6=9D=83?= =?UTF-8?q?=E9=99=90=E8=A6=81=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app_chatgpt/__manifest__.py | 1 + app_chatgpt/models/mail_channel.py | 22 ++++++++-------- app_chatgpt/security/ir.model.access.csv | 5 ++-- app_chatgpt/security/ir_rules.xml | 16 ++++++++++++ .../views/res_partner_ai_use_views.xml | 26 ++++++++++++++----- 5 files changed, 51 insertions(+), 19 deletions(-) create mode 100644 app_chatgpt/security/ir_rules.xml diff --git a/app_chatgpt/__manifest__.py b/app_chatgpt/__manifest__.py index b4833787..6942c088 100644 --- a/app_chatgpt/__manifest__.py +++ b/app_chatgpt/__manifest__.py @@ -50,6 +50,7 @@ ], 'data': [ 'security/ir.model.access.csv', + 'security/ir_rules.xml', 'data/mail_channel_data.xml', 'data/ai_robot_data.xml', 'data/user_partner_data.xml', diff --git a/app_chatgpt/models/mail_channel.py b/app_chatgpt/models/mail_channel.py index 194016d9..a966d14c 100644 --- a/app_chatgpt/models/mail_channel.py +++ b/app_chatgpt/models/mail_channel.py @@ -39,8 +39,8 @@ class Channel(models.Model): ai_msg_list = message_model.with_context(tz='UTC').search(domain, order="id desc", limit=chat_count) for ai_msg in ai_msg_list: # 判断这个 ai_msg 是不是ai发,有才 insert。 判断 user_msg 是不是 user发的,有才 insert - user_msg = ai_msg.parent_id - if ai_msg.author_id.gpt_id: + user_msg = ai_msg.parent_id.sudo() + if ai_msg.author_id.sudo().gpt_id: ai_content = str(ai_msg.body).replace("

", "").replace("

", "").replace("

", "") context_history.insert(0, { 'role': 'assistant', @@ -74,7 +74,7 @@ class Channel(models.Model): answer_id = self.env['res.partner'] user_id = self.env['res.users'] author_id = msg_vals.get('author_id') - ai = self.env['ai.robot'] + ai = self.env['ai.robot'].sudo() channel = self.env['mail.channel'] channel_type = self.channel_type messages = [] @@ -86,13 +86,13 @@ class Channel(models.Model): if channel_type == 'chat': channel_partner_ids = self.channel_partner_ids answer_id = channel_partner_ids - message.author_id - user_id = answer_id.mapped('user_ids').filtered(lambda r: r.gpt_id)[:1] + user_id = answer_id.mapped('user_ids').sudo().filtered(lambda r: r.gpt_id)[:1] if user_id and answer_id.gpt_id: 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 if gpt_policy == 'all' or (gpt_policy == 'limit' and is_allow): - ai = answer_id.gpt_id + ai = answer_id.sudo().gpt_id elif channel_type in ['group', 'channel']: # partner_ids = @ ids @@ -101,10 +101,10 @@ class Channel(models.Model): # 常规群聊 @ partners = self.env['res.partner'].search([('id', 'in', partner_ids)]) # user_id = user, who has binded gpt robot - user_id = partners.mapped('user_ids').filtered(lambda r: r.gpt_id)[:1] + user_id = partners.mapped('user_ids').sudo().filtered(lambda r: r.gpt_id)[:1] elif message.body == _('

joined the channel
'): # 欢迎的情况 - partners = self.channel_partner_ids.filtered(lambda r: r.gpt_id)[:1] + partners = self.channel_partner_ids.sudo().filtered(lambda r: r.gpt_id)[:1] user_id = partners.mapped('user_ids')[:1] elif self.member_count == 2: # 处理独聊频道 @@ -112,7 +112,7 @@ class Channel(models.Model): # 2个人的非私有频道不处理 pass else: - partners = self.channel_partner_ids.filtered(lambda r: r.gpt_id)[:1] + partners = self.channel_partner_ids.sudo().filtered(lambda r: r.gpt_id)[:1] user_id = partners.mapped('user_ids')[:1] elif not message.author_id.gpt_id: # 没有@时,默认第一个robot @@ -120,9 +120,9 @@ class Channel(models.Model): # 临时用azure robot = self.env.ref('app_chatgpt.chatgpt3_azure') if robot: - user_id = self.env['res.users'].search([('gpt_id', '=', robot.id)], limit=1) + user_id = self.env['res.users'].sudo().search([('gpt_id', '=', robot.id)], limit=1) else: - partners = self.channel_partner_ids.filtered(lambda r: r.gpt_id)[:1] + partners = self.channel_partner_ids.sudo().filtered(lambda r: r.gpt_id)[:1] user_id = partners.mapped('user_ids')[:1] if user_id: gpt_policy = user_id.gpt_policy @@ -130,7 +130,7 @@ class Channel(models.Model): 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.gpt_id + ai = user_id.sudo().gpt_id chatgpt_channel_id = self.env.ref('app_chatgpt.channel_chatgpt') diff --git a/app_chatgpt/security/ir.model.access.csv b/app_chatgpt/security/ir.model.access.csv index e299d261..b9e2237f 100644 --- a/app_chatgpt/security/ir.model.access.csv +++ b/app_chatgpt/security/ir.model.access.csv @@ -1,3 +1,4 @@ id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink -access_gpt_robt_user,AiRobotUser,model_ai_robot,base.group_user,1,1,1,1 -access_res_partner_ai_use_user,ResPartnerAiUseUser,model_res_partner_ai_use,base.group_user,1,1,1,1 \ No newline at end of file +access_gpt_robt_manager,AiRobotUser,model_ai_robot,base.group_erp_manager,1,1,1,1 +access_res_partner_ai_use_user,ResPartnerAiUseUser,model_res_partner_ai_use,base.group_user,1,0,0,0 +access_res_partner_ai_use_manager,ResPartnerAiUseUser,model_res_partner_ai_use,sales_team.group_sale_manager,1,1,1,1 \ No newline at end of file diff --git a/app_chatgpt/security/ir_rules.xml b/app_chatgpt/security/ir_rules.xml new file mode 100644 index 00000000..025766fd --- /dev/null +++ b/app_chatgpt/security/ir_rules.xml @@ -0,0 +1,16 @@ + + + + Personal AI Use + + [('name','=',user.partner_id.id)] + + + + + All AI Use + + [(1,'=',1)] + + + \ No newline at end of file diff --git a/app_chatgpt/views/res_partner_ai_use_views.xml b/app_chatgpt/views/res_partner_ai_use_views.xml index e51c5531..b3bd48d2 100644 --- a/app_chatgpt/views/res_partner_ai_use_views.xml +++ b/app_chatgpt/views/res_partner_ai_use_views.xml @@ -10,12 +10,12 @@ - - - - - - + + + + + + @@ -51,6 +51,20 @@ + + res.partner.ai.use.search + res.partner.ai.use + + + + + + + + + + + Partner Ai Use res.partner.ai.use