update chatgpt, deepseek, misc

This commit is contained in:
Ivan Office
2025-02-10 17:07:57 +08:00
parent b2f9348d87
commit b679f1b4ae
28 changed files with 459 additions and 277 deletions

View File

@@ -18,7 +18,7 @@ class AiRobot(models.Model):
_name = 'ai.robot'
_description = 'Ai Robot'
_order = 'sequence, name'
name = fields.Char(string='Name', translate=True, required=True)
provider = fields.Selection(string="AI Provider", selection=[('openai', 'OpenAI'), ('azure', 'Azure')],
required=True, default='openai', change_default=True)
@@ -39,8 +39,9 @@ class AiRobot(models.Model):
('code-davinci-002', 'Chatgpt 2 Code Optimized'),
('text-davinci-002', 'Chatgpt 2 Davinci'),
('dall-e2', 'Dall-E Image'),
], default='gpt-3.5-turbo-0125',
help="""
], default='gpt-4o',
help="""
GPT-4o: It is multimodal (accepting text or image inputs and outputting text), and it has the same high intelligence as GPT-4 Turbo but is much more efficient—it generates text 2x faster and is 50% cheaper.
GPT-4: Can understand Image, generate natural language or code.
GPT-3.5: A set of models that improve on GPT-3 and can understand as well as generate natural language or code
DALL·E: A model that can generate and edit images given a natural language prompt
@@ -110,36 +111,36 @@ GPT-3 A set of models that can understand and generate natural language
# end gpt 参数
endpoint = fields.Char('End Point', default='https://api.openai.com/v1/chat/completions')
engine = fields.Char('Engine', help='If use Azure, Please input the Model deployment name.')
api_version = fields.Char('API Version', default='2022-12-01')
api_version = fields.Char('API Version', default='gpt-4o')
ai_timeout = fields.Integer('Timeout(seconds)', help="Connect timeout for Ai response", default=120)
sequence = fields.Integer('Sequence', help="Determine the display order", default=10)
sensitive_words = fields.Text('Sensitive Words Plus', help='Sensitive word filtering. Separate keywords with a carriage return.')
is_filtering = fields.Boolean('Filter Sensitive Words', default=False, help='Use base Filter in dir models/lib/sensi_words.txt')
max_send_char = fields.Integer('Max Send Char', help='Max Send Prompt Length', default=8000)
image_avatar = fields.Image('Avatar')
partner_ids = fields.One2many('res.partner', 'gpt_id', string='Partner')
partner_count = fields.Integer('#Partner', compute='_compute_partner_count', store=False)
active = fields.Boolean('Active', default=True)
def _compute_partner_count(self):
for rec in self:
rec.partner_count = len(rec.partner_ids)
def action_disconnect(self):
requests.delete('https://chatgpt.com/v1/disconnect')
def get_ai_pre(self, data, author_id=False, answer_id=False, param={}):
# hook都正常
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)
@@ -149,19 +150,19 @@ GPT-3 A set of models that can understand and generate natural language
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, usage, is_ai = self.get_ai_post(res, author_id, answer_id, param)
res_post, usage, is_ai = self.get_ai_post(res, author_id, answer_id, param)
return res_post, usage, is_ai
def get_ai_origin(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)
@@ -171,19 +172,18 @@ GPT-3 A set of models that can understand and generate natural language
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, usage, is_ai = self.get_ai_post(res, author_id, answer_id, param)
return res
def get_ai_post(self, res, author_id=False, answer_id=False, param=None):
# hook高级版要替代
if param is None:
param = {}
if not res or not author_id or (not isinstance(res, list) and not isinstance(res, dict)):
return res, False, False
# 返回是个对象那么就是ai
usage = content = data = None
try:
if self.provider == 'openai':
@@ -214,7 +214,7 @@ GPT-3 A set of models that can understand and generate natural language
if sys_content:
return {"role": "system", "content": sys_content}
return {}
def get_ai_model_info(self):
self.ensure_one()
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {self.openapi_api_key}"}
@@ -222,7 +222,7 @@ GPT-3 A set of models that can understand and generate natural language
o_url = "https://api.openai.com/v1/models/%s" % self.ai_model
if self.endpoint:
o_url = self.endpoint.replace("/chat/completions", "") + "/models/%s" % self.ai_model
response = requests.get(o_url, headers=headers, timeout=R_TIMEOUT)
response.close()
if response:
@@ -231,32 +231,29 @@ GPT-3 A set of models that can understand and generate natural language
else:
r_text = 'No response.'
raise UserError(r_text)
def get_ai_list_model(self):
self.ensure_one()
if self.provider == 'openai':
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {self.openapi_api_key}"}
R_TIMEOUT = self.ai_timeout or 120
o_url = "https://api.openai.com/v1/models"
if self.endpoint:
o_url = self.endpoint.replace("/chat/completions", "") + "/models"
response = requests.get(o_url, headers=headers, timeout=R_TIMEOUT)
response.close()
if response:
res = response.json()
r_text = json.dumps(res, indent=2)
else:
r_text = 'No response.'
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {self.openapi_api_key}"}
R_TIMEOUT = self.ai_timeout or 120
o_url = "https://api.openai.com/v1/models"
if self.endpoint:
o_url = self.endpoint.replace("/chat/completions", "") + "/models"
response = requests.get(o_url, headers=headers, timeout=R_TIMEOUT)
response.close()
if response:
res = response.json()
r_text = json.dumps(res, indent=2)
else:
r_text = 'No response.'
raise UserError(r_text)
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') if param.get('max_tokens') else self.max_tokens
temperature = param.get('temperature') if param.get('temperature') else self.temperature
@@ -264,7 +261,7 @@ GPT-3 A set of models that can understand and generate natural language
frequency_penalty = param.get('frequency_penalty') if param.get('frequency_penalty') else self.frequency_penalty
presence_penalty = param.get('presence_penalty') if param.get('presence_penalty') else self.presence_penalty
request_timeout = param.get('request_timeout') if param.get('request_timeout') else self.ai_timeout
if self.stop:
stop = self.stop.split(',')
else:
@@ -294,7 +291,7 @@ GPT-3 A set of models that can understand and generate natural language
client = OpenAI(
api_key=self.openapi_api_key,
timeout=R_TIMEOUT
)
)
response = client.chat.completions.create(
messages=data,
model=self.ai_model,
@@ -304,7 +301,7 @@ GPT-3 A set of models that can understand and generate natural language
return res
else:
_logger.warning('=====================openai output data: %s' % response.json())
return _("Response Timeout, please speak again.")
def get_azure(self, data, author_id, answer_id, param={}):
@@ -312,10 +309,10 @@ GPT-3 A set of models that can understand and generate natural language
# only for azure
if not self.endpoint:
raise UserError(_("Please Set your AI robot's endpoint first."))
if not self.api_version:
raise UserError(_("Please Set your AI robot's API Version first."))
if self.stop:
stop = self.stop.split(',')
else:
@@ -324,22 +321,22 @@ GPT-3 A set of models that can understand and generate natural language
messages = data
else:
messages = [{"role": "user", "content": data}]
# 处理传参,传过来的优先于 robot 默认的
max_tokens = param.get('max_tokens') if param.get('max_tokens') else self.max_tokens
temperature = param.get('temperature') if param.get('temperature') else self.temperature
top_p = param.get('top_p') if param.get('top_p') else self.top_p
frequency_penalty = param.get('frequency_penalty') if param.get('frequency_penalty') else self.frequency_penalty
presence_penalty = param.get('presence_penalty') if param.get('presence_penalty') else self.presence_penalty
request_timeout= param.get('request_timeout') if param.get('request_timeout') else self.ai_timeout
request_timeout = param.get('request_timeout') if param.get('request_timeout') else self.ai_timeout
# Ai角色设定如果没设定则再处理
if messages[0].get('role') != 'system':
sys_content = self.get_ai_system(param.get('sys_content'))
if sys_content:
messages.insert(0, sys_content)
# 暂时不变
client = AzureOpenAI(
api_version=self.api_version,
azure_endpoint=self.endpoint,
@@ -372,14 +369,14 @@ GPT-3 A set of models that can understand and generate natural language
else:
_logger.warning('=====================azure output data: %s' % response.json())
return _("Response Timeout, please speak again.")
@api.onchange('provider')
def _onchange_provider(self):
if self.provider == 'openai':
self.endpoint = 'https://api.openai.com/v1/chat/completions'
elif self.provider == 'azure':
self.endpoint = 'https://odoo.openai.azure.com'
if self.provider:
# 取头像
module_path = modules.get_module_path('app_chatgpt', display_warning=False)
@@ -388,7 +385,7 @@ GPT-3 A set of models that can understand and generate natural language
if path:
image_file = tools.file_open(path, 'rb')
self.image_avatar = base64.b64encode(image_file.read())
@api.onchange('set_ai_model')
def _onchange_set_ai_model(self):
if self.set_ai_model:

View File

@@ -34,7 +34,7 @@ class Channel(models.Model):
('2000', 'Long'),
('3000', 'Overlength'),
('32000', '32K'),
], string='Max Response', default='600', help="越大返回内容越多,计费也越多")
], string='Max Response', default='1000', help="越大返回内容越多,计费也越多")
set_chat_count = fields.Selection([
('none', 'Ai Auto'),
('1', '1标准'),
@@ -71,7 +71,7 @@ class Channel(models.Model):
('-1', '允许较多重复'),
('-2', '更多强调重复'),
], string='Presence penalty', default='1', help="-2~2值越大越少重复词")
# todo: 这里用 compute?
max_tokens = fields.Integer('最长响应Token', default=600, help="越大返回内容越多,计费也越多")
chat_count = fields.Integer(string="上下文数量", default=0, help="0~3设定后会将最近n次对话发给Ai有助于他更好的回答")
@@ -79,16 +79,31 @@ class Channel(models.Model):
top_p = fields.Float(string="连贯性值", default=0.6, help="0~1值越大越富有想像力越小则越保守")
frequency_penalty = fields.Float('避免常用词值', default=1, help="-2~2值越大越少使用常用词")
presence_penalty = fields.Float('避免重复词值', default=1, help="-2~2值越大越少重复词")
is_current_channel = fields.Boolean('是否当前用户默认频道', compute='_compute_is_current_channel', help='是否当前用户默认微信对话频道')
# begin 处理Ai对话
is_ai_conversation = fields.Boolean('Ai Conversation', default=False,
help='Set active to make conversation between 2+ Ai Employee. You Just say first word, then Ai robots Auto Chat.')
# 主Ai角色设定
ai_sys_content = fields.Char('Main Robot Role', change_default=True,
help='The Role the First Ai robot play for. This is for Ai Conversation.')
# 辅助Ai角色设定
ext_ai_sys_content = fields.Char('Extend Robot Role', change_default=True,
help='The Role the Second Ai robot play for. This is for Ai Conversation.')
# end 处理Ai对话
def name_get(self):
result = []
for c in self:
pre = '[私]' if c.channel_type == 'channel' and c.is_private else ''
result.append((c.id, f"{pre}{c.name or ''}"))
if c.channel_type == 'channel' and c.is_private:
pre = '[私]'
else:
pre = ''
result.append((c.id, "%s%s" % (pre, c.name or '')))
return result
def get_openai_context(self, channel_id, author_id, answer_id, minutes=60, chat_count=0):
# 上下文处理,要处理群的方式,以及独聊的方式
# azure新api 处理
@@ -98,7 +113,7 @@ class Channel(models.Model):
# 处理消息: 取最新问题 + 上 chat_count=1次的交互将之前的交互按时间顺序拼接。
# 注意: ai 每一次回复都有 parent_id 来处理连续性
# 私聊处理
# todo: 更好的处理方式
domain = [('res_id', '=', channel_id),
('model', '=', 'discuss.channel'),
@@ -107,7 +122,7 @@ class Channel(models.Model):
('is_ai', '=', True),
('body', '!=', '<p>%s</p>' % _('Response Timeout, please speak again.')),
('body', '!=', _('温馨提示:您发送的内容含有敏感词,请修改内容后再向我发送。'))]
if self.channel_type in ['group', 'channel']:
# 群聊增加时间限制,当前找所有人,不限制 author_id
domain = expression.AND([domain, [('date', '>=', afterTime)]])
@@ -133,11 +148,11 @@ class Channel(models.Model):
'content': user_content,
})
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
@@ -164,7 +179,7 @@ class Channel(models.Model):
'ai_completion_tokens': completion_tokens,
'cost_tokens': total_tokens,
})
def _notify_thread(self, message, msg_vals=False, **kwargs):
rdata = super(Channel, self)._notify_thread(message, msg_vals=msg_vals, **kwargs)
# print(f'rdata:{rdata}')
@@ -175,13 +190,15 @@ class Channel(models.Model):
channel = self.env['discuss.channel']
channel_type = self.channel_type
messages = []
add_sys_content = ''
# 不处理 一般notify但处理欢迎
if '<div class="o_mail_notification' in message.body and message.body != _('<div class="o_mail_notification">joined the channel</div>'):
return rdata
if 'o_odoobot_command' in message.body:
return rdata
# begin: 找ai增加 ai二人转功能。 chat类型不用管 使用其中一个ai登录即可。 author_id 是 res.partner 模型
if channel_type == 'chat':
channel_partner_ids = self.channel_partner_ids
answer_id = channel_partner_ids - message.author_id
@@ -192,17 +209,25 @@ class Channel(models.Model):
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.sudo().gpt_id
elif channel_type in ['group', 'channel']:
# partner_ids = @ ids
partner_ids = list(msg_vals.get('partner_ids'))
if hasattr(self, 'ai_partner_id') and self.ai_partner_id:
# 当有主id时使用主id
if self.ai_partner_id.id in partner_ids:
if self.is_ai_conversation and self.ext_ai_partner_id:
# 二人转模式时处理回答ai以及叠加角色的设定
if author_id == self.ai_partner_id.id:
partner_ids = [self.ext_ai_partner_id.id]
add_sys_content = self.ext_ai_sys_content
else:
partner_ids = [self.ai_partner_id.id]
add_sys_content = self.ai_sys_content
elif self.ai_partner_id.id in partner_ids:
# 其它普通Ai群。当有主id时使用主id
partner_ids = [self.ai_partner_id.id]
if partner_ids:
# 常规群聊 @
partners = self.env['res.partner'].search([('id', 'in', partner_ids)])
partners = self.env['res.partner'].search([('id', 'in', partner_ids)]) - message.author_id
# user_id = user, who has binded gpt robot
user_id = partners.mapped('user_ids').sudo().filtered(lambda r: r.gpt_id)[:1]
elif message.body == _('<div class="o_mail_notification">joined the channel</div>'):
@@ -240,30 +265,38 @@ class Channel(models.Model):
# elif user_id.gpt_id and not is_allow:
# # 暂时有限用户的Ai
# raise UserError(_('此Ai暂时未开放请联系管理员。'))
# end: 找ai增加 ai二人转功能
if hasattr(ai, 'is_translator') and ai.is_translator and ai.ai_model == 'translator':
return rdata
chatgpt_channel_id = self.env.ref('app_chatgpt.channel_chatgpt')
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:
# 不能用 preview 如果用 : 提示词则 preview信息丢失
plaintext_ct = tools.mail.html_to_inner_content(message.body)
plaintext_ct = tools.html_to_inner_content(message.body)
msg = plaintext_ct.replace('@%s' % answer_id.name, '').lstrip()
if not msg:
return rdata
if self._context.get('app_ai_sync_config') and self._context.get('app_ai_sync_config') in ['sync', 'async']:
sync_config = self._context.get('app_ai_sync_config')
else:
sync_config = self.env['ir.config_parameter'].sudo().get_param('app_chatgpt.openai_sync_config')
if self._context.get('app_ai_chat_padding_time'):
padding_time = int(self._context.get('app_ai_chat_padding_time'))
else:
padding_time = int(self.env['ir.config_parameter'].sudo().get_param('app_chatgpt.ai_chat_padding_time'))
# api_key = self.env['ir.config_parameter'].sudo().get_param('app_chatgpt.openapi_api_key')
# 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."))
_logger.warning(_("ChatGPT Robot【%s】have not set open api key.") % ai.name)
return rdata
try:
openapi_context_timeout = int(self.env['ir.config_parameter'].sudo().get_param('app_chatgpt.openapi_context_timeout')) or 60
@@ -271,15 +304,11 @@ class Channel(models.Model):
openapi_context_timeout = 60
openai.api_key = api_key
# 非4版本取0次。其它取3 次历史
chat_count = 3
if '4' in ai.ai_model or '4' in ai.name:
chat_count = 1
if hasattr(self, 'chat_count'):
if self.chat_count > 0:
chat_count = 1
else:
chat_count = chat_count
chat_count = self.chat_count or 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}')
@@ -291,10 +320,11 @@ class Channel(models.Model):
elif author_id != answer_id.id and msg_vals.get('model', '') == 'discuss.channel' and self.channel_type in ['group', 'channel']:
# 高级用户自建的话题
channel = self.env[msg_vals.get('model')].browse(msg_vals.get('res_id'))
if hasattr(channel, 'is_private') and channel.description:
messages.append({"role": "system", "content": channel.description})
try:
# 处理提示词
sys_content = channel.description + add_sys_content
messages.append({"role": "system", "content": sys_content})
c_history = self.get_openai_context(channel.id, author_id, answer_id, openapi_context_timeout, chat_count)
if c_history:
messages += c_history
@@ -303,36 +333,34 @@ class Channel(models.Model):
# 接口最大接收 8430 Token
if msg_len * 2 > ai.max_send_char:
messages = []
if hasattr(channel, 'is_private') and channel.description:
messages.append({"role": "system", "content": channel.description})
messages.append({"role": "user", "content": msg})
msg_len = sum(len(str(m)) for m in messages)
if msg_len * 2 > ai.max_send_char:
new_msg = channel.with_user(user_id).message_post(body=_('您所发送的提示词已超长。'), message_type='comment',
subtype_xmlid='mail.mt_comment',
parent_id=message.id)
# if msg_len * 2 >= 8000:
# messages = [{"role": "user", "content": msg}]
if sync_config == 'sync':
self.get_ai_response(ai, messages, channel, user_id, message)
else:
if hasattr(self, 'with_delay'):
self.with_delay().get_ai_response(ai, messages, channel, user_id, message)
self.with_delay(priority=30, eta=padding_time).get_ai_response(ai, messages, channel, user_id, message)
else:
self.get_ai_response(ai, messages, channel, user_id, message)
except Exception as e:
raise UserError(e)
raise UserError(_(e))
return rdata
def _message_post_after_hook(self, message, msg_vals):
if message.author_id.gpt_id:
if msg_vals['body'] not in [_('Response Timeout, please speak again.'), _('温馨提示:您发送的内容含有敏感词,请修改内容后再向我发送。'),
_('此Ai暂时未开放请联系管理员。'), _('您所发送的提示词已超长。')]:
message.is_ai = True
return super(Channel, self)._message_post_after_hook(message, msg_vals)
@api.model
def _get_my_last_cid(self):
# 获取当前用户最后一次进入的channel返回该channel的id
@@ -355,8 +383,22 @@ class Channel(models.Model):
if c and not c.is_member:
c.sudo().add_members([user.partner_id.id])
return c_id
@api.onchange('ai_partner_id')
def _onchange_ai_partner_id(self):
if self.ai_partner_id and self.ai_partner_id.image_1920:
self.image_128 = self.ai_partner_id.avatar_128
if self.ai_partner_id and not self.ai_sys_content:
if self.ai_partner_id.gpt_id:
self.ai_sys_content = self.ai_partner_id.gpt_id.sys_content
@api.onchange('ext_ai_partner_id')
def _onchange_ext_ai_partner_id(self):
if self.ext_ai_partner_id and not self.ext_ai_sys_content:
if self.ext_ai_partner_id.gpt_id:
self.ai_sys_content = self.ext_ai_partner_id.gpt_id.sys_content
@api.onchange('set_chat_count')
def _onchange_set_chat_count(self):
if self.set_chat_count:
self.chat_count = int(self.set_chat_count) if self.set_chat_count != 'none' else 0

View File

@@ -6,8 +6,13 @@ 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')
], string='Sync Config', default='sync', config_parameter="app_chatgpt.openai_sync_config")
ai_chat_padding_time = fields.Integer(
string="Ai Padding Time", default=0.0, config_parameter="app_chatgpt.ai_chat_padding_time",
help="Amount of time (in seconds) between a conversation")