Laravel 请求流式(Stream)接口返回流式Http 响应

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Route;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;


Route::get('http', function (){
return response()->eventStream(function (){
$stream = Http::withHeaders([
'Authorization' => 'Bearer '.env('DEEPSEEK_API_KEY'),
'Accept' => 'application/json',
])
->throw()
//->accept('application/x-ndjson')
->asJson()
->timeout(2000)
->withOptions(['stream' => true])
->post(env('DEEPSEEK_API_BASE_URL').'/chat/completions',[
'messages' => [
[
'content' => 'Hello',
'role' => 'system',
],
[
'content' => '西南财经大学',
'role' => 'user',
]
],
'model' => 'deepseek_r1.deepseek-r1', //deepseek_r1.deepseek-r1
'frequency_penalty' => 0,
'max_tokens' => 2048,
'presence_penalty' => 0,
'response_format' => [
'type' => 'text'
],
'stream' => true
])
->toPsrResponse()
->getBody();

$buffer = '';
while (!$stream->eof()) {
$char = $stream->read(1);

$buffer .= $char;
if ($char === "\n") {
$line = trim($buffer);
$buffer = '';

if (!empty($line)) {
yield $line;
}
}
}
});
});


Route::get('guzzle', function (){
$client = new \GuzzleHttp\Client([
'base_uri' => config('swufe-chat.base_url')
]);

return response()->eventStream(function () use ($client){
$response = $client->request('POST', '/api/chat/completions', [
'headers' => [
'Authorization' => 'Bearer '.env('DEEPSEEK_API_KEY'),
'Accept' => 'application/json',
],
'json' => [
'messages' => [
[
'content' => 'Hello',
'role' => 'system',
],
[
'content' => '西南财经大学',
'role' => 'user',
]
],
'model' => 'deepseek_r1.deepseek-r1', //deepseek_r1.deepseek-r1
'frequency_penalty' => 0,
'max_tokens' => 2048,
'presence_penalty' => 0,
'response_format' => [
'type' => 'text'
],
'stream' => true
],
'stream' => true
]);

$stream = $response->getBody();
$buffer = '';
while (!$stream->eof()) {
$char = $stream->read(1);

$buffer .= $char;
if ($char === "\n") {
$line = trim($buffer);
$buffer = '';

if (!empty($line)) {
yield $line;
}
}
}
});
});


// https://github.com/openai-php/client

Route::get('openai', function (){
$client = OpenAI::factory()
->withApiKey(env('DEEPSEEK_API_KEY'))
->withBaseUri(env('DEEPSEEK_API_BASE_URL'))
->withHttpClient($httpClient = new \GuzzleHttp\Client())
->withStreamHandler(fn (RequestInterface $request): ResponseInterface => $httpClient->send($request, [
'stream' => true // Allows to provide a custom stream handler for the http client.
]))
->make();

return response()->eventStream(function () use ($client){
$stream = $client->chat()->createStreamed([
'model' => 'deepseek_r1.deepseek-r1',
'messages' => [
['role' => 'user', 'content' => 'Hello!'],
],
'stream_options'=>[
'include_usage' => true,
]
]);

foreach ($stream as $response) {
yield $response->choices[0];
}
});
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$stream = Http::throw()
->accept('application/x-ndjson')
->asJson()
->withOptions(['stream' => true])
->post($endpoint, $requestData)
->toPsrResponse()
->getBody();

$buffer = '';

while ($stream->eof() === false) {
$char = $stream->read(1);

$buffer .= $char;
if ($char === "\n") {
$line = trim($buffer);
$buffer = '';

if (!empty($line)) {
dump($line);
}
}
}

参考:

https://yellowduck.be/posts/using-streaming-http-responses-in-laravel

官方也有实现😓

https://laravel.com/docs/12.x/responses#streamed-json-responses