From 753fea8ebbcbc1105b3f30d41897f72f31d1cc4c Mon Sep 17 00:00:00 2001 From: ivan deng Date: Thu, 13 Apr 2023 06:55:59 +0800 Subject: [PATCH] =?UTF-8?q?prepare=20#I6SC9C=20=20=E5=A4=84=E7=90=86azure?= =?UTF-8?q?=E7=A7=81=E8=81=8A=E7=BE=A4=E8=81=8A=EF=BC=8Capp=5Fchatgpt?= =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=8C=E6=8C=87=E5=AE=9A=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E6=97=B6=E5=A2=9E=E5=8A=A0=E4=BD=BF=E7=94=A8=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app_chatgpt/models/ai_robot.py | 53 ++++++++-------- app_chatgpt/models/lib/sensi_words.txt | 1 - app_chatgpt/models/mail_channel.py | 74 +++++++++++------------ app_chatgpt/models/mail_thread.py | 20 +++--- app_chatgpt/models/res_config_settings.py | 2 +- 5 files changed, 77 insertions(+), 73 deletions(-) diff --git a/app_chatgpt/models/ai_robot.py b/app_chatgpt/models/ai_robot.py index 998893ef..ee94de68 100644 --- a/app_chatgpt/models/ai_robot.py +++ b/app_chatgpt/models/ai_robot.py @@ -128,8 +128,29 @@ GPT-3 A set of models that can understand and generate natural language return False def get_ai_post(self, res, author_id=False, answer_id=False, **kwargs): - # res = self.filter_sensitive_words(res) - return res + if res and isinstance(res, dict): + data = res['content'].replace(' .', '.').strip() + if 'usage' in res: + usage = res['usage'] + prompt_tokens = usage['prompt_tokens'] + completion_tokens = usage['completion_tokens'] + total_tokens = usage['total_tokens'] + vals = { + 'human_prompt_tokens': author_id.human_prompt_tokens + prompt_tokens, + 'ai_completion_tokens': author_id.ai_completion_tokens + completion_tokens, + 'tokens_total': author_id.tokens_total + total_tokens, + 'used_number': author_id.used_number + 1, + } + if not author_id.first_ask_time: + ask_date = fields.Datetime.now() + vals.update({ + 'first_ask_time': ask_date + }) + author_id.write(vals) + # res = self.filter_sensitive_words(data) + else: + data = res + return data def get_ai_system(self, content=None): # 获取基础ai角色设定, role system @@ -187,7 +208,7 @@ GPT-3 A set of models that can understand and generate natural language # Ai角色设定 sys_content = self.get_ai_system(kwargs.get('sys_content')) if sys_content: - messages.insert(sys_content) + messages.insert(0, sys_content) pdata = { "model": self.ai_model, "messages": messages, @@ -201,25 +222,7 @@ GPT-3 A set of models that can understand and generate natural language _logger.warning('=====================open input pdata: %s' % pdata) response = requests.post(o_url, data=json.dumps(pdata), headers=headers, timeout=R_TIMEOUT) try: - # todo: 将 res 总结果给 self.get_ai_post,再取实际内容 return,将 tokens 计算写在 post 方法中 res = response.json() - if 'usage' in res: - usage = res['usage'] - prompt_tokens = usage['prompt_tokens'] - completion_tokens = usage['completion_tokens'] - total_tokens = usage['total_tokens'] - vals = { - 'human_prompt_tokens': author_id.human_prompt_tokens + prompt_tokens, - 'ai_completion_tokens': author_id.ai_completion_tokens + completion_tokens, - 'tokens_total': author_id.tokens_total + total_tokens, - 'used_number': author_id.used_number + 1, - } - if not author_id.first_ask_time: - ask_date = response.headers.get("Date") - vals.update({ - 'first_ask_time': ask_date - }) - author_id.write(vals) if 'choices' in res: # for rec in res: # res = rec['message']['content'] @@ -278,7 +281,7 @@ GPT-3 A set of models that can understand and generate natural language # Ai角色设定 sys_content = self.get_ai_system(kwargs.get('sys_content')) if sys_content: - messages.insert(sys_content) + messages.insert(0, sys_content) response = openai.ChatCompletion.create( engine=self.engine, messages=messages, @@ -287,9 +290,11 @@ GPT-3 A set of models that can understand and generate natural language top_p=self.top_p or 0.6, frequency_penalty=self.frequency_penalty or 0.5, presence_penalty=self.presence_penalty or 0.2, - stop=stop) + stop=stop, + request_timeout=self.ai_timeout or 120, + ) if 'choices' in response: - res = response['choices'][0]['message']['content'].replace(' .', '.').strip() + res = response['choices'][0]['message'] return res else: _logger.warning('=====================azure output data: %s' % response) diff --git a/app_chatgpt/models/lib/sensi_words.txt b/app_chatgpt/models/lib/sensi_words.txt index 10e15ae2..555bcd94 100644 --- a/app_chatgpt/models/lib/sensi_words.txt +++ b/app_chatgpt/models/lib/sensi_words.txt @@ -87,7 +87,6 @@ fuck温总理 插阿姨的洞洞舔女朋友的下身 驶你爸 轮奸做爱高潮录象 -测试 赛你老母 內射 李岚清 diff --git a/app_chatgpt/models/mail_channel.py b/app_chatgpt/models/mail_channel.py index ec5087df..2f7d0ed7 100644 --- a/app_chatgpt/models/mail_channel.py +++ b/app_chatgpt/models/mail_channel.py @@ -14,13 +14,13 @@ _logger = logging.getLogger(__name__) class Channel(models.Model): _inherit = 'mail.channel' - def get_openai_context(self, channel_id, author_id, answer_id, minutes=30): + def get_openai_context(self, channel_id, author_id, answer_id, minutes=30, chat_count=1): # 上下文处理,要处理群的方式,以及独聊的方式 # azure新api 处理 context_history = [] afterTime = fields.Datetime.now() - datetime.timedelta(minutes=minutes) message_model = self.env['mail.message'].sudo() - # 处理消息: 取最新问题 + 上2次的交互,将之前的交互按时间顺序拼接 + # 处理消息: 取最新问题 + 上 chat_count=1次的交互,将之前的交互按时间顺序拼接。 # 注意: ai 每一次回复都有 parent_id 来处理连续性 # 私聊处理 domain = [('res_id', '=', channel_id), @@ -32,17 +32,18 @@ class Channel(models.Model): if self.channel_type in ['group', 'channel']: # 群聊增加时间限制,当前找所有人,不限制 author_id domain += [('date', '>=', afterTime)] - ai_msg_list = message_model.with_context(tz='UTC').search(domain, order="id desc", limit=2) - for ai_msg in ai_msg_list.sorted(key='id'): - user_content = ai_msg.parent_id.body.replace("

