mirror of
https://github.com/mudler/LocalAI.git
synced 2024-06-07 19:40:48 +00:00
fix: do not break on newlines on function returns (#864)
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
parent
4aa5dac768
commit
acd829a7a0
@ -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
13
pkg/utils/json.go
Normal 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")
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user