AI对话接口
This commit is contained in:
106
Server/application/chukebao/controller/AiChatController.php
Normal file
106
Server/application/chukebao/controller/AiChatController.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace app\chukebao\controller;
|
||||
|
||||
use app\ai\controller\DouBaoAI;
|
||||
use app\chukebao\controller\TokensRecordController as tokensRecord;
|
||||
use library\ResponseHelper;
|
||||
use think\Db;
|
||||
|
||||
class AiChatController extends BaseController
|
||||
{
|
||||
public function index(){
|
||||
$userId = $this->getUserInfo('id');
|
||||
$companyId = $this->getUserInfo('companyId');
|
||||
$friendId = $this->request->param('friendId', '');
|
||||
$wechatAccountId = $this->request->param('wechatAccountId', '');
|
||||
$content = $this->request->param('content', '');
|
||||
|
||||
if (empty($wechatAccountId) || empty($friendId)){
|
||||
return ResponseHelper::error('参数缺失');
|
||||
}
|
||||
|
||||
$tokens = Db::name('users')
|
||||
->where('id', $userId)
|
||||
->where('companyId', $companyId)
|
||||
->value('tokens');
|
||||
if ($tokens <= 0){
|
||||
return ResponseHelper::error('用户Tokens余额不足');
|
||||
}
|
||||
|
||||
|
||||
//读取AI配置
|
||||
$setting = Db::name('ai_settings')->where(['companyId' => $companyId,'userId' => $userId])->find();
|
||||
if(empty($setting)){
|
||||
return ResponseHelper::error('未找到配置信息,请先配置AI策略');
|
||||
}
|
||||
$config = json_decode($setting['config'],true);
|
||||
$modelSetting = $config['modelSetting'];
|
||||
$round = isset($config['round']) ? $config['round'] : 10;
|
||||
|
||||
|
||||
// 导出聊天
|
||||
$messages = Db::table('s2_wechat_message')
|
||||
->where('wechatFriendId', $friendId)
|
||||
->order('wechatTime desc')
|
||||
->field('id,content,msgType,isSend,wechatTime')
|
||||
->limit($round)
|
||||
->select();
|
||||
|
||||
usort($messages, function($a, $b) {
|
||||
return $a['wechatTime'] <=> $b['wechatTime'];
|
||||
});
|
||||
|
||||
//处理聊天数据
|
||||
$msg = [];
|
||||
foreach ($messages as $val){
|
||||
if (empty($val['content'])){
|
||||
continue;
|
||||
}
|
||||
if (!empty($val['isSend'])){
|
||||
$msg[] = '客服:' . $val['content'];
|
||||
}else{
|
||||
$msg[] = '用户:' . $val['content'];
|
||||
}
|
||||
}
|
||||
$content = implode("\n", $msg);
|
||||
|
||||
|
||||
$params = [
|
||||
'model' => 'doubao-1-5-pro-32k-250115',
|
||||
'messages' => [
|
||||
// ['role' => 'system', 'content' => '请完成跟客户的对话'],
|
||||
['role' => 'system', 'content' => '角色设定:' . $modelSetting['role']],
|
||||
['role' => 'system', 'content' => '公司背景:' . $modelSetting['businessBackground']],
|
||||
['role' => 'system', 'content' => '对话风格:' . $modelSetting['dialogueStyle']],
|
||||
['role' => 'user', 'content' => $content],
|
||||
],
|
||||
];
|
||||
|
||||
//AI处理
|
||||
$ai = new DouBaoAI();
|
||||
$res = $ai->text($params);
|
||||
$res = json_decode($res,true);
|
||||
|
||||
if ($res['code'] == 200) {
|
||||
//扣除Tokens
|
||||
$tokensRecord = new tokensRecord();
|
||||
$nickname = Db::table('s2_wechat_friend')->where(['id' => $friendId])->value('nickname');
|
||||
$remarks = !empty($nickname) ? '与好友【'.$nickname.'】聊天' : '与好友聊天';
|
||||
$data = [
|
||||
'tokens' => $res['data']['token'],
|
||||
'type' => 0,
|
||||
'form' => 1,
|
||||
'wechatAccountId' => $wechatAccountId,
|
||||
'friendIdOrGroupId' => $friendId,
|
||||
'remarks' => $remarks,
|
||||
];
|
||||
$tokensRecord->consumeTokens($data);
|
||||
return ResponseHelper::success($res['data']['content']);
|
||||
}else{
|
||||
return ResponseHelper::error($res['msg']);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user