fix #I6WHKN [app_chatgpt]敏感语处理有问题,要修 置顶

This commit is contained in:
ivan deng
2023-04-18 01:16:41 +08:00
parent 3072c7399b
commit 437da3d5b3
3 changed files with 48 additions and 46 deletions

View File

@@ -40,7 +40,7 @@ GPT-3 A set of models that can understand and generate natural language
# begin gpt 参数
# 1. stop表示聊天机器人停止生成回复的条件可以是一段文本或者一个列表当聊天机器人生成的回复中包含了这个条件就会停止继续生成回复。
# 2. temperature控制回复的“新颖度”值越高聊天机器人生成的回复越不确定和随机值越低聊天机器人生成的回复会更加可预测和常规化。
# 3. top_p与temperature有些类似也是控制回复的“新颖度”。不同的是top_p控制的是回复中概率最高的几个可能性的累计概率之和值越小生成的回复越保守值越大生成的回复越新颖。
# 3. top_p言语连贯性,与temperature有些类似也是控制回复的“新颖度”。不同的是top_p控制的是回复中概率最高的几个可能性的累计概率之和值越小生成的回复越保守值越大生成的回复越新颖。
# 4. frequency_penalty用于控制聊天机器人回复中出现频率过高的词汇的惩罚程度。聊天机器人会尝试避免在回复中使用频率较高的词汇以提高回复的多样性和新颖度。
# 5. presence_penalty与frequency_penalty相对用于控制聊天机器人回复中出现频率较低的词汇的惩罚程度。聊天机器人会尝试在回复中使用频率较低的词汇以提高回复的多样性和新颖度。
max_tokens = fields.Integer('Max response', default=600,
@@ -50,7 +50,7 @@ GPT-3 A set of models that can understand and generate natural language
(including system message, examples, message history, and user query) and the model's response.
One token is roughly 4 characters for typical English text.
""")
temperature = fields.Float(string='Temperature', default=0.9,
temperature = fields.Float(string='Temperature', default=0.8,
help="""
Controls randomness. Lowering the temperature means that the model will produce
more repetitive and deterministic responses.
@@ -105,38 +105,46 @@ GPT-3 A set of models that can understand and generate natural language
def action_disconnect(self):
requests.delete('https://chatgpt.com/v1/disconnect')
def get_ai(self, data, author_id=False, answer_id=False, param={}):
# 通用方法
# author_id: 请求的 partner_id 对象
# answer_id: 回答的 partner_id 对象
# kwargsdict 形式的可变参数
self.ensure_one()
# 前置勾子,一般返回 False有问题返回响应内容
res_pre = self.get_ai_pre(data, author_id, answer_id, param)
if res_pre:
return res_pre
if hasattr(self, 'get_%s' % self.provider):
res = getattr(self, 'get_%s' % self.provider)(data, author_id, answer_id, param)
else:
res = _('No robot provider found')
# 后置勾子,返回处理后的内容,用于处理敏感词等
res_post = self.get_ai_post(res, author_id, answer_id, param)
return res_post
def get_ai_pre(self, data, author_id=False, answer_id=False, param={}):
if self.is_filtering:
search = WordsSearch()
search.SetKeywords([])
content = data[0]['content']
if isinstance(data, list):
content = data[len(data)-1]['content']
else:
content = data
sensi = search.FindFirst(content)
if sensi is not None:
_logger.error('==========敏感词:%s' % sensi['Keyword'])
return _('温馨提示:您发送的内容含有敏感词,请修改内容后再向我发送。')
else:
return False
def get_ai(self, data, author_id=False, answer_id=False, param={}):
# 通用方法
# author_id: 请求的 partner_id 对象
# answer_id: 回答的 partner_id 对象
# paramdict 形式的参数
# 调整输出为2个参数res_post详细内容is_ai是否ai的响应
self.ensure_one()
# 前置勾子,一般返回 False有问题返回响应内容用于处理敏感词等
res_pre = self.get_ai_pre(data, author_id, answer_id, param)
if res_pre:
# 有错误内容,则返回上级内容及 is_ai为假
return res_pre, False
if not hasattr(self, 'get_%s' % self.provider):
res = _('No robot provider found')
return res, False
res = getattr(self, 'get_%s' % self.provider)(data, author_id, answer_id, param)
# 后置勾子,返回处理后的内容
res_post, is_ai = self.get_ai_post(res, author_id, answer_id, param)
return res_post, is_ai
def get_ai_post(self, res, author_id=False, answer_id=False, param={}):
if res and author_id and isinstance(res, openai.openai_object.OpenAIObject) or isinstance(res, list):
# 返回是个对象那么就是ai
usage = json.loads(json.dumps(res['usage']))
content = json.loads(json.dumps(res['choices'][0]['message']['content']))
data = content.replace(' .', '.').strip()
@@ -169,9 +177,10 @@ GPT-3 A set of models that can understand and generate natural language
'first_ask_time': ask_date
})
ai_use.write(vals)
return data, True
else:
data = res
return data
# 直接返回错误语句那么就是非ai
return res, False
def get_ai_system(self, content=None):
# 获取基础ai角色设定, role system
@@ -221,7 +230,7 @@ GPT-3 A set of models that can understand and generate natural language
# 处理传参,传过来的优先于 robot 默认的
max_tokens = param.get('max_tokens') or self.max_tokens or 600,
temperature = param.get('temperature') or self.temperature or 0.9,
temperature = param.get('temperature') or self.temperature or 0.8,
top_p = param.get('top_p') or self.top_p or 0.6,
frequency_penalty = param.get('frequency_penalty') or self.frequency_penalty or 0.5,
presence_penalty = param.get('presence_penalty') or self.presence_penalty or 0.5,
@@ -250,7 +259,7 @@ GPT-3 A set of models that can understand and generate natural language
model=self.ai_model,
messages=messages,
n=1,
temperature=self.temperature or 0.9,
temperature=self.temperature or 0.8,
max_tokens=self.max_tokens or 600,
top_p=self.top_p or 0.6,
frequency_penalty=self.frequency_penalty or 0.5,
@@ -276,7 +285,7 @@ GPT-3 A set of models that can understand and generate natural language
pdata = {
"model": self.ai_model,
"prompt": data,
"temperature": 0.9,
"temperature": 0.8,
"max_tokens": max_tokens,
"top_p": 1,
"frequency_penalty": 0.0,
@@ -311,9 +320,9 @@ GPT-3 A set of models that can understand and generate natural language
else:
messages = [{"role": "user", "content": data}]
# 处理传参,传过来的优先于 robot 默认的
# todo: 处理传参,传过来的优先于 robot 默认的当前有问题无法做tuple转换
max_tokens = param.get('max_tokens') or self.max_tokens or 600,
temperature = param.get('temperature') or self.temperature or 0.9,
temperature = param.get('temperature') or self.temperature or 0.8,
top_p = param.get('top_p') or self.top_p or 0.6,
frequency_penalty = param.get('frequency_penalty') or self.frequency_penalty or 0.5,
presence_penalty = param.get('presence_penalty') or self.presence_penalty or 0.5,
@@ -330,7 +339,7 @@ GPT-3 A set of models that can understand and generate natural language
messages=messages,
# 返回的回答数量
n=1,
temperature=self.temperature or 0.9,
temperature=self.temperature or 0.8,
max_tokens=self.max_tokens or 600,
top_p=self.top_p or 0.6,
frequency_penalty=self.frequency_penalty or 0.5,

View File

@@ -10567,7 +10567,6 @@ fuck兴奋剂
禽獸
明慧网
jiejie
嫩穴
床上激情自拍图
6。4
@@ -13533,7 +13532,6 @@ re xue ying hao
淫荡老师
小姨子的小嫩屄
create
亚洲激情BT
省长的儿媳妇
苹果日报
@@ -14077,7 +14075,6 @@ gong fu
89年的鬥爭
台湾十八电影
小淫虫电影
CREATE
外阴
外??挂
毛爷爷复活

View File

@@ -63,7 +63,7 @@ class Channel(models.Model):
answer_id = user_id.partner_id
# todo: 只有个人配置的群聊才给配置
param = self.get_ai_config(ai)
res = ai.get_ai(messages, author_id, answer_id, param)
res, is_ai = ai.get_ai(messages, author_id, answer_id, param)
if res:
res = res.replace('\n', '<br/>')
channel.with_user(user_id).message_post(body=res, message_type='comment', subtype_xmlid='mail.mt_comment', parent_id=message.id)
@@ -144,22 +144,18 @@ class Channel(models.Model):
if not msg:
return rdata
# api_key = self.env['ir.config_parameter'].sudo().get_param('app_chatgpt.openapi_api_key')
api_key = ''
if ai:
# ai处理不要自问自答
if ai and answer_id != message.author_id:
api_key = ai.openapi_api_key
if not api_key:
_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 60
except:
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:
try:
openapi_context_timeout = int(self.env['ir.config_parameter'].sudo().get_param('app_chatgpt.openapi_context_timeout')) or 60
except:
openapi_context_timeout = 60
sync_config = self.env['ir.config_parameter'].sudo().get_param('app_chatgpt.openai_sync_config')
openai.api_key = api_key
# 非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':