2024-01-05 17:04:46 +00:00
|
|
|
package openai
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2024-02-21 01:21:19 +00:00
|
|
|
"github.com/go-skynet/LocalAI/core/backend"
|
2024-04-13 07:45:34 +00:00
|
|
|
fiberContext "github.com/go-skynet/LocalAI/core/http/ctx"
|
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-13 07:45:34 +00:00
|
|
|
func EmbeddingsEndpoint(fce *fiberContext.FiberContextExtractor, ebs *backend.EmbeddingsBackendService) func(c *fiber.Ctx) error {
|
2024-01-05 17:04:46 +00:00
|
|
|
return func(c *fiber.Ctx) error {
|
2024-04-13 07:45:34 +00:00
|
|
|
_, input, err := fce.OpenAIRequestFromContext(c, true)
|
2024-01-05 17:04:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed reading parameters from request:%w", err)
|
|
|
|
}
|
|
|
|
|
2024-04-13 07:45:34 +00:00
|
|
|
responseChannel := ebs.Embeddings(input)
|
2024-01-05 17:04:46 +00:00
|
|
|
|
2024-04-13 07:45:34 +00:00
|
|
|
rawResponse := <-responseChannel
|
2024-01-05 17:04:46 +00:00
|
|
|
|
2024-04-13 07:45:34 +00:00
|
|
|
if rawResponse.Error != nil {
|
|
|
|
return rawResponse.Error
|
2024-01-05 17:04:46 +00:00
|
|
|
}
|
|
|
|
|
2024-04-13 07:45:34 +00:00
|
|
|
jsonResult, _ := json.Marshal(rawResponse.Value)
|
2024-01-05 17:04:46 +00:00
|
|
|
log.Debug().Msgf("Response: %s", jsonResult)
|
|
|
|
|
|
|
|
// Return the prediction in the response body
|
2024-04-13 07:45:34 +00:00
|
|
|
return c.JSON(rawResponse.Value)
|
2024-01-05 17:04:46 +00:00
|
|
|
}
|
|
|
|
}
|