1
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
use PHPMailer\PHPMailer\PHPMailer;
|
||||
use think\Db;
|
||||
use think\Env;
|
||||
use think\Cache;
|
||||
|
||||
//use TCPDF;
|
||||
// +----------------------------------------------------------------------
|
||||
@@ -258,6 +259,74 @@ function search_array_val($arr, $val)
|
||||
}
|
||||
}
|
||||
|
||||
function GptChar($message,$ckey,$mid="")
|
||||
{
|
||||
$url = 'https://api.openai.com/v1/chat/completions';
|
||||
|
||||
// OpenAI API key
|
||||
$api_key = Env::get("gpt.api_key");
|
||||
|
||||
$mes['role'] = "user";
|
||||
$mes['content'] = $message;
|
||||
if($mid!=""){
|
||||
$mes['name'] = $mid;
|
||||
}
|
||||
|
||||
// Request data
|
||||
$data = array(
|
||||
'model' => 'gpt-3.5-turbo',
|
||||
'messages' => [$mes],
|
||||
'temperature' => 0.2,
|
||||
'max_tokens' => 2048,
|
||||
'n' => 1,
|
||||
'stop' => '',
|
||||
'stream' => true // Use streaming mode
|
||||
);
|
||||
|
||||
// Create request headers
|
||||
$headers = array(
|
||||
'Content-Type: application/json',
|
||||
'Authorization: Bearer ' . $api_key
|
||||
);
|
||||
|
||||
// Create context for request
|
||||
$context = stream_context_create(array(
|
||||
'http' => array(
|
||||
'method' => 'POST',
|
||||
'header' => $headers,
|
||||
'content' => json_encode($data),
|
||||
'ignore_errors' => true
|
||||
)
|
||||
));
|
||||
|
||||
// Open connection to API endpoint
|
||||
$fp = fopen($url, 'r', false, $context);
|
||||
$cache_id = '';
|
||||
// Read response data
|
||||
if ($fp) {
|
||||
while (!feof($fp)) {
|
||||
$chunk = fgets($fp);
|
||||
// echo $chunk;
|
||||
$cache = object_to_array(json_decode(substr($chunk,6)));
|
||||
if(isset($cache['choices'][0]['delta']['content'])){
|
||||
$cc = Cache::get($ckey)?Cache::get($ckey):'';
|
||||
Cache::set($ckey,$cc.$cache['choices'][0]['delta']['content'],3600);
|
||||
}
|
||||
if(isset($cache['id'])&&$cache['id']!=''){
|
||||
$cache_id = $cache['id'];
|
||||
}
|
||||
// ob_flush();
|
||||
// flush();
|
||||
}
|
||||
fclose($fp);
|
||||
}
|
||||
if($cache_id!=''){
|
||||
Cache::set($ckey."_id",$cache_id);
|
||||
}
|
||||
Cache::set($ckey,$cc."[{enddata}]",3600);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* GET 请求
|
||||
* @param string $url
|
||||
@@ -519,8 +588,6 @@ function choiseJabbr($article_id, $jabbr)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
function myPost($url, $param = array())
|
||||
{
|
||||
|
||||
|
||||
Reference in New Issue
Block a user