2024-04-24 22:19:02 +00:00
|
|
|
package jina
|
|
|
|
|
|
|
|
import (
|
2024-06-23 08:24:36 +00:00
|
|
|
"github.com/mudler/LocalAI/core/backend"
|
|
|
|
"github.com/mudler/LocalAI/core/config"
|
2024-04-24 22:19:02 +00:00
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-06-23 08:24:36 +00:00
|
|
|
fiberContext "github.com/mudler/LocalAI/core/http/ctx"
|
|
|
|
"github.com/mudler/LocalAI/core/schema"
|
|
|
|
"github.com/mudler/LocalAI/pkg/grpc/proto"
|
|
|
|
"github.com/mudler/LocalAI/pkg/model"
|
2024-04-24 22:19:02 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
2024-07-14 03:46:42 +00:00
|
|
|
// JINARerankEndpoint acts like the Jina reranker endpoint (https://jina.ai/reranker/)
|
|
|
|
// @Summary Reranks a list of phrases by relevance to a given text query.
|
|
|
|
// @Param request body schema.JINARerankRequest true "query params"
|
|
|
|
// @Success 200 {object} schema.JINARerankResponse "Response"
|
|
|
|
// @Router /v1/rerank [post]
|
2024-04-24 22:19:02 +00:00
|
|
|
func JINARerankEndpoint(cl *config.BackendConfigLoader, ml *model.ModelLoader, appConfig *config.ApplicationConfig) func(c *fiber.Ctx) error {
|
|
|
|
return func(c *fiber.Ctx) error {
|
|
|
|
req := new(schema.JINARerankRequest)
|
|
|
|
if err := c.BodyParser(req); err != nil {
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
|
|
"error": "Cannot parse JSON",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
input := new(schema.TTSRequest)
|
|
|
|
|
|
|
|
// Get input data from the request body
|
|
|
|
if err := c.BodyParser(input); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-10 13:28:39 +00:00
|
|
|
modelFile, err := fiberContext.ModelFromContext(c, cl, ml, input.Model, false)
|
2024-04-24 22:19:02 +00:00
|
|
|
if err != nil {
|
|
|
|
modelFile = input.Model
|
|
|
|
log.Warn().Msgf("Model not found in context: %s", input.Model)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg, err := cl.LoadBackendConfigFileByName(modelFile, appConfig.ModelPath,
|
|
|
|
config.LoadOptionDebug(appConfig.Debug),
|
|
|
|
config.LoadOptionThreads(appConfig.Threads),
|
|
|
|
config.LoadOptionContextSize(appConfig.ContextSize),
|
|
|
|
config.LoadOptionF16(appConfig.F16),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
modelFile = input.Model
|
|
|
|
log.Warn().Msgf("Model not found in context: %s", input.Model)
|
|
|
|
} else {
|
|
|
|
modelFile = cfg.Model
|
|
|
|
}
|
2024-10-02 06:55:58 +00:00
|
|
|
|
2024-04-24 22:19:02 +00:00
|
|
|
log.Debug().Msgf("Request for model: %s", modelFile)
|
|
|
|
|
|
|
|
if input.Backend != "" {
|
|
|
|
cfg.Backend = input.Backend
|
|
|
|
}
|
|
|
|
|
|
|
|
request := &proto.RerankRequest{
|
|
|
|
Query: req.Query,
|
|
|
|
TopN: int32(req.TopN),
|
|
|
|
Documents: req.Documents,
|
|
|
|
}
|
|
|
|
|
2024-10-02 06:55:58 +00:00
|
|
|
results, err := backend.Rerank(modelFile, request, ml, appConfig, *cfg)
|
2024-04-24 22:19:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
response := &schema.JINARerankResponse{
|
|
|
|
Model: req.Model,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range results.Results {
|
|
|
|
response.Results = append(response.Results, schema.JINADocumentResult{
|
|
|
|
Index: int(r.Index),
|
|
|
|
Document: schema.JINAText{Text: r.Text},
|
|
|
|
RelevanceScore: float64(r.RelevanceScore),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Usage.TotalTokens = int(results.Usage.TotalTokens)
|
|
|
|
response.Usage.PromptTokens = int(results.Usage.PromptTokens)
|
|
|
|
|
|
|
|
return c.Status(fiber.StatusOK).JSON(response)
|
|
|
|
}
|
|
|
|
}
|