Add OpenAIProxy handler to route requests based on instance name

This commit is contained in:
2025-07-24 23:04:28 +02:00
parent 5c831a327c
commit 451076b2bf
2 changed files with 39 additions and 0 deletions

View File

@@ -452,3 +452,41 @@ func (h *Handler) ProxyToInstance() http.HandlerFunc {
proxy.ServeHTTP(w, r) proxy.ServeHTTP(w, r)
} }
} }
// OpenAIProxy godoc
func (h *Handler) OpenAIProxy() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Extract model name from request body
var requestBody map[string]any
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
modelName, ok := requestBody["model"].(string)
if !ok || modelName == "" {
http.Error(w, "Model name is required", http.StatusBadRequest)
return
}
// Route to the appropriate instance based on model name
instance, err := h.InstanceManager.GetInstance(modelName)
if err != nil {
http.Error(w, "Failed to get instance: "+err.Error(), http.StatusInternalServerError)
return
}
if !instance.Running {
http.Error(w, "Instance is not running", http.StatusServiceUnavailable)
return
}
proxy, err := instance.GetProxy()
if err != nil {
http.Error(w, "Failed to get proxy: "+err.Error(), http.StatusInternalServerError)
return
}
proxy.ServeHTTP(w, r)
}
}

View File

@@ -48,6 +48,7 @@ func SetupRouter(handler *Handler) *chi.Mux {
}) })
// OpenAI-compatible endpoints (model name in request body determines routing) // OpenAI-compatible endpoints (model name in request body determines routing)
r.Post("/v1/", handler.OpenAIProxy()) // Proxy to OpenAI-compatible endpoints based on instance name in request body
// r.Post("/v1/completions", handler.OpenAICompletions()) // Route based on model name in request // r.Post("/v1/completions", handler.OpenAICompletions()) // Route based on model name in request
// r.Post("/v1/chat/completions", handler.OpenAIChatCompletions()) // Route based on model name in request // r.Post("/v1/chat/completions", handler.OpenAIChatCompletions()) // Route based on model name in request
// r.Post("/v1/embeddings", handler.OpenAIEmbeddings()) // Route based on model name in request (if supported) // r.Post("/v1/embeddings", handler.OpenAIEmbeddings()) // Route based on model name in request (if supported)