Skip to content

Commit db15e49

Browse files
committed
dockerfile新增npm安装
1 parent cf67b4f commit db15e49

20 files changed

+175
-187
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/node_modules
33
/public/hot
44
/public/storage
5+
/public/build
56
/storage/*.key
67
/vendor
78
/resources/views/analysis.blade.php

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,71 @@ After that, you can view GeekChat by `http://localhost` in your local browser.
3939

4040
detail:<https://geekr.dev/posts/chatgpt-website-by-laravel-10>
4141

42+
### Stream Support
43+
44+
I implement stream with EventSource and curl, you can use it to receive stream response from OpenAI API.
45+
46+
Frontend:
47+
48+
```js
49+
ChatAPI.chatMessage(message).then(response => {
50+
...
51+
return response.json();
52+
}).then(data => {
53+
...
54+
const eventSource = new EventSource(`/stream?chat_id=${data.chat_id}`);
55+
eventSource.onmessage = function (e) {
56+
...
57+
if (e.data == "[DONE]") {
58+
eventSource.close();
59+
} else {
60+
let word = JSON.parse(e.data).choices[0].delta.content
61+
if (word !== undefined) {
62+
state.messages[state.messages.length - 1].content += JSON.parse(e.data).choices[0].delta.content
63+
}
64+
}
65+
};
66+
eventSource.onerror = function (e) {
67+
eventSource.close();
68+
...
69+
};
70+
}).catch(error => {
71+
...
72+
});
73+
```
74+
75+
Backend:
76+
77+
```php
78+
use App\Facades\OpenAI;
79+
use Symfony\Component\HttpFoundation\StreamedResponse;
80+
81+
...
82+
83+
$params = [
84+
'model' => 'gpt-3.5-turbo',
85+
'messages' => $messages,
86+
'stream' => true,
87+
];
88+
89+
$response = new StreamedResponse(function () use ($request, $params) {
90+
$respData = '';
91+
OpenAI::chat($params, function ($ch, $data) use (&$respData) {
92+
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
93+
if ($httpCode >= 400) {
94+
echo "data: [ERROR] $httpCode";
95+
} else {
96+
echo $data;
97+
}
98+
ob_flush();
99+
flush();
100+
return strlen($data);
101+
});
102+
...
103+
});
104+
$response->headers->set('Content-Type', 'text/event-stream');
105+
$response->headers->set('Cache-Control', 'no-cache');
106+
$response->headers->set('X-Accel-Buffering', 'no');
107+
return $response;
108+
```
109+

README.zh.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,69 @@ docker-compose up -d
3939

4040
开发流程详细文档在这里:<https://geekr.dev/posts/chatgpt-website-by-laravel-10>
4141

42+
### Stream 实现
43+
44+
我使用 EventSource 和 curl 实现了流式响应,你可以使用这个管道组合来接收 OpenAI API 的流式响应:
45+
46+
Frontend:
47+
48+
```js
49+
ChatAPI.chatMessage(message).then(response => {
50+
...
51+
return response.json();
52+
}).then(data => {
53+
...
54+
const eventSource = new EventSource(`/stream?chat_id=${data.chat_id}`);
55+
eventSource.onmessage = function (e) {
56+
...
57+
if (e.data == "[DONE]") {
58+
eventSource.close();
59+
} else {
60+
let word = JSON.parse(e.data).choices[0].delta.content
61+
if (word !== undefined) {
62+
state.messages[state.messages.length - 1].content += JSON.parse(e.data).choices[0].delta.content
63+
}
64+
}
65+
};
66+
eventSource.onerror = function (e) {
67+
eventSource.close();
68+
...
69+
};
70+
}).catch(error => {
71+
...
72+
});
73+
```
74+
75+
Backend:
76+
77+
```php
78+
use App\Facades\OpenAI;
79+
use Symfony\Component\HttpFoundation\StreamedResponse;
80+
81+
$params = [
82+
'model' => 'gpt-3.5-turbo',
83+
'messages' => $messages,
84+
'stream' => true,
85+
];
86+
87+
$response = new StreamedResponse(function () use ($request, $params) {
88+
$respData = '';
89+
OpenAI::chat($params, function ($ch, $data) use (&$respData) {
90+
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
91+
if ($httpCode >= 400) {
92+
echo "data: [ERROR] $httpCode";
93+
} else {
94+
echo $data;
95+
}
96+
ob_flush();
97+
flush();
98+
return strlen($data);
99+
});
100+
...
101+
});
102+
$response->headers->set('Content-Type', 'text/event-stream');
103+
$response->headers->set('Cache-Control', 'no-cache');
104+
$response->headers->set('X-Accel-Buffering', 'no');
105+
return $response;
106+
```
107+

app/Providers/AiServiceProvider.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
use App\Exceptions\OpenAI\ApiKeyIsMissing;
99
use Orhanerday\OpenAi\OpenAi;
1010

11-
use function GuzzleHttp\Promise\settle;
12-
1311
/**
1412
* @internal
1513
*/

context/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ FROM ubuntu:22.04
33
LABEL maintainer="Taylor Otwell"
44

55
ARG WWWGROUP
6+
ARG NODE_VERSION=18
67

78
WORKDIR /var/www/html
89

public/build/assets/ApplicationLogo-d2561183.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

public/build/assets/AudioWidget-420354d3.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

public/build/assets/Chat-be27e4cf.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)