Skip to content

Commit ba261b5

Browse files
committed
merge from remote
2 parents 3bbc519 + 89c4ba7 commit ba261b5

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
[@qiayue](https://github.com/qiayue/) 开源的 [纯 PHP 实现 GPT 流式调用和前端实时打印 webui ](https://github.com/qiayue/php-openai-gpt-stream-chat-api-webui)
44

5+
**4月13日更新:**
6+
7+
1、最近速度慢是因为 OpenAI 对于免费账号限速了,在 [platform.openai.com](https://platform.openai.com/) 绑定了信用卡的才是之前的正常速度;
8+
9+
2、限速指的是流式请求时,首个 token 返回需要 20 秒左右,而绑定了信用卡的账号,在 2 秒左右;
10+
11+
512
## 目录结构
613

714
```
@@ -60,7 +67,7 @@ api_key=sk-xxxxx
6067

6168
开了一个微信群,欢迎入群交流:
6269

63-
![微信答疑群](https://tlc.nali.net/qrcode/wxgroup/openaiqa/?v=1)
70+
![微信答疑群](https://raw.githubusercontent.com/qiayue/php-openai-gpt-stream-chat-api-webui/main/static/img/wxqrcode.0420.jpeg)
6471

6572

6673
## 原理说明
@@ -105,6 +112,9 @@ public function callback($ch, $data) {
105112
// 0、把上次缓冲区内数据拼接上本次的data
106113
$buffer = $this->data_buffer.$data;
107114

115+
//拼接完之后,要把缓冲字符串清空
116+
$this->data_buffer = '';
117+
108118
// 1、把所有的 'data: {' 替换为 '{' ,'data: [' 换成 '['
109119
$buffer = str_replace('data: {', '{', $buffer);
110120
$buffer = str_replace('data: [', '[', $buffer);

class/Class.StreamHandler.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,17 @@ public function callback($ch, $data) {
5050

5151
// 0、把上次缓冲区内数据拼接上本次的data
5252
$buffer = $this->data_buffer.$data;
53+
54+
//拼接完之后,要把缓冲字符串清空
55+
$this->data_buffer = '';
5356

5457
// 1、把所有的 'data: {' 替换为 '{' ,'data: [' 换成 '['
5558
$buffer = str_replace('data: {', '{', $buffer);
5659
$buffer = str_replace('data: [', '[', $buffer);
5760

5861
// 2、把所有的 '}\n\n{' 替换维 '}[br]{' , '}\n\n[' 替换为 '}[br]['
59-
$buffer = str_replace('}'.PHP_EOL.PHP_EOL.'{', '}[br]{', $buffer);
60-
$buffer = str_replace('}'.PHP_EOL.PHP_EOL.'[', '}[br][', $buffer);
62+
$buffer = str_replace("}\n\n{", '}[br]{', $buffer);
63+
$buffer = str_replace("}\n\n[", '}[br][', $buffer);
6164

6265
// 3、用 '[br]' 分割成多行数组
6366
$lines = explode('[br]', $buffer);

cloudflare.worker.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
感谢@赖嘉伟Gary https://github.com/paicha 提供 Worker 代码
3+
4+
用途:
5+
使用此方式,不需要购买海外服务器,替换 /class/Class.ChatGPT.php 中的 api.openai.com 域名后,直接将本项目开源代码部署到国内服务器即可使用。
6+
7+
使用方法:
8+
1、在 CloudFlare 添加一个域名,注意只能填一级域名,不能填子域名,所以可以新注册一个便宜域名专门做这个事情;
9+
2、添加域名后,在域名注册商处把域名的 dns 设为 CloudFlare 的 dns 地址;
10+
3、等待域名生效,大概需要十几分钟到一个小时不等;
11+
4、添加一个 Worker ,把以下代码复制到进去;
12+
5、点击 Worker 的 Trigger 界面,设置自定义域名,选择第一步添加的域名;
13+
6、等全部生效后,就可以用你的自定义域名替换 /class/Class.ChatGPT.php 中的 api.openai.com 域名。
14+
15+
*/
16+
async function handleRequest(request) {
17+
const url = new URL(request.url)
18+
const fetchAPI = request.url.replace(url.host, 'api.openai.com')
19+
20+
// 添加跨域处理
21+
const corsHeaders = {
22+
'Access-Control-Allow-Origin': '*',
23+
'Access-Control-Allow-Methods': 'OPTIONS',
24+
'Access-Control-Allow-Headers': '*',
25+
};
26+
if (request.method === 'OPTIONS') return new Response(null, { headers: corsHeaders });
27+
28+
return fetch(fetchAPI, { headers: request.headers, method: request.method, body: request.body })
29+
}
30+
31+
addEventListener("fetch", (event) => {
32+
event.respondWith(handleRequest(event.request))
33+
})

static/img/wxqrcode.0420.jpeg

141 KB
Loading

static/js/chat.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ function sendMessage() {
5555
}
5656

5757
function getAnswer(inputValue){
58-
inputValue = inputValue.replace('+', '{[$add$]}');
59-
const url = "./chat.php?q="+encodeURIComponent(inputValue);
58+
inputValue = encodeURIComponent(inputValue.replace(/\+/g, '{[$add$]}'));
59+
const url = "./chat.php?q="+inputValue;
6060
const eventSource = new EventSource(url);
6161

6262
eventSource.addEventListener("open", (event) => {

0 commit comments

Comments
 (0)