mirror of
https://github.com/guohuadeng/app-odoo.git
synced 2025-02-23 04:11:36 +02:00
prepare #I6SC9C 处理azure私聊群聊,app_chatgpt优化,指定用户时增加使用情况
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -87,7 +87,6 @@ fuck温总理
|
||||
插阿姨的洞洞舔女朋友的下身
|
||||
驶你爸
|
||||
轮奸做爱高潮录象
|
||||
测试
|
||||
赛你老母
|
||||
內射
|
||||
李岚清
|
||||
|
||||
@@ -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("<p>", "").replace("</p>", "")
|
||||
ai_content = ai_msg.body.replace("<p>", "").replace("</p>", "")
|
||||
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("<p>", "").replace("</p>", "").replace('@%s' % answer_id.name, '').lstrip()
|
||||
ai_content = str(ai_msg.body).replace("<p>", "").replace("</p>", "").replace("<p>", "")
|
||||
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
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user