Merge branch '18.0' of github.com:guohuadeng/app-odoo into 18.0

This commit is contained in:
Ivan Office
2025-02-11 16:18:43 +08:00
4 changed files with 120 additions and 83 deletions

View File

@@ -134,6 +134,10 @@ GPT-3 A set of models that can understand and generate natural language
# 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 对象
@@ -234,16 +238,19 @@ GPT-3 A set of models that can understand and generate natural language
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)

View File

@@ -182,7 +182,6 @@ class Channel(models.Model):
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')
@@ -275,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 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:
@@ -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:
@@ -320,25 +320,55 @@ 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}]
@@ -350,7 +380,7 @@ 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

View File

@@ -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">

View File

@@ -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>