File size: 12,647 Bytes
4727549
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1bca3a1
 
4727549
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0c0993
4727549
f0c0993
 
 
4727549
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46bfd69
 
 
 
 
4727549
46bfd69
4727549
46bfd69
4727549
46bfd69
 
 
 
 
 
 
 
 
 
4727549
46bfd69
4727549
46bfd69
 
4727549
 
 
 
46bfd69
4727549
 
 
 
 
 
 
 
 
 
 
 
 
 
f0c0993
 
 
 
4727549
46bfd69
4727549
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46bfd69
 
 
4727549
 
46bfd69
4727549
46bfd69
4727549
 
 
46bfd69
4727549
 
 
 
46bfd69
4727549
 
 
 
 
46bfd69
4727549
 
 
 
46bfd69
4727549
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46bfd69
4727549
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46bfd69
 
4727549
 
 
 
 
46bfd69
 
 
 
 
4727549
 
46bfd69
 
 
 
 
 
 
4727549
46bfd69
 
 
 
4727549
46bfd69
 
 
 
 
4727549
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46bfd69
 
 
4727549
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package main

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"log"
	"math/rand"
	"net/http"
	"os"
	"strings"
	"time"

	"github.com/google/generative-ai-go/genai"
	"github.com/rs/cors"
	"google.golang.org/api/iterator"
	"google.golang.org/api/option"
)

// --- 配置部分 ---

// API密钥列表 (请替换为您自己的密钥)
var apiKeys = []string{
	"AIzaSyBUIl9AisD8FHUn5HLQcriXZnF4n5MqnWU",
	"AIzaSyAId4YPsZSTLJ5_fA5BESjYxWZBzwADTJI",
	// 在此添加更多密钥
}

// 定义支持的模型信息
var supportedModels = []ModelInfo{
	{
		ID:          "gemini-2.5-flash-preview-05-20",
		Object:      "model",
		Created:     time.Now().Unix(),
		OwnedBy:     "google",
		Description: "Gemini 2.5 Flash Preview - 最新实验性模型",
	},
	{
		ID:          "gemini-2.5-flash",
		Object:      "model",
		Created:     time.Now().Unix(),
		OwnedBy:     "google",
		Description: "gemini-2.5-flash稳定经典专业模型",
	},
	{
		ID:          "gemini-2.5-pro",
		Object:      "model",
		Created:     time.Now().Unix(),
		OwnedBy:     "google",
		Description: "Gemini 2.5 Pro 专业模型",
	},
}

// 将OpenAI模型名称映射到Gemini模型名称
// 根据您的要求,键和值现在是相同的,不做任何转换。
var modelMapping = map[string]string{
	"gemini-2.5-flash-preview-05-20": "gemini-2.5-flash-preview-05-20",
	"gemini-2.5-flash":               "gemini-2.5-flash",
	"gemini-2.5-pro":                 "gemini-2.5-pro",
}

// 配置安全设置 (全部禁用)
var safetySettings = []*genai.SafetySetting{
	{
		Category:  genai.HarmCategoryHarassment,
		Threshold: genai.HarmBlockNone,
	},
	{
		Category:  genai.HarmCategoryHateSpeech,
		Threshold: genai.HarmBlockNone,
	},
	{
		Category:  genai.HarmCategorySexuallyExplicit,
		Threshold: genai.HarmBlockNone,
	},
	{
		Category:  genai.HarmCategoryDangerousContent,
		Threshold: genai.HarmBlockNone,
	},
}

const maxRetries = 3

// --- 数据结构 (用于JSON序列化/反序列化) ---

type ChatMessage struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

type ChatCompletionRequest struct {
	Model       string        `json:"model"`
	Messages    []ChatMessage `json:"messages"`
	Stream      bool          `json:"stream"`
	MaxTokens   int32         `json:"max_tokens,omitempty"`
	Temperature float32       `json:"temperature,omitempty"`
	TopP        float32       `json:"top_p,omitempty"`
}

type ChatCompletionResponse struct {
	ID      string   `json:"id"`
	Object  string   `json:"object"`
	Created int64    `json:"created"`
	Model   string   `json:"model"`
	Choices []Choice `json:"choices"`
	Usage   Usage    `json:"usage"`
}

type Choice struct {
	Index        int         `json:"index"`
	Message      ChatMessage `json:"message"`
	FinishReason string      `json:"finish_reason"`
}

type Usage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

type ChatCompletionStreamResponse struct {
	ID      string         `json:"id"`
	Object  string         `json:"object"`
	Created int64          `json:"created"`
	Model   string         `json:"model"`
	Choices []StreamChoice `json:"choices"`
}

type StreamChoice struct {
	Index        int         `json:"index"`
	Delta        ChatMessage `json:"delta"`
	FinishReason *string     `json:"finish_reason,omitempty"`
}

type ModelInfo struct {
	ID          string `json:"id"`
	Object      string `json:"object"`
	Created     int64  `json:"created"`
	OwnedBy     string `json:"owned_by"`
	Description string `json:"description"`
}

type ModelListResponse struct {
	Object string      `json:"object"`
	Data   []ModelInfo `json:"data"`
}

