mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-15 05:04:27 +08:00
### Summary
- Propagate the dataset language through Go DOCX, Markdown, PDF
figure-enhancement, and standalone-image vision paths.
- Explicitly render the shared figure prompt's `{{ language }}`
placeholder in Go.
- Use English when the dataset language is empty.
- Make the default standalone-image prompt request the dataset language
while preserving visible text in its original language.
- Add focused tests for caller propagation, language fallback, prompt
rendering, and prompt-cache isolation.
104 lines
3.0 KiB
Go
104 lines
3.0 KiB
Go
//
|
|
// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
//
|
|
|
|
package component
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestResolveVisionLanguage(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
inputs map[string]any
|
|
fallback string
|
|
want string
|
|
}{
|
|
{
|
|
name: "dataset language takes precedence",
|
|
inputs: map[string]any{"lang": " Japanese "},
|
|
fallback: "Chinese",
|
|
want: "Japanese",
|
|
},
|
|
{
|
|
name: "configured fallback is used",
|
|
inputs: map[string]any{"lang": ""},
|
|
fallback: " Korean ",
|
|
want: "Korean",
|
|
},
|
|
{
|
|
name: "empty values default to English",
|
|
inputs: nil,
|
|
fallback: "",
|
|
want: defaultVisionLanguage,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := resolveVisionLanguage(tt.inputs, tt.fallback); got != tt.want {
|
|
t.Fatalf("resolveVisionLanguage() = %q, want %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestRenderFigureVisionLanguage(t *testing.T) {
|
|
prompt := renderFigureVisionLanguage("spaced={{ language }} compact={{language}}", "Japanese")
|
|
if prompt != "spaced=Japanese compact=Japanese" {
|
|
t.Fatalf("renderFigureVisionLanguage() = %q", prompt)
|
|
}
|
|
|
|
prompt = renderFigureVisionLanguage("language={{ language }}", "")
|
|
if prompt != "language=English" {
|
|
t.Fatalf("empty-language rendering = %q, want English fallback", prompt)
|
|
}
|
|
}
|
|
|
|
func TestBuildFigureVisionPromptRendersLanguageAndContext(t *testing.T) {
|
|
prompt, err := buildFigureVisionPrompt("Context above", "Context below", "Japanese")
|
|
if err != nil {
|
|
t.Fatalf("buildFigureVisionPrompt: %v", err)
|
|
}
|
|
|
|
for _, want := range []string{
|
|
"Write all descriptions and field values in Japanese.",
|
|
"Context above",
|
|
"Context below",
|
|
} {
|
|
if !strings.Contains(prompt, want) {
|
|
t.Fatalf("prompt does not contain %q", want)
|
|
}
|
|
}
|
|
for _, unresolved := range []string{"{{ language }}", "{{ context_above }}", "{{ context_below }}"} {
|
|
if strings.Contains(prompt, unresolved) {
|
|
t.Fatalf("prompt contains unresolved placeholder %q", unresolved)
|
|
}
|
|
}
|
|
|
|
secondPrompt, err := buildFigureVisionPrompt("", "", "Chinese")
|
|
if err != nil {
|
|
t.Fatalf("buildFigureVisionPrompt with cached template: %v", err)
|
|
}
|
|
if !strings.Contains(secondPrompt, "Write all descriptions and field values in Chinese.") {
|
|
t.Fatalf("cached prompt did not render the second request language")
|
|
}
|
|
if strings.Contains(secondPrompt, "in Japanese.") {
|
|
t.Fatalf("cached prompt leaked the previous request language")
|
|
}
|
|
}
|