初始化仓库及v1.0.0提交
This commit is contained in:
77
app/Services/Providers/OpenAIProvider.php
Normal file
77
app/Services/Providers/OpenAIProvider.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Providers;
|
||||
|
||||
class OpenAIProvider
|
||||
{
|
||||
public static function stream(string $model, array $messages, array $options, callable $onChunk): void
|
||||
{
|
||||
$provider = $options['provider'];
|
||||
$apiUrl = rtrim($provider['apiUrl'] ?? 'https://api.openai.com', '/');
|
||||
$apiKey = $provider['apiKey'] ?? '';
|
||||
$systemPrompt = $options['systemPrompt'] ?? '';
|
||||
|
||||
$url = $apiUrl . '/v1/chat/completions';
|
||||
|
||||
if (!empty($systemPrompt)) {
|
||||
array_unshift($messages, ['role' => 'system', 'content' => $systemPrompt]);
|
||||
}
|
||||
|
||||
$body = json_encode([
|
||||
'model' => $model,
|
||||
'messages' => $messages,
|
||||
'stream' => true
|
||||
]);
|
||||
|
||||
$ch = curl_init($url);
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_POSTFIELDS => $body,
|
||||
CURLOPT_HTTPHEADER => [
|
||||
'Authorization: Bearer ' . $apiKey,
|
||||
'Content-Type: application/json',
|
||||
'Accept: text/event-stream'
|
||||
],
|
||||
CURLOPT_TIMEOUT => 120,
|
||||
CURLOPT_RETURNTRANSFER => false,
|
||||
CURLOPT_SSL_VERIFYPEER => true,
|
||||
CURLOPT_WRITEFUNCTION => function ($ch, $data) use ($onChunk) {
|
||||
$lines = explode("\n", $data);
|
||||
foreach ($lines as $line) {
|
||||
$line = trim($line);
|
||||
if ($line === '') {
|
||||
continue;
|
||||
}
|
||||
if (str_starts_with($line, 'data: ')) {
|
||||
$payload = substr($line, 6);
|
||||
if ($payload === '[DONE]') {
|
||||
return strlen($data);
|
||||
}
|
||||
$json = json_decode($payload, true);
|
||||
if ($json && isset($json['choices'][0]['delta']['content'])) {
|
||||
$content = $json['choices'][0]['delta']['content'];
|
||||
if ($content !== '') {
|
||||
$onChunk($content);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return strlen($data);
|
||||
}
|
||||
]);
|
||||
|
||||
$result = curl_exec($ch);
|
||||
if ($result === false) {
|
||||
$error = curl_error($ch);
|
||||
curl_close($ch);
|
||||
throw new \RuntimeException('API请求失败: ' . $error);
|
||||
}
|
||||
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($httpCode >= 400) {
|
||||
throw new \RuntimeException('API返回错误,状态码: ' . $httpCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user