Expand file tree Collapse file tree 2 files changed +16
-0
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ import (
12
12
"github.com/go-skynet/LocalAI/api/options"
13
13
"github.com/go-skynet/LocalAI/pkg/grammar"
14
14
model "github.com/go-skynet/LocalAI/pkg/model"
15
+ "github.com/go-skynet/LocalAI/pkg/utils"
15
16
"github.com/gofiber/fiber/v2"
16
17
"github.com/rs/zerolog/log"
17
18
"github.com/valyala/fasthttp"
@@ -274,6 +275,8 @@ func ChatEndpoint(cm *config.ConfigLoader, o *options.Option) func(c *fiber.Ctx)
274
275
if processFunctions {
275
276
// As we have to change the result before processing, we can't stream the answer (yet?)
276
277
ss := map [string ]interface {}{}
278
+ // This prevent newlines to break JSON parsing for clients
279
+ s = utils .EscapeNewLines (s )
277
280
json .Unmarshal ([]byte (s ), & ss )
278
281
log .Debug ().Msgf ("Function return: %s %+v" , s , ss )
279
282
Original file line number Diff line number Diff line change
1
+ package utils
2
+
3
+ import "regexp"
4
+
5
+ var matchNewlines = regexp .MustCompile (`[\r\n]` )
6
+
7
+ const doubleQuote = `"[^"\\]*(?:\\[\s\S][^"\\]*)*"`
8
+
9
+ func EscapeNewLines (s string ) string {
10
+ return regexp .MustCompile (doubleQuote ).ReplaceAllStringFunc (s , func (s string ) string {
11
+ return matchNewlines .ReplaceAllString (s , "\\ n" )
12
+ })
13
+ }
0 commit comments