Skip to content

Commit

Permalink
feat: Support /api/show for inspecting model information
Browse files Browse the repository at this point in the history
  • Loading branch information
prantlf committed Jun 4, 2024
1 parent d2f59f1 commit 8a2c2bc
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/ovai/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func main() {
http.HandleFunc("/api/embeddings", web.WrapHandler(routes.HandleEmbeddings, []string{"POST"}))
http.HandleFunc("/api/generate", web.WrapHandler(routes.HandleGenerate, []string{"POST"}))
http.HandleFunc("/api/ping", web.WrapHandler(routes.HandlePing, []string{"GET"}))
http.HandleFunc("/api/show", web.WrapHandler(routes.HandleShow, []string{"POST"}))
http.HandleFunc("/api/shutdown", web.WrapHandler(routes.HandleShutdown, []string{"POST"}))
http.HandleFunc("/api/tags", web.WrapHandler(routes.HandleTags, []string{"GET"}))

Expand Down
9 changes: 7 additions & 2 deletions internal/routes/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ func proxyRequest(name string, input []byte, w http.ResponseWriter, result strin
return failRequest(w, status, err.Error())
}
if log.IsDbg {
log.Dbg("< %s by %s with %d byte%s", result, model,
len(output), log.GetPlural(len(output)))
if len(model) > 0 {
log.Dbg("< %s by %s with %d byte%s", result, model,
len(output), log.GetPlural(len(output)))
} else {
log.Dbg("< %s with %d byte%s", result,
len(output), log.GetPlural(len(output)))
}
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
Expand Down
69 changes: 69 additions & 0 deletions internal/routes/show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package routes

import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"

"github.com/prantlf/ovai/internal/log"
)

type showInput struct {
Name string `json:"name"`
}

type showOutput struct {
License string `json:"license"`
ModelFile string `json:"modelfile"`
Parameters string `json:"parameters"`
Template string `json:"template"`
Details modelDetails `json:"details"`
}

func HandleShow(w http.ResponseWriter, r *http.Request) int {
var input showInput
reqPayload, err := io.ReadAll(r.Body)
if err != nil {
return wrongInput(w, fmt.Sprintf("reading request body failed: %v", err))
}
if err := json.Unmarshal(reqPayload, &input); err != nil {
return wrongInput(w, fmt.Sprintf("decoding request body failed: %v", err))
}
// if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
// return wrongInput(w, fmt.Sprintf("decoding request body failed: %v", err))
// }
if len(input.Name) == 0 {
return wrongInput(w, "model name missing")
}
log.Dbg("> look for %s", input.Name)
if strings.HasPrefix(input.Name, "gemini") ||
strings.HasPrefix(input.Name, "text-bison") ||
strings.HasPrefix(input.Name, "chat-bison") ||
strings.HasPrefix(input.Name, "textembedding-gecko") {
var details *modelDetails
for _, model := range googleModels {
if model.Name == input.Name {
details = &model.Details
}
}
if details != nil {
if log.IsDbg {
log.Dbg("< found %s", input.Name)
}
output := &showOutput{
Details: *details,
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(output); err != nil {
log.Dbg("! encoding response body failed: %v", err)
}
return http.StatusOK
}
} else if canProxy {
return proxyRequest("show", reqPayload, w, "model", "")
}
return wrongInput(w, fmt.Sprintf("unrecognised model %q", input.Name))
}
Loading

0 comments on commit 8a2c2bc

Please sign in to comment.