fix(proxy): 优化流式输出

This commit is contained in:
yokowu
2025-07-11 15:56:23 +08:00
parent 31798c2914
commit 69285fdf75
2 changed files with 53 additions and 40 deletions

View File

@@ -703,6 +703,7 @@ func (p *LLMProxy) handleChatCompletionStream(ctx context.Context, w http.Respon
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Transfer-Encoding", "chunked")
w.Header().Set("X-Accel-Buffering", "no")
rc := &domain.RecordParam{
RequestID: c.RequestID,
@@ -714,8 +715,6 @@ func (p *LLMProxy) handleChatCompletionStream(ctx context.Context, w http.Respon
Prompt: prompt,
Role: consts.ChatRoleAssistant,
}
buf := bufio.NewWriterSize(w, 32*1024)
defer buf.Flush()
ch := make(chan []byte, 1024)
defer close(ch)
@@ -769,10 +768,13 @@ func (p *LLMProxy) handleChatCompletionStream(ctx context.Context, w http.Respon
err = streamRead(ctx, resp.Body, func(line []byte) error {
ch <- line
if _, err := buf.Write(line); err != nil {
if _, err := w.Write(line); err != nil {
return fmt.Errorf("写入响应失败: %w", err)
}
return buf.Flush()
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
return nil
})
return err
})

View File

@@ -1,54 +1,65 @@
worker_processes auto;
user nginx nginx;
events {
worker_connections 4096;
worker_connections 4096;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$host"';
log_format main '$remote_addr - $remote_user [$time_local] $status "$request" $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" "$host"';
error_log /var/log/nginx/error.log error;
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log error;
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
client_body_buffer_size 128k;
client_header_buffer_size 2k;
sendfile on;
keepalive_timeout 65;
client_body_buffer_size 128k;
client_header_buffer_size 2k;
gzip on;
gzip_types applicaiton/javascript text/css image/png image/jpeg image/gif;
gzip on;
gzip_types application/javascript text/css image/png image/jpeg image/gif;
upstream backend {
server monkeycode-server:8888;
}
server {
listen 80;
listen [::]:80;
server_name _;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
upstream backend {
server monkeycode-server:8888;
}
location /api {
server {
listen 80;
listen [::]:80;
server_name _;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /api {
proxy_pass http://backend;
}
}
location /v1 {
location /v1 {
proxy_pass http://backend;
}
location /v1/chat/completions {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_buffering off;
proxy_cache off;
proxy_read_timeout 24h;
proxy_connect_timeout 24h;
proxy_send_timeout 24h;
keepalive_timeout 65;
}
}
}
}