Skip to content

Commit af92361

Browse files
authored
feat: add models api (#1)
1 parent 9814784 commit af92361

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

main.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ func escapeJSON(str string) string {
222222
return string(b[1 : len(b)-1])
223223
}
224224

225+
var allowModels = []string{"claude-3-haiku-20240307", "claude-3-sonnet-20240229", "claude-3-opus-20240229", "claude-3-5-sonnet-20240620"}
226+
225227
func handler(c *gin.Context) {
226228
var openAIReq OpenAIRequest
227229

@@ -230,11 +232,9 @@ func handler(c *gin.Context) {
230232
return
231233
}
232234

233-
allowModels := []string{"claude-3-haiku-20240307", "claude-3-sonnet-20240229", "claude-3-opus-20240229"}
234-
235235
// Default model is claude-3-haiku-20240307
236236
if !isInSlice(openAIReq.Model, allowModels) {
237-
openAIReq.Model = "claude-3-haiku-20240307"
237+
openAIReq.Model = allowModels[0]
238238
}
239239

240240
// If stream is true, proxy to Claude with stream
@@ -245,6 +245,20 @@ func handler(c *gin.Context) {
245245
}
246246
}
247247

248+
func modelsHandler(c *gin.Context) {
249+
openAIResp := OpenAIModelsResponse{
250+
Object: "list",
251+
}
252+
for _, model := range allowModels {
253+
openAIResp.Data = append(openAIResp.Data, OpenAIModel{
254+
ID: model,
255+
Object: "model",
256+
OwnedBy: "user",
257+
})
258+
}
259+
c.JSON(http.StatusOK, openAIResp)
260+
}
261+
248262
func isInSlice(str string, list []string) bool {
249263
for _, item := range list {
250264
if item == str {
@@ -264,6 +278,7 @@ func main() {
264278
})
265279
})
266280
r.POST("/v1/chat/completions", handler)
281+
r.GET("/v1/models", modelsHandler)
267282
r.NoRoute(func(c *gin.Context) {
268283
c.JSON(http.StatusNotFound, gin.H{
269284
"code": http.StatusNotFound,

types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,15 @@ type OpenAIResponse struct {
5353
TotalTokens int `json:"total_tokens"`
5454
} `json:"usage"`
5555
}
56+
57+
type OpenAIModel struct {
58+
ID string `json:"id"`
59+
Object string `json:"object"`
60+
Created int64 `json:"created"`
61+
OwnedBy string `json:"owned_by"`
62+
}
63+
64+
type OpenAIModelsResponse struct {
65+
Object string `json:"object"`
66+
Data []OpenAIModel `json:"data"`
67+
}

0 commit comments

Comments
 (0)