forked from danilofalcao/cursor-deepseek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.go
718 lines (620 loc) · 18.6 KB
/
proxy.go
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
package main
import (
"bufio"
"bytes"
"compress/flate"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/andybalholm/brotli"
"github.com/joho/godotenv"
"golang.org/x/net/http2"
)
const (
deepseekEndpoint = "https://api.deepseek.com"
deepseekBetaEndpoint = "https://api.deepseek.com/beta"
deepseekChatModel = "deepseek-chat"
deepseekCoderModel = "deepseek-coder"
gpt4oModel = "gpt-4o"
)
var deepseekAPIKey string
// Configuration structure
type Config struct {
endpoint string
model string
}
var activeConfig Config
func init() {
// Load .env file
if err := godotenv.Load(); err != nil {
log.Printf("Warning: .env file not found or error loading it: %v", err)
}
// Get DeepSeek API key
deepseekAPIKey = os.Getenv("DEEPSEEK_API_KEY")
if deepseekAPIKey == "" {
log.Fatal("DEEPSEEK_API_KEY environment variable is required")
}
// Parse command line arguments
modelFlag := "chat" // default value
for i, arg := range os.Args {
if arg == "-model" && i+1 < len(os.Args) {
modelFlag = os.Args[i+1]
}
}
// Configure the active endpoint and model based on the flag
switch modelFlag {
case "coder":
activeConfig = Config{
endpoint: deepseekBetaEndpoint,
model: deepseekCoderModel,
}
case "chat":
activeConfig = Config{
endpoint: deepseekEndpoint,
model: deepseekChatModel,
}
default:
log.Printf("Invalid model specified: %s. Using default chat model.", modelFlag)
activeConfig = Config{
endpoint: deepseekEndpoint,
model: deepseekChatModel,
}
}
log.Printf("Initialized with model: %s using endpoint: %s", activeConfig.model, activeConfig.endpoint)
}
// Models response structure
type ModelsResponse struct {
Object string `json:"object"`
Data []Model `json:"data"`
}
type Model struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
OwnedBy string `json:"owned_by"`
}
// OpenAI compatible request structure
type ChatRequest struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
Stream bool `json:"stream"`
Functions []Function `json:"functions,omitempty"`
Tools []Tool `json:"tools,omitempty"`
ToolChoice interface{} `json:"tool_choice,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
MaxTokens *int `json:"max_tokens,omitempty"`
}
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
Name string `json:"name,omitempty"`
}
type Function struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters any `json:"parameters"`
}
type Tool struct {
Type string `json:"type"`
Function Function `json:"function"`
}
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Function struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
} `json:"function"`
}
func convertToolChoice(choice interface{}) string {
if choice == nil {
return ""
}
// If string "auto" or "none"
if str, ok := choice.(string); ok {
switch str {
case "auto", "none":
return str
}
}
// Try to parse as map for function call
if choiceMap, ok := choice.(map[string]interface{}); ok {
if choiceMap["type"] == "function" {
return "auto" // DeepSeek doesn't support specific function selection, default to auto
}
}
return ""
}
func convertMessages(messages []Message) []Message {
converted := make([]Message, len(messages))
for i, msg := range messages {
log.Printf("Converting message %d - Role: %s", i, msg.Role)
converted[i] = msg
// Handle assistant messages with tool calls
if msg.Role == "assistant" && len(msg.ToolCalls) > 0 {
log.Printf("Processing assistant message with %d tool calls", len(msg.ToolCalls))
// DeepSeek expects tool_calls in a specific format
toolCalls := make([]ToolCall, len(msg.ToolCalls))
for j, tc := range msg.ToolCalls {
toolCalls[j] = ToolCall{
ID: tc.ID,
Type: "function",
Function: tc.Function,
}
log.Printf("Tool call %d - ID: %s, Function: %s", j, tc.ID, tc.Function.Name)
}
converted[i].ToolCalls = toolCalls
}
// Handle function response messages
if msg.Role == "function" {
log.Printf("Converting function response to tool response")
// Convert to tool response format
converted[i].Role = "tool"
}
}
// Log the final converted messages
for i, msg := range converted {
log.Printf("Final message %d - Role: %s, Content: %s", i, msg.Role, truncateString(msg.Content, 50))
if len(msg.ToolCalls) > 0 {
log.Printf("Message %d has %d tool calls", i, len(msg.ToolCalls))
}
}
return converted
}
func truncateString(s string, maxLen int) string {
if len(s) <= maxLen {
return s
}
return s[:maxLen] + "..."
}
// DeepSeek request structure
type DeepSeekRequest struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
Stream bool `json:"stream"`
Temperature float64 `json:"temperature,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
Tools []Tool `json:"tools,omitempty"`
ToolChoice string `json:"tool_choice,omitempty"`
}
func main() {
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile)
server := &http.Server{
Addr: ":9000",
Handler: http.HandlerFunc(proxyHandler),
}
// Enable HTTP/2 support
http2.ConfigureServer(server, &http2.Server{})
log.Printf("Starting proxy server on %s", server.Addr)
if err := server.ListenAndServe(); err != nil {
log.Fatalf("Server failed: %v", err)
}
}
func enableCors(w http.ResponseWriter) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Origin, Content-Type, Accept, Authorization")
w.Header().Set("Access-Control-Expose-Headers", "Content-Length")
w.Header().Set("Access-Control-Allow-Credentials", "true")
}
func proxyHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("Received request: %s %s", r.Method, r.URL.Path)
if r.Method == "OPTIONS" {
enableCors(w)
return
}
enableCors(w)
// Validate API key
authHeader := r.Header.Get("Authorization")
if !strings.HasPrefix(authHeader, "Bearer ") {
log.Printf("Missing or invalid Authorization header")
http.Error(w, "Missing or invalid Authorization header", http.StatusUnauthorized)
return
}
userAPIKey := strings.TrimPrefix(authHeader, "Bearer ")
if userAPIKey != deepseekAPIKey {
log.Printf("Invalid API key provided")
http.Error(w, "Invalid API key", http.StatusUnauthorized)
return
}
// Handle /v1/models endpoint
if r.URL.Path == "/v1/models" && r.Method == "GET" {
log.Printf("Handling /v1/models request")
handleModelsRequest(w)
return
}
// Log headers for debugging
log.Printf("Request headers: %+v", r.Header)
// Read and log request body for debugging
var chatReq ChatRequest
body, err := io.ReadAll(r.Body)
if err != nil {
log.Printf("Error reading request body: %v", err)
http.Error(w, "Error reading request", http.StatusBadRequest)
return
}
r.Body = io.NopCloser(bytes.NewBuffer(body))
if err := json.Unmarshal(body, &chatReq); err != nil {
log.Printf("Error parsing request JSON: %v", err)
log.Printf("Raw request body: %s", string(body))
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
log.Printf("Parsed request: %+v", chatReq)
// Handle models endpoint
if r.URL.Path == "/v1/models" {
handleModelsRequest(w)
return
}
// Only handle API requests with /v1/ prefix
if !strings.HasPrefix(r.URL.Path, "/v1/") {
log.Printf("Invalid path: %s", r.URL.Path)
http.Error(w, "Not found", http.StatusNotFound)
return
}
// Restore the body for further reading
r.Body = io.NopCloser(bytes.NewBuffer(body))
log.Printf("Request body: %s", string(body))
// Parse the request to check for streaming - reuse existing chatReq
if err := json.Unmarshal(body, &chatReq); err != nil {
log.Printf("Error parsing request JSON: %v", err)
http.Error(w, "Error parsing request", http.StatusBadRequest)
return
}
log.Printf("Requested model: %s", chatReq.Model)
// Replace gpt-4o model with deepseek-chat
if chatReq.Model == gpt4oModel {
chatReq.Model = deepseekChatModel
log.Printf("Model converted to: %s", deepseekChatModel)
} else {
log.Printf("Unsupported model requested: %s", chatReq.Model)
http.Error(w, fmt.Sprintf("Model %s not supported. Use %s instead.", chatReq.Model, gpt4oModel), http.StatusBadRequest)
return
}
// Convert to DeepSeek request format
deepseekReq := DeepSeekRequest{
Model: deepseekChatModel,
Messages: convertMessages(chatReq.Messages),
Stream: chatReq.Stream,
}
// Copy optional parameters if present
if chatReq.Temperature != nil {
deepseekReq.Temperature = *chatReq.Temperature
}
if chatReq.MaxTokens != nil {
deepseekReq.MaxTokens = *chatReq.MaxTokens
}
// Handle tools/functions
if len(chatReq.Tools) > 0 {
deepseekReq.Tools = chatReq.Tools
if tc := convertToolChoice(chatReq.ToolChoice); tc != "" {
deepseekReq.ToolChoice = tc
}
} else if len(chatReq.Functions) > 0 {
// Convert functions to tools format
tools := make([]Tool, len(chatReq.Functions))
for i, fn := range chatReq.Functions {
tools[i] = Tool{
Type: "function",
Function: fn,
}
}
deepseekReq.Tools = tools
// Convert tool_choice if present
if tc := convertToolChoice(chatReq.ToolChoice); tc != "" {
deepseekReq.ToolChoice = tc
}
}
// Create new request body
modifiedBody, err := json.Marshal(deepseekReq)
if err != nil {
log.Printf("Error creating modified request body: %v", err)
http.Error(w, "Error creating modified request", http.StatusInternalServerError)
return
}
log.Printf("Modified request body: %s", string(modifiedBody))
// Create the proxy request to DeepSeek
targetURL := activeConfig.endpoint + r.URL.Path
if r.URL.RawQuery != "" {
targetURL += "?" + r.URL.RawQuery
}
log.Printf("Forwarding to: %s", targetURL)
proxyReq, err := http.NewRequest(r.Method, targetURL, bytes.NewReader(modifiedBody))
if err != nil {
log.Printf("Error creating proxy request: %v", err)
http.Error(w, "Error creating proxy request", http.StatusInternalServerError)
return
}
// Copy headers
copyHeaders(proxyReq.Header, r.Header)
// Set DeepSeek API key and content type
proxyReq.Header.Set("Authorization", "Bearer "+deepseekAPIKey)
proxyReq.Header.Set("Content-Type", "application/json")
if chatReq.Stream {
proxyReq.Header.Set("Accept", "text/event-stream")
}
// Add Accept-Language header from request
if acceptLanguage := r.Header.Get("Accept-Language"); acceptLanguage != "" {
proxyReq.Header.Set("Accept-Language", acceptLanguage)
}
log.Printf("Proxy request headers: %v", proxyReq.Header)
// Create a custom client with keepalive
client := &http.Client{
Transport: &http2.Transport{
AllowHTTP: true,
DialTLS: nil,
},
Timeout: 5 * time.Minute,
}
// Send the request
resp, err := client.Do(proxyReq)
if err != nil {
log.Printf("Error forwarding request: %v", err)
http.Error(w, "Error forwarding request", http.StatusBadGateway)
return
}
defer resp.Body.Close()
log.Printf("DeepSeek response status: %d", resp.StatusCode)
log.Printf("DeepSeek response headers: %v", resp.Header)
// Handle error responses
if resp.StatusCode >= 400 {
respBody, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("Error reading error response: %v", err)
http.Error(w, "Error reading response", http.StatusInternalServerError)
return
}
log.Printf("DeepSeek error response: %s", string(respBody))
// Forward the error response
for k, v := range resp.Header {
w.Header()[k] = v
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resp.StatusCode)
w.Write(respBody)
return
}
// Handle streaming response
if chatReq.Stream {
handleStreamingResponse(w, r, resp)
return
}
// Handle regular response
handleRegularResponse(w, resp)
}
func handleStreamingResponse(w http.ResponseWriter, r *http.Request, resp *http.Response) {
log.Printf("Starting streaming response handling")
log.Printf("Response status: %d", resp.StatusCode)
log.Printf("Response headers: %+v", resp.Header)
// Set headers for streaming response
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.WriteHeader(resp.StatusCode)
// Create a buffered reader for the response body
reader := bufio.NewReader(resp.Body)
// Create a context with cancel for cleanup
ctx, cancel := context.WithCancel(r.Context())
defer cancel()
// Start a goroutine to send heartbeats
go func() {
ticker := time.NewTicker(15 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
// Send a heartbeat comment
if _, err := w.Write([]byte(": heartbeat\n\n")); err != nil {
log.Printf("Error sending heartbeat: %v", err)
cancel()
return
}
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
case <-ctx.Done():
return
}
}
}()
for {
select {
case <-ctx.Done():
log.Printf("Context cancelled, ending stream")
return
default:
line, err := reader.ReadBytes('\n')
if err != nil {
if err == io.EOF {
continue
}
log.Printf("Error reading stream: %v", err)
cancel()
return
}
// Skip empty lines
if len(bytes.TrimSpace(line)) == 0 {
continue
}
// Write the line to the response
if _, err := w.Write(line); err != nil {
log.Printf("Error writing to response: %v", err)
cancel()
return
}
// Flush the response writer
if f, ok := w.(http.Flusher); ok {
f.Flush()
} else {
log.Printf("Warning: ResponseWriter does not support Flush")
}
}
}
}
func handleRegularResponse(w http.ResponseWriter, resp *http.Response) {
log.Printf("Handling regular (non-streaming) response")
log.Printf("Response status: %d", resp.StatusCode)
log.Printf("Response headers: %+v", resp.Header)
// Read and log response body
body, err := readResponse(resp)
if err != nil {
log.Printf("Error reading response: %v", err)
http.Error(w, "Error reading response from upstream", http.StatusInternalServerError)
return
}
log.Printf("Original response body: %s", string(body))
// Parse the DeepSeek response
var deepseekResp struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []struct {
Index int `json:"index"`
Message Message `json:"message"`
FinishReason string `json:"finish_reason"`
} `json:"choices"`
Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
} `json:"usage"`
}
if err := json.Unmarshal(body, &deepseekResp); err != nil {
log.Printf("Error parsing DeepSeek response: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
// Convert to OpenAI format
openAIResp := struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []struct {
Index int `json:"index"`
Message Message `json:"message"`
FinishReason string `json:"finish_reason"`
} `json:"choices"`
Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
} `json:"usage"`
}{
ID: deepseekResp.ID,
Object: "chat.completion",
Created: deepseekResp.Created,
Model: activeConfig.model,
Usage: deepseekResp.Usage,
}
// Convert choices and ensure tool calls are properly handled
openAIResp.Choices = make([]struct {
Index int `json:"index"`
Message Message `json:"message"`
FinishReason string `json:"finish_reason"`
}, len(deepseekResp.Choices))
for i, choice := range deepseekResp.Choices {
openAIResp.Choices[i] = struct {
Index int `json:"index"`
Message Message `json:"message"`
FinishReason string `json:"finish_reason"`
}{
Index: choice.Index,
Message: choice.Message,
FinishReason: choice.FinishReason,
}
// Ensure tool calls are properly formatted in the message
if len(choice.Message.ToolCalls) > 0 {
log.Printf("Processing %d tool calls in choice %d", len(choice.Message.ToolCalls), i)
for j, tc := range choice.Message.ToolCalls {
log.Printf("Tool call %d: %+v", j, tc)
// Ensure the tool call has the required fields
if tc.Function.Name == "" {
log.Printf("Warning: Empty function name in tool call %d", j)
continue
}
// Keep the tool call as is since it's already in the correct format
openAIResp.Choices[i].Message.ToolCalls = append(openAIResp.Choices[i].Message.ToolCalls, tc)
}
}
}
// Convert back to JSON
modifiedBody, err := json.Marshal(openAIResp)
if err != nil {
log.Printf("Error creating modified response: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
log.Printf("Modified response body: %s", string(modifiedBody))
// Set response headers
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resp.StatusCode)
w.Write(modifiedBody)
log.Printf("Modified response sent successfully")
}
func copyHeaders(dst, src http.Header) {
// Headers to skip
skipHeaders := map[string]bool{
"Content-Length": true,
"Content-Encoding": true,
"Transfer-Encoding": true,
"Connection": true,
}
for k, vv := range src {
if !skipHeaders[k] {
for _, v := range vv {
dst.Add(k, v)
}
}
}
}
func handleModelsRequest(w http.ResponseWriter) {
log.Printf("Handling models request")
response := ModelsResponse{
Object: "list",
Data: []Model{
{
ID: "gpt-4o",
Object: "model",
Created: time.Now().Unix(),
OwnedBy: "openai",
},
{
ID: "deepseek-chat",
Object: "model",
Created: time.Now().Unix(),
OwnedBy: "deepseek",
},
},
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
log.Printf("Models response sent successfully")
}
func readResponse(resp *http.Response) ([]byte, error) {
var reader io.Reader = resp.Body
switch resp.Header.Get("Content-Encoding") {
case "gzip":
gzReader, err := gzip.NewReader(resp.Body)
if err != nil {
return nil, fmt.Errorf("error creating gzip reader: %v", err)
}
defer gzReader.Close()
reader = gzReader
case "br":
reader = brotli.NewReader(resp.Body)
case "deflate":
reader = flate.NewReader(resp.Body)
}
return io.ReadAll(reader)
}