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:
@@ -18,7 +18,7 @@ class AiRobot(models.Model):
|
|||||||
_name = 'ai.robot'
|
_name = 'ai.robot'
|
||||||
_description = 'Ai Robot'
|
_description = 'Ai Robot'
|
||||||
_order = 'sequence, name'
|
_order = 'sequence, name'
|
||||||
|
|
||||||
name = fields.Char(string='Name', translate=True, required=True)
|
name = fields.Char(string='Name', translate=True, required=True)
|
||||||
provider = fields.Selection(string="AI Provider", selection=[('openai', 'OpenAI'), ('azure', 'Azure')],
|
provider = fields.Selection(string="AI Provider", selection=[('openai', 'OpenAI'), ('azure', 'Azure')],
|
||||||
required=True, default='openai', change_default=True)
|
required=True, default='openai', change_default=True)
|
||||||
@@ -116,31 +116,35 @@ GPT-3 A set of models that can understand and generate natural language
|
|||||||
sequence = fields.Integer('Sequence', help="Determine the display order", default=10)
|
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.')
|
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')
|
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)
|
max_send_char = fields.Integer('Max Send Char', help='Max Send Prompt Length', default=8000)
|
||||||
image_avatar = fields.Image('Avatar')
|
image_avatar = fields.Image('Avatar')
|
||||||
partner_ids = fields.One2many('res.partner', 'gpt_id', string='Partner')
|
partner_ids = fields.One2many('res.partner', 'gpt_id', string='Partner')
|
||||||
partner_count = fields.Integer('#Partner', compute='_compute_partner_count', store=False)
|
partner_count = fields.Integer('#Partner', compute='_compute_partner_count', store=False)
|
||||||
active = fields.Boolean('Active', default=True)
|
active = fields.Boolean('Active', default=True)
|
||||||
|
|
||||||
def _compute_partner_count(self):
|
def _compute_partner_count(self):
|
||||||
for rec in self:
|
for rec in self:
|
||||||
rec.partner_count = len(rec.partner_ids)
|
rec.partner_count = len(rec.partner_ids)
|
||||||
|
|
||||||
def action_disconnect(self):
|
def action_disconnect(self):
|
||||||
requests.delete('https://chatgpt.com/v1/disconnect')
|
requests.delete('https://chatgpt.com/v1/disconnect')
|
||||||
|
|
||||||
def get_ai_pre(self, data, author_id=False, answer_id=False, param={}):
|
def get_ai_pre(self, data, author_id=False, answer_id=False, param={}):
|
||||||
# hook,都正常
|
# hook,都正常
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def get_msg_file_content(self, message):
|
||||||
|
# hook
|
||||||
|
return False
|
||||||
|
|
||||||
def get_ai(self, data, author_id=False, answer_id=False, param={}):
|
def get_ai(self, data, author_id=False, answer_id=False, param={}):
|
||||||
# 通用方法
|
# 通用方法
|
||||||
# author_id: 请求的 partner_id 对象
|
# author_id: 请求的 partner_id 对象
|
||||||
# answer_id: 回答的 partner_id 对象
|
# answer_id: 回答的 partner_id 对象
|
||||||
# param,dict 形式的参数
|
# param,dict 形式的参数
|
||||||
# 调整输出为2个参数:res_post详细内容,is_ai是否ai的响应
|
# 调整输出为2个参数:res_post详细内容,is_ai是否ai的响应
|
||||||
|
|
||||||
self.ensure_one()
|
self.ensure_one()
|
||||||
# 前置勾子,一般返回 False,有问题返回响应内容,用于处理敏感词等
|
# 前置勾子,一般返回 False,有问题返回响应内容,用于处理敏感词等
|
||||||
res_pre = self.get_ai_pre(data, author_id, answer_id, param)
|
res_pre = self.get_ai_pre(data, author_id, answer_id, param)
|
||||||
@@ -150,19 +154,19 @@ GPT-3 A set of models that can understand and generate natural language
|
|||||||
if not hasattr(self, 'get_%s' % self.provider):
|
if not hasattr(self, 'get_%s' % self.provider):
|
||||||
res = _('No robot provider found')
|
res = _('No robot provider found')
|
||||||
return res, {}, False
|
return res, {}, False
|
||||||
|
|
||||||
res = getattr(self, 'get_%s' % self.provider)(data, author_id, answer_id, param)
|
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
|
return res_post, usage, is_ai
|
||||||
|
|
||||||
def get_ai_origin(self, data, author_id=False, answer_id=False, param={}):
|
def get_ai_origin(self, data, author_id=False, answer_id=False, param={}):
|
||||||
# 通用方法
|
# 通用方法
|
||||||
# author_id: 请求的 partner_id 对象
|
# author_id: 请求的 partner_id 对象
|
||||||
# answer_id: 回答的 partner_id 对象
|
# answer_id: 回答的 partner_id 对象
|
||||||
# param,dict 形式的参数
|
# param,dict 形式的参数
|
||||||
# 调整输出为2个参数:res_post详细内容,is_ai是否ai的响应
|
# 调整输出为2个参数:res_post详细内容,is_ai是否ai的响应
|
||||||
|
|
||||||
self.ensure_one()
|
self.ensure_one()
|
||||||
# 前置勾子,一般返回 False,有问题返回响应内容,用于处理敏感词等
|
# 前置勾子,一般返回 False,有问题返回响应内容,用于处理敏感词等
|
||||||
res_pre = self.get_ai_pre(data, author_id, answer_id, param)
|
res_pre = self.get_ai_pre(data, author_id, answer_id, param)
|
||||||
@@ -172,12 +176,12 @@ GPT-3 A set of models that can understand and generate natural language
|
|||||||
if not hasattr(self, 'get_%s' % self.provider):
|
if not hasattr(self, 'get_%s' % self.provider):
|
||||||
res = _('No robot provider found')
|
res = _('No robot provider found')
|
||||||
return res, {}, False
|
return res, {}, False
|
||||||
|
|
||||||
res = getattr(self, 'get_%s' % self.provider)(data, author_id, answer_id, param)
|
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
|
return res
|
||||||
|
|
||||||
def get_ai_post(self, res, author_id=False, answer_id=False, param=None):
|
def get_ai_post(self, res, author_id=False, answer_id=False, param=None):
|
||||||
# hook,高级版要替代
|
# hook,高级版要替代
|
||||||
if param is None:
|
if param is None:
|
||||||
@@ -214,7 +218,7 @@ GPT-3 A set of models that can understand and generate natural language
|
|||||||
if sys_content:
|
if sys_content:
|
||||||
return {"role": "system", "content": sys_content}
|
return {"role": "system", "content": sys_content}
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
def get_ai_model_info(self):
|
def get_ai_model_info(self):
|
||||||
self.ensure_one()
|
self.ensure_one()
|
||||||
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {self.openapi_api_key}"}
|
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {self.openapi_api_key}"}
|
||||||
@@ -222,7 +226,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
|
o_url = "https://api.openai.com/v1/models/%s" % self.ai_model
|
||||||
if self.endpoint:
|
if self.endpoint:
|
||||||
o_url = self.endpoint.replace("/chat/completions", "") + "/models/%s" % self.ai_model
|
o_url = self.endpoint.replace("/chat/completions", "") + "/models/%s" % self.ai_model
|
||||||
|
|
||||||
response = requests.get(o_url, headers=headers, timeout=R_TIMEOUT)
|
response = requests.get(o_url, headers=headers, timeout=R_TIMEOUT)
|
||||||
response.close()
|
response.close()
|
||||||
if response:
|
if response:
|
||||||
@@ -231,29 +235,32 @@ GPT-3 A set of models that can understand and generate natural language
|
|||||||
else:
|
else:
|
||||||
r_text = 'No response.'
|
r_text = 'No response.'
|
||||||
raise UserError(r_text)
|
raise UserError(r_text)
|
||||||
|
|
||||||
def get_ai_list_model(self):
|
def get_ai_list_model(self):
|
||||||
self.ensure_one()
|
self.ensure_one()
|
||||||
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {self.openapi_api_key}"}
|
if self.provider == 'openai':
|
||||||
R_TIMEOUT = self.ai_timeout or 120
|
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {self.openapi_api_key}"}
|
||||||
o_url = "https://api.openai.com/v1/models"
|
R_TIMEOUT = self.ai_timeout or 120
|
||||||
if self.endpoint:
|
o_url = "https://api.openai.com/v1/models"
|
||||||
o_url = self.endpoint.replace("/chat/completions", "") + "/models"
|
if self.endpoint:
|
||||||
response = requests.get(o_url, headers=headers, timeout=R_TIMEOUT)
|
o_url = self.endpoint.replace("/chat/completions", "") + "/models"
|
||||||
response.close()
|
response = requests.get(o_url, headers=headers, timeout=R_TIMEOUT)
|
||||||
if response:
|
response.close()
|
||||||
res = response.json()
|
if response:
|
||||||
r_text = json.dumps(res, indent=2)
|
res = response.json()
|
||||||
|
r_text = json.dumps(res, indent=2)
|
||||||
|
else:
|
||||||
|
r_text = 'No response.'
|
||||||
else:
|
else:
|
||||||
r_text = 'No response.'
|
r_text = 'No response.'
|
||||||
raise UserError(r_text)
|
raise UserError(r_text)
|
||||||
|
|
||||||
def get_openai(self, data, author_id, answer_id, param={}):
|
def get_openai(self, data, author_id, answer_id, param={}):
|
||||||
self.ensure_one()
|
self.ensure_one()
|
||||||
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {self.openapi_api_key}"}
|
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {self.openapi_api_key}"}
|
||||||
R_TIMEOUT = self.ai_timeout or 120
|
R_TIMEOUT = self.ai_timeout or 120
|
||||||
o_url = self.endpoint or "https://api.openai.com/v1/chat/completions"
|
o_url = self.endpoint or "https://api.openai.com/v1/chat/completions"
|
||||||
|
|
||||||
# 处理传参,传过来的优先于 robot 默认的
|
# 处理传参,传过来的优先于 robot 默认的
|
||||||
max_tokens = param.get('max_tokens') if param.get('max_tokens') else self.max_tokens
|
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
|
temperature = param.get('temperature') if param.get('temperature') else self.temperature
|
||||||
@@ -261,7 +268,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
|
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
|
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
|
||||||
|
|
||||||
if self.stop:
|
if self.stop:
|
||||||
stop = self.stop.split(',')
|
stop = self.stop.split(',')
|
||||||
else:
|
else:
|
||||||
@@ -301,7 +308,7 @@ GPT-3 A set of models that can understand and generate natural language
|
|||||||
return res
|
return res
|
||||||
else:
|
else:
|
||||||
_logger.warning('=====================openai output data: %s' % response.json())
|
_logger.warning('=====================openai output data: %s' % response.json())
|
||||||
|
|
||||||
return _("Response Timeout, please speak again.")
|
return _("Response Timeout, please speak again.")
|
||||||
|
|
||||||
def get_azure(self, data, author_id, answer_id, param={}):
|
def get_azure(self, data, author_id, answer_id, param={}):
|
||||||
@@ -309,10 +316,10 @@ GPT-3 A set of models that can understand and generate natural language
|
|||||||
# only for azure
|
# only for azure
|
||||||
if not self.endpoint:
|
if not self.endpoint:
|
||||||
raise UserError(_("Please Set your AI robot's endpoint first."))
|
raise UserError(_("Please Set your AI robot's endpoint first."))
|
||||||
|
|
||||||
if not self.api_version:
|
if not self.api_version:
|
||||||
raise UserError(_("Please Set your AI robot's API Version first."))
|
raise UserError(_("Please Set your AI robot's API Version first."))
|
||||||
|
|
||||||
if self.stop:
|
if self.stop:
|
||||||
stop = self.stop.split(',')
|
stop = self.stop.split(',')
|
||||||
else:
|
else:
|
||||||
@@ -321,7 +328,7 @@ GPT-3 A set of models that can understand and generate natural language
|
|||||||
messages = data
|
messages = data
|
||||||
else:
|
else:
|
||||||
messages = [{"role": "user", "content": data}]
|
messages = [{"role": "user", "content": data}]
|
||||||
|
|
||||||
# 处理传参,传过来的优先于 robot 默认的
|
# 处理传参,传过来的优先于 robot 默认的
|
||||||
max_tokens = param.get('max_tokens') if param.get('max_tokens') else self.max_tokens
|
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
|
temperature = param.get('temperature') if param.get('temperature') else self.temperature
|
||||||
@@ -329,14 +336,14 @@ 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
|
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
|
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角色设定,如果没设定则再处理
|
# Ai角色设定,如果没设定则再处理
|
||||||
if messages[0].get('role') != 'system':
|
if messages[0].get('role') != 'system':
|
||||||
sys_content = self.get_ai_system(param.get('sys_content'))
|
sys_content = self.get_ai_system(param.get('sys_content'))
|
||||||
if sys_content:
|
if sys_content:
|
||||||
messages.insert(0, sys_content)
|
messages.insert(0, sys_content)
|
||||||
# 暂时不变
|
# 暂时不变
|
||||||
|
|
||||||
client = AzureOpenAI(
|
client = AzureOpenAI(
|
||||||
api_version=self.api_version,
|
api_version=self.api_version,
|
||||||
azure_endpoint=self.endpoint,
|
azure_endpoint=self.endpoint,
|
||||||
@@ -369,14 +376,14 @@ GPT-3 A set of models that can understand and generate natural language
|
|||||||
else:
|
else:
|
||||||
_logger.warning('=====================azure output data: %s' % response.json())
|
_logger.warning('=====================azure output data: %s' % response.json())
|
||||||
return _("Response Timeout, please speak again.")
|
return _("Response Timeout, please speak again.")
|
||||||
|
|
||||||
@api.onchange('provider')
|
@api.onchange('provider')
|
||||||
def _onchange_provider(self):
|
def _onchange_provider(self):
|
||||||
if self.provider == 'openai':
|
if self.provider == 'openai':
|
||||||
self.endpoint = 'https://api.openai.com/v1/chat/completions'
|
self.endpoint = 'https://api.openai.com/v1/chat/completions'
|
||||||
elif self.provider == 'azure':
|
elif self.provider == 'azure':
|
||||||
self.endpoint = 'https://odoo.openai.azure.com'
|
self.endpoint = 'https://odoo.openai.azure.com'
|
||||||
|
|
||||||
if self.provider:
|
if self.provider:
|
||||||
# 取头像
|
# 取头像
|
||||||
module_path = modules.get_module_path('app_chatgpt', display_warning=False)
|
module_path = modules.get_module_path('app_chatgpt', display_warning=False)
|
||||||
@@ -385,7 +392,7 @@ GPT-3 A set of models that can understand and generate natural language
|
|||||||
if path:
|
if path:
|
||||||
image_file = tools.file_open(path, 'rb')
|
image_file = tools.file_open(path, 'rb')
|
||||||
self.image_avatar = base64.b64encode(image_file.read())
|
self.image_avatar = base64.b64encode(image_file.read())
|
||||||
|
|
||||||
@api.onchange('set_ai_model')
|
@api.onchange('set_ai_model')
|
||||||
def _onchange_set_ai_model(self):
|
def _onchange_set_ai_model(self):
|
||||||
if self.set_ai_model:
|
if self.set_ai_model:
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ class Channel(models.Model):
|
|||||||
('-1', '允许较多重复'),
|
('-1', '允许较多重复'),
|
||||||
('-2', '更多强调重复'),
|
('-2', '更多强调重复'),
|
||||||
], string='Presence penalty', default='1', help="-2~2,值越大越少重复词")
|
], string='Presence penalty', default='1', help="-2~2,值越大越少重复词")
|
||||||
|
|
||||||
# todo: 这里用 compute?
|
# todo: 这里用 compute?
|
||||||
max_tokens = fields.Integer('最长响应Token', default=600, help="越大返回内容越多,计费也越多")
|
max_tokens = fields.Integer('最长响应Token', default=600, help="越大返回内容越多,计费也越多")
|
||||||
chat_count = fields.Integer(string="上下文数量", default=0, help="0~3,设定后,会将最近n次对话发给Ai,有助于他更好的回答")
|
chat_count = fields.Integer(string="上下文数量", default=0, help="0~3,设定后,会将最近n次对话发给Ai,有助于他更好的回答")
|
||||||
@@ -79,9 +79,9 @@ class Channel(models.Model):
|
|||||||
top_p = fields.Float(string="连贯性值", default=0.6, help="0~1,值越大越富有想像力,越小则越保守")
|
top_p = fields.Float(string="连贯性值", default=0.6, help="0~1,值越大越富有想像力,越小则越保守")
|
||||||
frequency_penalty = fields.Float('避免常用词值', default=1, help="-2~2,值越大越少使用常用词")
|
frequency_penalty = fields.Float('避免常用词值', default=1, help="-2~2,值越大越少使用常用词")
|
||||||
presence_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='是否当前用户默认微信对话频道')
|
is_current_channel = fields.Boolean('是否当前用户默认频道', compute='_compute_is_current_channel', help='是否当前用户默认微信对话频道')
|
||||||
|
|
||||||
# begin 处理Ai对话
|
# begin 处理Ai对话
|
||||||
is_ai_conversation = fields.Boolean('Ai Conversation', default=False,
|
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.')
|
help='Set active to make conversation between 2+ Ai Employee. You Just say first word, then Ai robots Auto Chat.')
|
||||||
@@ -91,9 +91,9 @@ class Channel(models.Model):
|
|||||||
# 辅助Ai角色设定
|
# 辅助Ai角色设定
|
||||||
ext_ai_sys_content = fields.Char('Extend Robot Role', change_default=True,
|
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.')
|
help='The Role the Second Ai robot play for. This is for Ai Conversation.')
|
||||||
|
|
||||||
# end 处理Ai对话
|
# end 处理Ai对话
|
||||||
|
|
||||||
def name_get(self):
|
def name_get(self):
|
||||||
result = []
|
result = []
|
||||||
for c in self:
|
for c in self:
|
||||||
@@ -103,7 +103,7 @@ class Channel(models.Model):
|
|||||||
pre = ''
|
pre = ''
|
||||||
result.append((c.id, "%s%s" % (pre, c.name or '')))
|
result.append((c.id, "%s%s" % (pre, c.name or '')))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def get_openai_context(self, channel_id, author_id, answer_id, minutes=60, chat_count=0):
|
def get_openai_context(self, channel_id, author_id, answer_id, minutes=60, chat_count=0):
|
||||||
# 上下文处理,要处理群的方式,以及独聊的方式
|
# 上下文处理,要处理群的方式,以及独聊的方式
|
||||||
# azure新api 处理
|
# azure新api 处理
|
||||||
@@ -113,7 +113,7 @@ class Channel(models.Model):
|
|||||||
# 处理消息: 取最新问题 + 上 chat_count=1次的交互,将之前的交互按时间顺序拼接。
|
# 处理消息: 取最新问题 + 上 chat_count=1次的交互,将之前的交互按时间顺序拼接。
|
||||||
# 注意: ai 每一次回复都有 parent_id 来处理连续性
|
# 注意: ai 每一次回复都有 parent_id 来处理连续性
|
||||||
# 私聊处理
|
# 私聊处理
|
||||||
|
|
||||||
# todo: 更好的处理方式
|
# todo: 更好的处理方式
|
||||||
domain = [('res_id', '=', channel_id),
|
domain = [('res_id', '=', channel_id),
|
||||||
('model', '=', 'discuss.channel'),
|
('model', '=', 'discuss.channel'),
|
||||||
@@ -122,7 +122,7 @@ class Channel(models.Model):
|
|||||||
('is_ai', '=', True),
|
('is_ai', '=', True),
|
||||||
('body', '!=', '<p>%s</p>' % _('Response Timeout, please speak again.')),
|
('body', '!=', '<p>%s</p>' % _('Response Timeout, please speak again.')),
|
||||||
('body', '!=', _('温馨提示:您发送的内容含有敏感词,请修改内容后再向我发送。'))]
|
('body', '!=', _('温馨提示:您发送的内容含有敏感词,请修改内容后再向我发送。'))]
|
||||||
|
|
||||||
if self.channel_type in ['group', 'channel']:
|
if self.channel_type in ['group', 'channel']:
|
||||||
# 群聊增加时间限制,当前找所有人,不限制 author_id
|
# 群聊增加时间限制,当前找所有人,不限制 author_id
|
||||||
domain = expression.AND([domain, [('date', '>=', afterTime)]])
|
domain = expression.AND([domain, [('date', '>=', afterTime)]])
|
||||||
@@ -148,11 +148,11 @@ class Channel(models.Model):
|
|||||||
'content': user_content,
|
'content': user_content,
|
||||||
})
|
})
|
||||||
return context_history
|
return context_history
|
||||||
|
|
||||||
def get_ai_config(self, ai):
|
def get_ai_config(self, ai):
|
||||||
# 勾子,用于取ai 配置
|
# 勾子,用于取ai 配置
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
def get_ai_response(self, ai, messages, channel, user_id, message):
|
def get_ai_response(self, ai, messages, channel, user_id, message):
|
||||||
author_id = message.create_uid.partner_id
|
author_id = message.create_uid.partner_id
|
||||||
answer_id = user_id.partner_id
|
answer_id = user_id.partner_id
|
||||||
@@ -179,10 +179,9 @@ class Channel(models.Model):
|
|||||||
'ai_completion_tokens': completion_tokens,
|
'ai_completion_tokens': completion_tokens,
|
||||||
'cost_tokens': total_tokens,
|
'cost_tokens': total_tokens,
|
||||||
})
|
})
|
||||||
|
|
||||||
def _notify_thread(self, message, msg_vals=False, **kwargs):
|
def _notify_thread(self, message, msg_vals=False, **kwargs):
|
||||||
rdata = super(Channel, self)._notify_thread(message, msg_vals=msg_vals, **kwargs)
|
rdata = super(Channel, self)._notify_thread(message, msg_vals=msg_vals, **kwargs)
|
||||||
# print(f'rdata:{rdata}')
|
|
||||||
answer_id = self.env['res.partner']
|
answer_id = self.env['res.partner']
|
||||||
user_id = self.env['res.users']
|
user_id = self.env['res.users']
|
||||||
author_id = msg_vals.get('author_id')
|
author_id = msg_vals.get('author_id')
|
||||||
@@ -191,13 +190,13 @@ class Channel(models.Model):
|
|||||||
channel_type = self.channel_type
|
channel_type = self.channel_type
|
||||||
messages = []
|
messages = []
|
||||||
add_sys_content = ''
|
add_sys_content = ''
|
||||||
|
|
||||||
# 不处理 一般notify,但处理欢迎
|
# 不处理 一般notify,但处理欢迎
|
||||||
if '<div class="o_mail_notification' in message.body and message.body != _('<div class="o_mail_notification">joined the channel</div>'):
|
if '<div class="o_mail_notification' in message.body and message.body != _('<div class="o_mail_notification">joined the channel</div>'):
|
||||||
return rdata
|
return rdata
|
||||||
if 'o_odoobot_command' in message.body:
|
if 'o_odoobot_command' in message.body:
|
||||||
return rdata
|
return rdata
|
||||||
|
|
||||||
# begin: 找ai,增加 ai二人转功能。 chat类型不用管, 使用其中一个ai登录即可。 author_id 是 res.partner 模型
|
# begin: 找ai,增加 ai二人转功能。 chat类型不用管, 使用其中一个ai登录即可。 author_id 是 res.partner 模型
|
||||||
if channel_type == 'chat':
|
if channel_type == 'chat':
|
||||||
channel_partner_ids = self.channel_partner_ids
|
channel_partner_ids = self.channel_partner_ids
|
||||||
@@ -209,7 +208,7 @@ class Channel(models.Model):
|
|||||||
is_allow = message.author_id.id in gpt_wl_partners.ids
|
is_allow = message.author_id.id in gpt_wl_partners.ids
|
||||||
if gpt_policy == 'all' or (gpt_policy == 'limit' and is_allow):
|
if gpt_policy == 'all' or (gpt_policy == 'limit' and is_allow):
|
||||||
ai = answer_id.sudo().gpt_id
|
ai = answer_id.sudo().gpt_id
|
||||||
|
|
||||||
elif channel_type in ['group', 'channel']:
|
elif channel_type in ['group', 'channel']:
|
||||||
# partner_ids = @ ids
|
# partner_ids = @ ids
|
||||||
partner_ids = list(msg_vals.get('partner_ids'))
|
partner_ids = list(msg_vals.get('partner_ids'))
|
||||||
@@ -266,31 +265,31 @@ class Channel(models.Model):
|
|||||||
# # 暂时有限用户的Ai
|
# # 暂时有限用户的Ai
|
||||||
# raise UserError(_('此Ai暂时未开放,请联系管理员。'))
|
# raise UserError(_('此Ai暂时未开放,请联系管理员。'))
|
||||||
# end: 找ai,增加 ai二人转功能
|
# end: 找ai,增加 ai二人转功能
|
||||||
|
|
||||||
if hasattr(ai, 'is_translator') and ai.is_translator and ai.ai_model == 'translator':
|
if hasattr(ai, 'is_translator') and ai.is_translator and ai.ai_model == 'translator':
|
||||||
return rdata
|
return rdata
|
||||||
chatgpt_channel_id = self.env.ref('app_chatgpt.channel_chatgpt')
|
chatgpt_channel_id = self.env.ref('app_chatgpt.channel_chatgpt')
|
||||||
|
|
||||||
if message.body == _('<div class="o_mail_notification">joined the channel</div>'):
|
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
|
msg = _("Please warmly welcome our new partner %s and send him the best wishes.") % message.author_id.name
|
||||||
else:
|
else:
|
||||||
# 不能用 preview, 如果用 : 提示词则 preview信息丢失
|
# 不能用 preview, 如果用 : 提示词则 preview信息丢失
|
||||||
plaintext_ct = tools.html_to_inner_content(message.body)
|
plaintext_ct = tools.mail.html_to_inner_content(message.body)
|
||||||
msg = plaintext_ct.replace('@%s' % answer_id.name, '').lstrip()
|
msg = plaintext_ct.replace('@%s' % answer_id.name, '').lstrip()
|
||||||
|
|
||||||
if not msg:
|
if not msg:
|
||||||
return rdata
|
return rdata
|
||||||
|
|
||||||
if self._context.get('app_ai_sync_config') and self._context.get('app_ai_sync_config') in ['sync', 'async']:
|
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')
|
sync_config = self._context.get('app_ai_sync_config')
|
||||||
else:
|
else:
|
||||||
sync_config = self.env['ir.config_parameter'].sudo().get_param('app_chatgpt.openai_sync_config')
|
sync_config = self.env['ir.config_parameter'].sudo().get_param('app_chatgpt.openai_sync_config')
|
||||||
|
|
||||||
if self._context.get('app_ai_chat_padding_time'):
|
if self._context.get('app_ai_chat_padding_time'):
|
||||||
padding_time = int(self._context.get('app_ai_chat_padding_time'))
|
padding_time = int(self._context.get('app_ai_chat_padding_time'))
|
||||||
else:
|
else:
|
||||||
padding_time = int(self.env['ir.config_parameter'].sudo().get_param('app_chatgpt.ai_chat_padding_time'))
|
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')
|
# api_key = self.env['ir.config_parameter'].sudo().get_param('app_chatgpt.openapi_api_key')
|
||||||
# ai处理,不要自问自答
|
# ai处理,不要自问自答
|
||||||
if ai and answer_id != message.author_id:
|
if ai and answer_id != message.author_id:
|
||||||
@@ -298,6 +297,7 @@ class Channel(models.Model):
|
|||||||
if not api_key:
|
if not api_key:
|
||||||
_logger.warning(_("ChatGPT Robot【%s】have not set open api key.") % ai.name)
|
_logger.warning(_("ChatGPT Robot【%s】have not set open api key.") % ai.name)
|
||||||
return rdata
|
return rdata
|
||||||
|
|
||||||
try:
|
try:
|
||||||
openapi_context_timeout = int(self.env['ir.config_parameter'].sudo().get_param('app_chatgpt.openapi_context_timeout')) or 60
|
openapi_context_timeout = int(self.env['ir.config_parameter'].sudo().get_param('app_chatgpt.openapi_context_timeout')) or 60
|
||||||
except:
|
except:
|
||||||
@@ -308,7 +308,7 @@ class Channel(models.Model):
|
|||||||
chat_count = 1
|
chat_count = 1
|
||||||
else:
|
else:
|
||||||
chat_count = self.chat_count or 3
|
chat_count = self.chat_count or 3
|
||||||
|
|
||||||
if author_id != answer_id.id and self.channel_type == 'chat':
|
if author_id != answer_id.id and self.channel_type == 'chat':
|
||||||
# 私聊
|
# 私聊
|
||||||
_logger.info(f'私聊:author_id:{author_id},partner_chatgpt.id:{answer_id.id}')
|
_logger.info(f'私聊:author_id:{author_id},partner_chatgpt.id:{answer_id.id}')
|
||||||
@@ -320,26 +320,56 @@ class Channel(models.Model):
|
|||||||
elif author_id != answer_id.id and msg_vals.get('model', '') == 'discuss.channel' and self.channel_type in ['group', 'channel']:
|
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'))
|
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:
|
try:
|
||||||
# 处理提示词
|
# 处理提示词
|
||||||
sys_content = channel.description + add_sys_content
|
sys_content = '%s%s' % (channel.description if channel.description else "", add_sys_content if add_sys_content else "")
|
||||||
messages.append({"role": "system", "content": sys_content})
|
if len(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)
|
c_history = self.get_openai_context(channel.id, author_id, answer_id, openapi_context_timeout, chat_count)
|
||||||
if c_history:
|
if c_history:
|
||||||
messages += c_history
|
messages += c_history
|
||||||
messages.append({"role": "user", "content": msg})
|
if message.attachment_ids:
|
||||||
|
attachment = message.attachment_ids[:1]
|
||||||
|
file_content = ai.get_msg_file_content(message)
|
||||||
|
if not file_content:
|
||||||
|
messages.append({"role": "user", "content": msg})
|
||||||
|
if attachment.mimetype in ['image/jpeg', 'image/png', 'image/gif', 'image/bmp', 'image/webp']:
|
||||||
|
messages.append({
|
||||||
|
"role": "user",
|
||||||
|
"content": [
|
||||||
|
{
|
||||||
|
"type": "image_url",
|
||||||
|
"image_url": {
|
||||||
|
"url": file_content,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"text": msg
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
messages.append({"role": "system", "content": file_content})
|
||||||
|
messages.append({"role": "user", "content": msg})
|
||||||
|
else:
|
||||||
|
messages.append({"role": "user", "content": msg})
|
||||||
msg_len = sum(len(str(m)) for m in messages)
|
msg_len = sum(len(str(m)) for m in messages)
|
||||||
# 接口最大接收 8430 Token
|
# 接口最大接收 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:
|
if msg_len * 2 > ai.max_send_char:
|
||||||
messages = []
|
new_msg = channel.with_user(user_id).message_post(body=_('您所发送的提示词已超长。'), message_type='comment',
|
||||||
messages.append({"role": "user", "content": msg})
|
subtype_xmlid='mail.mt_comment',
|
||||||
msg_len = sum(len(str(m)) for m in messages)
|
parent_id=message.id)
|
||||||
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:
|
# if msg_len * 2 >= 8000:
|
||||||
# messages = [{"role": "user", "content": msg}]
|
# messages = [{"role": "user", "content": msg}]
|
||||||
if sync_config == 'sync':
|
if sync_config == 'sync':
|
||||||
@@ -350,17 +380,17 @@ class Channel(models.Model):
|
|||||||
else:
|
else:
|
||||||
self.get_ai_response(ai, messages, channel, user_id, message)
|
self.get_ai_response(ai, messages, channel, user_id, message)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise UserError(_(e))
|
raise UserError(e)
|
||||||
|
|
||||||
return rdata
|
return rdata
|
||||||
|
|
||||||
def _message_post_after_hook(self, message, msg_vals):
|
def _message_post_after_hook(self, message, msg_vals):
|
||||||
if message.author_id.gpt_id:
|
if message.author_id.gpt_id:
|
||||||
if msg_vals['body'] not in [_('Response Timeout, please speak again.'), _('温馨提示:您发送的内容含有敏感词,请修改内容后再向我发送。'),
|
if msg_vals['body'] not in [_('Response Timeout, please speak again.'), _('温馨提示:您发送的内容含有敏感词,请修改内容后再向我发送。'),
|
||||||
_('此Ai暂时未开放,请联系管理员。'), _('您所发送的提示词已超长。')]:
|
_('此Ai暂时未开放,请联系管理员。'), _('您所发送的提示词已超长。')]:
|
||||||
message.is_ai = True
|
message.is_ai = True
|
||||||
return super(Channel, self)._message_post_after_hook(message, msg_vals)
|
return super(Channel, self)._message_post_after_hook(message, msg_vals)
|
||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
def _get_my_last_cid(self):
|
def _get_my_last_cid(self):
|
||||||
# 获取当前用户最后一次进入的channel,返回该channel的id
|
# 获取当前用户最后一次进入的channel,返回该channel的id
|
||||||
@@ -383,7 +413,7 @@ class Channel(models.Model):
|
|||||||
if c and not c.is_member:
|
if c and not c.is_member:
|
||||||
c.sudo().add_members([user.partner_id.id])
|
c.sudo().add_members([user.partner_id.id])
|
||||||
return c_id
|
return c_id
|
||||||
|
|
||||||
@api.onchange('ai_partner_id')
|
@api.onchange('ai_partner_id')
|
||||||
def _onchange_ai_partner_id(self):
|
def _onchange_ai_partner_id(self):
|
||||||
if self.ai_partner_id and self.ai_partner_id.image_1920:
|
if self.ai_partner_id and self.ai_partner_id.image_1920:
|
||||||
@@ -391,13 +421,13 @@ class Channel(models.Model):
|
|||||||
if self.ai_partner_id and not self.ai_sys_content:
|
if self.ai_partner_id and not self.ai_sys_content:
|
||||||
if self.ai_partner_id.gpt_id:
|
if self.ai_partner_id.gpt_id:
|
||||||
self.ai_sys_content = self.ai_partner_id.gpt_id.sys_content
|
self.ai_sys_content = self.ai_partner_id.gpt_id.sys_content
|
||||||
|
|
||||||
@api.onchange('ext_ai_partner_id')
|
@api.onchange('ext_ai_partner_id')
|
||||||
def _onchange_ext_ai_partner_id(self):
|
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 and not self.ext_ai_sys_content:
|
||||||
if self.ext_ai_partner_id.gpt_id:
|
if self.ext_ai_partner_id.gpt_id:
|
||||||
self.ai_sys_content = self.ext_ai_partner_id.gpt_id.sys_content
|
self.ai_sys_content = self.ext_ai_partner_id.gpt_id.sys_content
|
||||||
|
|
||||||
@api.onchange('set_chat_count')
|
@api.onchange('set_chat_count')
|
||||||
def _onchange_set_chat_count(self):
|
def _onchange_set_chat_count(self):
|
||||||
if self.set_chat_count:
|
if self.set_chat_count:
|
||||||
|
|||||||
@@ -32,9 +32,9 @@
|
|||||||
<field name="ext_ai_partner_id" widget="many2one_avatar"
|
<field name="ext_ai_partner_id" widget="many2one_avatar"
|
||||||
options="{'no_open': True, 'no_create': True}"/>
|
options="{'no_open': True, 'no_create': True}"/>
|
||||||
<field name="is_ai_conversation" widget="boolean_toggle"/>
|
<field name="is_ai_conversation" widget="boolean_toggle"/>
|
||||||
<field name="ai_sys_content" attrs="{'invisible':[('is_ai_conversation','!=',True)]}"
|
<field name="ai_sys_content" invisible="not is_ai_conversation"
|
||||||
widget="text" rowCount="4" colspan="2"/>
|
widget="text" rowCount="4" colspan="2"/>
|
||||||
<field name="ext_ai_sys_content" attrs="{'invisible':[('is_ai_conversation','!=',True)]}"
|
<field name="ext_ai_sys_content" invisible="not is_ai_conversation"
|
||||||
widget="text" rowCount="4" colspan="2"/>
|
widget="text" rowCount="4" colspan="2"/>
|
||||||
</group>
|
</group>
|
||||||
<group name="param_set" string="Ai Character Set">
|
<group name="param_set" string="Ai Character Set">
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" ?>
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
<odoo>
|
<odoo>
|
||||||
<record id="app_ai_robot_deepseek_form_view" model="ir.ui.view">
|
<!-- <record id="app_ai_robot_deepseek_form_view" model="ir.ui.view">
|
||||||
<field name="name">app.ai.robot.deepseek.form</field>
|
<field name="name">app.ai.robot.deepseek.form</field>
|
||||||
<field name="model">ai.robot</field>
|
<field name="model">ai.robot</field>
|
||||||
<field name="inherit_id" ref="app_chatgpt.ai_robot_form_view"/>
|
<field name="inherit_id" ref="app_chatgpt.ai_robot_form_view"/>
|
||||||
@@ -9,5 +9,5 @@
|
|||||||
<attribute name="attrs">{'invisible': 0}</attribute>
|
<attribute name="attrs">{'invisible': 0}</attribute>
|
||||||
</xpath>
|
</xpath>
|
||||||
</field>
|
</field>
|
||||||
</record>
|
</record> -->
|
||||||
</odoo>
|
</odoo>
|
||||||
|
|||||||
Reference in New Issue
Block a user