mirror of
https://github.com/guohuadeng/app-odoo.git
synced 2025-02-23 04:11:36 +02:00
Merge remote-tracking branch 'origin/16.0' into 16.0
This commit is contained in:
@@ -5,3 +5,4 @@ from . import mail_channel
|
|||||||
from . import res_config_settings
|
from . import res_config_settings
|
||||||
from . import gpt_robot
|
from . import gpt_robot
|
||||||
from . import res_users
|
from . import res_users
|
||||||
|
from . import mail_message
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import requests
|
||||||
from odoo import api, fields, models, _
|
from odoo import api, fields, models, _
|
||||||
|
|
||||||
|
|
||||||
@@ -8,8 +9,11 @@ class GptRobot(models.Model):
|
|||||||
_description = 'Gpt Robot'
|
_description = 'Gpt Robot'
|
||||||
_order = 'sequence, name'
|
_order = 'sequence, name'
|
||||||
|
|
||||||
|
|
||||||
name = fields.Char(string='Name')
|
name = fields.Char(string='Name')
|
||||||
openapi_api_key = fields.Char(string="API Key", help="Provide the API key here")
|
openapi_api_key = fields.Char(string="API Key", help="Provide the API key here")
|
||||||
temperature = fields.Float(string='Temperature', default=0.9)
|
temperature = fields.Float(string='Temperature', default=0.9)
|
||||||
sequence = fields.Integer('Sequence', help="Determine the display order", default=10)
|
sequence = fields.Integer('Sequence', help="Determine the display order", default=10)
|
||||||
|
|
||||||
|
def action_disconnect(self):
|
||||||
|
requests.delete('https://chatgpt.com/v1/disconnect')
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ from odoo import api, fields, models, _
|
|||||||
from odoo.exceptions import UserError
|
from odoo.exceptions import UserError
|
||||||
import logging
|
import logging
|
||||||
_logger = logging.getLogger(__name__)
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Channel(models.Model):
|
class Channel(models.Model):
|
||||||
_inherit = 'mail.channel'
|
_inherit = 'mail.channel'
|
||||||
|
|
||||||
@@ -18,7 +20,7 @@ class Channel(models.Model):
|
|||||||
"model": "text-davinci-003",
|
"model": "text-davinci-003",
|
||||||
"prompt": data,
|
"prompt": data,
|
||||||
"temperature": 0.9,
|
"temperature": 0.9,
|
||||||
"max_tokens": 1000,
|
"max_tokens": 2000,
|
||||||
"top_p": 1,
|
"top_p": 1,
|
||||||
"frequency_penalty": 0.0,
|
"frequency_penalty": 0.0,
|
||||||
"presence_penalty": 0.6,
|
"presence_penalty": 0.6,
|
||||||
@@ -80,69 +82,81 @@ 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}')
|
# print(f'rdata:{rdata}')
|
||||||
|
to_partner_id = self.env['res.partner']
|
||||||
chatgpt_channel_id = self.env.ref('app_chatgpt.channel_chatgpt')
|
user_id = self.env['res.users']
|
||||||
user_chatgpt = self.env.ref("app_chatgpt.user_chatgpt")
|
|
||||||
partner_chatgpt = self.env.ref("app_chatgpt.partner_chatgpt")
|
|
||||||
|
|
||||||
author_id = msg_vals.get('author_id')
|
author_id = msg_vals.get('author_id')
|
||||||
# print('author_id:',author_id)
|
|
||||||
|
|
||||||
gpt_id = self.env['gpt.robot']
|
gpt_id = self.env['gpt.robot']
|
||||||
# todo: 应该先确定 gpt_id,才处理对话黑名单。 黑名单是指是否允许与 gpt对话,不是是否允许绑定 gpt
|
channel_type = self.channel_type
|
||||||
partner_ids = list(msg_vals.get('partner_ids'))
|
if channel_type == 'chat':
|
||||||
if partner_ids:
|
channel_partner_ids = self.channel_partner_ids
|
||||||
partners = self.env['res.partner'].search([('id', 'in', partner_ids)])
|
to_partner_id = channel_partner_ids - message.author_id
|
||||||
user_id = partners.mapped('user_ids').filtered(lambda r: r.gpt_id)[:1]
|
user_id = to_partner_id.mapped('user_ids').filtered(lambda r: r.gpt_id)[:1]
|
||||||
if user_id:
|
if user_id:
|
||||||
gpt_policy = user_id.gpt_policy
|
gpt_policy = user_id.gpt_policy
|
||||||
gpt_wl_users = user_id.gpt_wl_users
|
gpt_wl_users = user_id.gpt_wl_users
|
||||||
is_allow = message.create_uid.id in gpt_wl_users.ids
|
is_allow = message.create_uid.id in gpt_wl_users.ids
|
||||||
if gpt_policy == 'all' or (gpt_policy == 'limit' and is_allow):
|
if gpt_policy == 'all' or (gpt_policy == 'limit' and is_allow):
|
||||||
user_chatgpt = user_id
|
|
||||||
partner_chatgpt = user_id.partner_id
|
|
||||||
gpt_id = user_id.gpt_id
|
gpt_id = user_id.gpt_id
|
||||||
|
|
||||||
|
elif channel_type in ['group', 'channel']:
|
||||||
|
# partner_ids = @ ids
|
||||||
|
partner_ids = list(msg_vals.get('partner_ids'))
|
||||||
|
if partner_ids:
|
||||||
|
partners = self.env['res.partner'].search([('id', 'in', partner_ids)])
|
||||||
|
# user_id = user has binded gpt robot
|
||||||
|
user_id = partners.mapped('user_ids').filtered(lambda r: r.gpt_id)[:1]
|
||||||
|
if user_id:
|
||||||
|
gpt_policy = user_id.gpt_policy
|
||||||
|
gpt_wl_users = user_id.gpt_wl_users
|
||||||
|
is_allow = message.create_uid.id in gpt_wl_users.ids
|
||||||
|
to_partner_id = user_id.partner_id
|
||||||
|
if gpt_policy == 'all' or (gpt_policy == 'limit' and is_allow):
|
||||||
|
gpt_id = user_id.gpt_id
|
||||||
|
|
||||||
|
chatgpt_channel_id = self.env.ref('app_chatgpt.channel_chatgpt')
|
||||||
|
|
||||||
|
# print('author_id:',author_id)
|
||||||
|
|
||||||
# print('partner_chatgpt.id:',partner_chatgpt.id)
|
# print('partner_chatgpt.id:',partner_chatgpt.id)
|
||||||
chatgpt_name = str(partner_chatgpt.name or '') + ', '
|
|
||||||
# print('chatgpt_name:', chatgpt_name)
|
|
||||||
prompt = msg_vals.get('body')
|
prompt = msg_vals.get('body')
|
||||||
# print('prompt:', prompt)
|
# print('prompt:', prompt)
|
||||||
# print('-----')
|
# print('-----')
|
||||||
if not prompt:
|
if not prompt:
|
||||||
return rdata
|
return rdata
|
||||||
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')
|
||||||
|
api_key = ''
|
||||||
if gpt_id:
|
if gpt_id:
|
||||||
api_key = gpt_id.openapi_api_key
|
api_key = gpt_id.openapi_api_key
|
||||||
|
if not api_key:
|
||||||
|
_logger.warning(_("ChatGPT Robot【%s】have not set open api key."))
|
||||||
|
return rdata
|
||||||
try:
|
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 600
|
||||||
except:
|
except:
|
||||||
openapi_context_timeout = 600
|
openapi_context_timeout = 600
|
||||||
|
|
||||||
openai.api_key = api_key
|
openai.api_key = api_key
|
||||||
Partner = self.env['res.partner']
|
|
||||||
partner_name = ''
|
partner_name = ''
|
||||||
if author_id:
|
|
||||||
partner_id = Partner.browse(author_id)
|
|
||||||
if partner_id:
|
|
||||||
user_id = partner_id.user_ids[:1]
|
|
||||||
if user_id.gpt_id:
|
|
||||||
return rdata
|
|
||||||
partner_name = partner_id.name
|
|
||||||
# print(msg_vals)
|
# print(msg_vals)
|
||||||
# print(msg_vals.get('record_name', ''))
|
# print(msg_vals.get('record_name', ''))
|
||||||
# print('self.channel_type :',self.channel_type)
|
# print('self.channel_type :',self.channel_type)
|
||||||
if author_id != partner_chatgpt.id and (chatgpt_name in msg_vals.get('record_name', '') or 'ChatGPT,' in msg_vals.get('record_name', '') ) and self.channel_type == 'chat':
|
if gpt_id:
|
||||||
_logger.info(f'私聊:author_id:{author_id},partner_chatgpt.id:{partner_chatgpt.id}')
|
chatgpt_name = str(gpt_id.name or '') + ', '
|
||||||
|
# print('chatgpt_name:', chatgpt_name)
|
||||||
|
# if author_id != to_partner_id.id and (chatgpt_name in msg_vals.get('record_name', '') or 'ChatGPT' in msg_vals.get('record_name', '') ) and self.channel_type == 'chat':
|
||||||
|
if author_id != to_partner_id.id and self.channel_type == 'chat':
|
||||||
|
_logger.info(f'私聊:author_id:{author_id},partner_chatgpt.id:{to_partner_id.id}')
|
||||||
try:
|
try:
|
||||||
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'))
|
||||||
prompt = self.get_openai_context(channel.id, partner_chatgpt.id, prompt,openapi_context_timeout)
|
prompt = self.get_openai_context(channel.id, to_partner_id.id, prompt,openapi_context_timeout)
|
||||||
print(prompt)
|
print(prompt)
|
||||||
# res = self.get_chatgpt_answer(prompt,partner_name)
|
# res = self.get_chatgpt_answer(prompt,partner_name)
|
||||||
res = self.get_openai(api_key, prompt, partner_name)
|
res = self.get_openai(api_key, prompt, partner_name)
|
||||||
|
res = res.replace('\n', '<br/>')
|
||||||
# print('res:',res)
|
# print('res:',res)
|
||||||
# print('channel:',channel)
|
# print('channel:',channel)
|
||||||
channel.with_user(user_chatgpt).message_post(body=res, message_type='comment',subtype_xmlid='mail.mt_comment',parent_id=message.id)
|
channel.with_user(user_id).message_post(body=res, message_type='comment',subtype_xmlid='mail.mt_comment',parent_id=message.id)
|
||||||
# channel.with_user(user_chatgpt).message_post(body=res, message_type='notification', subtype_xmlid='mail.mt_comment')
|
# channel.with_user(user_chatgpt).message_post(body=res, message_type='notification', subtype_xmlid='mail.mt_comment')
|
||||||
# channel.sudo().message_post(
|
# channel.sudo().message_post(
|
||||||
# body=res,
|
# body=res,
|
||||||
@@ -154,14 +168,15 @@ class Channel(models.Model):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise UserError(_(e))
|
raise UserError(_(e))
|
||||||
|
|
||||||
elif author_id != partner_chatgpt.id and msg_vals.get('model', '') == 'mail.channel' and msg_vals.get('res_id', 0) == chatgpt_channel_id.id:
|
elif author_id != to_partner_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:{partner_chatgpt.id}')
|
_logger.info(f'频道群聊:author_id:{author_id},partner_chatgpt.id:{to_partner_id.id}')
|
||||||
try:
|
try:
|
||||||
prompt = self.get_openai_context(chatgpt_channel_id.id, partner_chatgpt.id, prompt,openapi_context_timeout)
|
prompt = self.get_openai_context(chatgpt_channel_id.id, to_partner_id.id, prompt,openapi_context_timeout)
|
||||||
# print(prompt)
|
# print(prompt)
|
||||||
# res = self.get_chatgpt_answer(prompt, partner_name)
|
# res = self.get_chatgpt_answer(prompt, partner_name)
|
||||||
res = self.get_openai(api_key, prompt, partner_name)
|
res = self.get_openai(api_key, prompt, partner_name)
|
||||||
chatgpt_channel_id.with_user(user_chatgpt).message_post(body=res, message_type='comment', subtype_xmlid='mail.mt_comment',parent_id=message.id)
|
res = res.replace('\n', '<br/>')
|
||||||
|
chatgpt_channel_id.with_user(user_id).message_post(body=res, message_type='comment', subtype_xmlid='mail.mt_comment',parent_id=message.id)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise UserError(_(e))
|
raise UserError(_(e))
|
||||||
|
|
||||||
|
|||||||
13
app_chatgpt/models/mail_message.py
Normal file
13
app_chatgpt/models/mail_message.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from odoo import fields, models
|
||||||
|
|
||||||
|
|
||||||
|
class Message(models.Model):
|
||||||
|
_inherit = "mail.message"
|
||||||
|
|
||||||
|
def _message_add_reaction(self, content):
|
||||||
|
super(Message, self)._message_add_reaction(content)
|
||||||
|
if self.create_uid.gpt_id:
|
||||||
|
# 处理反馈
|
||||||
|
pass
|
||||||
@@ -43,6 +43,15 @@
|
|||||||
</field>
|
</field>
|
||||||
</record>
|
</record>
|
||||||
|
|
||||||
|
<record id="model_gpt_robot_action_disconnect" model="ir.actions.server">
|
||||||
|
<field name="name">Disconnect</field>
|
||||||
|
<field name="model_id" ref="app_chatgpt.model_gpt_robot"/>
|
||||||
|
<field name="binding_model_id" ref="app_chatgpt.model_gpt_robot"/>
|
||||||
|
<field name="binding_view_types">list,form</field>
|
||||||
|
<field name="state">code</field>
|
||||||
|
<field name="code">action = records.action_disconnect()</field>
|
||||||
|
</record>
|
||||||
|
|
||||||
<menuitem
|
<menuitem
|
||||||
id="menu_gpt_robot"
|
id="menu_gpt_robot"
|
||||||
name="GPT Robot"
|
name="GPT Robot"
|
||||||
|
|||||||
Reference in New Issue
Block a user