diff --git a/api/v1/interface.go b/api/v1/interface.go index 7e518906a5..dd6c0b67bd 100644 --- a/api/v1/interface.go +++ b/api/v1/interface.go @@ -45,3 +45,15 @@ type imageResponse struct { type imageData struct { Url string `json:"url"` } + +type modelStruct struct { + Id string `json:"id"` + Object string `json:"object"` + Created int64 `json:"created"` + OwnedBy string `json:"owned_by"` +} + +type modelResponse struct { + Object string `json:"object"` + Data []modelStruct `json:"data"` +} diff --git a/api/v1/model.go b/api/v1/model.go index 2b76a798a9..b952e1a901 100644 --- a/api/v1/model.go +++ b/api/v1/model.go @@ -1,7 +1,45 @@ package v1 -import "net/http" +import ( + "encoding/json" + "net/http" + "strings" +) -func ModelsHandler(w http.ResponseWriter, r *http.Request) { +func ModelHandler(w http.ResponseWriter, r *http.Request) { + if apikey != "" { + if r.Header.Get("Authorization") != "Bearer "+apikey { + w.WriteHeader(http.StatusUnauthorized) + w.Write([]byte("Unauthorized")) + return + } + } + parts := strings.Split(r.URL.Path, "/") + modelId := parts[len(parts)-1] + + if modelId == "" { + ModelsHandler(w, r) + return + } + + if modelId != "dall-e-3" && !isInArray(chatMODELS, modelId) { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte("Not Found")) + return + } + + resp := modelStruct{ + Id: modelId, + Object: "model", + Created: 1687579610, + OwnedBy: "Go-Proxy-BingAI", + } + respData, err := json.Marshal(resp) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + w.Write(respData) } diff --git a/api/v1/models.go b/api/v1/models.go new file mode 100644 index 0000000000..437540fe3d --- /dev/null +++ b/api/v1/models.go @@ -0,0 +1,46 @@ +package v1 + +import ( + "encoding/json" + "net/http" +) + +func ModelsHandler(w http.ResponseWriter, r *http.Request) { + if apikey != "" { + if r.Header.Get("Authorization") != "Bearer "+apikey { + w.WriteHeader(http.StatusUnauthorized) + w.Write([]byte("Unauthorized")) + return + } + } + + models := []modelStruct{ + { + Id: "dall-e-3", + Object: "model", + Created: 1687579610, + OwnedBy: "Go-Proxy-BingAI", + }, + } + for _, model := range chatMODELS { + models = append(models, modelStruct{ + Id: model, + Object: "model", + Created: 1687579610, + OwnedBy: "Go-Proxy-BingAI", + }) + } + + resp := modelResponse{ + Object: "list", + Data: models, + } + respData, err := json.Marshal(resp) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + w.Write(respData) +} diff --git a/main.go b/main.go index 921f7a1020..8b44f10914 100644 --- a/main.go +++ b/main.go @@ -12,8 +12,12 @@ import ( func main() { http.HandleFunc("/v1/chat/completions", v1.ChatHandler) http.HandleFunc("/v1/images/generations", v1.ImageHandler) + http.HandleFunc("/v1/models/", v1.ModelHandler) + http.HandleFunc("/v1/models", v1.ModelsHandler) http.HandleFunc("/api/v1/chat/completions", v1.ChatHandler) http.HandleFunc("/api/v1/images/generations", v1.ImageHandler) + http.HandleFunc("/api/v1/models/", v1.ModelHandler) + http.HandleFunc("/api/v1/models", v1.ModelsHandler) http.HandleFunc("/sysconf", api.SysConf)