fix gpt and 优化外观

This commit is contained in:
ivan deng
2023-04-16 00:19:09 +08:00
parent a5f3ba360a
commit 02cea839d3
2 changed files with 40 additions and 17 deletions

View File

@@ -105,26 +105,26 @@ 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, **kwargs):
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, **kwargs)
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, **kwargs)
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, **kwargs)
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, **kwargs):
def get_ai_pre(self, data, author_id=False, answer_id=False, param={}):
if self.is_filtering:
search = WordsSearch()
search.SetKeywords([])
@@ -135,7 +135,7 @@ GPT-3 A set of models that can understand and generate natural language
else:
return False
def get_ai_post(self, res, author_id=False, answer_id=False, **kwargs):
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):
usage = json.loads(json.dumps(res['usage']))
content = json.loads(json.dumps(res['choices'][0]['message']['content']))
@@ -213,12 +213,20 @@ GPT-3 A set of models that can understand and generate natural language
r_text = 'No response.'
raise UserError(r_text)
def get_openai(self, data, author_id, answer_id, **kwargs):
def get_openai(self, data, author_id, answer_id, param={}):
self.ensure_one()
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {self.openapi_api_key}"}
R_TIMEOUT = self.ai_timeout or 120
o_url = self.endpoint or "https://api.openai.com/v1/chat/completions"
# 处理传参,传过来的优先于 robot 默认的
max_tokens = param.get('max_tokens') or self.max_tokens or 600,
temperature = param.get('temperature') or self.temperature or 0.9,
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,
# request_timeout = param.get('request_timeout') or self.ai_timeout or 120,
if self.stop:
stop = self.stop.split(',')
else:
@@ -234,9 +242,10 @@ GPT-3 A set of models that can understand and generate natural language
messages = [{"role": "user", "content": data}]
# Ai角色设定如果没设定则再处理
if messages[0].get('role') != 'system':
sys_content = self.get_ai_system(kwargs.get('sys_content'))
sys_content = self.get_ai_system(param.get('sys_content'))
if sys_content:
messages.insert(0, sys_content)
# 暂时不变
response = openai.ChatCompletion.create(
model=self.ai_model,
messages=messages,
@@ -268,11 +277,11 @@ GPT-3 A set of models that can understand and generate natural language
"model": self.ai_model,
"prompt": data,
"temperature": 0.9,
"max_tokens": self.max_tokens or 1000,
"max_tokens": max_tokens,
"top_p": 1,
"frequency_penalty": 0.0,
"presence_penalty": 0.6,
"stop": ["Human:", "AI:"]
"stop": stop
}
response = requests.post(o_url, data=json.dumps(pdata), headers=headers, timeout=R_TIMEOUT)
res = response.json()
@@ -282,7 +291,7 @@ GPT-3 A set of models that can understand and generate natural language
return _("Response Timeout, please speak again.")
def get_azure(self, data, author_id, answer_id, **kwargs):
def get_azure(self, data, author_id, answer_id, param={}):
self.ensure_one()
# only for azure
openai.api_type = self.provider
@@ -301,13 +310,21 @@ GPT-3 A set of models that can understand and generate natural language
messages = data
else:
messages = [{"role": "user", "content": data}]
# Ai角色设定
# 处理传参,传过来的优先于 robot 默认的
max_tokens = param.get('max_tokens') or self.max_tokens or 600,
temperature = param.get('temperature') or self.temperature or 0.9,
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,
# request_timeout = param.get('request_timeout') or self.ai_timeout or 120,
# Ai角色设定如果没设定则再处理
if messages[0].role != 'system':
sys_content = self.get_ai_system(kwargs.get('sys_content'))
if messages[0].get('role') != 'system':
sys_content = self.get_ai_system(param.get('sys_content'))
if sys_content:
messages.insert(0, sys_content)
# 暂时不变
response = openai.ChatCompletion.create(
engine=self.engine,
messages=messages,

View File

@@ -52,10 +52,16 @@ class Channel(models.Model):
})
return context_history
def get_ai_config(self, ai):
# 勾子用于取ai 配置
return {}
def get_ai_response(self, ai, messages, channel, user_id, message):
author_id = message.create_uid.partner_id
answer_id = user_id.partner_id
res = ai.get_ai(messages, author_id, answer_id)
# todo: 只有个人配置的群聊才给配置
param = self.get_ai_config(ai)
res = 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)
@@ -120,7 +126,8 @@ class Channel(models.Model):
if message.body == _('<div class="o_mail_notification">joined the channel</div>'):
msg = _("Please warmly welcome our new partner %s and send him the best wishes.") % message.author_id.name
else:
msg = message.preview.replace('@%s' % answer_id.name, '').lstrip()
# 不能用 preview 如果用 : 提示词则 preview信息丢失
msg = message.description.replace('@%s' % answer_id.name, '').lstrip()
if not msg:
return rdata
@@ -142,7 +149,6 @@ class Channel(models.Model):
# print('self.channel_type :',self.channel_type)
if ai:
# 非4版本取0次。其它取3 次历史
# todo: channel中只有2个人1个是ai1个不是的时候直接用ai回话不用处理 @
chat_count = 0 if '4' in ai.ai_model else 3
if author_id != answer_id.id and self.channel_type == 'chat':
# 私聊