forked from chaitin/MonkeyCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.go
More file actions
855 lines (738 loc) · 25 KB
/
Copy pathproxy.go
File metadata and controls
855 lines (738 loc) · 25 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
package proxy
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"github.com/rokku-c/go-openai"
"github.com/chaitin/MonkeyCode/backend/config"
"github.com/chaitin/MonkeyCode/backend/consts"
"github.com/chaitin/MonkeyCode/backend/domain"
"github.com/chaitin/MonkeyCode/backend/pkg/logger"
"github.com/chaitin/MonkeyCode/backend/pkg/request"
)
// ErrResp 错误响应
type ErrResp struct {
Error struct {
Message string `json:"message"`
Type string `json:"type"`
Code string `json:"code,omitempty"`
} `json:"error"`
}
// RequestResponseLog 请求响应日志结构
type RequestResponseLog struct {
Timestamp time.Time `json:"timestamp"`
RequestID string `json:"request_id"`
Endpoint string `json:"endpoint"`
ModelName string `json:"model_name"`
ModelType consts.ModelType `json:"model_type"`
UpstreamURL string `json:"upstream_url"`
RequestBody any `json:"request_body"`
RequestHeader map[string][]string `json:"request_header"`
StatusCode int `json:"status_code"`
ResponseBody any `json:"response_body"`
ResponseHeader map[string][]string `json:"response_header"`
Latency int64 `json:"latency_ms"`
Error string `json:"error,omitempty"`
IsStream bool `json:"is_stream"`
SourceIP string `json:"source_ip"` // 请求来源IP地址
}
// LLMProxy LLM API代理实现
type LLMProxy struct {
usecase domain.ProxyUsecase
cfg *config.Config
client *http.Client
logger *slog.Logger
requestLogPath string // 请求日志保存路径
}
// NewLLMProxy 创建LLM API代理实例
func NewLLMProxy(
usecase domain.ProxyUsecase,
cfg *config.Config,
logger *slog.Logger,
) domain.Proxy {
// 解析超时时间
timeout, err := time.ParseDuration(cfg.LLMProxy.Timeout)
if err != nil {
timeout = 30 * time.Second
logger.Warn("解析超时时间失败, 使用默认值 30s", "error", err)
}
// 解析保持连接时间
keepAlive, err := time.ParseDuration(cfg.LLMProxy.KeepAlive)
if err != nil {
keepAlive = 60 * time.Second
logger.Warn("解析保持连接时间失败, 使用默认值 60s", "error", err)
}
// 创建HTTP客户端
client := &http.Client{
Timeout: timeout,
Transport: &http.Transport{
MaxIdleConns: cfg.LLMProxy.ClientPoolSize,
MaxConnsPerHost: cfg.LLMProxy.ClientPoolSize,
MaxIdleConnsPerHost: cfg.LLMProxy.ClientPoolSize,
IdleConnTimeout: keepAlive,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
DisableCompression: false,
ForceAttemptHTTP2: true,
},
}
// 获取日志配置
requestLogPath := ""
if cfg.LLMProxy.RequestLogPath != "" {
requestLogPath = cfg.LLMProxy.RequestLogPath
// 确保目录存在
if err := os.MkdirAll(requestLogPath, 0755); err != nil {
logger.Warn("创建请求日志目录失败", "error", err, "path", requestLogPath)
}
}
return &LLMProxy{
usecase: usecase,
client: client,
cfg: cfg,
requestLogPath: requestLogPath,
logger: logger,
}
}
// saveRequestResponseLog 保存请求响应日志到文件
func (p *LLMProxy) saveRequestResponseLog(log *RequestResponseLog) {
if p.requestLogPath == "" {
return
}
// 创建文件名,格式:YYYYMMDD_HHMMSS_请求ID.json
timestamp := log.Timestamp.Format("20060102_150405") + fmt.Sprintf("_%03d", log.Timestamp.Nanosecond()/1e6)
filename := fmt.Sprintf("%s_%s.json", timestamp, log.RequestID)
filepath := filepath.Join(p.requestLogPath, filename)
// 将日志序列化为JSON
logData, err := json.MarshalIndent(log, "", " ")
if err != nil {
p.logger.Error("序列化请求日志失败", "error", err)
return
}
// 写入文件
if err := os.WriteFile(filepath, logData, 0644); err != nil {
p.logger.Error("写入请求日志文件失败", "error", err, "path", filepath)
return
}
p.logger.Debug("请求响应日志已保存", "path", filepath)
}
func (p *LLMProxy) AcceptCompletion(ctx context.Context, req *domain.AcceptCompletionReq) error {
return p.usecase.AcceptCompletion(ctx, req)
}
func writeErrResp(w http.ResponseWriter, code int, message, errorType string) {
resp := ErrResp{
Error: struct {
Message string `json:"message"`
Type string `json:"type"`
Code string `json:"code,omitempty"`
}{
Message: message,
Type: errorType,
},
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
b, _ := json.Marshal(resp)
w.Write(b)
}
type Ctx struct {
UserID string
RequestID string
SourceIP string
}
func (p *LLMProxy) handle(ctx context.Context, fn func(ctx *Ctx, log *RequestResponseLog) error) {
// 获取用户ID
userID := "unknown"
if id, ok := ctx.Value(logger.UserIDKey).(string); ok {
userID = id
}
requestID := "unknown"
if id, ok := ctx.Value(logger.RequestIDKey).(string); ok {
requestID = id
}
sourceip := "unknown"
if ip, ok := ctx.Value("remote_addr").(string); ok {
sourceip = ip
}
// 创建请求日志结构
l := &RequestResponseLog{
Timestamp: time.Now(),
RequestID: requestID,
ModelType: consts.ModelTypeCoder,
SourceIP: sourceip,
}
c := &Ctx{
UserID: userID,
RequestID: requestID,
SourceIP: sourceip,
}
if err := fn(c, l); err != nil {
p.logger.With("userID", userID, "requestID", requestID, "sourceip", sourceip).ErrorContext(ctx, "处理请求失败", "error", err)
l.Error = err.Error()
}
p.saveRequestResponseLog(l)
}
func (p *LLMProxy) HandleCompletion(ctx context.Context, w http.ResponseWriter, req domain.CompletionRequest) {
if req.Stream {
p.handleCompletionStream(ctx, w, req)
} else {
p.handleCompletion(ctx, w, req)
}
}
func (p *LLMProxy) handleCompletionStream(ctx context.Context, w http.ResponseWriter, req domain.CompletionRequest) {
endpoint := "/completions"
p.handle(ctx, func(c *Ctx, log *RequestResponseLog) error {
// 使用负载均衡算法选择模型
m, err := p.usecase.SelectModelWithLoadBalancing(req.Model, consts.ModelTypeCoder)
if err != nil {
p.logger.With("modelName", req.Model, "modelType", consts.ModelTypeCoder).WarnContext(ctx, "模型选择失败", "error", err)
writeErrResp(w, http.StatusNotFound, "模型未找到", "proxy_error")
return err
}
// 构造上游API URL
upstream := m.APIBase + endpoint
log.UpstreamURL = upstream
startTime := time.Now()
// 创建上游请求
body, err := json.Marshal(req)
if err != nil {
p.logger.ErrorContext(ctx, "序列化请求体失败", "error", err)
return fmt.Errorf("序列化请求体失败: %w", err)
}
upreq, err := http.NewRequestWithContext(ctx, http.MethodPost, upstream, bytes.NewReader(body))
if err != nil {
p.logger.With("upstream", upstream).WarnContext(ctx, "创建上游流式请求失败", "error", err)
return fmt.Errorf("创建上游请求失败: %w", err)
}
// 设置请求头
upreq.Header.Set("Content-Type", "application/json")
upreq.Header.Set("Accept", "text/event-stream")
if m.APIKey != "" && m.APIKey != "none" {
upreq.Header.Set("Authorization", "Bearer "+m.APIKey)
}
// 保存请求头(去除敏感信息)
requestHeaders := make(map[string][]string)
for k, v := range upreq.Header {
if k != "Authorization" {
requestHeaders[k] = v
} else {
// 敏感信息脱敏
requestHeaders[k] = []string{"Bearer ***"}
}
}
log.RequestHeader = requestHeaders
p.logger.With(
"upstreamURL", upstream,
"modelName", m.ModelName,
"modelType", consts.ModelTypeLLM,
"apiBase", m.APIBase,
"requestHeader", upreq.Header,
"requestBody", upreq,
).DebugContext(ctx, "转发流式请求到上游API")
// 发送请求
resp, err := p.client.Do(upreq)
if err != nil {
p.logger.With("upstreamURL", upstream).WarnContext(ctx, "发送上游流式请求失败", "error", err)
return fmt.Errorf("发送上游请求失败: %w", err)
}
defer resp.Body.Close()
// 检查响应状态
if resp.StatusCode != http.StatusOK {
responseBody, _ := io.ReadAll(resp.Body)
// 更新日志错误信息
log.StatusCode = resp.StatusCode
log.ResponseHeader = resp.Header
log.ResponseBody = string(responseBody)
log.Latency = time.Since(startTime).Milliseconds()
// 在debug级别记录错误的流式响应内容
p.logger.With(
"statusCode", resp.StatusCode,
"responseHeader", resp.Header,
"responseBody", string(responseBody),
).DebugContext(ctx, "上游流式响应错误原始内容")
var errorResp ErrResp
if err := json.Unmarshal(responseBody, &errorResp); err == nil {
p.logger.With(
"endpoint", endpoint,
"upstreamURL", upstream,
"requestBody", upreq,
"statusCode", resp.StatusCode,
"errorType", errorResp.Error.Type,
"errorCode", errorResp.Error.Code,
"errorMessage", errorResp.Error.Message,
"latency", time.Since(startTime),
).WarnContext(ctx, "上游API流式请求异常详情")
return fmt.Errorf("上游API返回错误: %s", errorResp.Error.Message)
}
p.logger.With(
"endpoint", endpoint,
"upstreamURL", upstream,
"requestBody", upreq,
"statusCode", resp.StatusCode,
"responseBody", string(responseBody),
).WarnContext(ctx, "上游API流式请求异常详情")
return fmt.Errorf("上游API返回非200状态码: %d, 响应: %s", resp.StatusCode, string(responseBody))
}
// 更新日志信息
log.StatusCode = resp.StatusCode
log.ResponseHeader = resp.Header
// 在debug级别记录流式响应头信息
p.logger.With(
"statusCode", resp.StatusCode,
"responseHeader", resp.Header,
).DebugContext(ctx, "上游流式响应头信息")
// 设置响应头
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Transfer-Encoding", "chunked")
rc := &domain.RecordParam{
UserID: c.UserID,
ModelID: m.ID,
ModelType: consts.ModelTypeLLM,
Prompt: req.Prompt.(string),
}
buf := bufio.NewWriterSize(w, 32*1024)
defer buf.Flush()
ch := make(chan []byte, 1024)
defer close(ch)
go func(rc *domain.RecordParam) {
for line := range ch {
if bytes.HasPrefix(line, []byte("data:")) {
line = bytes.TrimPrefix(line, []byte("data: "))
line = bytes.TrimSpace(line)
if len(line) == 0 {
continue
}
if bytes.Equal(line, []byte("[DONE]")) {
break
}
var t openai.CompletionResponse
if err := json.Unmarshal(line, &t); err != nil {
p.logger.With("line", string(line)).WarnContext(ctx, "解析流式数据失败", "error", err)
continue
}
p.logger.With("response", t).DebugContext(ctx, "流式响应数据")
if len(t.Choices) > 0 {
rc.Completion += t.Choices[0].Text
}
rc.InputTokens = int64(t.Usage.PromptTokens)
rc.OutputTokens += int64(t.Usage.CompletionTokens)
}
}
if rc.OutputTokens == 0 {
return
}
p.logger.With("record", rc).DebugContext(ctx, "流式记录")
if err := p.usecase.Record(context.Background(), rc); err != nil {
p.logger.With("modelID", m.ID, "modelName", m.ModelName, "modelType", consts.ModelTypeLLM).
WarnContext(ctx, "插入流式记录失败", "error", err)
}
}(rc)
err = streamRead(ctx, resp.Body, func(line []byte) error {
ch <- line
if _, err := buf.Write(line); err != nil {
return fmt.Errorf("写入响应失败: %w", err)
}
return buf.Flush()
})
return err
})
}
func (p *LLMProxy) handleCompletion(ctx context.Context, w http.ResponseWriter, req domain.CompletionRequest) {
endpoint := "/completions"
p.handle(ctx, func(c *Ctx, log *RequestResponseLog) error {
// 使用负载均衡算法选择模型
m, err := p.usecase.SelectModelWithLoadBalancing(req.Model, consts.ModelTypeCoder)
if err != nil {
p.logger.With("modelName", req.Model, "modelType", consts.ModelTypeCoder).WarnContext(ctx, "模型选择失败", "error", err)
writeErrResp(w, http.StatusNotFound, "模型未找到", "proxy_error")
return err
}
// 构造上游API URL
upstream := m.APIBase + endpoint
log.UpstreamURL = upstream
u, err := url.Parse(upstream)
if err != nil {
p.logger.With("upstreamURL", upstream).WarnContext(ctx, "解析上游URL失败", "error", err)
writeErrResp(w, http.StatusInternalServerError, "无效的上游URL", "proxy_error")
return err
}
startTime := time.Now()
client := request.NewClient(u.Scheme, u.Host, 30*time.Second)
client.SetDebug(p.cfg.Debug)
resp, err := request.Post[openai.CompletionResponse](client, u.Path, req, request.WithHeader(request.Header{
"Authorization": "Bearer " + m.APIKey,
}))
if err != nil {
p.logger.With("upstreamURL", upstream).WarnContext(ctx, "发送上游请求失败", "error", err)
writeErrResp(w, http.StatusInternalServerError, "发送上游请求失败", "proxy_error")
log.Latency = time.Since(startTime).Milliseconds()
return err
}
latency := time.Since(startTime)
// 记录请求信息
p.logger.With(
"statusCode", http.StatusOK,
"upstreamURL", upstream,
"modelName", req.Model,
"modelType", consts.ModelTypeCoder,
"apiBase", m.APIBase,
"requestBody", req,
"resp", resp,
"latency", latency.String(),
).DebugContext(ctx, "转发请求到上游API")
go p.recordCompletion(c, m.ID, req, resp)
// 更新请求日志
log.StatusCode = http.StatusOK
log.ResponseBody = resp
log.ResponseHeader = resp.Header()
log.Latency = latency.Milliseconds()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
b, err := json.Marshal(resp)
if err != nil {
p.logger.With("response", resp).WarnContext(ctx, "序列化响应失败", "error", err)
return err
}
w.Write(b)
return nil
})
}
func (p *LLMProxy) recordCompletion(c *Ctx, modelID string, req domain.CompletionRequest, resp *openai.CompletionResponse) {
if resp.Usage.CompletionTokens == 0 {
return
}
ctx := context.Background()
prompt := req.Prompt.(string)
rc := &domain.RecordParam{
TaskID: resp.ID,
UserID: c.UserID,
ModelID: modelID,
ModelType: consts.ModelTypeCoder,
Prompt: prompt,
ProgramLanguage: req.Metadata["program_language"],
InputTokens: int64(resp.Usage.PromptTokens),
OutputTokens: int64(resp.Usage.CompletionTokens),
}
for _, choice := range resp.Choices {
rc.Completion += choice.Text
}
lines := strings.Count(rc.Completion, "\n") + 1
rc.CodeLines = int64(lines)
if err := p.usecase.Record(ctx, rc); err != nil {
p.logger.With("modelID", modelID, "modelName", req.Model, "modelType", consts.ModelTypeCoder).WarnContext(ctx, "记录请求失败", "error", err)
}
}
func (p *LLMProxy) HandleChatCompletion(ctx context.Context, w http.ResponseWriter, req *openai.ChatCompletionRequest) {
if req.Stream {
p.handleChatCompletionStream(ctx, w, req)
} else {
p.handleChatCompletion(ctx, w, req)
}
}
func streamRead(ctx context.Context, r io.Reader, fn func([]byte) error) error {
reader := bufio.NewReaderSize(r, 32*1024)
for {
select {
case <-ctx.Done():
return fmt.Errorf("流式请求被取消: %w", ctx.Err())
default:
line, err := reader.ReadBytes('\n')
if err == io.EOF {
return nil
}
if err != nil {
return fmt.Errorf("读取流式数据失败: %w", err)
}
line = bytes.TrimSpace(line)
if len(line) == 0 {
continue
}
line = append(line, '\n')
line = append(line, '\n')
fn(line)
}
}
}
func getPrompt(req *openai.ChatCompletionRequest) string {
for _, message := range req.Messages {
if message.Role == "system" {
continue
}
if strings.Contains(message.Content, "<task>") {
return message.Content
}
for _, m := range message.MultiContent {
if strings.Contains(m.Text, "<task>") {
return m.Text
}
}
}
return ""
}
func (p *LLMProxy) handleChatCompletionStream(ctx context.Context, w http.ResponseWriter, req *openai.ChatCompletionRequest) {
endpoint := "/chat/completions"
p.handle(ctx, func(c *Ctx, log *RequestResponseLog) error {
// 记录开始时间用于性能监控
startTime := time.Now()
// 使用负载均衡算法选择模型
m, err := p.usecase.SelectModelWithLoadBalancing(req.Model, consts.ModelTypeLLM)
if err != nil {
p.logger.With("modelName", req.Model, "modelType", consts.ModelTypeLLM).WarnContext(ctx, "流式请求模型选择失败", "error", err)
writeErrResp(w, http.StatusNotFound, "模型未找到", "proxy_error")
return err
}
prompt := getPrompt(req)
mode := req.Metadata["mode"]
taskID := req.Metadata["task_id"]
// 构造上游API URL
upstream := m.APIBase + endpoint
log.UpstreamURL = upstream
// 创建上游请求
body, err := json.Marshal(req)
if err != nil {
p.logger.ErrorContext(ctx, "序列化请求体失败", "error", err)
return fmt.Errorf("序列化请求体失败: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, upstream, bytes.NewReader(body))
if err != nil {
p.logger.With("upstream", upstream).WarnContext(ctx, "创建上游流式请求失败", "error", err)
return fmt.Errorf("创建上游请求失败: %w", err)
}
// 设置请求头
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "text/event-stream")
if m.APIKey != "" && m.APIKey != "none" {
req.Header.Set("Authorization", "Bearer "+m.APIKey)
}
// 保存请求头(去除敏感信息)
requestHeaders := make(map[string][]string)
for k, v := range req.Header {
if k != "Authorization" {
requestHeaders[k] = v
} else {
// 敏感信息脱敏
requestHeaders[k] = []string{"Bearer ***"}
}
}
log.RequestHeader = requestHeaders
p.logger.With(
"upstreamURL", upstream,
"modelName", m.ModelName,
"modelType", consts.ModelTypeLLM,
"apiBase", m.APIBase,
"work_mode", mode,
"requestHeader", req.Header,
"requestBody", req,
"taskID", taskID,
).DebugContext(ctx, "转发流式请求到上游API")
// 发送请求
resp, err := p.client.Do(req)
if err != nil {
p.logger.With("upstreamURL", upstream).WarnContext(ctx, "发送上游流式请求失败", "error", err)
return fmt.Errorf("发送上游请求失败: %w", err)
}
defer resp.Body.Close()
// 检查响应状态
if resp.StatusCode != http.StatusOK {
responseBody, _ := io.ReadAll(resp.Body)
// 更新日志错误信息
log.StatusCode = resp.StatusCode
log.ResponseHeader = resp.Header
log.ResponseBody = string(responseBody)
log.Latency = time.Since(startTime).Milliseconds()
// 在debug级别记录错误的流式响应内容
p.logger.With(
"statusCode", resp.StatusCode,
"responseHeader", resp.Header,
"responseBody", string(responseBody),
).DebugContext(ctx, "上游流式响应错误原始内容")
var errorResp ErrResp
if err := json.Unmarshal(responseBody, &errorResp); err == nil {
p.logger.With(
"endpoint", endpoint,
"upstreamURL", upstream,
"requestBody", req,
"statusCode", resp.StatusCode,
"errorType", errorResp.Error.Type,
"errorCode", errorResp.Error.Code,
"errorMessage", errorResp.Error.Message,
"latency", time.Since(startTime),
).WarnContext(ctx, "上游API流式请求异常详情")
return fmt.Errorf("上游API返回错误: %s", errorResp.Error.Message)
}
p.logger.With(
"endpoint", endpoint,
"upstreamURL", upstream,
"requestBody", req,
"statusCode", resp.StatusCode,
"responseBody", string(responseBody),
).WarnContext(ctx, "上游API流式请求异常详情")
return fmt.Errorf("上游API返回非200状态码: %d, 响应: %s", resp.StatusCode, string(responseBody))
}
// 更新日志信息
log.StatusCode = resp.StatusCode
log.ResponseHeader = resp.Header
// 在debug级别记录流式响应头信息
p.logger.With(
"statusCode", resp.StatusCode,
"responseHeader", resp.Header,
).DebugContext(ctx, "上游流式响应头信息")
// 设置响应头
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Transfer-Encoding", "chunked")
rc := &domain.RecordParam{
RequestID: c.RequestID,
TaskID: taskID,
UserID: c.UserID,
ModelID: m.ID,
ModelType: consts.ModelTypeLLM,
WorkMode: mode,
Prompt: prompt,
}
buf := bufio.NewWriterSize(w, 32*1024)
defer buf.Flush()
ch := make(chan []byte, 1024)
defer close(ch)
go func(rc *domain.RecordParam) {
for line := range ch {
if bytes.HasPrefix(line, []byte("data:")) {
line = bytes.TrimPrefix(line, []byte("data: "))
line = bytes.TrimSpace(line)
if len(line) == 0 {
continue
}
if bytes.Equal(line, []byte("[DONE]")) {
break
}
var t openai.ChatCompletionStreamResponse
if err := json.Unmarshal(line, &t); err != nil {
p.logger.With("line", string(line)).WarnContext(ctx, "解析流式数据失败", "error", err)
continue
}
p.logger.With("response", t).DebugContext(ctx, "流式响应数据")
if len(t.Choices) > 0 {
rc.Completion += t.Choices[0].Delta.Content
}
if t.Usage != nil {
rc.InputTokens = int64(t.Usage.PromptTokens)
rc.OutputTokens = int64(t.Usage.CompletionTokens)
}
}
}
p.logger.With("record", rc).DebugContext(ctx, "流式记录")
if err := p.usecase.Record(context.Background(), rc); err != nil {
p.logger.With("modelID", m.ID, "modelName", m.ModelName, "modelType", consts.ModelTypeLLM).
WarnContext(ctx, "插入流式记录失败", "error", err)
}
}(rc)
err = streamRead(ctx, resp.Body, func(line []byte) error {
ch <- line
if _, err := buf.Write(line); err != nil {
return fmt.Errorf("写入响应失败: %w", err)
}
return buf.Flush()
})
return err
})
}
func (p *LLMProxy) handleChatCompletion(ctx context.Context, w http.ResponseWriter, req *openai.ChatCompletionRequest) {
endpoint := "/chat/completions"
p.handle(ctx, func(c *Ctx, log *RequestResponseLog) error {
// 使用负载均衡算法选择模型
m, err := p.usecase.SelectModelWithLoadBalancing(req.Model, consts.ModelTypeCoder)
if err != nil {
p.logger.With("modelName", req.Model, "modelType", consts.ModelTypeCoder).WarnContext(ctx, "模型选择失败", "error", err)
writeErrResp(w, http.StatusNotFound, "模型未找到", "proxy_error")
return err
}
// 构造上游API URL
upstream := m.APIBase + endpoint
log.UpstreamURL = upstream
u, err := url.Parse(upstream)
if err != nil {
p.logger.With("upstreamURL", upstream).WarnContext(ctx, "解析上游URL失败", "error", err)
writeErrResp(w, http.StatusInternalServerError, "无效的上游URL", "proxy_error")
return err
}
startTime := time.Now()
prompt := getPrompt(req)
mode := req.Metadata["mode"]
taskID := req.Metadata["task_id"]
client := request.NewClient(u.Scheme, u.Host, 30*time.Second)
resp, err := request.Post[openai.ChatCompletionResponse](client, u.Path, req, request.WithHeader(request.Header{
"Authorization": "Bearer " + m.APIKey,
}))
if err != nil {
p.logger.With("upstreamURL", upstream).WarnContext(ctx, "发送上游请求失败", "error", err)
writeErrResp(w, http.StatusInternalServerError, "发送上游请求失败", "proxy_error")
log.Latency = time.Since(startTime).Milliseconds()
return err
}
// 记录请求信息
p.logger.With(
"upstreamURL", upstream,
"modelName", req.Model,
"modelType", consts.ModelTypeCoder,
"apiBase", m.APIBase,
"requestBody", req,
).DebugContext(ctx, "转发请求到上游API")
go func() {
rc := &domain.RecordParam{
TaskID: taskID,
UserID: c.UserID,
Prompt: prompt,
WorkMode: mode,
ModelID: m.ID,
ModelType: m.ModelType,
InputTokens: int64(resp.Usage.PromptTokens),
OutputTokens: int64(resp.Usage.CompletionTokens),
ProgramLanguage: req.Metadata["program_language"],
}
for _, choice := range resp.Choices {
rc.Completion += choice.Message.Content + "\n\n"
}
p.logger.With("record", rc).DebugContext(ctx, "记录")
if err := p.usecase.Record(ctx, rc); err != nil {
p.logger.With("modelID", m.ID, "modelName", req.Model, "modelType", consts.ModelTypeCoder).WarnContext(ctx, "记录请求失败", "error", err)
}
}()
// 计算请求耗时
latency := time.Since(startTime)
// 更新请求日志
log.StatusCode = http.StatusOK
log.ResponseBody = resp
log.ResponseHeader = resp.Header()
log.Latency = latency.Milliseconds()
// 记录响应状态
p.logger.With(
"statusCode", http.StatusOK,
"responseHeader", resp.Header(),
"responseBody", resp,
"latency", latency.String(),
).DebugContext(ctx, "上游API响应")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
b, err := json.Marshal(resp)
if err != nil {
p.logger.With("response", resp).WarnContext(ctx, "序列化响应失败", "error", err)
return err
}
w.Write(b)
return nil
})
}
func (p *LLMProxy) HandleEmbeddings(ctx context.Context, w http.ResponseWriter, req *openai.EmbeddingRequest) {
}