This commit is contained in:
wangjinlei
2025-03-06 09:16:11 +08:00
parent 7a0cd98d3b
commit 62faa9326d
2 changed files with 92 additions and 9 deletions

View File

@@ -4,6 +4,8 @@ namespace app\api\controller;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Think\Env; use Think\Env;
use Tectalic\OpenAi\Authentication;
use Tectalic\OpenAi\Manager;
/** /**
* @title 公共管理相关 * @title 公共管理相关
@@ -17,7 +19,7 @@ class Aigpt extends Base {
} }
public function myTestGpt(){ public function myTestGpt(){
$apiKey = Env::get("gpt.api_key");//"你的API密钥"; // 请替换为你的 OpenAI API Key $apiKey = Env::get("gpt.api_key1");//"你的API密钥"; // 请替换为你的 OpenAI API Key
$url = "https://api.openai.com/v1/chat/completions"; $url = "https://api.openai.com/v1/chat/completions";
$client = new Client(); $client = new Client();
@@ -33,7 +35,77 @@ class Aigpt extends Base {
] ]
]); ]);
return json($response->getBody()->getContents()); $res = object_to_array(json_decode($response->getBody()->getContents()));
return jsonSuccess($res);
}
public function myTestGpt1(){
$openaiClient = Manager::build(
new \GuzzleHttp\Client(),
new Authentication(Env::get("gpt.api_key1"))
);
$response = $openaiClient->chatCompletions()->create(
new \Tectalic\OpenAi\Models\ChatCompletions\CreateRequest([
'model' => 'gpt-4',
'messages' => [
[
'role' => 'user',
'content' => 'Will using a well designed and supported third party package save time?用中文回答'
],
],
])
)->toModel();
echo $response->choices[0]->message->content;
}
public function myTestGpt2(){
header('Access-Control-Allow-Origin: *'); // 允许任何来源
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header('Connection: keep-alive');
// $request = Request::instance();
$data = $this->request->post();
$input = $data['message'];
// $input = $this->request->post('message'); // 获取前端传递的用户输入
$client = new Client();
$response = $client->post('https://api.openai.com/v1/chat/completions', [
'headers' => [
'Authorization' => 'Bearer '.Env::get("gpt.api_key1"),
'Content-Type' => 'application/json',
],
'json' => [
'model' => 'gpt-4',
'messages' => [['role' => 'user', 'content' => $input]],
'stream' => true,
],
'stream' => true
]);
$body = $response->getBody();
while (!$body->eof()) {
$chunk = $body->read(1024); // 读取流数据
echo "data: " . json_encode($chunk, JSON_UNESCAPED_UNICODE) . "\n\n";
ob_flush();
flush();
}
exit;
} }

View File

@@ -905,17 +905,28 @@ class Preaccept extends Base
if(!$rule->check($data)){ if(!$rule->check($data)){
return jsonError($rule->getError()); return jsonError($rule->getError());
} }
$this->article_main_obj->where("am_id",$data['am_id'])->update(['state'=>1]); $this->article_main_obj->where("am_id",$data['am_id'])->update(['state'=>1]);
return jsonSuccess([]); return jsonSuccess([]);
} }
/**批量删除正文的行
* @return void
*/
public function delMoreArticleMains(){
$data = $this->request->post();
$rule = new Validate([
"ids"=>"require"
]);
if(!$rule->check($data)){
return jsonError($rule->getError());
}
$idList = explode(",",$data['ids']);
$this->article_main_obj->whereIn("am_id",$idList)->update(['state'=>1]);
return jsonSuccess([]);
}
public function editArticleMainsForAuthor(){ public function editArticleMainsForAuthor(){
$data = $this->request->post(); $data = $this->request->post();
$rule = new Validate([ $rule = new Validate([