Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions runtime/krillinai/internal/service/audio2subtitle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.ExtractJSONObject(response, "align")), &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)
}
Expand Down Expand Up @@ -1911,7 +1911,7 @@ func (s Service) splitOriginLongSentence(sentence string) ([]string, error) {
} `json:"short_sentences"`
}

cleanResponse := util.CleanMarkdownCodeBlock(response)
cleanResponse := util.ExtractJSONObject(response, "short_sentences")
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
Expand Down
119 changes: 119 additions & 0 deletions runtime/krillinai/internal/service/audio2subtitle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,122 @@ func TestSplitTranslateItemReportsLongSentenceProgress(t *testing.T) {
t.Fatalf("progress = %s", got)
}
}

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)
}
})
}
}

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])
}
}

func TestSplitLongSentenceRejectsDecoyObject(t *testing.T) {
log.InitLogger()
completer := &scriptedCompleter{responses: []string{
"格式示例:{}\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])
}
}

func TestSplitOriginLongSentenceRejectsDecoyObject(t *testing.T) {
log.InitLogger()
completer := &scriptedCompleter{responses: []string{
"请按如下格式输出:{\"example\": true}\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}",
}}
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)
}
}

func TestSplitLongSentenceStillFailsOnGarbage(t *testing.T) {
log.InitLogger()
completer := &scriptedCompleter{responses: []string{"抱歉,我无法完成这个请求"}}
service := Service{ChatCompleter: completer}

if _, err := service.splitLongSentence(&TranslatedItem{
OriginText: "I want to show you how it works",
TranslatedText: "Ich möchte es Ihnen zeigen wie es funktioniert",
}); err == nil {
t.Fatal("splitLongSentence() error = nil, want parse error")
}
}
4 changes: 2 additions & 2 deletions runtime/krillinai/internal/service/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (t *Translator) splitOriginLongSentence(sentence string) ([]string, error)
} `json:"short_sentences"`
}

cleanResponse := util.CleanMarkdownCodeBlock(response)
cleanResponse := util.ExtractJSONObject(response, "short_sentences")
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
Expand Down Expand Up @@ -724,7 +724,7 @@ Required JSON format (output ONLY this structure):
zap.Int("响应长度", len(response)))

// 解析JSON响应
cleanResponse := util.CleanMarkdownCodeBlock(response)
cleanResponse := util.ExtractJSONObject(response, "translations")

var result struct {
Translations []struct {
Expand Down
118 changes: 118 additions & 0 deletions runtime/krillinai/internal/service/translate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package service

import (
"fmt"
"krillin-ai/internal/types"
"krillin-ai/log"
"testing"
)

func TestTranslatorSplitOriginLongSentenceToleratesLLMNoise(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}}
translator := &Translator{chatCompleter: completer}

sentences, err := translator.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)
}
})
}
}

func TestBatchTranslateTextsToleratesConversationalPrefix(t *testing.T) {
log.InitLogger()
completer := &scriptedCompleter{responses: []string{
"好的,以下是翻译结果:\n\n```json\n{\n \"translations\": [\n { \"index\": 1, \"text\": \"第一句\" },\n { \"index\": 2, \"text\": \"第二句\" },\n ]\n}\n```",
}}
translator := &Translator{chatCompleter: completer}

translations, err := translator.batchTranslateTexts(
[]string{"first sentence", "second sentence"},
types.StandardLanguageCode("en"),
types.StandardLanguageCode("zh_cn"),
)
if err != nil {
t.Fatalf("batchTranslateTexts() error = %v, want nil", err)
}
if completer.calls != 1 {
t.Fatalf("ChatCompletion 调用次数 = %d, want 1(无需重试)", completer.calls)
}
want := []string{"第一句", "第二句"}
if fmt.Sprint(translations) != fmt.Sprint(want) {
t.Fatalf("translations = %v, want %v", translations, want)
}
}

func TestTranslatorSplitOriginLongSentenceRejectsDecoyObject(t *testing.T) {
log.InitLogger()
completer := &scriptedCompleter{responses: []string{
"请按如下格式输出:{}\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}",
}}
translator := &Translator{chatCompleter: completer}

sentences, err := translator.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)
}
}

func TestBatchTranslateTextsRejectsDecoyObject(t *testing.T) {
log.InitLogger()
completer := &scriptedCompleter{responses: []string{
"输出格式示例:{}\n```json\n{\n \"translations\": [\n { \"index\": 1, \"text\": \"第一句\" },\n { \"index\": 2, \"text\": \"第二句\" },\n ]\n}\n```",
}}
translator := &Translator{chatCompleter: completer}

translations, err := translator.batchTranslateTexts(
[]string{"first sentence", "second sentence"},
types.StandardLanguageCode("en"),
types.StandardLanguageCode("zh_cn"),
)
if err != nil {
t.Fatalf("batchTranslateTexts() error = %v, want nil", err)
}
if completer.calls != 1 {
t.Fatalf("ChatCompletion 调用次数 = %d, want 1(无需重试)", completer.calls)
}
want := []string{"第一句", "第二句"}
if fmt.Sprint(translations) != fmt.Sprint(want) {
t.Fatalf("translations = %v, want %v", translations, want)
}
}
107 changes: 107 additions & 0 deletions runtime/krillinai/pkg/util/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package util

import (
"encoding/json"
"strings"
)

// ExtractJSONObject 从大模型返回的文本中提取顶层带有 requiredKey 字段的 JSON 对象。
// 本地模型(如通过 Ollama 运行的 llama3.1)经常在 JSON 前后附带对话式说明,
// 或在 } 、] 之前多输出一个逗号,这两种情况都会让 encoding/json 解析失败。
// 说明里还可能带上示例或解释用的 JSON(如 {} 或 {"note":"..."}),
// 因此这里按顺序扫描文本中的每个完整 JSON 值,只接受顶层带 requiredKey 的对象,
// 避免把示例当成模型真正的回答,从而解析出一个空结果。
// 若文本中没有这样的对象,则原样返回去除首尾空白的输入,
// 让调用方拿到真实的解析错误并走原有的重试逻辑。
func ExtractJSONObject(response, requiredKey string) string {
trimmed := CleanMarkdownCodeBlock(response)

for offset := 0; offset < len(trimmed); {
value, end, ok := extractFirstJSONValue(trimmed, offset)
if !ok {
break
}
if hasTopLevelKey(value, requiredKey) {
return value
}
offset = end
}

return trimmed
}

// extractFirstJSONValue 从 from 开始找到第一个 { 或 [,按嵌套深度取到匹配的结束符,
// 返回该 JSON 值、它在 s 中结束后的下一个位置,以及是否找到完整结构。
// 扫描会跳过字符串字面量中的内容,因此正文里的括号、引号和逗号不会被破坏;
// 结束符前的尾随逗号会被去掉。结构不完整(缺少结束符)时不做猜测。
func extractFirstJSONValue(s string, from int) (string, int, bool) {
start := strings.IndexAny(s[from:], "{[")
if start < 0 {
return "", len(s), false
}
start += from

var (
builder strings.Builder
depth int
inString bool
escaped bool
)
for i := start; i < len(s); i++ {
c := s[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(), i + 1, true
}
}

return "", len(s), false
}

// hasTopLevelKey 判断 value 是不是顶层含有 key 字段的 JSON 对象。
// 数组、标量以及解析失败的内容都不算,嵌套在下层的同名字段也不算。
func hasTopLevelKey(value, key string) bool {
var fields map[string]json.RawMessage
if err := json.Unmarshal([]byte(value), &fields); err != nil {
return false
}
_, ok := fields[key]
return ok
}

// 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)
}
Loading