add /v1/models endpoint and proxy everything to llama-server

This commit is contained in:
Benson Wong
2024-10-04 12:28:50 -07:00
parent e0103d1884
commit 3e90f8328d

View File

@@ -28,10 +28,41 @@ func New(config *Config) *ProxyManager {
}
func (pm *ProxyManager) HandleFunc(w http.ResponseWriter, r *http.Request) {
// https://github.com/ggerganov/llama.cpp/blob/master/examples/server/README.md#api-endpoints
if r.URL.Path == "/v1/chat/completions" {
// extracts the `model` from json body
pm.proxyChatRequest(w, r)
} else if r.URL.Path == "/v1/models" {
// Transform Models to the desired JSON structure
jsonOutput := map[string]interface{}{
"object": "list",
"data": []map[string]string{},
}
for id := range pm.config.Models {
modelData := map[string]string{
"id": id,
"object": "model",
}
jsonOutput["data"] = append(jsonOutput["data"].([]map[string]string), modelData)
}
// Marshal the JSON output for display
result, err := json.MarshalIndent(jsonOutput, "", " ")
if err != nil {
http.Error(w, "JSON marshal error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(result)
} else {
http.Error(w, "endpoint not supported", http.StatusNotFound)
pm.proxyRequest(w, r)
}
}