", "").replace("

", "") - ai_content = ai_msg.body.replace("

", "").replace("

", "") - context_history.append({ - 'role': 'user', - 'content': user_content, - }, { + ai_msg_list = message_model.with_context(tz='UTC').search(domain, order="id desc", limit=chat_count) + for ai_msg in ai_msg_list: + user_content = ai_msg.parent_id.description.replace("

", "").replace("

", "").replace('@%s' % answer_id.name, '').lstrip() + ai_content = str(ai_msg.body).replace("

", "").replace("

", "").replace("

", "") + context_history.insert(0, { 'role': 'assistant', 'content': ai_content, }) + context_history.insert(0, { + 'role': 'user', + 'content': user_content, + }) return context_history def get_ai_response(self, ai, messages, channel, user_id, message): @@ -88,8 +89,8 @@ class Channel(models.Model): ai = user_id.gpt_id chatgpt_channel_id = self.env.ref('app_chatgpt.channel_chatgpt') - - msg = msg_vals.get('body') + msg = message.description.replace('@%s' % answer_id.name, '').lstrip() + # print('prompt:', prompt) # print('-----') if not msg: @@ -102,43 +103,40 @@ class Channel(models.Model): _logger.warning(_("ChatGPT Robot【%s】have not set open api key.")) return rdata try: - openapi_context_timeout = int(self.env['ir.config_parameter'].sudo().get_param('app_chatgpt.openapi_context_timeout')) or 600 + openapi_context_timeout = int(self.env['ir.config_parameter'].sudo().get_param('app_chatgpt.openapi_context_timeout')) or 60 except: - openapi_context_timeout = 600 + openapi_context_timeout = 60 sync_config = self.env['ir.config_parameter'].sudo().get_param('app_chatgpt.openai_sync_config') openai.api_key = api_key # print(msg_vals) # print(msg_vals.get('record_name', '')) # print('self.channel_type :',self.channel_type) if ai: + # 非4版本,取0次。其它取3 次历史 + chat_count = 0 if '4' in ai.ai_model else 3 if author_id != answer_id.id and self.channel_type == 'chat': + # 私聊 _logger.info(f'私聊:author_id:{author_id},partner_chatgpt.id:{answer_id.id}') - try: - channel = self.env[msg_vals.get('model')].browse(msg_vals.get('res_id')) - # if ai_model not in ['gpt-3.5-turbo', 'gpt-3.5-turbo-0301']: - messages = [{"role": "user", "content": msg}] - c_history = self.get_openai_context(channel.id, author_id, answer_id, openapi_context_timeout) - if c_history: - messages.insert(0, c_history) - if sync_config == 'sync': - self.get_ai_response(ai, messages, channel, user_id, message) - else: - self.with_delay().get_ai_response(ai, messages, channel, user_id, message) - except Exception as e: - raise UserError(_(e)) + channel = self.env[msg_vals.get('model')].browse(msg_vals.get('res_id')) elif author_id != answer_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:{answer_id.id}') - try: - messages = [{"role": "user", "content": msg}] - c_history = self.get_openai_context(chatgpt_channel_id.id, author_id, answer_id, openapi_context_timeout) - if c_history: - messages.insert(0, c_history) - if sync_config == 'sync': - self.get_ai_response(ai, messages, chatgpt_channel_id, user_id, message) - else: - self.with_delay().get_ai(ai, messages, chatgpt_channel_id, user_id, message) - except Exception as e: - raise UserError(_(e)) + channel = chatgpt_channel_id + elif author_id != answer_id.id and msg_vals.get('model', '') == 'mail.channel' and self.channel_type in ['group', 'channel']: + # 其它群聊 + channel = self + + try: + messages = [{"role": "user", "content": msg}] + c_history = self.get_openai_context(channel.id, author_id, answer_id, openapi_context_timeout, chat_count) + if c_history: + messages += c_history + if sync_config == 'sync': + self.get_ai_response(ai, messages, channel, user_id, message) + else: + self.with_delay().get_ai(ai, messages, channel, user_id, message) + except Exception as e: + raise UserError(_(e)) return rdata diff --git a/app_chatgpt/models/mail_thread.py b/app_chatgpt/models/mail_thread.py index 14154a23..b1bf7894 100644 --- a/app_chatgpt/models/mail_thread.py +++ b/app_chatgpt/models/mail_thread.py @@ -7,12 +7,14 @@ from .lib.WordsSearch import WordsSearch class MailThread(models.AbstractModel): _inherit = "mail.thread" - @api.returns('mail.message', lambda value: value.id) - def message_post(self, **kwargs): - self.ensure_one() - search = WordsSearch() - search.SetKeywords([]) - body = kwargs.get('body', False) - body = search.Replace(text=body) - kwargs.update({'body': body}) - return super(MailThread, self).message_post(**kwargs) + # todo: 不是说在此屏蔽,而是当用户发出敏感词时,提示下,同时不发送到 ai + # @api.returns('mail.message', lambda value: value.id) + # def message_post(self, **kwargs): + + # self.ensure_one() + # search = WordsSearch() + # search.SetKeywords([]) + # body = kwargs.get('body', False) + # body = search.Replace(text=body) + # kwargs.update({'body': body}) + # return super(MailThread, self).message_post(**kwargs) diff --git a/app_chatgpt/models/res_config_settings.py b/app_chatgpt/models/res_config_settings.py index 811aad58..32f708e9 100644 --- a/app_chatgpt/models/res_config_settings.py +++ b/app_chatgpt/models/res_config_settings.py @@ -6,7 +6,7 @@ from odoo import fields, models class ResConfigSettings(models.TransientModel): _inherit = "res.config.settings" - openapi_context_timeout = fields.Integer(string="Connect Timout", help="多少秒以内的聊天信息作为上下文继续", config_parameter="app_chatgpt.openapi_context_timeout") + openapi_context_timeout = fields.Integer(string="Connect Timout", help="群聊中多少分钟以内的聊天信息作为上下文继续", config_parameter="app_chatgpt.openapi_context_timeout") openai_sync_config = fields.Selection([ ('sync', 'Synchronous'), ('async', 'Asynchronous')