Skip to content

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.

fix(subtitle): tolerate LLM JSON with conversational text and trailing commas

askalf22459f4c4d8e
diff --git a/runtime/krillinai/internal/service/audio2subtitle.go b/runtime/krillinai/internal/service/audio2subtitle.go
index 9fd61909..8f9a9f15 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, err
TranslatedPart string `json:"translated_part"`
} `json:"align"`
}
- if err := json.Unmarshal([]byte(util.CleanMarkdownCodeBlock(response)), &splitResult); err != nil {
+ if err := json.Unmarshal([]byte(util.ExtractJSON(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.ExtractJSON(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
diff --git a/runtime/krillinai/internal/service/audio2subtitle_test.go b/runtime/krillinai/internal/service/audio2subtitle_test.go
index 230d9b42..dd48a93d 100644
--- a/runtime/krillinai/internal/service/audio2subtitle_test.go
+++ b/runtime/krillinai/internal/service/audio2subtitle_test.go
@@ -435,3 +435,69 @@ func TestSplitTranslateItemReportsLongSentenceProgress(t *testing.T) {
t.Fatalf("progress = %s", got)
}
}
+
+// TestSplitOriginLongSentenceToleratesLLMNoise 覆盖 issue #291 的两类真实响应:
+// 结束符前的尾随逗号,以及 JSON 之前的中文对话式说明。
+// 两者在修复前都会让 json.Unmarshal 失败,三次重试耗尽后返回空结果。
+func TestSplitOriginLongSentenceToleratesLLMNoise(t *testing.T) {
+ log.InitLogger()
+ tests := []struct {
+ name string
+ response string
+ }{
+ {
+ name: "尾随逗号",
+ response: "{\n\"short_sentences\":[{\n\"text\": \"And the reason why they chose the owl is because\",\n},\n{\n\"text\": \"the owl is a symbol used in Europe\",\n}] \n}",
+ },
+ {
+ name: "中文对话式前缀",
+ response: "以下是分割后的结果:\n\n\n{\n \"short_sentences\": [\n { \"text\": \"And the reason why they chose the owl is because\" },\n { \"text\": \"the owl is a symbol used in Europe\" }\n ]\n}\n",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ completer := &scriptedCompleter{responses: []string{tt.response}}
+ service := Service{ChatCompleter: completer}
+
+ sentences, err := service.splitOriginLongSentence("And the reason why they chose the owl is because the owl is a symbol used in Europe")
+ if err != nil {
+ t.Fatalf("splitOriginLongSentence() error = %v, want nil", err)
+ }
+ if completer.calls != 1 {
+ t.Fatalf("ChatCompletion 调用次数 = %d, want 1(无需重试)", completer.calls)
+ }
+ want := []string{
+ "And the reason why they chose the owl is because",
+ "the owl is a symbol used in Europe",
+ }
+ if fmt.Sprint(sentences) != fmt.Sprint(want) {
+ t.Fatalf("sentences = %v, want %v", sentences, want)
+ }
+ })
+ }
+}
+
+// TestSplitLongSentenceToleratesConversationalPrefix 覆盖 issue #291 日志中
+// splitLongSentence 的失败响应:LLM 在对齐结果前输出中文说明。
+func TestSplitLongSentenceToleratesConversationalPrefix(t *testing.T) {
+ log.InitLogger()
+ completer := &scriptedCompleter{responses: []string{
+ "以下是分割后的结果:\n\n{\n \"align\": [\n { \"origin_part\": \"I want to show you\", \"translated_part\": \"Ich möchte es Ihnen zeigen\" },\n { \"origin_part\": \"how it works\", \"translated_part\": \"wie es funktioniert\" }\n ]\n}",
+ }}
+ service := Service{ChatCompleter: completer}
+
+ items, err := service.splitLongSentence(&TranslatedItem{
+ OriginText: "I want to show you how it works",
+ TranslatedText: "Ich möchte es Ihnen zeigen wie es funktioniert",
+ })
+ if err != nil {
+ t.Fatalf("splitLongSentence() error = %v, want nil", err)
+ }
+ if len(items) != 2 {
+ t.Fatalf("items = %+v, want 2 items", items)
+ }
+ if items[0].OriginText != "I want to show you" || items[1].TranslatedText != "wie es funktioniert" {
+ t.Fatalf("unexpected items: %+v, %+v", items[0], items[1])
+ }
+}
diff --git a/runtime/krillinai/internal/service/translate.go b/runtime/krillinai/internal/service/translate.go
index e6349afe..4274feaa 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.ExtractJSON(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.ExtractJSON(response)
var result struct {
Translations []struct {
diff --git a/runtime/krillinai/pkg/util/json.go b/runtime/krillinai/pkg/util/json.go
new file mode 100644
index 00000000..42166c4b
--- /dev/null
+++ b/runtime/krillinai/pkg/util/json.go
@@ -0,0 +1,74 @@
+package util
+
+import "strings"
+
+// ExtractJSON 从大模型返回的文本中提取第一个完整的 JSON 值。
+// 本地模型(如通过 Ollama 运行的 llama3.1)经常在 JSON 前后附带对话式说明,
+// 或在 } 、] 之前多输出一个逗号,这两种情况都会让 encoding/json 解析失败。
+// 该函数先去掉 Markdown 代码块标记,再从第一个 { 或 [ 开始按嵌套深度找到匹配的结束符,
+// 丢弃前后多余的文本,并去掉结束符前的尾随逗号。
+// 扫描会跳过字符串字面量中的内容,因此正文里的括号、引号和逗号不会被破坏。
+// 若文本中不存在 JSON 结构,或结构不完整(缺少结束符),则原样返回去除首尾空白的输入,
+// 让调用方拿到真实的解析错误。
+func ExtractJSON(response string) string {
+ trimmed := CleanMarkdownCodeBlock(response)
+
+ start := strings.IndexAny(trimmed, "{[")
+ if start < 0 {
+ return trimmed
+ }
+
+ var (
+ builder strings.Builder
+ depth int
+ inString bool
+ escaped bool
+ )
+ for i := start; i < len(trimmed); i++ {
+ c := trimmed[i]
+
+ if inString {
+ builder.WriteByte(c)
+ switch {
+ case escaped:
+ escaped = false
+ case c == '\\':
+ escaped = true
+ case c == '"':
+ inString = false
+ }
+ continue
+ }
+
+ switch c {
+ case '"':
+ inString = true
+ case '{', '[':
+ depth++
+ case '}', ']':
+ trimTrailingComma(&builder)
+ depth--
+ }
+
+ builder.WriteByte(c)
+
+ if depth == 0 {
+ return builder.String()
+ }
+ }
+
+ // 结构不完整,不做猜测
+ return trimmed
+}
+
+// trimTrailingComma 去掉已写入内容末尾的逗号(含其后的空白),用于容忍结束符前的尾随逗号。
+// 末尾没有逗号时保持原内容不变,避免改动合法 JSON 的缩进。
+func trimTrailingComma(builder *strings.Builder) {
+ current := builder.String()
+ cleaned := strings.TrimRight(current, " \t\r\n,")
+ if !strings.Contains(current[len(cleaned):], ",") {
+ return
+ }
+ builder.Reset()
+ builder.WriteString(cleaned)
+}
diff --git a/runtime/krillinai/pkg/util/json_test.go b/runtime/krillinai/pkg/util/json_test.go
new file mode 100644
index 00000000..4b8765ac
--- /dev/null
+++ b/runtime/krillinai/pkg/util/json_test.go
@@ -0,0 +1,137 @@
+package util
+
+import (
+ "encoding/json"
+ "testing"
+)
+
+func TestExtractJSON(t *testing.T) {
+ tests := []struct {
+ name string
+ response string
+ want string
+ }{
+ {
+ name: "对话式前缀",
+ response: "以下是分割后的结果:\n\n\n{\n \"align\": [\n { \"origin_part\": \"I want to show you\" }\n ]\n}\n",
+ want: "{\n \"align\": [\n { \"origin_part\": \"I want to show you\" }\n ]\n}",
+ },
+ {
+ name: "对话式后缀",
+ response: "{\"short_sentences\":[{\"text\":\"a\"}]}\n\n希望这个结果对你有帮助!",
+ want: `{"short_sentences":[{"text":"a"}]}`,
+ },
+ {
+ name: "对象尾随逗号",
+ response: "{\n\"short_sentences\":[{\n\"text\": \"the owl is a symbol\",\n}] \n}",
+ want: "{\n\"short_sentences\":[{\n\"text\": \"the owl is a symbol\"}] \n}",
+ },
+ {
+ name: "数组尾随逗号",
+ response: `{"short_sentences":[{"text":"a"},{"text":"b"},]}`,
+ want: `{"short_sentences":[{"text":"a"},{"text":"b"}]}`,
+ },
+ {
+ name: "前缀与尾随逗号同时出现",
+ response: "以下是分割后的结果:\n```json\n{\"short_sentences\":[{\"text\":\"a\"},]}\n```",
+ want: `{"short_sentences":[{"text":"a"}]}`,
+ },
+ {
+ name: "Markdown 代码块",
+ response: "```json\n{\"align\":[{\"origin_part\":\"a\",\"translated_part\":\"b\"}]}\n```",
+ want: `{"align":[{"origin_part":"a","translated_part":"b"}]}`,
+ },
+ {
+ name: "顶层数组",
+ response: "Sure! Here you go:\n[{\"text\":\"a\"},]",
+ want: `[{"text":"a"}]`,
+ },
+ {
+ name: "已经是合法 JSON 时保持不变",
+ response: `{"short_sentences":[{"text":"a"}]}`,
+ want: `{"short_sentences":[{"text":"a"}]}`,
+ },
+ {
+ name: "JSON 之后的多余内容被丢弃",
+ response: `{"a":1} {"b":2}`,
+ want: `{"a":1}`,
+ },
+ {
+ name: "字符串内的括号不影响匹配",
+ response: `{"text":"a } b ] c"}`,
+ want: `{"text":"a } b ] c"}`,
+ },
+ {
+ name: "字符串内的逗号不被删除",
+ response: `{"text":"a, "}`,
+ want: `{"text":"a, "}`,
+ },
+ {
+ name: "转义引号不会提前结束字符串",
+ response: `{"text":"he said \"} \" and left",}`,
+ want: `{"text":"he said \"} \" and left"}`,
+ },
+ {
+ name: "无 JSON 结构时原样返回",
+ response: " 抱歉,我无法完成这个请求 ",
+ want: "抱歉,我无法完成这个请求",
+ },
+ {
+ name: "空字符串",
+ response: "",
+ want: "",
+ },
+ {
+ name: "结构不完整时原样返回",
+ response: `{"short_sentences":[{"text":"a"}`,
+ want: `{"short_sentences":[{"text":"a"}`,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if got := ExtractJSON(tt.response); got != tt.want {
+ t.Errorf("ExtractJSON() = %q, want %q", got, tt.want)
+ }
+ })
+ }
+}
+
+// TestExtractJSONOutputIsParsable 保证提取结果确实能被 encoding/json 解析,
+// 这正是 issue #291 中失败的两类响应。
+func TestExtractJSONOutputIsParsable(t *testing.T) {
+ responses := []struct {
+ name string
+ response string
+ }{
+ {
+ name: "场景一_尾随逗号",
+ response: "{\n\"short_sentences\":[{\n\"text\": \"And the reason why they chose the owl is because\",\n},\n{\n\"text\": \"the owl is a symbol used in Europe...\",\n}] \n}",
+ },
+ {
+ name: "场景二_中文对话式前缀",
+ response: "以下是分割后的结果:\n\n\n{\n \"align\": [\n { \"origin_part\": \"I want to show you\", \"translated_part\": \"Ich möchte es Ihnen zeigen\" }\n ]\n}\n",
+ },
+ }
+
+ for _, tt := range responses {
+ t.Run(tt.name, func(t *testing.T) {
+ var payload map[string]any
+ if err := json.Unmarshal([]byte(ExtractJSON(tt.response)), &payload); err != nil {
+ t.Fatalf("解析提取结果失败: %v", err)
+ }
+ if len(payload) == 0 {
+ t.Fatal("提取结果为空对象")
+ }
+ })
+ }
+}
+
+// TestCleanMarkdownCodeBlockStillStripsFences 固定 ExtractJSON 所依赖的代码块清理行为。
+// (control) 该行为在修复前后一致,用于证明 ExtractJSON 没有改变已有的去围栏逻辑。
+func TestCleanMarkdownCodeBlockStillStripsFences(t *testing.T) {
+ got := CleanMarkdownCodeBlock("```json\n{\"a\":1}\n```")
+ if got != `{"a":1}` {
+ t.Errorf("CleanMarkdownCodeBlock() = %q, want %q", got, `{"a":1}`)
+ }
+}