mirror of
https://github.com/guohuadeng/app-odoo.git
synced 2025-02-23 04:11:36 +02:00
Merge branch '18.0' of github.com:guohuadeng/app-odoo into 18.0
This commit is contained in:
@@ -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,7 +111,7 @@ 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.')
|
||||
@@ -156,7 +157,7 @@ GPT-3 A set of models that can understand and generate natural language
|
||||
|
||||
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={}):
|
||||
@@ -187,7 +188,6 @@ GPT-3 A set of models that can understand and generate natural language
|
||||
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':
|
||||
@@ -298,7 +298,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,
|
||||
@@ -335,7 +335,7 @@ GPT-3 A set of models that can understand and generate natural language
|
||||
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':
|
||||
|
||||
@@ -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标准'),
|
||||
@@ -82,11 +82,26 @@ class Channel(models.Model):
|
||||
|
||||
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):
|
||||
@@ -174,6 +189,7 @@ 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>'):
|
||||
@@ -181,6 +197,7 @@ class Channel(models.Model):
|
||||
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
|
||||
@@ -196,12 +213,20 @@ class Channel(models.Model):
|
||||
# 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>'):
|
||||
@@ -239,6 +264,8 @@ 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')
|
||||
@@ -247,7 +274,7 @@ class Channel(models.Model):
|
||||
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:
|
||||
@@ -257,12 +284,18 @@ class Channel(models.Model):
|
||||
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:
|
||||
@@ -271,14 +304,10 @@ 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':
|
||||
# 私聊
|
||||
@@ -300,6 +329,9 @@ class Channel(models.Model):
|
||||
messages.append({"role": "system", "content": file_content})
|
||||
|
||||
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
|
||||
@@ -323,11 +355,11 @@ class Channel(models.Model):
|
||||
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
|
||||
|
||||
@@ -365,3 +397,17 @@ class Channel(models.Model):
|
||||
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
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user