// --- 核心逻辑 ---

func getRandomAPIKey() string {
	if len(apiKeys) == 0 {
		log.Fatal("API密钥列表为空,请在 `apiKeys` 变量中配置密钥。")
	}
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	return apiKeys[r.Intn(len(apiKeys))]
}

// convertMessages 将OpenAI格式的消息转换为Gemini格式的历史记录和最后一个用户的提示
func convertMessages(messages []ChatMessage) (history []*genai.Content, lastPrompt []genai.Part, systemInstruction *genai.Content) {
	if len(messages) == 0 {
		return nil, nil, nil
	}

	for i, msg := range messages {
		var role string
		if msg.Role == "system" {
			systemInstruction = &genai.Content{Parts: []genai.Part{genai.Text(msg.Content)}}
			continue
		}

		if i == len(messages)-1 && msg.Role == "user" {
			lastPrompt = append(lastPrompt, genai.Text(msg.Content))
			continue
		}

		if msg.Role == "assistant" {
			role = "model"
		} else {
			role = "user"
		}

		history = append(history, &genai.Content{
			Role:  role,
			Parts: []genai.Part{genai.Text(msg.Content)},
		})
	}
	return history, lastPrompt, systemInstruction
}

func chatCompletionsHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodPost {
		http.Error(w, "仅支持POST方法", http.StatusMethodNotAllowed)
		return
	}

	var req ChatCompletionRequest
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		http.Error(w, fmt.Sprintf("解析请求体失败: %v", err), http.StatusBadRequest)
		return
	}

	// 根据您的要求,直接使用请求中的模型名称
	modelName := req.Model
	log.Printf("接收到模型请求: '%s',将直接使用该名称。", modelName)


	history, lastPrompt, systemInstruction := convertMessages(req.Messages)

	var lastErr error
	usedKeys := make(map[string]bool)

	for i := 0; i < maxRetries; i++ {
		ctx := context.Background()
		apiKey := getRandomAPIKey()

		if len(usedKeys) < len(apiKeys) {
			for usedKeys[apiKey] {
				apiKey = getRandomAPIKey()
			}
		}
		usedKeys[apiKey] = true

		log.Printf("尝试第 %d 次, 使用密钥: ...%s", i+1, apiKey[len(apiKey)-4:])

		client, err := genai.NewClient(ctx, option.WithAPIKey(apiKey))
		if err != nil {
			lastErr = fmt.Errorf("创建客户端失败: %v", err)
			log.Println(lastErr)
			continue
		}
		defer client.Close()

		model := client.GenerativeModel(modelName)
		model.SystemInstruction = systemInstruction
		model.SafetySettings = safetySettings
		model.SetTemperature(req.Temperature)
		model.SetTopP(req.TopP)
		if req.MaxTokens > 0 {
			model.SetMaxOutputTokens(req.MaxTokens)
		}
		
		chat := model.StartChat()
		chat.History = history

		if req.Stream {
			err = handleStream(w, ctx, chat, lastPrompt, req.Model)
		} else {
			err = handleNonStream(w, ctx, model, chat, lastPrompt, req.Model)
		}

		if err == nil {
			return 
		}

		lastErr = err
		log.Printf("第 %d 次尝试失败: %v", i+1, err)
		time.Sleep(1 * time.Second)
	}

	http.Error(w, fmt.Sprintf("所有重试均失败: %v", lastErr), http.StatusInternalServerError)
}

func handleStream(w http.ResponseWriter, ctx context.Context, chat *genai.ChatSession, prompt []genai.Part, modelID string) error {
	w.Header().Set("Content-Type", "text/event-stream")
	w.Header().Set("Cache-Control", "no-cache")
	w.Header().Set("Connection", "keep-alive")

	iter := chat.SendMessageStream(ctx, prompt...)
	for {
		resp, err := iter.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("流式生成内容失败: %v", err)
		}

		var contentBuilder strings.Builder
		for _, part := range resp.Candidates[0].Content.Parts {
			if txt, ok := part.(genai.Text); ok {
				contentBuilder.WriteString(string(txt))
			}
		}
		
		chunk := ChatCompletionStreamResponse{
			ID:      fmt.Sprintf("chatcmpl-%d", time.Now().Unix()),
			Object:  "chat.completion.chunk",
			Created: time.Now().Unix(),
			Model:   modelID,
			Choices: []StreamChoice{
				{
					Index: 0,
					Delta: ChatMessage{
						Role:    "assistant",
						Content: contentBuilder.String(),
					},
				},
			},
		}

		var buf bytes.Buffer
		if err := json.NewEncoder(&buf).Encode(chunk); err != nil {
			return fmt.Errorf("序列化流式块失败: %v", err)
		}

		fmt.Fprintf(w, "data: %s\n\n", buf.String())
		if flusher, ok := w.(http.Flusher); ok {
			flusher.Flush()
		}
	}

	finishReason := "stop"
	doneChunk := ChatCompletionStreamResponse{
		ID:      fmt.Sprintf("chatcmpl-%d-done", time.Now().Unix()),
		Object:  "chat.completion.chunk",
		Created: time.Now().Unix(),
		Model:   modelID,
		Choices: []StreamChoice{
			{
				Index:        0,
				FinishReason: &finishReason,
			},
		},
	}
	var buf bytes.Buffer
	json.NewEncoder(&buf).Encode(doneChunk)
	fmt.Fprintf(w, "data: %s\n\n", buf.String())
	fmt.Fprintf(w, "data: [DONE]\n\n")
	if flusher, ok := w.(http.Flusher); ok {
		flusher.Flush()
	}

	return nil
}

