mirror of
https://github.com/mudler/LocalAI.git
synced 2025-04-26 05:50:01 +00:00
* squash past, centralize request middleware PR Signed-off-by: Dave Lee <dave@gray101.com> * migrate bruno request files to examples repo Signed-off-by: Dave Lee <dave@gray101.com> * fix Signed-off-by: Dave Lee <dave@gray101.com> * Update tests/e2e-aio/e2e_test.go Signed-off-by: Ettore Di Giacinto <mudler@users.noreply.github.com> --------- Signed-off-by: Dave Lee <dave@gray101.com> Signed-off-by: Ettore Di Giacinto <mudler@users.noreply.github.com> Co-authored-by: Ettore Di Giacinto <mudler@users.noreply.github.com>
64 lines
1.9 KiB
Go
64 lines
1.9 KiB
Go
package jina
|
|
|
|
import (
|
|
"github.com/mudler/LocalAI/core/backend"
|
|
"github.com/mudler/LocalAI/core/config"
|
|
"github.com/mudler/LocalAI/core/http/middleware"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/mudler/LocalAI/core/schema"
|
|
"github.com/mudler/LocalAI/pkg/grpc/proto"
|
|
"github.com/mudler/LocalAI/pkg/model"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
// 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]
|
|
func JINARerankEndpoint(cl *config.BackendConfigLoader, ml *model.ModelLoader, appConfig *config.ApplicationConfig) func(c *fiber.Ctx) error {
|
|
return func(c *fiber.Ctx) error {
|
|
|
|
input, ok := c.Locals(middleware.CONTEXT_LOCALS_KEY_LOCALAI_REQUEST).(*schema.JINARerankRequest)
|
|
if !ok || input.Model == "" {
|
|
return fiber.ErrBadRequest
|
|
}
|
|
|
|
cfg, ok := c.Locals(middleware.CONTEXT_LOCALS_KEY_MODEL_CONFIG).(*config.BackendConfig)
|
|
if !ok || cfg == nil {
|
|
return fiber.ErrBadRequest
|
|
}
|
|
|
|
log.Debug().Str("model", input.Model).Msg("JINA Rerank Request recieved")
|
|
|
|
request := &proto.RerankRequest{
|
|
Query: input.Query,
|
|
TopN: int32(input.TopN),
|
|
Documents: input.Documents,
|
|
}
|
|
|
|
results, err := backend.Rerank(request, ml, appConfig, *cfg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
response := &schema.JINARerankResponse{
|
|
Model: input.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)
|
|
}
|
|
}
|