2024-01-05 17:04:46 +00:00
|
|
|
package openai
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"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-02-21 01:21:19 +00:00
|
|
|
"github.com/go-skynet/LocalAI/core/schema"
|
2024-01-05 17:04:46 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
2024-03-29 21:29:33 +00:00
|
|
|
// CompletionEndpoint is the OpenAI Completion API endpoint https://platform.openai.com/docs/api-reference/completions
|
|
|
|
// @Summary Generate completions for a given prompt and model.
|
|
|
|
// @Param request body schema.OpenAIRequest true "query params"
|
|
|
|
// @Success 200 {object} schema.OpenAIResponse "Response"
|
|
|
|
// @Router /v1/completions [post]
|
2024-04-13 07:45:34 +00:00
|
|
|
func CompletionEndpoint(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
|
|
|
log.Debug().Msgf("`OpenAIRequest`: %+v", request)
|
2024-01-05 17:04:46 +00:00
|
|
|
|
2024-04-13 07:45:34 +00:00
|
|
|
traceID, finalResultChannel, _, _, tokenChannel, err := oais.Completion(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
|
|
|
if request.Stream {
|
|
|
|
log.Debug().Msgf("Completion Stream request received")
|
2024-01-05 17:04:46 +00:00
|
|
|
|
|
|
|
c.Context().SetContentType("text/event-stream")
|
|
|
|
//c.Response().Header.SetContentType(fiber.MIMETextHTMLCharsetUTF8)
|
|
|
|
//c.Set("Content-Type", "text/event-stream")
|
|
|
|
c.Set("Cache-Control", "no-cache")
|
|
|
|
c.Set("Connection", "keep-alive")
|
|
|
|
c.Set("Transfer-Encoding", "chunked")
|
|
|
|
|
|
|
|
c.Context().SetBodyStreamWriter(fasthttp.StreamWriter(func(w *bufio.Writer) {
|
2024-04-13 07:45:34 +00:00
|
|
|
for ev := range tokenChannel {
|
2024-01-05 17:04:46 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
enc := json.NewEncoder(&buf)
|
2024-04-13 07:45:34 +00:00
|
|
|
if ev.Error != nil {
|
|
|
|
log.Debug().Msgf("[CompletionEndpoint] error to debug during tokenChannel handler: %q", ev.Error)
|
|
|
|
enc.Encode(ev.Error)
|
|
|
|
} else {
|
|
|
|
enc.Encode(ev.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().Msgf("completion streaming sending chunk: %s", buf.String())
|
2024-01-05 17:04:46 +00:00
|
|
|
fmt.Fprintf(w, "data: %v\n", buf.String())
|
|
|
|
w.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &schema.OpenAIResponse{
|
2024-04-13 07:45:34 +00:00
|
|
|
ID: traceID.ID,
|
|
|
|
Created: traceID.Created,
|
|
|
|
Model: request.Model, // we have to return what the user sent here, due to OpenAI spec.
|
2024-01-05 17:04:46 +00:00
|
|
|
Choices: []schema.Choice{
|
|
|
|
{
|
|
|
|
Index: 0,
|
|
|
|
FinishReason: "stop",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Object: "text_completion",
|
|
|
|
}
|
|
|
|
respData, _ := json.Marshal(resp)
|
|
|
|
|
|
|
|
w.WriteString(fmt.Sprintf("data: %s\n\n", respData))
|
|
|
|
w.WriteString("data: [DONE]\n\n")
|
|
|
|
w.Flush()
|
|
|
|
}))
|
|
|
|
return nil
|
|
|
|
}
|
2024-04-13 07:45:34 +00:00
|
|
|
// TODO is this proper to have exclusive from Stream, or do we need to issue both responses?
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|