func handleNonStream(w http.ResponseWriter, ctx context.Context, model *genai.GenerativeModel, chat *genai.ChatSession, prompt []genai.Part, modelID string) error {
	resp, err := chat.SendMessage(ctx, prompt...)
	if err != nil {
		return fmt.Errorf("生成内容失败: %v", err)
	}

	var contentBuilder strings.Builder
	if len(resp.Candidates) > 0 && resp.Candidates[0].Content != nil {
		for _, part := range resp.Candidates[0].Content.Parts {
			if txt, ok := part.(genai.Text); ok {
				contentBuilder.WriteString(string(txt))
			}
		}
	}
	
	// 计算Token
	var promptParts []genai.Part
	for _, c := range chat.History {
		promptParts = append(promptParts, c.Parts...)
	}
	promptParts = append(promptParts, prompt...)

	promptTokenCount, err := model.CountTokens(ctx, promptParts...)
	if err != nil {
		return fmt.Errorf("计算prompt tokens失败: %v", err)
	}

	completionTokenCount, err := model.CountTokens(ctx, resp.Candidates[0].Content.Parts...)
	if err != nil {
		return fmt.Errorf("计算completion tokens失败: %v", err)
	}
	
	response := ChatCompletionResponse{
		ID:      fmt.Sprintf("chatcmpl-%d", time.Now().Unix()),
		Object:  "chat.completion",
		Created: time.Now().Unix(),
		Model:   modelID,
		Choices: []Choice{
			{
				Index: 0,
				Message: ChatMessage{
					Role:    "assistant",
					Content: contentBuilder.String(),
				},
				FinishReason: "stop",
			},
		},
		Usage: Usage{
			PromptTokens:     int(promptTokenCount.TotalTokens),
			CompletionTokens: int(completionTokenCount.TotalTokens),
			TotalTokens:      int(promptTokenCount.TotalTokens) + int(completionTokenCount.TotalTokens),
		},
	}

	w.Header().Set("Content-Type", "application/json")
	return json.NewEncoder(w).Encode(response)
}


func modelsHandler(w http.ResponseWriter, r *http.Request) {
	resp := ModelListResponse{
		Object: "list",
		Data:   supportedModels,
	}
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(resp)
}

func rootHandler(w http.ResponseWriter, r *http.Request) {
	info := map[string]interface{}{
		"name":        "Gemini Official API (Go Version)",
		"version":     "1.3.0",
		"description": "Google Gemini官方API接口服务",
		"endpoints": map[string]string{
			"models": "/v1/models",
			"chat":   "/v1/chat/completions",
			"health": "/health",
		},
	}
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(info)
}

func healthHandler(w http.ResponseWriter, r *http.Request) {
	var modelIDs []string
	for _, m := range supportedModels {
		modelIDs = append(modelIDs, m.ID)
	}
	health := map[string]interface{}{
		"status":           "healthy",
		"timestamp":        time.Now().Unix(),
		"api":              "gemini-official-go",
		"available_models": modelIDs,
		"version":          "1.3.0",
	}
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(health)
}

func main() {
	mux := http.NewServeMux()

	mux.HandleFunc("/", rootHandler)
	mux.HandleFunc("/health", healthHandler)
	mux.HandleFunc("/v1/models", modelsHandler)
	mux.HandleFunc("/v1/chat/completions", chatCompletionsHandler)
	mux.HandleFunc("/v1/chat/completions/v1/models", modelsHandler)

	c := cors.New(cors.Options{
		AllowedOrigins:   []string{"*"},
		AllowedMethods:   []string{"GET", "POST", "OPTIONS"},
		AllowedHeaders:   []string{"*"},
		AllowCredentials: true,
	})
	handler := c.Handler(mux)

	port := "7860"
	log.Println("🚀 启动Gemini官方API服务器 (Go版本)")
	log.Printf("📊 支持的模型: %v", func() []string {
		var ids []string
		for _, m := range supportedModels {
			ids = append(ids, m.ID)
		}
		return ids
	}())
	log.Printf("🔑 已配置 %d 个API密钥", len(apiKeys))
	log.Println("🔄 支持自动重试和密钥轮换")
	log.Printf("🔗 服务器正在监听 http://0.0.0.0:%s", port)

	envKey := os.Getenv("GEMINI_API_KEY")
	if envKey != "" {
		apiKeys = strings.Split(envKey, ",")
		log.Printf("从环境变量 GEMINI_API_KEY 加载了 %d 个密钥", len(apiKeys))
	}

	if err := http.ListenAndServe(":"+port, handler); err != nil {
		log.Fatalf("启动服务器失败: %v", err)
	}
}