2024-01-05 17:04:46 +00:00
|
|
|
package openai
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-04-17 21:33:49 +00:00
|
|
|
"time"
|
2024-01-05 17:04:46 +00:00
|
|
|
|
2024-02-21 01:21:19 +00:00
|
|
|
"github.com/go-skynet/LocalAI/core/backend"
|
2024-04-17 21:33:49 +00:00
|
|
|
"github.com/go-skynet/LocalAI/core/config"
|
|
|
|
"github.com/go-skynet/LocalAI/pkg/model"
|
|
|
|
|
|
|
|
"github.com/go-skynet/LocalAI/core/schema"
|
|
|
|
"github.com/google/uuid"
|
2024-01-05 17:04:46 +00:00
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
2024-03-29 21:29:33 +00:00
|
|
|
// EmbeddingsEndpoint is the OpenAI Embeddings API endpoint https://platform.openai.com/docs/api-reference/embeddings
|
|
|
|
// @Summary Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.
|
|
|
|
// @Param request body schema.OpenAIRequest true "query params"
|
|
|
|
// @Success 200 {object} schema.OpenAIResponse "Response"
|
|
|
|
// @Router /v1/embeddings [post]
|
2024-04-17 21:33:49 +00:00
|
|
|
func EmbeddingsEndpoint(cl *config.BackendConfigLoader, ml *model.ModelLoader, appConfig *config.ApplicationConfig) func(c *fiber.Ctx) error {
|
2024-01-05 17:04:46 +00:00
|
|
|
return func(c *fiber.Ctx) error {
|
2024-04-17 21:33:49 +00:00
|
|
|
model, input, err := readRequest(c, ml, appConfig, true)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed reading parameters from request:%w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config, input, err := mergeRequestWithConfig(model, input, cl, ml, appConfig.Debug, appConfig.Threads, appConfig.ContextSize, appConfig.F16)
|
2024-01-05 17:04:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed reading parameters from request:%w", err)
|
|
|
|
}
|
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
log.Debug().Msgf("Parameter Config: %+v", config)
|
|
|
|
items := []schema.Item{}
|
2024-01-05 17:04:46 +00:00
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
for i, s := range config.InputToken {
|
|
|
|
// get the model function to call for the result
|
|
|
|
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 {
|
|
|
|
// get the model function to call for the result
|
|
|
|
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"})
|
|
|
|
}
|
2024-01-05 17:04:46 +00:00
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
id := uuid.New().String()
|
|
|
|
created := int(time.Now().Unix())
|
|
|
|
resp := &schema.OpenAIResponse{
|
|
|
|
ID: id,
|
|
|
|
Created: created,
|
|
|
|
Model: input.Model, // we have to return what the user sent here, due to OpenAI spec.
|
|
|
|
Data: items,
|
|
|
|
Object: "list",
|
2024-01-05 17:04:46 +00:00
|
|
|
}
|
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
jsonResult, _ := json.Marshal(resp)
|
2024-01-05 17:04:46 +00:00
|
|
|
log.Debug().Msgf("Response: %s", jsonResult)
|
|
|
|
|
|
|
|
// Return the prediction in the response body
|
2024-04-17 21:33:49 +00:00
|
|
|
return c.JSON(resp)
|
2024-01-05 17:04:46 +00:00
|
|
|
}
|
|
|
|
}
|