mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-25 07:00:48 +09:00
feat: models import (#164)
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -36,6 +38,7 @@ func (h *ProvidersHandler) Register(e *echo.Echo) {
|
||||
group.DELETE("/:id", h.Delete)
|
||||
group.GET("/count", h.Count)
|
||||
group.POST("/:id/test", h.Test)
|
||||
group.POST("/:id/import-models", h.ImportModels)
|
||||
}
|
||||
|
||||
// Create godoc
|
||||
@@ -282,3 +285,68 @@ func (h *ProvidersHandler) Test(c echo.Context) error {
|
||||
|
||||
return c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
// ImportModels godoc
|
||||
// @Summary Import models from provider
|
||||
// @Description Fetch models from provider's /v1/models endpoint and import them
|
||||
// @Tags providers
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "Provider ID (UUID)"
|
||||
// @Param request body providers.ImportModelsRequest true "Import configuration"
|
||||
// @Success 200 {object} providers.ImportModelsResponse
|
||||
// @Failure 400 {object} ErrorResponse
|
||||
// @Failure 404 {object} ErrorResponse
|
||||
// @Failure 500 {object} ErrorResponse
|
||||
// @Router /providers/{id}/import-models [post]
|
||||
func (h *ProvidersHandler) ImportModels(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "id is required")
|
||||
}
|
||||
|
||||
var req providers.ImportModelsRequest
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
if req.ClientType == "" {
|
||||
req.ClientType = string(models.ClientTypeOpenAICompletions)
|
||||
}
|
||||
|
||||
remoteModels, err := h.service.FetchRemoteModels(c.Request().Context(), id)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("fetch remote models: %v", err))
|
||||
}
|
||||
|
||||
resp := providers.ImportModelsResponse{
|
||||
Models: make([]string, 0),
|
||||
}
|
||||
|
||||
for _, m := range remoteModels {
|
||||
// Try to create the model
|
||||
_, err := h.modelsService.Create(c.Request().Context(), models.AddRequest{
|
||||
ModelID: m.ID,
|
||||
Name: m.ID,
|
||||
LlmProviderID: id,
|
||||
ClientType: models.ClientType(req.ClientType),
|
||||
Type: models.ModelTypeChat,
|
||||
InputModalities: []string{models.ModelInputText},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, models.ErrModelIDAlreadyExists) {
|
||||
resp.Skipped++
|
||||
continue
|
||||
}
|
||||
// Log error but continue with other models
|
||||
h.logger.Warn("failed to import model", slog.String("model_id", m.ID), slog.Any("error", err))
|
||||
continue
|
||||
}
|
||||
|
||||
resp.Created++
|
||||
resp.Models = append(resp.Models, m.ID)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user