feat: use language-neutral English prompts for multilingual LLM responses
Convert all LLM-facing prompts, templates, and tool output labels to language-neutral English so the model responds in the user's input language. Update frontend relevance/match-type mappings to match. Output language strategy: - User-facing: reply in user's input language - Vector-indexed: match source document language - LLM-internal: fixed English
This commit is contained in:
@@ -71,31 +71,31 @@ func (p *PluginIntoChatMessage) OnEvent(ctx context.Context,
|
||||
pipelineWarn(ctx, "IntoChatMessage", "invalid_query", map[string]interface{}{
|
||||
"session_id": chatManage.SessionID,
|
||||
})
|
||||
return ErrTemplateExecute.WithError(fmt.Errorf("用户查询包含非法内容"))
|
||||
return ErrTemplateExecute.WithError(fmt.Errorf("user query contains invalid content"))
|
||||
}
|
||||
|
||||
// Prepare weekday names
|
||||
weekdayName := []string{"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}
|
||||
weekdayName := []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
|
||||
|
||||
var contextsBuilder strings.Builder
|
||||
|
||||
// Build contexts string based on FAQ priority strategy
|
||||
if chatManage.FAQPriorityEnabled && len(faqResults) > 0 {
|
||||
// Build structured context with FAQ prioritization
|
||||
contextsBuilder.WriteString("### 资料来源 1:标准问答库 (FAQ)\n")
|
||||
contextsBuilder.WriteString("【高置信度 - 请优先参考】\n")
|
||||
contextsBuilder.WriteString("### Source 1: FAQ Knowledge Base\n")
|
||||
contextsBuilder.WriteString("[High Confidence - Prioritize these results]\n")
|
||||
for i, result := range faqResults {
|
||||
passage := getEnrichedPassageForChat(ctx, result)
|
||||
if hasHighConfidenceFAQ && i == 0 {
|
||||
contextsBuilder.WriteString(fmt.Sprintf("[FAQ-%d] ⭐ 精准匹配: %s\n", i+1, passage))
|
||||
contextsBuilder.WriteString(fmt.Sprintf("[FAQ-%d] Exact Match: %s\n", i+1, passage))
|
||||
} else {
|
||||
contextsBuilder.WriteString(fmt.Sprintf("[FAQ-%d] %s\n", i+1, passage))
|
||||
}
|
||||
}
|
||||
|
||||
if len(docResults) > 0 {
|
||||
contextsBuilder.WriteString("\n### 资料来源 2:参考文档\n")
|
||||
contextsBuilder.WriteString("【补充资料 - 仅在FAQ无法解答时参考】\n")
|
||||
contextsBuilder.WriteString("\n### Source 2: Reference Documents\n")
|
||||
contextsBuilder.WriteString("[Supplementary - Use only when FAQ cannot answer the question]\n")
|
||||
for i, result := range docResults {
|
||||
passage := getEnrichedPassageForChat(ctx, result)
|
||||
contextsBuilder.WriteString(fmt.Sprintf("[DOC-%d] %s\n", i+1, passage))
|
||||
@@ -208,10 +208,10 @@ func enrichContentWithImageInfo(ctx context.Context, content string, imageInfoJS
|
||||
if found && imgInfo != nil {
|
||||
replacement := match[0] + "\n"
|
||||
if imgInfo.Caption != "" {
|
||||
replacement += fmt.Sprintf("图片描述: %s\n", imgInfo.Caption)
|
||||
replacement += fmt.Sprintf("Image Caption: %s\n", imgInfo.Caption)
|
||||
}
|
||||
if imgInfo.OCRText != "" {
|
||||
replacement += fmt.Sprintf("图片文本: %s\n", imgInfo.OCRText)
|
||||
replacement += fmt.Sprintf("Image Text: %s\n", imgInfo.OCRText)
|
||||
}
|
||||
content = strings.Replace(content, match[0], replacement, 1)
|
||||
}
|
||||
@@ -227,10 +227,10 @@ func enrichContentWithImageInfo(ctx context.Context, content string, imageInfoJS
|
||||
|
||||
var imgTexts []string
|
||||
if imgInfo.Caption != "" {
|
||||
imgTexts = append(imgTexts, fmt.Sprintf("图片 %s 的描述信息: %s", imgInfo.URL, imgInfo.Caption))
|
||||
imgTexts = append(imgTexts, fmt.Sprintf("Image %s caption: %s", imgInfo.URL, imgInfo.Caption))
|
||||
}
|
||||
if imgInfo.OCRText != "" {
|
||||
imgTexts = append(imgTexts, fmt.Sprintf("图片 %s 的文本: %s", imgInfo.URL, imgInfo.OCRText))
|
||||
imgTexts = append(imgTexts, fmt.Sprintf("Image %s text: %s", imgInfo.URL, imgInfo.OCRText))
|
||||
}
|
||||
|
||||
if len(imgTexts) > 0 {
|
||||
@@ -243,7 +243,7 @@ func enrichContentWithImageInfo(ctx context.Context, content string, imageInfoJS
|
||||
if content != "" {
|
||||
content += "\n\n"
|
||||
}
|
||||
content += "附加图片信息:\n" + strings.Join(additionalImageTexts, "\n")
|
||||
content += "Additional Image Info:\n" + strings.Join(additionalImageTexts, "\n")
|
||||
}
|
||||
|
||||
pipelineInfo(ctx, "IntoChatMessage", "image_enrich_summary", map[string]interface{}{
|
||||
|
||||
@@ -218,9 +218,9 @@ func formatConversationHistory(historyList []*types.History) string {
|
||||
var builder strings.Builder
|
||||
for _, h := range historyList {
|
||||
builder.WriteString("------BEGIN------\n")
|
||||
builder.WriteString("用户的问题是:")
|
||||
builder.WriteString("User question: ")
|
||||
builder.WriteString(h.Query)
|
||||
builder.WriteString("\n助手的回答是:")
|
||||
builder.WriteString("\nAssistant answer: ")
|
||||
builder.WriteString(h.Answer)
|
||||
builder.WriteString("\n------END------\n")
|
||||
}
|
||||
|
||||
@@ -23,58 +23,60 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// tableDescriptionPromptTemplate 表格描述生成的 prompt 模板
|
||||
tableDescriptionPromptTemplate = `你是一个数据分析专家。请根据以下表格的结构信息和数据样本,生成一段简洁的表格元数据描述(200-300字)。
|
||||
// tableDescriptionPromptTemplate is the prompt template for generating table descriptions
|
||||
tableDescriptionPromptTemplate = `You are a data analysis expert. Based on the following table structure information and data samples, generate a concise table metadata description (200-300 words).
|
||||
|
||||
表名: %s
|
||||
Table name: %s
|
||||
|
||||
%s
|
||||
|
||||
%s
|
||||
|
||||
请从以下维度描述这个表格:
|
||||
1. **数据主题**:这个表格记录的是什么类型的数据?(如:用户信息、销售记录、日志数据等)
|
||||
2. **核心字段**:列出3-5个最重要的字段及其含义
|
||||
3. **数据规模**:总行数和列数
|
||||
4. **业务场景**:这个表格可能用于什么业务分析或应用场景?
|
||||
5. **关键特征**:数据有什么显著特点?(如:包含地理位置、有分类标签、存在层级关系等)
|
||||
Please describe the table from the following dimensions:
|
||||
1. **Data Subject**: What type of data does this table record? (e.g., user information, sales records, log data, etc.)
|
||||
2. **Core Fields**: List 3-5 most important fields and their meanings
|
||||
3. **Data Scale**: Total number of rows and columns
|
||||
4. **Business Scenarios**: What business analysis or application scenarios might this table be used for?
|
||||
5. **Key Characteristics**: What notable features does the data have? (e.g., contains geographic locations, has category labels, has hierarchical relationships, etc.)
|
||||
|
||||
**重要提示**:
|
||||
- 不要输出具体的数据值或样本内容
|
||||
- 使用概括性的描述,让用户能快速判断这个表格是否包含他们需要的信息
|
||||
- 用简洁专业的语言,便于检索和理解`
|
||||
**Important Notes**:
|
||||
- Do not output specific data values or sample content
|
||||
- Use general descriptions so users can quickly determine if this table contains the information they need
|
||||
- Use concise and professional language for easy retrieval and understanding
|
||||
- Write the description in the same language as the data content`
|
||||
|
||||
// columnDescriptionsPromptTemplate 列描述生成的 prompt 模板
|
||||
columnDescriptionsPromptTemplate = `你是一个数据分析专家。请根据以下表格的结构信息和数据样本,为每一列生成结构化的描述信息。
|
||||
// columnDescriptionsPromptTemplate is the prompt template for generating column descriptions
|
||||
columnDescriptionsPromptTemplate = `You are a data analysis expert. Based on the following table structure information and data samples, generate structured description information for each column.
|
||||
|
||||
表名: %s
|
||||
Table name: %s
|
||||
|
||||
%s
|
||||
|
||||
%s
|
||||
|
||||
请为每一列生成详细的描述,包含以下信息:
|
||||
1. **字段含义**:这一列存储的是什么信息?(如:用户ID、订单金额、创建时间等)
|
||||
2. **数据类型**:数据的类型和格式(如:整数、字符串、日期时间、布尔值等)
|
||||
3. **业务用途**:这个字段在业务中的作用(如:用于用户识别、金额计算、时间排序等)
|
||||
4. **数据特征**:数据的显著特点(如:唯一标识、可为空、有枚举值、有单位等)
|
||||
Please generate a detailed description for each column, including the following information:
|
||||
1. **Field Meaning**: What information does this column store? (e.g., user ID, order amount, creation time, etc.)
|
||||
2. **Data Type**: The type and format of the data (e.g., integer, string, datetime, boolean, etc.)
|
||||
3. **Business Purpose**: The role of this field in business (e.g., for user identification, amount calculation, time sorting, etc.)
|
||||
4. **Data Characteristics**: Notable features of the data (e.g., unique identifier, nullable, has enum values, has units, etc.)
|
||||
|
||||
请按以下格式输出(每列一个段落):
|
||||
Please output in the following format (one paragraph per column):
|
||||
|
||||
**列名1** (数据类型)
|
||||
- 字段含义:xxx
|
||||
- 业务用途:xxx
|
||||
- 数据特征:xxx
|
||||
**Column1** (data type)
|
||||
- Field Meaning: xxx
|
||||
- Business Purpose: xxx
|
||||
- Data Characteristics: xxx
|
||||
|
||||
**列名2** (数据类型)
|
||||
- 字段含义:xxx
|
||||
- 业务用途:xxx
|
||||
- 数据特征:xxx
|
||||
**Column2** (data type)
|
||||
- Field Meaning: xxx
|
||||
- Business Purpose: xxx
|
||||
- Data Characteristics: xxx
|
||||
|
||||
**重要提示**:
|
||||
- 不要输出具体的数据值,只描述字段的元信息
|
||||
- 使用清晰的业务术语,便于用户理解和搜索
|
||||
- 如果从样本数据中能推断出枚举值范围,可以概括说明(如:状态字段包含待处理/进行中/已完成等状态)`
|
||||
**Important Notes**:
|
||||
- Do not output specific data values, only describe the field metadata
|
||||
- Use clear business terms for easy user understanding and search
|
||||
- If enum value ranges can be inferred from sample data, provide a summary (e.g., status field contains pending/in-progress/completed states)
|
||||
- Write descriptions in the same language as the data content`
|
||||
)
|
||||
|
||||
// NewChunkExtractTask creates a new chunk extract task
|
||||
@@ -612,7 +614,7 @@ func (s *DataTableSummaryService) generateTableDescription(ctx context.Context,
|
||||
return "", fmt.Errorf("failed to generate table description: %w", err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("# 表格摘要\n\n表名: %s\n\n%s", tableName, response.Content), nil
|
||||
return fmt.Sprintf("# Table Summary\n\nTable name: %s\n\n%s", tableName, response.Content), nil
|
||||
}
|
||||
|
||||
// generateColumnDescriptions generates descriptions for each column in batch
|
||||
@@ -634,13 +636,13 @@ func (s *DataTableSummaryService) generateColumnDescriptions(ctx context.Context
|
||||
return "", fmt.Errorf("failed to generate column descriptions: %w", err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("# 表格列信息\n\n表名: %s\n\n%s", tableName, response.Content), nil
|
||||
return fmt.Sprintf("# Table Column Information\n\nTable name: %s\n\n%s", tableName, response.Content), nil
|
||||
}
|
||||
|
||||
// buildSampleDataDescription builds a formatted sample data description
|
||||
func (s *DataTableSummaryService) buildSampleDataDescription(sampleData *types.ToolResult, maxRows int) string {
|
||||
var builder strings.Builder
|
||||
builder.WriteString(fmt.Sprintf("前%d行数据示例:\n", maxRows))
|
||||
builder.WriteString(fmt.Sprintf("Sample data (first %d rows):\n", maxRows))
|
||||
|
||||
rows, ok := sampleData.Data["rows"].([]map[string]interface{})
|
||||
if !ok {
|
||||
|
||||
@@ -992,7 +992,7 @@ func (b *graphBuilder) generateKnowledgeGraphDiagram(ctx context.Context) string
|
||||
// only draw if there are multiple entities or at least one relationship in the subgraph
|
||||
if hasRelations {
|
||||
subgraphCount++
|
||||
sb.WriteString(fmt.Sprintf("\n subgraph 子图%d\n", subgraphCount))
|
||||
sb.WriteString(fmt.Sprintf("\n subgraph Subgraph%d\n", subgraphCount))
|
||||
|
||||
// add all entities in this subgraph
|
||||
entitiesInComponent := make(map[string]bool)
|
||||
|
||||
@@ -22,14 +22,14 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
vlmOCRPrompt = "请提取这张文档图片中的所有正文内容,用纯 Markdown 格式输出。要求:\n" +
|
||||
"1. 忽略页眉、页脚\n" +
|
||||
"2. 表格使用 Markdown 表格语法\n" +
|
||||
"3. 公式使用 LaTeX 格式(用 $ 或 $$ 包裹)\n" +
|
||||
"4. 按照原文阅读顺序组织\n" +
|
||||
"5. 只输出提取到的文本内容,不要添加任何 HTML 标签\n" +
|
||||
"如果图片中没有可识别的文字内容,请回复:无文字内容。"
|
||||
vlmCaptionPrompt = "简单凝炼的描述图片的主要内容"
|
||||
vlmOCRPrompt = "Extract all body text content from this document image and output in pure Markdown format. Requirements:\n" +
|
||||
"1. Ignore headers and footers\n" +
|
||||
"2. Use Markdown table syntax for tables\n" +
|
||||
"3. Use LaTeX format for formulas (wrapped with $ or $$)\n" +
|
||||
"4. Organize content in the original reading order\n" +
|
||||
"5. Only output extracted text content, do not add any HTML tags\n" +
|
||||
"If there is no recognizable text content in the image, reply: No text content."
|
||||
vlmCaptionPrompt = "Provide a brief and concise description of the main content of the image"
|
||||
)
|
||||
|
||||
// ImageMultimodalService handles image:multimodal asynq tasks.
|
||||
|
||||
@@ -1809,10 +1809,10 @@ func (s *knowledgeService) getSummary(ctx context.Context,
|
||||
var imageAnnotations string
|
||||
for _, img := range allImageInfos {
|
||||
if img.Caption != "" {
|
||||
imageAnnotations += fmt.Sprintf("\n[图片描述: %s]", img.Caption)
|
||||
imageAnnotations += fmt.Sprintf("\n[Image Description: %s]", img.Caption)
|
||||
}
|
||||
if img.OCRText != "" {
|
||||
imageAnnotations += fmt.Sprintf("\n[图片文字: %s]", img.OCRText)
|
||||
imageAnnotations += fmt.Sprintf("\n[Image OCR Text: %s]", img.OCRText)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1829,15 +1829,15 @@ func (s *knowledgeService) getSummary(ctx context.Context,
|
||||
|
||||
// Add knowledge metadata if available
|
||||
if knowledge != nil {
|
||||
metadataIntro := fmt.Sprintf("文档类型: %s\n文件名称: %s\n", knowledge.FileType, knowledge.FileName)
|
||||
metadataIntro := fmt.Sprintf("Document Type: %s\nFile Name: %s\n", knowledge.FileType, knowledge.FileName)
|
||||
|
||||
// Add additional metadata if available
|
||||
if knowledge.Type != "" {
|
||||
metadataIntro += fmt.Sprintf("知识类型: %s\n", knowledge.Type)
|
||||
metadataIntro += fmt.Sprintf("Knowledge Type: %s\n", knowledge.Type)
|
||||
}
|
||||
|
||||
// Prepend metadata to content
|
||||
contentWithMetadata = metadataIntro + "\n内容:\n" + contentWithMetadata
|
||||
contentWithMetadata = metadataIntro + "\nContent:\n" + contentWithMetadata
|
||||
}
|
||||
|
||||
// Generate summary using AI model
|
||||
@@ -2035,7 +2035,7 @@ func (s *knowledgeService) ProcessSummaryGeneration(ctx context.Context, t *asyn
|
||||
TenantID: knowledge.TenantID,
|
||||
KnowledgeID: knowledge.ID,
|
||||
KnowledgeBaseID: knowledge.KnowledgeBaseID,
|
||||
Content: fmt.Sprintf("# 文档名称\n%s\n\n# 摘要\n%s", knowledge.FileName, summary),
|
||||
Content: fmt.Sprintf("# Document\n%s\n\n# Summary\n%s", knowledge.FileName, summary),
|
||||
ChunkIndex: maxChunkIndex + 1,
|
||||
IsEnabled: true,
|
||||
CreatedAt: time.Now(),
|
||||
@@ -2282,12 +2282,12 @@ func (s *knowledgeService) generateQuestionsWithContext(ctx context.Context,
|
||||
// Build context section
|
||||
var contextSection string
|
||||
if prevContent != "" || nextContent != "" {
|
||||
contextSection = "## 上下文信息(仅供参考,帮助理解主要内容)\n"
|
||||
contextSection = "## Context Information (for reference only, to help understand the main content)\n"
|
||||
if prevContent != "" {
|
||||
contextSection += fmt.Sprintf("【前文】%s\n", prevContent)
|
||||
contextSection += fmt.Sprintf("[Preceding Context] %s\n", prevContent)
|
||||
}
|
||||
if nextContent != "" {
|
||||
contextSection += fmt.Sprintf("【后文】%s\n", nextContent)
|
||||
contextSection += fmt.Sprintf("[Following Context] %s\n", nextContent)
|
||||
}
|
||||
contextSection += "\n"
|
||||
}
|
||||
@@ -2335,32 +2335,38 @@ func (s *knowledgeService) generateQuestionsWithContext(ctx context.Context,
|
||||
}
|
||||
|
||||
// Default prompt for question generation with context support
|
||||
const defaultQuestionGenerationPrompt = `你是一个专业的问题生成助手。你的任务是根据给定的【主要内容】生成用户可能会问的相关问题。
|
||||
const defaultQuestionGenerationPrompt = `You are a professional question generation assistant. Your task is to generate related questions that users might ask based on the given [Main Content].
|
||||
|
||||
{{context}}
|
||||
## 主要内容(请基于此内容生成问题)
|
||||
文档名称:{{doc_name}}
|
||||
文档内容:
|
||||
## Main Content (generate questions based on this content)
|
||||
Document name: {{doc_name}}
|
||||
Document content:
|
||||
{{content}}
|
||||
|
||||
## 核心要求
|
||||
- 生成的问题必须与【主要内容】直接相关
|
||||
- 问题中禁止使用任何代词或指代词(如"它"、"这个"、"该文档"、"本文"、"文中"、"其"等),必须用具体名称替代
|
||||
- 问题必须是完整独立的,脱离上下文也能被理解
|
||||
- 问题应该是用户在实际场景中可能会提出的自然问题
|
||||
- 问题应该多样化,覆盖内容的不同方面
|
||||
- 每个问题应该简洁明了,长度控制在30字以内
|
||||
- 生成的问题数量为 {{question_count}} 个
|
||||
## Core Requirements
|
||||
- Generated questions must be directly related to the [Main Content]
|
||||
- Questions must NOT use any pronouns or referential words (such as "it", "this", "that document", "this article", "the text", "its", etc.); use specific names instead
|
||||
- Questions must be complete and self-contained, understandable without additional context
|
||||
- Questions should be natural questions that users would likely ask in real scenarios
|
||||
- Questions should be diverse, covering different aspects of the content
|
||||
- Each question should be concise and clear, within 30 words
|
||||
- Generate {{question_count}} questions
|
||||
|
||||
## 问题类型建议
|
||||
- 定义类:什么是...?...是什么?
|
||||
- 原因类:为什么...?...的原因是什么?
|
||||
- 方法类:如何...?怎样...?
|
||||
- 比较类:...和...有什么区别?
|
||||
- 应用类:...可以用于什么场景?
|
||||
## Suggested Question Types
|
||||
- Definition: What is...? What does... mean?
|
||||
- Reason: Why...? What is the reason for...?
|
||||
- Method: How to...? What is the way to...?
|
||||
- Comparison: What is the difference between... and...?
|
||||
- Application: What scenarios can... be used for?
|
||||
|
||||
## 输出格式
|
||||
直接输出问题列表,每行一个问题,不要有序号或其他前缀。`
|
||||
## Output Format
|
||||
Output the question list directly, one question per line, without numbering or other prefixes.
|
||||
|
||||
## CRITICAL: Language Rule
|
||||
- Generate questions in the SAME LANGUAGE as the source document
|
||||
- If the document is in Korean, generate questions in Korean
|
||||
- If the document is in English, generate questions in English
|
||||
- If the document is in Chinese, generate questions in Chinese`
|
||||
|
||||
// GetKnowledgeFile retrieves the physical file associated with a knowledge entry
|
||||
func (s *knowledgeService) GetKnowledgeFile(ctx context.Context, id string) (io.ReadCloser, string, error) {
|
||||
|
||||
@@ -16,6 +16,7 @@ var (
|
||||
"无文字内容",
|
||||
"无法识别",
|
||||
"no text",
|
||||
"no text content",
|
||||
"no content",
|
||||
"empty",
|
||||
"图片中没有文字",
|
||||
@@ -94,8 +95,11 @@ func ocrHTMLToMarkdown(content string) string {
|
||||
|
||||
// isKnownEmptyReply checks whether the text matches a known "no content"
|
||||
// reply pattern that VLM models produce when the image has no text.
|
||||
// Trailing punctuation (., !, ?) is stripped before comparison so that
|
||||
// responses like "No text content." still match "no text content".
|
||||
func isKnownEmptyReply(text string) bool {
|
||||
lower := strings.ToLower(strings.TrimSpace(text))
|
||||
lower = strings.TrimRight(lower, ".!?。!?")
|
||||
for _, phrase := range knownEmptyReplies {
|
||||
if lower == strings.ToLower(phrase) {
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user