fix: do not break on newlines on function returns (#864)

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
Ettore Di Giacinto 2023-08-04 21:46:36 +02:00 committed by GitHub
parent 4aa5dac768
commit acd829a7a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View File

@ -12,6 +12,7 @@ import (
"github.com/go-skynet/LocalAI/api/options"
"github.com/go-skynet/LocalAI/pkg/grammar"
model "github.com/go-skynet/LocalAI/pkg/model"
"github.com/go-skynet/LocalAI/pkg/utils"
"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog/log"
"github.com/valyala/fasthttp"
@ -274,6 +275,8 @@ func ChatEndpoint(cm *config.ConfigLoader, o *options.Option) func(c *fiber.Ctx)
if processFunctions {
// As we have to change the result before processing, we can't stream the answer (yet?)
ss := map[string]interface{}{}
// This prevent newlines to break JSON parsing for clients
s = utils.EscapeNewLines(s)
json.Unmarshal([]byte(s), &ss)
log.Debug().Msgf("Function return: %s %+v", s, ss)

13
pkg/utils/json.go Normal file
View File

@ -0,0 +1,13 @@
package utils
import "regexp"
var matchNewlines = regexp.MustCompile(`[\r\n]`)
const doubleQuote = `"[^"\\]*(?:\\[\s\S][^"\\]*)*"`
func EscapeNewLines(s string) string {
return regexp.MustCompile(doubleQuote).ReplaceAllStringFunc(s, func(s string) string {
return matchNewlines.ReplaceAllString(s, "\\n")
})
}