ai群公告接口提交包含扣费功能
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace app\chukebao\controller;
|
||||
|
||||
use app\ai\controller\DouBaoAI;
|
||||
use app\chukebao\controller\TokensRecordController as tokensRecord;
|
||||
use library\ResponseHelper;
|
||||
use think\Db;
|
||||
|
||||
@@ -78,4 +80,65 @@ class WechatChatroomController extends BaseController
|
||||
|
||||
return ResponseHelper::success(['list'=>$list,'total'=>$total]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function aiAnnouncement()
|
||||
{
|
||||
$userId = $this->getUserInfo('id');
|
||||
$companyId = $this->getUserInfo('companyId');
|
||||
$wechatAccountId = $this->request->param('wechatAccountId', '');
|
||||
$groupId = $this->request->param('groupId', '');
|
||||
$content = $this->request->param('content', '');
|
||||
|
||||
if (empty($groupId) || empty($content)|| empty($wechatAccountId)){
|
||||
return ResponseHelper::error('参数缺失');
|
||||
}
|
||||
|
||||
$tokens = Db::name('users')
|
||||
->where('id', $userId)
|
||||
->where('companyId', $companyId)
|
||||
->value('tokens');
|
||||
if ($tokens <= 0){
|
||||
return ResponseHelper::error('用户Tokens余额不足');
|
||||
}
|
||||
|
||||
$params = [
|
||||
'model' => 'doubao-1-5-pro-32k-250115',
|
||||
'messages' => [
|
||||
['role' => 'system', 'content' => '你现在是存客宝的AI助理,你精通中国大陆的法律'],
|
||||
['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_chatroom')->where(['id' => $groupId])->value('nickname');
|
||||
$remarks = !empty($nickname) ? '生成【'.$nickname.'】群公告' : '生成群公告';
|
||||
$data = [
|
||||
'tokens' => $res['data']['token'],
|
||||
'type' => 0,
|
||||
'form' => 3,
|
||||
'wechatAccountId' => $wechatAccountId,
|
||||
'friendIdOrGroupId' => $groupId,
|
||||
'remarks' => $remarks,
|
||||
];
|
||||
$tokensRecord->consumeTokens($data);
|
||||
return ResponseHelper::success($res['data']['content']);
|
||||
}else{
|
||||
return ResponseHelper::error($res['msg']);
|
||||
}
|
||||
|
||||
|
||||
exit_data($res);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user