Skip to content

Commit

Permalink
[Add] ✨ Models API
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry-zklcdc committed Jan 15, 2024
1 parent 6e5579c commit ab744ec
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
12 changes: 12 additions & 0 deletions api/v1/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
42 changes: 40 additions & 2 deletions api/v1/model.go
Original file line number Diff line number Diff line change
@@ -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)
}
46 changes: 46 additions & 0 deletions api/v1/models.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit ab744ec

Please sign in to comment.