Skip to content

Commit eaf211b

Browse files
Implement Function Calling, Streamlined Model Listing, and OpenAI-Compatible Chunking (#57)
* fix auto model list * add fuction and tool calling * clear code and add chunk as openai * error handle * error handle and rate limite handle and chunk by character * rename models from models/modelname for modelname * add ResponseFormat format * removing delay for steaming * fix lint and over head streming
1 parent c72954f commit eaf211b

File tree

8 files changed

+2352
-48
lines changed

8 files changed

+2352
-48
lines changed

.DS_Store

6 KB
Binary file not shown.

api/handler.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,37 @@ func IndexHandler(c *gin.Context) {
2424

2525
func ModelListHandler(c *gin.Context) {
2626
owner := adapter.GetOwner()
27+
28+
// Get authorization header to initialize models if needed
29+
authorizationHeader := c.GetHeader("Authorization")
30+
var apiKey string
31+
_, err := fmt.Sscanf(authorizationHeader, "Bearer %s", &apiKey)
32+
if err == nil {
33+
adapter.InitGeminiModels(apiKey)
34+
}
35+
36+
if !adapter.USE_MODEL_MAPPING {
37+
// When model mapping is disabled, return the actual Gemini models
38+
models := adapter.GetAvailableGeminiModels()
39+
modelList := make([]any, 0, len(models))
40+
41+
for _, modelName := range models {
42+
modelList = append(modelList, openai.Model{
43+
CreatedAt: 1686935002,
44+
ID: modelName,
45+
Object: "model",
46+
OwnedBy: owner,
47+
})
48+
}
49+
50+
c.JSON(http.StatusOK, gin.H{
51+
"object": "list",
52+
"data": modelList,
53+
})
54+
return
55+
}
56+
57+
// When model mapping is enabled, return the OpenAI models
2758
c.JSON(http.StatusOK, gin.H{
2859
"object": "list",
2960
"data": []any{
@@ -89,6 +120,17 @@ func ChatProxyHandler(c *gin.Context) {
89120
handleGenerateContentError(c, err)
90121
return
91122
}
123+
124+
// Initialize Gemini models if not already initialized
125+
if err := adapter.InitGeminiModels(openaiAPIKey); err != nil {
126+
log.Printf("Error initializing Gemini models: %v", err)
127+
c.JSON(http.StatusInternalServerError, openai.APIError{
128+
Code: http.StatusInternalServerError,
129+
Message: "Failed to initialize Gemini models: " + err.Error(),
130+
Type: "server_error",
131+
})
132+
return
133+
}
92134

93135
req := &adapter.ChatCompletionRequest{}
94136
// Bind the JSON data from the request to the struct

gemini-server

26.6 MB
Binary file not shown.

0 commit comments

Comments
 (0)