Files
cunkebao_v3/Server/application/ai/controller/DouBaoAI.php

68 lines
1.9 KiB
PHP
Raw Normal View History

2025-08-22 10:23:05 +08:00
<?php
namespace app\ai\controller;
2025-09-13 10:45:32 +08:00
use app\common\util\JwtUtil;
2025-08-22 10:23:05 +08:00
use think\facade\Env;
2025-11-03 14:06:58 +08:00
use think\Controller;
2025-08-22 10:23:05 +08:00
2025-11-03 14:06:58 +08:00
class DouBaoAI extends Controller
2025-08-22 10:23:05 +08:00
{
protected $apiUrl;
protected $apiKey;
protected $headers;
2025-11-03 14:06:58 +08:00
public function __construct()
2025-08-22 10:23:05 +08:00
{
2025-11-03 14:06:58 +08:00
parent::__construct();
2025-08-22 10:23:05 +08:00
$this->apiUrl = Env::get('doubaoAi.api_url');
$this->apiKey = Env::get('doubaoAi.api_key');
// 设置请求头
$this->headers = [
'Content-Type: application/json',
'Authorization: Bearer ' . $this->apiKey
];
if (empty($this->apiKey) || empty($this->apiUrl)) {
return json_encode(['code' => 500, 'msg' => '参数缺失']);
}
}
public function text($params = [])
2025-08-22 10:23:05 +08:00
{
2025-11-03 14:06:58 +08:00
if (empty($params)){
$content = $this->request->param('content', '');
2025-12-29 16:09:22 +08:00
$model = $this->request->param('model', 'doubao-seed-1-8-251215');
if(empty($content)){
return json_encode(['code' => 500, 'msg' => '提示词缺失']);
}
$params = [
2025-12-29 16:09:22 +08:00
'model' => $model,
'messages' => [
['role' => 'system', 'content' => '你现在是存客宝的AI助理你精通中国大陆的法律'],
['role' => 'user', 'content' => $content],
],
];
2025-09-13 10:45:32 +08:00
}
2025-08-22 10:23:05 +08:00
$result = requestCurl($this->apiUrl, $params, 'POST', $this->headers, 'json');
$result = json_decode($result, true);
if(isset($result['error'])){
$error = $result['error'];
return json_encode(['code' => 500, 'msg' => $error['message']]);
}else{
$content = $result['choices'][0]['message']['content'];
$token = intval($result['usage']['total_tokens']) * 20;
exit_data($content);
return json_encode(['code' => 200, 'msg' => '成功','data' => ['token' => $token,'content' => $content]]);
2025-09-17 16:51:11 +08:00
}
}
2025-08-22 10:23:05 +08:00
}