Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.
TypeScript agent-skillscodex-clideskop-appdubbingimage-generationlocalizationskillsttsvideo-editingvideo-generationvideo-transcriptionvideo-translation
fix(runtime): tolerate prose around and trailing commas in LLM JSON
yi111d1f232792bde
diff --git a/runtime/krillinai/internal/service/audio2subtitle.go b/runtime/krillinai/internal/service/audio2subtitle.goindex 9fd61909..3a462589 100644--- a/runtime/krillinai/internal/service/audio2subtitle.go+++ b/runtime/krillinai/internal/service/audio2subtitle.go@@ -1872,7 +1872,7 @@ func (s Service) splitLongSentence(item *TranslatedItem) ([]*TranslatedItem, errTranslatedPart string `json:"translated_part"`} `json:"align"`}- if err := json.Unmarshal([]byte(util.CleanMarkdownCodeBlock(response)), &splitResult); err != nil {+ if err := json.Unmarshal([]byte(util.CleanLLMJSON(response)), &splitResult); err != nil {log.GetLogger().Error("splitLongSentence parse split result error", zap.Error(err), zap.Any("response", response))return nil, fmt.Errorf("parse split result error: %w", err)}@@ -1911,7 +1911,7 @@ func (s Service) splitOriginLongSentence(sentence string) ([]string, error) {} `json:"short_sentences"`}- cleanResponse := util.CleanMarkdownCodeBlock(response)+ cleanResponse := util.CleanLLMJSON(response)if err = json.Unmarshal([]byte(cleanResponse), &splitResult); err != nil {log.GetLogger().Error("splitOriginLongSentence parse split result error", zap.Error(err), zap.Any("response", response))continuediff --git a/runtime/krillinai/internal/service/translate.go b/runtime/krillinai/internal/service/translate.goindex e6349afe..2b54ec8b 100644--- a/runtime/krillinai/internal/service/translate.go+++ b/runtime/krillinai/internal/service/translate.go@@ -139,7 +139,7 @@ func (t *Translator) splitOriginLongSentence(sentence string) ([]string, error)} `json:"short_sentences"`}- cleanResponse := util.CleanMarkdownCodeBlock(response)+ cleanResponse := util.CleanLLMJSON(response)if err = json.Unmarshal([]byte(cleanResponse), &splitResult); err != nil {log.GetLogger().Error("splitOriginLongSentence parse split result error", zap.Error(err), zap.Any("response", response))continue@@ -724,7 +724,7 @@ Required JSON format (output ONLY this structure):zap.Int("响应长度", len(response)))// 解析JSON响应- cleanResponse := util.CleanMarkdownCodeBlock(response)+ cleanResponse := util.CleanLLMJSON(response)var result struct {Translations []struct {diff --git a/runtime/krillinai/pkg/util/base.go b/runtime/krillinai/pkg/util/base.goindex 0adb56e8..3ddc76eb 100644--- a/runtime/krillinai/pkg/util/base.go+++ b/runtime/krillinai/pkg/util/base.go@@ -22,6 +22,9 @@ import (var strWithUpperLowerNum = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789")+// 围栏代码块开栏行 ``` 之后的语言标记,如 json、JSON、python3+var fenceLangPattern = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9_+.-]*\b`)+func GenerateRandStringWithUpperLowerNum(n int) string {b := make([]rune, n)for i := range b {@@ -307,8 +310,105 @@ func LoadFromDisk(filename string) (any, error) {}// 清理 Markdown 的 ```json 标记+// 若回复中存在围栏代码块,则只取第一个围栏代码块的内容,+// 避免把大模型在代码块前后的说明性文字(如"以下是分割后的结果:")一起交给 JSON 解析器func CleanMarkdownCodeBlock(response string) string {+ if block, ok := extractFencedBlock(response); ok {+ return block+ }re := regexp.MustCompile("(?m)^```(json|[a-zA-Z]*)?\n?|```$")cleaned := re.ReplaceAllString(response, "")return strings.TrimSpace(cleaned)}++// 提取回复中第一个围栏代码块的内容,回复中没有围栏代码块时返回 false+func extractFencedBlock(response string) (string, bool) {+ lines := strings.Split(response, "\n")+ open := -1+ for i, line := range lines {+ if strings.HasPrefix(strings.TrimSpace(line), "```") {+ open = i+ break+ }+ }+ if open < 0 {+ return "", false+ }++ // 开栏行 ```lang 之后的内容:只有语言标记时不算正文,其余情况视为正文首行+ body := []string{unwrapFenceInfo(strings.TrimSpace(lines[open]))}+ for _, line := range lines[open+1:] {+ if strings.HasPrefix(strings.TrimSpace(line), "```") {+ return strings.TrimSpace(strings.Join(body, "\n")), true+ }+ body = append(body, line)+ }+ // 没有闭合的围栏,取剩余全部内容+ return strings.TrimSpace(strings.Join(body, "\n")), true+}++// 取掉围栏开栏行的 ``` 前缀,语言标记返回空字符串,其余内容视为正文首行+func unwrapFenceInfo(line string) string {+ info := strings.TrimSpace(strings.TrimPrefix(line, "```"))+ if m := fenceLangPattern.FindStringSubmatch(info); m != nil {+ return strings.TrimSpace(info[len(m[0]):])+ }+ return info+}++// CleanLLMJSON 清理大模型返回的 JSON 文本:取围栏代码块的内容,并移除结尾多余逗号+func CleanLLMJSON(response string) string {+ return StripJSONTrailingCommas(CleanMarkdownCodeBlock(response))+}++// StripJSONTrailingCommas 移除 JSON 中对象或数组结尾的多余逗号,如 {"a": 1,} -> {"a": 1}+// 字符串字面量内部的逗号会被保留+func StripJSONTrailingCommas(jsonText string) string {+ if !strings.Contains(jsonText, ",") {+ return jsonText+ }+ var out strings.Builder+ out.Grow(len(jsonText))+ inString := false+ escaped := false+ for i := 0; i < len(jsonText); i++ {+ c := jsonText[i]+ if inString {+ out.WriteByte(c)+ switch {+ case escaped:+ escaped = false+ case c == '\\':+ escaped = true+ case c == '"':+ inString = false+ }+ continue+ }+ if c == '"' {+ inString = true+ out.WriteByte(c)+ continue+ }+ if c == ',' && closesAfterWhitespace(jsonText, i+1) {+ continue+ }+ out.WriteByte(c)+ }+ return out.String()+}++// 判断 from 处开始跳过空白后是否遇到 } 或 ],即当前位置的逗号是否多余+func closesAfterWhitespace(jsonText string, from int) bool {+ for i := from; i < len(jsonText); i++ {+ switch jsonText[i] {+ case ' ', '\t', '\n', '\r':+ continue+ case '}', ']':+ return true+ default:+ return false+ }+ }+ return false+}diff --git a/runtime/krillinai/pkg/util/base_test.go b/runtime/krillinai/pkg/util/base_test.gonew file mode 100644index 00000000..64c19f00--- /dev/null+++ b/runtime/krillinai/pkg/util/base_test.go@@ -0,0 +1,177 @@+package util++import (+ "encoding/json"+ "testing"+)++func TestCleanMarkdownCodeBlock(t *testing.T) {+ cases := []struct {+ name string+ response string+ want string+ }{+ {+ name: "bare json",+ response: `{"align": []}`,+ want: `{"align": []}`,+ },+ {+ name: "fenced json without language tag",+ response: "```\n{\"align\": []}\n```",+ want: `{"align": []}`,+ },+ {+ name: "fenced json with language tag",+ response: "```json\n{\"align\": []}\n```",+ want: `{"align": []}`,+ },+ {+ name: "prose before the fence",+ response: "以下是分割后的结果:\n\n```json\n{\"align\": []}\n```",+ want: `{"align": []}`,+ },+ {+ name: "prose before and after the fence",+ response: "Sure! Here you go:\n\n```json\n{\"align\": []}\n```\n\nLet me know if you need more detail.",+ want: `{"align": []}`,+ },+ {+ name: "only the first fenced block is used",+ response: "```json\n{\"align\": []}\n```\n\nAnd a second block:\n\n```json\n{\"align\": [1]}\n```",+ want: `{"align": []}`,+ },+ {+ name: "fence without closing marker",+ response: "```json\n{\"align\": []}",+ want: `{"align": []}`,+ },+ {+ name: "json opened on the fence line",+ response: "```json {\"align\": []}",+ want: `{"align": []}`,+ },+ {+ name: "language tag is not part of the body",+ response: "```JSON\n[1]\n```",+ want: `[1]`,+ },+ {+ name: "windows line endings",+ response: "```json\r\n{\"align\": []}\r\n```",+ want: `{"align": []}`,+ },+ {+ name: "no fence at all",+ response: `{"align": []}`,+ want: `{"align": []}`,+ },+ {+ name: "empty response",+ response: "",+ want: "",+ },+ }++ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ if got := CleanMarkdownCodeBlock(tc.response); got != tc.want {+ t.Errorf("CleanMarkdownCodeBlock() = %q, want %q", got, tc.want)+ }+ })+ }+}++func TestStripJSONTrailingCommas(t *testing.T) {+ cases := []struct {+ name string+ input string+ want string+ }{+ {+ name: "trailing comma in an object",+ input: `{"a": 1,}`,+ want: `{"a": 1}`,+ },+ {+ name: "trailing comma in an array",+ input: `{"a": [1, 2, 3,]}`,+ want: `{"a": [1, 2, 3]}`,+ },+ {+ name: "trailing comma in nested values",+ input: "{\n \"align\": [\n {\"origin_part\": \"a\",},\n ],\n}",+ want: "{\n \"align\": [\n {\"origin_part\": \"a\"}\n ]\n}",+ },+ {+ name: "only the comma is dropped, the newline after it is kept",+ input: "{\"a\": 1,\n}",+ want: "{\"a\": 1\n}",+ },+ {+ name: "comma inside a string literal is kept",+ input: `{"text": "wait, then go"}`,+ want: `{"text": "wait, then go"}`,+ },+ {+ name: "trailing comma inside a string literal is kept",+ input: `{"text": "a, }"}`,+ want: `{"text": "a, }"}`,+ },+ {+ name: "escaped quote before a comma",+ input: `{"text": "say \"hi\", ok"}`,+ want: `{"text": "say \"hi\", ok"}`,+ },+ {+ name: "escaped backslash before a quote",+ input: `{"text": "back\\", "n": 1}`,+ want: `{"text": "back\\", "n": 1}`,+ },+ {+ name: "valid json is untouched",+ input: `{"a": [1, 2], "b": {"c": 3}}`,+ want: `{"a": [1, 2], "b": {"c": 3}}`,+ },+ {+ name: "no comma at all",+ input: `[1]`,+ want: `[1]`,+ },+ {+ name: "truncated input keeps the trailing comma",+ input: `{"a": 1,`,+ want: `{"a": 1,`,+ },+ }++ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ if got := StripJSONTrailingCommas(tc.input); got != tc.want {+ t.Errorf("StripJSONTrailingCommas() = %q, want %q", got, tc.want)+ }+ })+ }+}++// 大模型经常在 ```json 代码块前后附加说明文字,并偶尔输出结尾多余逗号,+// 两者都会让 json.Unmarshal 失败,见 issue #291+func TestCleanLLMJSON(t *testing.T) {+ response := "以下是分割后的结果:\n\n```json\n{\n \"align\": [\n {\"origin_part\": \"a\", \"translated_part\": \"A\",},\n ],\n}\n```\n\n希望有帮助。"++ var splitResult struct {+ Align []struct {+ OriginPart string `json:"origin_part"`+ TranslatedPart string `json:"translated_part"`+ } `json:"align"`+ }+ if err := json.Unmarshal([]byte(CleanLLMJSON(response)), &splitResult); err != nil {+ t.Fatalf("CleanLLMJSON() produced unparsable JSON %q: %v", CleanLLMJSON(response), err)+ }+ if len(splitResult.Align) != 1 {+ t.Fatalf("expected 1 aligned part, got %d", len(splitResult.Align))+ }+ if splitResult.Align[0].OriginPart != "a" || splitResult.Align[0].TranslatedPart != "A" {+ t.Errorf("unexpected aligned part: %+v", splitResult.Align[0])+ }+}