|
|
|
@ -6,9 +6,7 @@ import ( |
|
|
|
|
"fmt" |
|
|
|
|
"os" |
|
|
|
|
"path/filepath" |
|
|
|
|
"regexp" |
|
|
|
|
"strings" |
|
|
|
|
"sync" |
|
|
|
|
|
|
|
|
|
model "github.com/go-skynet/LocalAI/pkg/model" |
|
|
|
|
"github.com/gofiber/fiber/v2" |
|
|
|
@ -60,6 +58,10 @@ type OpenAIRequest struct { |
|
|
|
|
// Prompt is read only by completion API calls
|
|
|
|
|
Prompt string `json:"prompt" yaml:"prompt"` |
|
|
|
|
|
|
|
|
|
// Edit endpoint
|
|
|
|
|
Instruction string `json:"instruction" yaml:"instruction"` |
|
|
|
|
Input string `json:"input" yaml:"input"` |
|
|
|
|
|
|
|
|
|
Stop string `json:"stop" yaml:"stop"` |
|
|
|
|
|
|
|
|
|
// Messages is read only by chat/completion API calls
|
|
|
|
@ -143,26 +145,11 @@ func updateConfig(config *Config, input *OpenAIRequest) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var cutstrings map[string]*regexp.Regexp = make(map[string]*regexp.Regexp) |
|
|
|
|
var mu sync.Mutex = sync.Mutex{} |
|
|
|
|
|
|
|
|
|
// https://platform.openai.com/docs/api-reference/completions
|
|
|
|
|
func openAIEndpoint(cm ConfigMerger, chat, debug bool, loader *model.ModelLoader, threads, ctx int, f16 bool) func(c *fiber.Ctx) error { |
|
|
|
|
return func(c *fiber.Ctx) error { |
|
|
|
|
|
|
|
|
|
func readConfig(cm ConfigMerger, c *fiber.Ctx, loader *model.ModelLoader, debug bool, threads, ctx int, f16 bool) (*Config, *OpenAIRequest, error) { |
|
|
|
|
input := new(OpenAIRequest) |
|
|
|
|
// Get input data from the request body
|
|
|
|
|
if err := c.BodyParser(input); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if input.Stream { |
|
|
|
|
log.Debug().Msgf("Stream request received") |
|
|
|
|
//c.Response().Header.SetContentType(fiber.MIMETextHTMLCharsetUTF8)
|
|
|
|
|
c.Set("Content-Type", "text/event-stream; charset=utf-8") |
|
|
|
|
c.Set("Cache-Control", "no-cache") |
|
|
|
|
c.Set("Connection", "keep-alive") |
|
|
|
|
c.Set("Transfer-Encoding", "chunked") |
|
|
|
|
return nil, nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
modelFile := input.Model |
|
|
|
@ -182,7 +169,7 @@ func openAIEndpoint(cm ConfigMerger, chat, debug bool, loader *model.ModelLoader |
|
|
|
|
log.Debug().Msgf("No model specified, using: %s", modelFile) |
|
|
|
|
} else { |
|
|
|
|
log.Debug().Msgf("No model specified, returning error") |
|
|
|
|
return fmt.Errorf("no model specified") |
|
|
|
|
return nil, nil, fmt.Errorf("no model specified") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -196,7 +183,7 @@ func openAIEndpoint(cm ConfigMerger, chat, debug bool, loader *model.ModelLoader |
|
|
|
|
modelConfig := filepath.Join(loader.ModelPath, modelFile+".yaml") |
|
|
|
|
if _, err := os.Stat(modelConfig); err == nil { |
|
|
|
|
if err := cm.LoadConfig(modelConfig); err != nil { |
|
|
|
|
return fmt.Errorf("failed loading model config (%s) %s", modelConfig, err.Error()) |
|
|
|
|
return nil, nil, fmt.Errorf("failed loading model config (%s) %s", modelConfig, err.Error()) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -227,30 +214,23 @@ func openAIEndpoint(cm ConfigMerger, chat, debug bool, loader *model.ModelLoader |
|
|
|
|
config.Debug = true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
log.Debug().Msgf("Parameter Config: %+v", config) |
|
|
|
|
|
|
|
|
|
predInput := input.Prompt |
|
|
|
|
if chat { |
|
|
|
|
mess := []string{} |
|
|
|
|
for _, i := range input.Messages { |
|
|
|
|
r := config.Roles[i.Role] |
|
|
|
|
if r == "" { |
|
|
|
|
r = i.Role |
|
|
|
|
} |
|
|
|
|
return config, input, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
content := fmt.Sprint(r, " ", i.Content) |
|
|
|
|
mess = append(mess, content) |
|
|
|
|
// https://platform.openai.com/docs/api-reference/completions
|
|
|
|
|
func completionEndpoint(cm ConfigMerger, debug bool, loader *model.ModelLoader, threads, ctx int, f16 bool) func(c *fiber.Ctx) error { |
|
|
|
|
return func(c *fiber.Ctx) error { |
|
|
|
|
config, input, err := readConfig(cm, c, loader, debug, threads, ctx, f16) |
|
|
|
|
if err != nil { |
|
|
|
|
return fmt.Errorf("failed reading parameters from request:%w", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
predInput = strings.Join(mess, "\n") |
|
|
|
|
} |
|
|
|
|
log.Debug().Msgf("Parameter Config: %+v", config) |
|
|
|
|
|
|
|
|
|
predInput := input.Prompt |
|
|
|
|
templateFile := config.Model |
|
|
|
|
if config.TemplateConfig.Chat != "" && chat { |
|
|
|
|
templateFile = config.TemplateConfig.Chat |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if config.TemplateConfig.Completion != "" && !chat { |
|
|
|
|
if config.TemplateConfig.Completion != "" { |
|
|
|
|
templateFile = config.TemplateConfig.Completion |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -263,77 +243,96 @@ func openAIEndpoint(cm ConfigMerger, chat, debug bool, loader *model.ModelLoader |
|
|
|
|
log.Debug().Msgf("Template found, input modified to: %s", predInput) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
result := []Choice{} |
|
|
|
|
result, err := ComputeChoices(predInput, input, config, loader, func(s string, c *[]Choice) { |
|
|
|
|
*c = append(*c, Choice{Text: s}) |
|
|
|
|
}) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
n := input.N |
|
|
|
|
resp := &OpenAIResponse{ |
|
|
|
|
Model: input.Model, // we have to return what the user sent here, due to OpenAI spec.
|
|
|
|
|
Choices: result, |
|
|
|
|
Object: "text_completion", |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
jsonResult, _ := json.Marshal(resp) |
|
|
|
|
log.Debug().Msgf("Response: %s", jsonResult) |
|
|
|
|
|
|
|
|
|
if input.N == 0 { |
|
|
|
|
n = 1 |
|
|
|
|
// Return the prediction in the response body
|
|
|
|
|
return c.JSON(resp) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// get the model function to call for the result
|
|
|
|
|
predFunc, err := ModelInference(predInput, loader, *config) |
|
|
|
|
func chatEndpoint(cm ConfigMerger, debug bool, loader *model.ModelLoader, threads, ctx int, f16 bool) func(c *fiber.Ctx) error { |
|
|
|
|
return func(c *fiber.Ctx) error { |
|
|
|
|
config, input, err := readConfig(cm, c, loader, debug, threads, ctx, f16) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
return fmt.Errorf("failed reading parameters from request:%w", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
finetunePrediction := func(prediction string) string { |
|
|
|
|
if config.Echo { |
|
|
|
|
prediction = predInput + prediction |
|
|
|
|
} |
|
|
|
|
log.Debug().Msgf("Parameter Config: %+v", config) |
|
|
|
|
|
|
|
|
|
for _, c := range config.Cutstrings { |
|
|
|
|
mu.Lock() |
|
|
|
|
reg, ok := cutstrings[c] |
|
|
|
|
if !ok { |
|
|
|
|
cutstrings[c] = regexp.MustCompile(c) |
|
|
|
|
reg = cutstrings[c] |
|
|
|
|
} |
|
|
|
|
mu.Unlock() |
|
|
|
|
prediction = reg.ReplaceAllString(prediction, "") |
|
|
|
|
var predInput string |
|
|
|
|
|
|
|
|
|
mess := []string{} |
|
|
|
|
for _, i := range input.Messages { |
|
|
|
|
r := config.Roles[i.Role] |
|
|
|
|
if r == "" { |
|
|
|
|
r = i.Role |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, c := range config.TrimSpace { |
|
|
|
|
prediction = strings.TrimSpace(strings.TrimPrefix(prediction, c)) |
|
|
|
|
content := fmt.Sprint(r, " ", i.Content) |
|
|
|
|
mess = append(mess, content) |
|
|
|
|
} |
|
|
|
|
return prediction |
|
|
|
|
|
|
|
|
|
predInput = strings.Join(mess, "\n") |
|
|
|
|
|
|
|
|
|
if input.Stream { |
|
|
|
|
log.Debug().Msgf("Stream request received") |
|
|
|
|
//c.Response().Header.SetContentType(fiber.MIMETextHTMLCharsetUTF8)
|
|
|
|
|
c.Set("Content-Type", "text/event-stream; charset=utf-8") |
|
|
|
|
c.Set("Cache-Control", "no-cache") |
|
|
|
|
c.Set("Connection", "keep-alive") |
|
|
|
|
c.Set("Transfer-Encoding", "chunked") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for i := 0; i < n; i++ { |
|
|
|
|
prediction, err := predFunc() |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
templateFile := config.Model |
|
|
|
|
|
|
|
|
|
if config.TemplateConfig.Chat != "" { |
|
|
|
|
templateFile = config.TemplateConfig.Chat |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
prediction = finetunePrediction(prediction) |
|
|
|
|
// A model can have a "file.bin.tmpl" file associated with a prompt template prefix
|
|
|
|
|
templatedInput, err := loader.TemplatePrefix(templateFile, struct { |
|
|
|
|
Input string |
|
|
|
|
}{Input: predInput}) |
|
|
|
|
if err == nil { |
|
|
|
|
predInput = templatedInput |
|
|
|
|
log.Debug().Msgf("Template found, input modified to: %s", predInput) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if chat { |
|
|
|
|
result, err := ComputeChoices(predInput, input, config, loader, func(s string, c *[]Choice) { |
|
|
|
|
if input.Stream { |
|
|
|
|
result = append(result, Choice{Delta: &Message{Role: "assistant", Content: prediction}}) |
|
|
|
|
} else { |
|
|
|
|
result = append(result, Choice{Message: &Message{Role: "assistant", Content: prediction}}) |
|
|
|
|
} |
|
|
|
|
*c = append(*c, Choice{Delta: &Message{Role: "assistant", Content: s}}) |
|
|
|
|
} else { |
|
|
|
|
result = append(result, Choice{Text: prediction}) |
|
|
|
|
*c = append(*c, Choice{Message: &Message{Role: "assistant", Content: s}}) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
resp := &OpenAIResponse{ |
|
|
|
|
Model: input.Model, // we have to return what the user sent here, due to OpenAI spec.
|
|
|
|
|
Choices: result, |
|
|
|
|
} |
|
|
|
|
if input.Stream && chat { |
|
|
|
|
resp.Object = "chat.completion.chunk" |
|
|
|
|
} else if chat { |
|
|
|
|
resp.Object = "chat.completion" |
|
|
|
|
} else { |
|
|
|
|
resp.Object = "text_completion" |
|
|
|
|
Object: "chat.completion", |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if input.Stream { |
|
|
|
|
resp.Object = "chat.completion.chunk" |
|
|
|
|
jsonResult, _ := json.Marshal(resp) |
|
|
|
|
log.Debug().Msgf("Response: %s", jsonResult) |
|
|
|
|
|
|
|
|
|
if input.Stream { |
|
|
|
|
log.Debug().Msgf("Handling stream request") |
|
|
|
|
c.Context().SetBodyStreamWriter(fasthttp.StreamWriter(func(w *bufio.Writer) { |
|
|
|
|
fmt.Fprintf(w, "event: data\n") |
|
|
|
@ -347,7 +346,7 @@ func openAIEndpoint(cm ConfigMerger, chat, debug bool, loader *model.ModelLoader |
|
|
|
|
|
|
|
|
|
resp := &OpenAIResponse{ |
|
|
|
|
Model: input.Model, // we have to return what the user sent here, due to OpenAI spec.
|
|
|
|
|
Choices: []Choice{Choice{FinishReason: "stop"}}, |
|
|
|
|
Choices: []Choice{{FinishReason: "stop"}}, |
|
|
|
|
} |
|
|
|
|
respData, _ := json.Marshal(resp) |
|
|
|
|
|
|
|
|
@ -358,10 +357,57 @@ func openAIEndpoint(cm ConfigMerger, chat, debug bool, loader *model.ModelLoader |
|
|
|
|
// w.Flush()
|
|
|
|
|
})) |
|
|
|
|
return nil |
|
|
|
|
} else { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Return the prediction in the response body
|
|
|
|
|
return c.JSON(resp) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func editEndpoint(cm ConfigMerger, debug bool, loader *model.ModelLoader, threads, ctx int, f16 bool) func(c *fiber.Ctx) error { |
|
|
|
|
return func(c *fiber.Ctx) error { |
|
|
|
|
config, input, err := readConfig(cm, c, loader, debug, threads, ctx, f16) |
|
|
|
|
if err != nil { |
|
|
|
|
return fmt.Errorf("failed reading parameters from request:%w", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
log.Debug().Msgf("Parameter Config: %+v", config) |
|
|
|
|
|
|
|
|
|
predInput := input.Input |
|
|
|
|
templateFile := config.Model |
|
|
|
|
|
|
|
|
|
if config.TemplateConfig.Edit != "" { |
|
|
|
|
templateFile = config.TemplateConfig.Edit |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// A model can have a "file.bin.tmpl" file associated with a prompt template prefix
|
|
|
|
|
templatedInput, err := loader.TemplatePrefix(templateFile, struct { |
|
|
|
|
Input string |
|
|
|
|
Instruction string |
|
|
|
|
}{Input: predInput, Instruction: input.Instruction}) |
|
|
|
|
if err == nil { |
|
|
|
|
predInput = templatedInput |
|
|
|
|
log.Debug().Msgf("Template found, input modified to: %s", predInput) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
result, err := ComputeChoices(predInput, input, config, loader, func(s string, c *[]Choice) { |
|
|
|
|
*c = append(*c, Choice{Text: s}) |
|
|
|
|
}) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
resp := &OpenAIResponse{ |
|
|
|
|
Model: input.Model, // we have to return what the user sent here, due to OpenAI spec.
|
|
|
|
|
Choices: result, |
|
|
|
|
Object: "edit", |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
jsonResult, _ := json.Marshal(resp) |
|
|
|
|
log.Debug().Msgf("Response: %s", jsonResult) |
|
|
|
|
|
|
|
|
|
// Return the prediction in the response body
|
|
|
|
|
return c.JSON(resp) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|