2023-07-14 23:19:43 +00:00
|
|
|
package openai
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2023-08-18 19:23:14 +00:00
|
|
|
"github.com/go-skynet/LocalAI/api/backend"
|
2023-07-14 23:19:43 +00:00
|
|
|
config "github.com/go-skynet/LocalAI/api/config"
|
|
|
|
"github.com/go-skynet/LocalAI/api/options"
|
2023-07-22 15:31:39 +00:00
|
|
|
model "github.com/go-skynet/LocalAI/pkg/model"
|
2023-07-14 23:19:43 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func EditEndpoint(cm *config.ConfigLoader, o *options.Option) func(c *fiber.Ctx) error {
|
|
|
|
return func(c *fiber.Ctx) error {
|
2023-07-24 21:10:54 +00:00
|
|
|
modelFile, input, err := readInput(c, o, true)
|
2023-07-14 23:19:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed reading parameters from request:%w", err)
|
|
|
|
}
|
|
|
|
|
2023-07-22 15:31:39 +00:00
|
|
|
config, input, err := readConfig(modelFile, input, cm, o.Loader, o.Debug, o.Threads, o.ContextSize, o.F16)
|
2023-07-14 23:19:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed reading parameters from request:%w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().Msgf("Parameter Config: %+v", config)
|
|
|
|
|
|
|
|
templateFile := config.Model
|
|
|
|
|
|
|
|
if config.TemplateConfig.Edit != "" {
|
|
|
|
templateFile = config.TemplateConfig.Edit
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []Choice
|
2023-08-18 19:23:14 +00:00
|
|
|
totalTokenUsage := backend.TokenUsage{}
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
for _, i := range config.InputStrings {
|
|
|
|
// A model can have a "file.bin.tmpl" file associated with a prompt template prefix
|
2023-07-22 15:31:39 +00:00
|
|
|
templatedInput, err := o.Loader.EvaluateTemplateForPrompt(model.EditPromptTemplate, templateFile, model.PromptTemplateData{
|
2023-08-02 22:19:55 +00:00
|
|
|
Input: i,
|
|
|
|
Instruction: input.Instruction,
|
|
|
|
SystemPrompt: config.SystemPrompt,
|
2023-07-22 15:31:39 +00:00
|
|
|
})
|
2023-07-14 23:19:43 +00:00
|
|
|
if err == nil {
|
|
|
|
i = templatedInput
|
|
|
|
log.Debug().Msgf("Template found, input modified to: %s", i)
|
|
|
|
}
|
|
|
|
|
2023-08-18 19:23:14 +00:00
|
|
|
r, tokenUsage, err := ComputeChoices(input, i, config, o, o.Loader, func(s string, c *[]Choice) {
|
2023-07-14 23:19:43 +00:00
|
|
|
*c = append(*c, Choice{Text: s})
|
|
|
|
}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-18 19:23:14 +00:00
|
|
|
totalTokenUsage.Prompt += tokenUsage.Prompt
|
|
|
|
totalTokenUsage.Completion += tokenUsage.Completion
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
result = append(result, r...)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &OpenAIResponse{
|
|
|
|
Model: input.Model, // we have to return what the user sent here, due to OpenAI spec.
|
|
|
|
Choices: result,
|
|
|
|
Object: "edit",
|
2023-08-18 19:23:14 +00:00
|
|
|
Usage: OpenAIUsage{
|
|
|
|
PromptTokens: totalTokenUsage.Prompt,
|
|
|
|
CompletionTokens: totalTokenUsage.Completion,
|
|
|
|
TotalTokens: totalTokenUsage.Prompt + totalTokenUsage.Completion,
|
|
|
|
},
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jsonResult, _ := json.Marshal(resp)
|
|
|
|
log.Debug().Msgf("Response: %s", jsonResult)
|
|
|
|
|
|
|
|
// Return the prediction in the response body
|
|
|
|
return c.JSON(resp)
|
|
|
|
}
|
|
|
|
}
|