2024-01-05 17:04:46 +00:00
|
|
|
package openai
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2024-04-13 07:45:34 +00:00
|
|
|
fiberContext "github.com/go-skynet/LocalAI/core/http/ctx"
|
|
|
|
"github.com/go-skynet/LocalAI/core/services"
|
2024-03-01 15:19:53 +00:00
|
|
|
|
2024-01-05 17:04:46 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
2024-04-13 07:45:34 +00:00
|
|
|
func EditEndpoint(fce *fiberContext.FiberContextExtractor, oais *services.OpenAIService) 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
|
|
|
_, request, err := fce.OpenAIRequestFromContext(c, false)
|
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
|
|
|
_, finalResultChannel, _, _, _, err := oais.Edit(request, false, request.Stream)
|
2024-01-05 17:04:46 +00:00
|
|
|
if err != nil {
|
2024-04-13 07:45:34 +00:00
|
|
|
return err
|
2024-01-05 17:04:46 +00:00
|
|
|
}
|
|
|
|
|
2024-04-13 07:45:34 +00:00
|
|
|
rawResponse := <-finalResultChannel
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|