-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
191 lines (172 loc) · 4.83 KB
/
main.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
package main
import (
"context"
"encoding/json"
"github.com/cheahjs/gemini-to-openai-proxy/pkg/openai"
"github.com/google/generative-ai-go/genai"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"io"
"net/http"
"os"
"slices"
"strings"
"sync/atomic"
)
const (
openAIEmbeddingsEndpoint = "/v1/embeddings"
openAIModelsEndpoints = "/v1/models"
)
var (
GeminiApiKey = os.Getenv("GEMINI_API_KEY")
GeminiApiKeys = strings.Split(GeminiApiKey, ";")
ListenAddr = os.Getenv("LISTEN_ADDR")
geminiClients []*genai.Client
currentClient atomic.Int32
)
func embeddingsHandler(w http.ResponseWriter, r *http.Request) {
requestLogger := log.With().
Str("path", r.URL.Path).
Str("user-agent", r.Header.Get("User-Agent")).
Logger()
if r.Method != http.MethodPost {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
requestLogger.
Error().
Int("status-code", http.StatusMethodNotAllowed).
Msg("")
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
requestLogger.
Error().
Err(errors.Wrap(err, "failed to read request body")).
Int("status-code", http.StatusBadRequest).
Msg("")
return
}
var openAIReq openai.EmbedRequest
err = json.Unmarshal(body, &openAIReq)
if err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
requestLogger.
Error().
Err(errors.Wrap(err, "failed to unmarshal request body")).
Int("status-code", http.StatusBadRequest).
Msg("")
return
}
useIndex := currentClient.Add(1) % int32(len(geminiClients))
requestLogger.Info().Str("model", openAIReq.Model).Int32("client", useIndex).Msg("Processing request")
embeddingModel := geminiClients[useIndex].EmbeddingModel(openAIReq.Model)
geminiBatchReq, err := openai.ConvertOpenAIRequestToGemini(&openAIReq, embeddingModel)
if err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
requestLogger.
Error().
Err(errors.Wrap(err, "failed to convert OpenAI request to Gemini request")).
Int("status-code", http.StatusBadRequest).
Msg("")
return
}
geminiBatchResp, err := embeddingModel.BatchEmbedContents(r.Context(), geminiBatchReq)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
requestLogger.
Error().
Err(errors.Wrap(err, "failed to batch embed contents")).
Int("status-code", http.StatusInternalServerError).
Msg("")
return
}
openAIResp := openai.ConvertGeminiResponseToOpenAI(geminiBatchResp, openAIReq.Model)
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(openAIResp)
if err != nil {
requestLogger.
Error().
Err(errors.Wrap(err, "failed to encode response")).
Int("status-code", http.StatusInternalServerError).
Msg("")
return
}
}
func modelsHandler(w http.ResponseWriter, r *http.Request) {
requestLogger := log.With().
Str("path", r.URL.Path).
Str("user-agent", r.Header.Get("User-Agent")).
Logger()
if r.Method != http.MethodGet {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
requestLogger.
Error().
Int("status-code", http.StatusMethodNotAllowed).
Msg("")
return
}
var models []*openai.ModelResponseData
iter := geminiClients[0].ListModels(r.Context())
for {
m, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
requestLogger.Error().Err(err).Msg("Failed to list models")
return
}
if !slices.Contains(m.SupportedGenerationMethods, "embedContent") {
continue
}
models = append(models, &openai.ModelResponseData{
Object: "model",
ID: m.Name,
Created: 0,
OwnedBy: "google",
})
}
err := json.NewEncoder(w).Encode(&openai.ModelResponse{
Object: "list",
Data: models,
})
if err != nil {
requestLogger.
Error().
Err(errors.Wrap(err, "failed to encode response")).
Int("status-code", http.StatusInternalServerError).
Msg("")
return
}
}
func main() {
if ListenAddr == "" {
ListenAddr = ":8080"
}
if GeminiApiKey == "" {
log.Fatal().Msg("GEMINI_API_KEY is required")
}
currentClient.Store(0)
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
for _, key := range GeminiApiKeys {
client, err := genai.NewClient(context.Background(), option.WithAPIKey(key))
if err != nil {
log.
Fatal().
Err(errors.Wrap(err, "failed to create Gemini client")).
Int("status-code", http.StatusInternalServerError).
Msg("")
return
}
geminiClients = append(geminiClients, client)
}
http.HandleFunc(openAIEmbeddingsEndpoint, embeddingsHandler)
http.HandleFunc(openAIModelsEndpoints, modelsHandler)
log.Info().Msgf("Listening on %s", ListenAddr)
log.Fatal().Err(http.ListenAndServe(ListenAddr, nil)).Msg("Failed to listen and serve")
}