Files
tougao/application/api/controller/Aigpt.php
wangjinlei 7a0cd98d3b 1
2025-03-05 11:46:20 +08:00

42 lines
1003 B
PHP

<?php
namespace app\api\controller;
use GuzzleHttp\Client;
use Think\Env;
/**
* @title 公共管理相关
* @description 公共管理相关
*/
class Aigpt extends Base {
public function __construct(\think\Request $request = null) {
parent::__construct($request);
}
public function myTestGpt(){
$apiKey = Env::get("gpt.api_key");//"你的API密钥"; // 请替换为你的 OpenAI API Key
$url = "https://api.openai.com/v1/chat/completions";
$client = new Client();
$response = $client->post($url, [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $apiKey
],
'json' => [
'model' => 'gpt-3.5-turbo',
'messages' => [['role' => 'user', 'content' => 'Say this is a test!']],
'temperature' => 0.7
]
]);
return json($response->getBody()->getContents());
}
}