121 lines
3.6 KiB
PHP
121 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace web\admin\demo\dyn;
|
|
|
|
class sse {
|
|
public static function sse_get($senddata, $sendevent) {
|
|
$bb = get('bb');
|
|
$senddata('bb: ' . $bb);
|
|
for ($i = 0; $i < 100; $i++) {
|
|
if ($i % 10 == 1)
|
|
$senddata('sse: ' . $i, $i);
|
|
$sendevent($i);
|
|
usleep(50000);
|
|
}
|
|
$sendevent('ok');
|
|
}
|
|
public static function sse_post($senddata, $sendevent) {
|
|
global $db;
|
|
$post = new \ciy\post();
|
|
$senddata('post: ' . $post->get('bb'));
|
|
for ($i = 0; $i < 100; $i++) {
|
|
if ($i % 10 == 1)
|
|
$senddata('sse: ' . $i, $i);
|
|
$sendevent($i);
|
|
usleep(50000);
|
|
}
|
|
$sendevent('ok');
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// set_time_limit(0);
|
|
// header('Content-Type: text/event-stream');
|
|
// header('Cache-Control: no-cache');
|
|
// header('Connection: keep-alive');
|
|
// header('X-Accel-Buffering: no'); // 禁用Nginx缓冲
|
|
|
|
// function send_sse_data($data, $event = '') {
|
|
// if(empty($event))
|
|
// $event = 'data';
|
|
// if(strpos($data, "\n\n") === false)
|
|
// echo $event . ': ' . $data . "\n\n";
|
|
// else
|
|
// echo $event . '64: ' . base64_encode($data) . "\n\n";
|
|
// ob_flush();
|
|
// flush();
|
|
// }
|
|
// $prompt = $_GET['prompt'] ?? '';
|
|
// // if (empty($prompt)) {
|
|
// // send_sse_data('[错误] 请输入提示词');
|
|
// // exit;
|
|
// // }
|
|
|
|
// // foreach($_SERVER as $key => $value)
|
|
// // send_sse_data('{"choices":[{"delta":{"content":"post:'.$key . '=>' . $value.'\n"}}]}');
|
|
|
|
// send_sse_data('{"choices":[{"delta":{"content":"header:'.$_SERVER['HTTP_AUTHORIZATION'].'\n"}}]}');
|
|
|
|
// send_sse_data('{"choices":[{"delta":{"content":"post:'.str_replace('"','\\"',file_get_contents('php://input')) .'\n"}}]}');
|
|
// for($i=0;$i<10;$i++){
|
|
// send_sse_data('{"choices":[{"delta":{"content":"正在生成第'.($i+1).'句...\n"}}]}');
|
|
// sleep(1);
|
|
// }
|
|
// send_sse_data('[DONE]');
|
|
// exit;
|
|
|
|
// // OpenAI API配置
|
|
// $apiKey = 'sk-f73708fd9acd4497bffe38c820cc5fa4'; // 替换为你的API密钥
|
|
// $model = "deepseek-chat";
|
|
// $url = 'https://api.deepseek.com/chat/completions';
|
|
|
|
// // 构建请求体
|
|
// $data = [
|
|
// 'model' => $model,
|
|
// 'messages' => [['role' => 'user', 'content' => $prompt]],
|
|
// 'stream' => true,
|
|
// 'temperature' => 0.7,
|
|
// ];
|
|
|
|
// // 初始化cURL
|
|
// $ch = curl_init();
|
|
// curl_setopt($ch, CURLOPT_URL, $url);
|
|
// curl_setopt($ch, CURLOPT_POST, true);
|
|
// curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
|
// curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
// 'Content-Type: application/json',
|
|
// 'Authorization: Bearer ' . $apiKey
|
|
// ]);
|
|
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
|
// curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) {
|
|
// // 处理流式响应
|
|
// send_sse_data($data);
|
|
// // $lines = explode("\n", $data);
|
|
|
|
// // foreach ($lines as $line) {
|
|
// // if (strpos($line, 'data: ') === 0) {
|
|
// // $json = substr($line, 6);
|
|
// // if ($json === '[DONE]') {
|
|
// // send_sse_data('[DONE]');
|
|
// // return strlen($data);
|
|
// // }
|
|
|
|
// // $response = json_decode($json, true);
|
|
// // if (isset($response['choices'][0]['delta']['content'])) {
|
|
// // $content = $response['choices'][0]['delta']['content'];
|
|
// // send_sse_data($content);
|
|
// // }
|
|
// // }
|
|
// // }
|
|
|
|
// return strlen($data);
|
|
// });
|
|
|
|
// curl_exec($ch);
|
|
// if (curl_errno($ch)) {
|
|
// send_sse_data('[错误] ' . curl_error($ch));
|
|
// }
|
|
// curl_close($ch);
|