59 lines
1.5 KiB
PHP
59 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace app\ai\controller;
|
|
|
|
use app\common\util\JwtUtil;
|
|
use think\facade\Env;
|
|
|
|
class DouBaoAI
|
|
{
|
|
protected $apiUrl;
|
|
protected $apiKey;
|
|
protected $headers;
|
|
|
|
public function __init()
|
|
{
|
|
$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()
|
|
{
|
|
$this->__init();
|
|
|
|
$content = input('content','');
|
|
if (empty($content)){
|
|
return json_encode(['code' => 500, 'msg' => '提示词缺失']);
|
|
}
|
|
|
|
// 发送请求
|
|
$params = [
|
|
'model' => 'doubao-1-5-pro-32k-250115',
|
|
'messages' => [
|
|
['role' => 'system', 'content' => '你是人工智能助手.'],
|
|
['role' => 'user', 'content' => $content],
|
|
],
|
|
/*'extra_headers' => [
|
|
'x-is-encrypted' => true
|
|
],
|
|
'temperature' => 1,
|
|
'top_p' => 0.7,
|
|
'max_tokens' => 4096,
|
|
'frequency_penalty' => 0,*/
|
|
];
|
|
$result = requestCurl($this->apiUrl, $params, 'POST', $this->headers, 'json');
|
|
$result = json_decode($result, true);
|
|
return successJson($result);
|
|
}
|
|
} |