| | package openai |
| |
|
| | import ( |
| | "encoding/json" |
| | "time" |
| |
|
| | "github.com/labstack/echo/v4" |
| | "github.com/mudler/LocalAI/core/backend" |
| | "github.com/mudler/LocalAI/core/config" |
| | "github.com/mudler/LocalAI/core/http/middleware" |
| | "github.com/mudler/LocalAI/pkg/model" |
| |
|
| | "github.com/google/uuid" |
| | "github.com/mudler/LocalAI/core/schema" |
| |
|
| | "github.com/mudler/xlog" |
| | ) |
| |
|
| | |
| | |
| | |
| | |
| | |
| | func EmbeddingsEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, appConfig *config.ApplicationConfig) echo.HandlerFunc { |
| | return func(c echo.Context) error { |
| | input, ok := c.Get(middleware.CONTEXT_LOCALS_KEY_LOCALAI_REQUEST).(*schema.OpenAIRequest) |
| | if !ok || input.Model == "" { |
| | return echo.ErrBadRequest |
| | } |
| |
|
| | config, ok := c.Get(middleware.CONTEXT_LOCALS_KEY_MODEL_CONFIG).(*config.ModelConfig) |
| | if !ok || config == nil { |
| | return echo.ErrBadRequest |
| | } |
| |
|
| | xlog.Debug("Parameter Config", "config", config) |
| | items := []schema.Item{} |
| |
|
| | for i, s := range config.InputToken { |
| | |
| | embedFn, err := backend.ModelEmbedding("", s, ml, *config, appConfig) |
| | if err != nil { |
| | return err |
| | } |
| |
|
| | embeddings, err := embedFn() |
| | if err != nil { |
| | return err |
| | } |
| | items = append(items, schema.Item{Embedding: embeddings, Index: i, Object: "embedding"}) |
| | } |
| |
|
| | for i, s := range config.InputStrings { |
| | |
| | embedFn, err := backend.ModelEmbedding(s, []int{}, ml, *config, appConfig) |
| | if err != nil { |
| | return err |
| | } |
| |
|
| | embeddings, err := embedFn() |
| | if err != nil { |
| | return err |
| | } |
| | items = append(items, schema.Item{Embedding: embeddings, Index: i, Object: "embedding"}) |
| | } |
| |
|
| | id := uuid.New().String() |
| | created := int(time.Now().Unix()) |
| | resp := &schema.OpenAIResponse{ |
| | ID: id, |
| | Created: created, |
| | Model: input.Model, |
| | Data: items, |
| | Object: "list", |
| | } |
| |
|
| | jsonResult, _ := json.Marshal(resp) |
| | xlog.Debug("Response", "response", string(jsonResult)) |
| |
|
| | |
| | return c.JSON(200, resp) |
| | } |
| | } |
| |
|