feat: Implement FAQ functionality in Knowledge Base

- Added support for creating and managing FAQ knowledge bases, including new API endpoints for listing, upserting, updating, and deleting FAQ entries.
- Enhanced the frontend to allow users to create FAQ knowledge bases and manage FAQ entries with a dedicated interface.
- Introduced new types and configurations for FAQ handling in the backend, ensuring proper indexing and retrieval of FAQ entries.
- Updated localization files to include new strings related to FAQ management in multiple languages.
- Refactored existing components to accommodate the new FAQ features, improving user experience and functionality.
This commit is contained in:
wizardchen
2025-11-15 01:30:17 +08:00
parent 0ea5e6a2a2
commit 25fdaed8ca
37 changed files with 4778 additions and 198 deletions
+7
View File
@@ -16,10 +16,12 @@ import (
type KnowledgeBase struct {
ID string `json:"id"`
Name string `json:"name"` // Name must be unique within the same tenant
Type string `json:"type"`
Description string `json:"description"`
TenantID uint `json:"tenant_id"` // Changed to uint type
ChunkingConfig ChunkingConfig `json:"chunking_config"`
ImageProcessingConfig ImageProcessingConfig `json:"image_processing_config"`
FAQConfig *FAQConfig `json:"faq_config"`
EmbeddingModelID string `json:"embedding_model_id"`
SummaryModelID string `json:"summary_model_id"` // Summary model ID
CreatedAt time.Time `json:"created_at"`
@@ -40,6 +42,11 @@ type ChunkingConfig struct {
EnableMultimodal bool `json:"enable_multimodal"` // Whether to enable multimodal processing
}
// FAQConfig represents faq-specific configuration
type FAQConfig struct {
IndexMode string `json:"index_mode"`
}
// ImageProcessingConfig represents image processing configuration
type ImageProcessingConfig struct {
ModelID string `json:"model_id"` // Multimodal model ID
+105 -1
View File
@@ -21,7 +21,8 @@
"vue": "^3.5.13",
"vue-i18n": "^11.1.12",
"vue-router": "^4.5.0",
"webpack": "^5.94.0"
"webpack": "^5.94.0",
"xlsx": "^0.18.5"
},
"devDependencies": {
"@tsconfig/node22": "^22.0.1",
@@ -1894,6 +1895,15 @@
"acorn": "^8.14.0"
}
},
"node_modules/adler-32": {
"version": "1.3.1",
"resolved": "https://mirrors.tencent.com/npm/adler-32/-/adler-32-1.3.1.tgz",
"integrity": "sha512-ynZ4w/nUUv5rrsR8UUGoe1VC9hZj6V5hU9Qw1HlMDJGEJw5S7TfTErWTjMys6M7vr0YWcPqs3qAr4ss0nDfP+A==",
"license": "Apache-2.0",
"engines": {
"node": ">=0.8"
}
},
"node_modules/ajv": {
"version": "8.17.1",
"resolved": "https://mirrors.tencent.com/npm/ajv/-/ajv-8.17.1.tgz",
@@ -2065,6 +2075,19 @@
],
"license": "CC-BY-4.0"
},
"node_modules/cfb": {
"version": "1.2.2",
"resolved": "https://mirrors.tencent.com/npm/cfb/-/cfb-1.2.2.tgz",
"integrity": "sha512-KfdUZsSOw19/ObEWasvBP/Ac4reZvAGauZhs6S/gqNhXhI7cKwvlH7ulj+dOEYnca4bm4SGo8C1bTAQvnTjgQA==",
"license": "Apache-2.0",
"dependencies": {
"adler-32": "~1.3.0",
"crc-32": "~1.2.0"
},
"engines": {
"node": ">=0.8"
}
},
"node_modules/chrome-trace-event": {
"version": "1.0.4",
"resolved": "https://mirrors.tencent.com/npm/chrome-trace-event/-/chrome-trace-event-1.0.4.tgz",
@@ -2074,6 +2097,15 @@
"node": ">=6.0"
}
},
"node_modules/codepage": {
"version": "1.15.0",
"resolved": "https://mirrors.tencent.com/npm/codepage/-/codepage-1.15.0.tgz",
"integrity": "sha512-3g6NUTPd/YtuuGrhMnOMRjFc+LJw/bnMp3+0r/Wcz3IXUuCosKRJvMphm5+Q+bvTVGcJJuRvVLuYba+WojaFaA==",
"license": "Apache-2.0",
"engines": {
"node": ">=0.8"
}
},
"node_modules/combined-stream": {
"version": "1.0.8",
"resolved": "https://mirrors.tencent.com/npm/combined-stream/-/combined-stream-1.0.8.tgz",
@@ -2110,6 +2142,18 @@
"url": "https://github.com/sponsors/mesqueeb"
}
},
"node_modules/crc-32": {
"version": "1.2.2",
"resolved": "https://mirrors.tencent.com/npm/crc-32/-/crc-32-1.2.2.tgz",
"integrity": "sha512-ROmzCKrTnOwybPcJApAA6WBWij23HVfGVNKqqrZpuyZOHqK2CwHSvpGuyt/UNNvaIjEd8X5IFGp4Mh+Ie1IHJQ==",
"license": "Apache-2.0",
"bin": {
"crc32": "bin/crc32.njs"
},
"engines": {
"node": ">=0.8"
}
},
"node_modules/cross-spawn": {
"version": "7.0.6",
"resolved": "https://mirrors.tencent.com/npm/cross-spawn/-/cross-spawn-7.0.6.tgz",
@@ -2479,6 +2523,15 @@
"node": ">= 6"
}
},
"node_modules/frac": {
"version": "1.1.2",
"resolved": "https://mirrors.tencent.com/npm/frac/-/frac-1.1.2.tgz",
"integrity": "sha512-w/XBfkibaTl3YDqASwfDUqkna4Z2p9cFSr1aHDt0WoMTECnRfBOv2WArlZILlqgWlmdIlALXGpM2AOhEk5W3IA==",
"license": "Apache-2.0",
"engines": {
"node": ">=0.8"
}
},
"node_modules/fsevents": {
"version": "2.3.3",
"resolved": "https://mirrors.tencent.com/npm/fsevents/-/fsevents-2.3.3.tgz",
@@ -3398,6 +3451,18 @@
"node": ">=0.10.0"
}
},
"node_modules/ssf": {
"version": "0.11.2",
"resolved": "https://mirrors.tencent.com/npm/ssf/-/ssf-0.11.2.tgz",
"integrity": "sha512-+idbmIXoYET47hH+d7dfm2epdOMUDjqcB4648sTZ+t2JwoyBFL/insLfB/racrDmsKB3diwsDA696pZMieAC5g==",
"license": "Apache-2.0",
"dependencies": {
"frac": "~1.1.2"
},
"engines": {
"node": ">=0.8"
}
},
"node_modules/superjson": {
"version": "2.2.2",
"resolved": "https://mirrors.tencent.com/npm/superjson/-/superjson-2.2.2.tgz",
@@ -3904,6 +3969,45 @@
"node": "^18.17.0 || >=20.5.0"
}
},
"node_modules/wmf": {
"version": "1.0.2",
"resolved": "https://mirrors.tencent.com/npm/wmf/-/wmf-1.0.2.tgz",
"integrity": "sha512-/p9K7bEh0Dj6WbXg4JG0xvLQmIadrner1bi45VMJTfnbVHsc7yIajZyoSoK60/dtVBs12Fm6WkUI5/3WAVsNMw==",
"license": "Apache-2.0",
"engines": {
"node": ">=0.8"
}
},
"node_modules/word": {
"version": "0.3.0",
"resolved": "https://mirrors.tencent.com/npm/word/-/word-0.3.0.tgz",
"integrity": "sha512-OELeY0Q61OXpdUfTp+oweA/vtLVg5VDOXh+3he3PNzLGG/y0oylSOC1xRVj0+l4vQ3tj/bB1HVHv1ocXkQceFA==",
"license": "Apache-2.0",
"engines": {
"node": ">=0.8"
}
},
"node_modules/xlsx": {
"version": "0.18.5",
"resolved": "https://mirrors.tencent.com/npm/xlsx/-/xlsx-0.18.5.tgz",
"integrity": "sha512-dmg3LCjBPHZnQp5/F/+nnTa+miPJxUXB6vtk42YjBBKayDNagxGEeIdWApkYPOf3Z3pm3k62Knjzp7lMeTEtFQ==",
"license": "Apache-2.0",
"dependencies": {
"adler-32": "~1.3.0",
"cfb": "~1.2.1",
"codepage": "~1.15.0",
"crc-32": "~1.2.1",
"ssf": "~0.11.2",
"wmf": "~1.0.1",
"word": "~0.3.0"
},
"bin": {
"xlsx": "bin/xlsx.njs"
},
"engines": {
"node": ">=0.8"
}
},
"node_modules/yallist": {
"version": "3.1.1",
"resolved": "https://mirrors.tencent.com/npm/yallist/-/yallist-3.1.1.tgz",
+2 -1
View File
@@ -25,7 +25,8 @@
"vue": "^3.5.13",
"vue-i18n": "^11.1.12",
"vue-router": "^4.5.0",
"webpack": "^5.94.0"
"webpack": "^5.94.0",
"xlsx": "^0.18.5"
},
"devDependencies": {
"@tsconfig/node22": "^22.0.1",
+41
View File
@@ -8,6 +8,7 @@ export function listKnowledgeBases() {
export function createKnowledgeBase(data: {
name: string;
description?: string;
type?: 'document' | 'faq';
chunking_config?: any;
embedding_model_id?: string;
summary_model_id?: string;
@@ -16,6 +17,7 @@ export function createKnowledgeBase(data: {
vlm_config?: any;
cos_config?: any;
extract_config?: any;
faq_config?: { index_mode: string; question_index_mode?: string };
}) {
return post(`/api/v1/knowledge-bases`, data);
}
@@ -76,4 +78,43 @@ export function getKnowledgeDetailsCon(id: string, page: number) {
// Get chunk by chunk_id only (new endpoint - to be added to backend)
export function getChunkByIdOnly(chunkId: string) {
return get(`/api/v1/chunks/by-id/${chunkId}`);
}
const buildQuery = (params?: Record<string, any>) => {
if (!params) return '';
const query = new URLSearchParams();
Object.entries(params).forEach(([key, value]) => {
if (value === undefined || value === null || value === '') return;
query.append(key, String(value));
});
const queryString = query.toString();
return queryString ? `?${queryString}` : '';
};
export function listFAQEntries(kbId: string, params?: { page?: number; page_size?: number }) {
const query = buildQuery(params);
return get(`/api/v1/knowledge-bases/${kbId}/faq/entries${query}`);
}
export function upsertFAQEntries(kbId: string, data: { entries: any[]; mode: 'append' | 'replace' }) {
return post(`/api/v1/knowledge-bases/${kbId}/faq/entries`, data);
}
export function updateFAQEntry(kbId: string, entryId: string, data: any) {
return put(`/api/v1/knowledge-bases/${kbId}/faq/entries/${entryId}`, data);
}
export function deleteFAQEntries(kbId: string, ids: string[]) {
return del(`/api/v1/knowledge-bases/${kbId}/faq/entries`, { ids });
}
export function searchFAQEntries(
kbId: string,
data: {
query_text: string
vector_threshold?: number
match_count?: number
}
) {
return post(`/api/v1/knowledge-bases/${kbId}/faq/search`, data);
}
+80 -2
View File
@@ -482,6 +482,7 @@ export default {
refresh: 'Refresh',
loading: 'Loading...',
noData: 'No data',
noMoreData: 'All content loaded',
error: 'Error',
success: 'Success',
warning: 'Warning',
@@ -658,6 +659,7 @@ export default {
},
knowledgeList: {
create: 'Create Knowledge Base',
createFAQ: 'Create FAQ Knowledge Base',
uninitializedBanner: 'Some knowledge bases are not initialized. Configure model information in settings before adding documents.',
empty: {
title: 'No knowledge bases yet',
@@ -680,11 +682,16 @@ export default {
basic: 'Basic Information',
models: 'Model Configuration',
chunking: 'Chunking Settings',
advanced: 'Advanced Settings'
advanced: 'Advanced Settings',
faq: 'FAQ Settings'
},
basic: {
title: 'Basic Information',
description: 'Configure the knowledge base name and description',
typeLabel: 'Knowledge Base Type',
typeDocument: 'Document-based',
typeFAQ: 'FAQ Q&A',
typeDescription: 'FAQ suits structured Q&A datasets; document type supports file parsing and chunking.',
nameLabel: 'Knowledge Base Name',
namePlaceholder: 'Enter knowledge base name',
descriptionLabel: 'Knowledge Base Description',
@@ -706,7 +713,78 @@ export default {
createFailed: 'Failed to create knowledge base',
missingId: 'Knowledge base ID is missing',
buildDataFailed: 'Failed to construct submission data',
updateSuccess: 'Configuration saved successfully'
updateSuccess: 'Configuration saved successfully',
indexModeRequired: 'Please select an indexing mode for FAQ knowledge bases'
},
faq: {
title: 'FAQ Configuration',
description: 'Configure indexing strategy and guidance for FAQ-style knowledge bases',
indexModeLabel: 'Indexing Mode',
indexModeDescription: 'Question-only indexing improves precision, question+answer improves recall.',
questionIndexModeLabel: 'Question Indexing Mode',
questionIndexModeDescription: 'Combined: Standard and similar questions are indexed together. Separate: Each question is indexed independently for more precise retrieval but requires more storage.',
entryGuide: 'Each FAQ entry contains a primary question, similar questions, negative examples, and multiple answers. Manage them in the FAQ knowledge base detail view.',
modes: {
questionOnly: 'Questions only',
questionAnswer: 'Question + answer',
combined: 'Combined',
separate: 'Separate'
},
standardQuestion: 'Primary Question',
answers: 'Answers',
similarQuestions: 'Similar Questions',
negativeQuestions: 'Negative Examples',
editorCreate: 'Create FAQ Entry',
editorEdit: 'Edit FAQ Entry',
addAnswer: 'Add Answer',
answerPlaceholder: 'Enter answer content, supports multi-line text, press Ctrl+Enter or click button to add',
similarPlaceholder: 'Enter similar question and click plus icon to add',
negativePlaceholder: 'Enter negative example and click plus icon to add',
answerRequired: 'Please provide at least one answer',
noAnswer: 'No answers',
noSimilar: 'No similar questions',
noNegative: 'No negative examples',
emptyTitle: 'No FAQ entries',
emptyDesc: 'Click "Create FAQ Entry" above to get started',
searchTest: 'Search Test',
searchTestTitle: 'FAQ Search Test',
queryLabel: 'Query',
queryPlaceholder: 'Enter a question to search',
vectorThresholdLabel: 'Vector Similarity Threshold',
vectorThresholdDesc: 'Range 0-1, default 0.7',
keywordThresholdLabel: 'Keyword Match Threshold',
keywordThresholdDesc: 'Range 0-1, default 0.5',
matchCountLabel: 'Result Count',
matchCountDesc: 'Range 1-50, default 10',
searchButton: 'Search',
searching: 'Searching...',
searchResults: 'Search Results',
noResults: 'No matching FAQ entries found',
score: 'Similarity',
matchType: 'Match Type',
matchTypeEmbedding: 'Vector Match',
matchTypeKeywords: 'Keyword Match',
similarityThresholdLabel: 'Similarity Threshold',
},
faqImport: {
title: 'Batch Import FAQ',
modeLabel: 'Import Mode',
appendMode: 'Append',
replaceMode: 'Replace existing entries',
fileLabel: 'Select File',
fileTip: 'Supports JSON / CSV / Excel with fields standard_question, answers, similar_questions, negative_questions',
clickToUpload: 'Click to upload file',
dragDropTip: 'or drag and drop file here',
importButton: 'Import FAQ',
deleteSelected: 'Delete Selected',
deleteSuccess: 'Selected entries deleted',
previewCount: '{count} entries parsed',
previewMore: '{count} more entries not shown',
importSuccess: 'Import completed',
parseFailed: 'Failed to parse file',
invalidJSON: 'Invalid JSON format',
unsupportedFormat: 'Unsupported file format',
selectFile: 'Please select a file to import first',
},
models: {
title: 'Model Configuration',
+80 -2
View File
@@ -481,6 +481,7 @@ export default {
refresh: 'Обновить',
loading: 'Загрузка...',
noData: 'Нет данных',
noMoreData: 'Весь контент загружен',
error: 'Ошибка',
success: 'Успешно',
warning: 'Предупреждение',
@@ -747,6 +748,7 @@ export default {
},
knowledgeList: {
create: 'Создать базу знаний',
createFAQ: 'Создать FAQ-базу',
uninitializedBanner: 'Некоторые базы знаний не инициализированы. Сначала настройте модели в разделе настроек, чтобы добавлять документы.',
empty: {
title: 'Базы знаний отсутствуют',
@@ -769,11 +771,16 @@ export default {
basic: 'Основная информация',
models: 'Конфигурация моделей',
chunking: 'Настройки разбиения',
advanced: 'Дополнительные настройки'
advanced: 'Дополнительные настройки',
faq: 'FAQ настройки'
},
basic: {
title: 'Основная информация',
description: 'Укажите название и описание базы знаний',
typeLabel: 'Тип базы знаний',
typeDocument: 'Документальная',
typeFAQ: 'FAQ (вопрос-ответ)',
typeDescription: 'FAQ подходит для структурированных Q&A; документальный тип поддерживает загрузку файлов и разбиение.',
nameLabel: 'Название базы знаний',
namePlaceholder: 'Введите название базы знаний',
descriptionLabel: 'Описание базы знаний',
@@ -795,7 +802,78 @@ export default {
createFailed: 'Не удалось создать базу знаний',
missingId: 'Отсутствует ID базы знаний',
buildDataFailed: 'Не удалось сформировать данные для отправки',
updateSuccess: 'Настройки сохранены'
updateSuccess: 'Настройки сохранены',
indexModeRequired: 'Выберите режим индексации для FAQ базы знаний'
},
faq: {
title: 'Настройки FAQ',
description: 'Определите стратегию индексации и общие правила для FAQ-базы знаний',
indexModeLabel: 'Режим индексации',
indexModeDescription: 'Только вопросы дают более высокую точность, вопросы+ответы повышают полноту выдачи.',
questionIndexModeLabel: 'Режим индексации вопросов',
questionIndexModeDescription: 'Объединенная: стандартные и похожие вопросы индексируются вместе. Раздельная: каждый вопрос индексируется независимо для более точного поиска, но требует больше места.',
entryGuide: 'Каждый FAQ включает основной вопрос, похожие вопросы, негативные примеры и несколько ответов. Управляйте ими в деталях FAQ-базы.',
modes: {
questionOnly: 'Только вопросы',
questionAnswer: 'Вопрос + ответ',
combined: 'Объединенная',
separate: 'Раздельная'
},
standardQuestion: 'Основной вопрос',
answers: 'Ответы',
similarQuestions: 'Похожие вопросы',
negativeQuestions: 'Негативные примеры',
editorCreate: 'Создать FAQ запись',
editorEdit: 'Редактировать FAQ запись',
addAnswer: 'Добавить ответ',
answerPlaceholder: 'Введите содержимое ответа, поддерживается многострочный текст, нажмите Ctrl+Enter или нажмите кнопку для добавления',
similarPlaceholder: 'Введите похожий вопрос и нажмите значок плюса для добавления',
negativePlaceholder: 'Введите негативный пример и нажмите значок плюса для добавления',
answerRequired: 'Добавьте хотя бы один ответ',
noAnswer: 'Ответы отсутствуют',
noSimilar: 'Похожие вопросы отсутствуют',
noNegative: 'Нет негативных примеров',
emptyTitle: 'Нет записей FAQ',
emptyDesc: 'Нажмите "Создать FAQ запись" выше, чтобы начать',
searchTest: 'Тест поиска',
searchTestTitle: 'Тест поиска FAQ',
queryLabel: 'Запрос',
queryPlaceholder: 'Введите вопрос для поиска',
vectorThresholdLabel: 'Порог векторного сходства',
vectorThresholdDesc: 'Диапазон 0-1, по умолчанию 0.7',
keywordThresholdLabel: 'Порог совпадения ключевых слов',
keywordThresholdDesc: 'Диапазон 0-1, по умолчанию 0.5',
matchCountLabel: 'Количество результатов',
matchCountDesc: 'Диапазон 1-50, по умолчанию 10',
searchButton: 'Начать поиск',
searching: 'Поиск...',
searchResults: 'Результаты поиска',
noResults: 'Совпадающих записей FAQ не найдено',
score: 'Сходство',
matchType: 'Тип совпадения',
matchTypeEmbedding: 'Векторное совпадение',
matchTypeKeywords: 'Совпадение ключевых слов',
similarityThresholdLabel: 'Порог сходства',
},
faqImport: {
title: 'Пакетный импорт FAQ',
modeLabel: 'Режим импорта',
appendMode: 'Добавить',
replaceMode: 'Заменить существующие записи',
fileLabel: 'Выберите файл',
fileTip: 'Поддерживаются JSON / CSV / Excel. Поля: standard_question, answers, similar_questions, negative_questions',
clickToUpload: 'Нажмите для загрузки файла',
dragDropTip: 'или перетащите файл сюда',
importButton: 'Импортировать FAQ',
deleteSelected: 'Удалить выбранные',
deleteSuccess: 'Выбранные записи удалены',
previewCount: 'Разобрано записей: {count}',
previewMore: 'Ещё {count} записей не показано',
importSuccess: 'Импорт завершён',
parseFailed: 'Не удалось разобрать файл',
invalidJSON: 'Неверный формат JSON',
unsupportedFormat: 'Формат файла не поддерживается',
selectFile: 'Сначала выберите файл для импорта',
},
models: {
title: 'Конфигурация моделей',
+78
View File
@@ -566,6 +566,7 @@ export default {
refresh: "刷新",
loading: "加载中...",
noData: "暂无数据",
noMoreData: "已加载全部内容",
error: "错误",
success: "成功",
warning: "警告",
@@ -970,6 +971,7 @@ export default {
},
knowledgeList: {
create: "新建知识库",
createFAQ: "新建 FAQ 知识库",
uninitializedBanner:
"部分知识库尚未初始化,需要先在设置中配置模型信息才能添加知识文档",
empty: {
@@ -994,10 +996,15 @@ export default {
models: "模型配置",
chunking: "分块设置",
advanced: "高级设置",
faq: "FAQ 设置",
},
basic: {
title: "基本信息",
description: "设置知识库的名称和描述信息",
typeLabel: "知识库类型",
typeDocument: "文档型",
typeFAQ: "FAQ 问答",
typeDescription: "FAQ 类型适合结构化问答数据;文档型支持文件解析与分块。",
nameLabel: "知识库名称",
namePlaceholder: "请输入知识库名称",
descriptionLabel: "知识库描述",
@@ -1020,6 +1027,77 @@ export default {
missingId: "缺少知识库 ID",
buildDataFailed: "数据构建失败",
updateSuccess: "配置保存成功",
indexModeRequired: "请选择 FAQ 的索引方式",
},
faq: {
title: "FAQ 配置",
description: "设置 FAQ 知识库的索引策略和问答组织方式",
indexModeLabel: "索引方式",
indexModeDescription: "仅索引问题可提升精度,索引问答可提高召回率",
questionIndexModeLabel: "问题索引方式",
questionIndexModeDescription: "一起索引:标准问和相似问合并索引;分别索引:标准问和每个相似问独立索引,检索更精确但需要更多存储",
entryGuide: "FAQ 条目由标准问、相似问、反例和多个答案组成,可在知识库详情中批量导入、编辑。",
modes: {
questionOnly: "仅标准问/相似问",
questionAnswer: "标准问 + 答案",
combined: "一起索引",
separate: "分别索引",
},
standardQuestion: "标准问",
answers: "答案",
similarQuestions: "相似问",
negativeQuestions: "反例",
editorCreate: "新增 FAQ 条目",
editorEdit: "编辑 FAQ 条目",
addAnswer: "添加答案",
answerPlaceholder: "请输入答案内容,支持多行文本,按 Ctrl+Enter 或点击按钮添加",
similarPlaceholder: "输入相似问题后点击加号添加",
negativePlaceholder: "输入反例后点击加号添加",
answerRequired: "请至少填写一个答案",
noAnswer: "暂无答案",
noSimilar: "暂无相似问",
noNegative: "暂无反例",
emptyTitle: "暂无 FAQ 条目",
emptyDesc: "点击上方“新增 FAQ 条目”按钮开始创建",
searchTest: "检索测试",
searchTestTitle: "FAQ 检索测试",
queryLabel: "查询内容",
queryPlaceholder: "请输入要检索的问题",
vectorThresholdLabel: "向量相似度阈值",
vectorThresholdDesc: "范围 0-1,默认 0.7",
keywordThresholdLabel: "关键词匹配阈值",
keywordThresholdDesc: "范围 0-1,默认 0.5",
matchCountLabel: "结果数量",
matchCountDesc: "范围 1-50,默认 10",
searchButton: "开始检索",
searching: "检索中...",
searchResults: "检索结果",
noResults: "未找到匹配的 FAQ 条目",
score: "相似度",
matchType: "匹配类型",
matchTypeEmbedding: "向量匹配",
matchTypeKeywords: "关键词匹配",
similarityThresholdLabel: "相似度阈值",
},
faqImport: {
title: "批量导入 FAQ",
modeLabel: "导入模式",
appendMode: "追加导入",
replaceMode: "替换现有条目",
fileLabel: "选择文件",
fileTip: "支持 JSON / CSV / Excel,字段包含 standard_question、answers、similar_questions、negative_questions",
clickToUpload: "点击上传文件",
dragDropTip: "或拖拽文件到此处",
importButton: "导入 FAQ",
deleteSelected: "批量删除",
deleteSuccess: "选中条目已删除",
previewCount: "共解析 {count} 条记录",
previewMore: "还有 {count} 条未展示",
importSuccess: "导入成功",
parseFailed: "解析文件失败",
invalidJSON: "JSON 文件格式不正确",
unsupportedFormat: "暂不支持该文件格式",
selectFile: "请先选择需要导入的文件",
},
models: {
title: "模型配置",
+5 -1
View File
@@ -6,6 +6,7 @@ export const useUIStore = defineStore('ui', {
showKBEditorModal: false,
kbEditorMode: 'create' as 'create' | 'edit',
currentKBId: null as string | null,
kbEditorType: 'document' as 'document' | 'faq',
kbEditorInitialSection: null as string | null,
settingsInitialSection: null as string | null,
settingsInitialSubSection: null as string | null,
@@ -39,6 +40,7 @@ export const useUIStore = defineStore('ui', {
openKBSettings(kbId: string, initialSection?: string) {
this.currentKBId = kbId
this.kbEditorMode = 'edit'
this.kbEditorType = 'document'
this.kbEditorInitialSection = initialSection || null
this.showKBEditorModal = true
},
@@ -47,9 +49,10 @@ export const useUIStore = defineStore('ui', {
this.openKBSettings(kbId, initialSection)
},
openCreateKB() {
openCreateKB(type: 'document' | 'faq' = 'document') {
this.currentKBId = null
this.kbEditorMode = 'create'
this.kbEditorType = type
this.kbEditorInitialSection = null
this.showKBEditorModal = true
},
@@ -58,6 +61,7 @@ export const useUIStore = defineStore('ui', {
this.showKBEditorModal = false
this.currentKBId = null
this.kbEditorInitialSection = null
this.kbEditorType = 'document'
},
openManualEditor(options: {
+2 -2
View File
@@ -193,6 +193,6 @@ export function put(url: string, data = {}) {
return instance.put(url, data);
}
export function del(url: string) {
return instance.delete(url);
export function del(url: string, data?: any) {
return instance.delete(url, { data });
}
+126 -80
View File
@@ -15,12 +15,17 @@ const router = useRouter();
import {
batchQueryKnowledge,
listKnowledgeFiles,
getKnowledgeBaseById,
} from "@/api/knowledge-base/index";
import FAQEntryManager from './components/FAQEntryManager.vue';
import { formatStringDate } from "@/utils/index";
import { useI18n } from 'vue-i18n';
const route = useRoute();
const { t } = useI18n();
const kbId = computed(() => (route.params as any).kbId as string || '');
const kbInfo = ref<any>(null);
const kbLoading = ref(false);
const isFAQ = computed(() => (kbInfo.value?.type || '') === 'faq');
let { cardList, total, moreIndex, details, getKnowled, delKnowledge, openMore, onVisibleChange, getCardDetails, getfDetails } = useKnowledgeBase(kbId.value)
let isCardDetails = ref(false);
let timeout: ReturnType<typeof setInterval> | null = null;
@@ -76,10 +81,34 @@ const loadKnowledgeFiles = async (kbIdValue: string) => {
}
};
const loadKnowledgeBaseInfo = async (targetKbId: string) => {
if (!targetKbId) {
kbInfo.value = null;
return;
}
kbLoading.value = true;
try {
const res: any = await getKnowledgeBaseById(targetKbId);
kbInfo.value = res?.data || null;
if (!isFAQ.value) {
getKnowled({ page: 1, page_size: pageSize }, targetKbId);
loadKnowledgeFiles(targetKbId);
} else {
cardList.value = [];
total.value = 0;
}
} catch (error) {
console.error('Failed to load knowledge base info:', error);
kbInfo.value = null;
} finally {
kbLoading.value = false;
}
};
// 监听路由参数变化,重新获取知识库内容
watch(() => kbId.value, (newKbId, oldKbId) => {
if (newKbId && newKbId !== oldKbId) {
loadKnowledgeFiles(newKbId);
loadKnowledgeBaseInfo(newKbId);
}
}, { immediate: false });
@@ -87,7 +116,7 @@ watch(() => kbId.value, (newKbId, oldKbId) => {
const handleFileUploaded = (event: CustomEvent) => {
const uploadedKbId = event.detail.kbId;
console.log('接收到文件上传事件,上传的知识库ID:', uploadedKbId, '当前知识库ID:', kbId.value);
if (uploadedKbId && uploadedKbId === kbId.value) {
if (uploadedKbId && uploadedKbId === kbId.value && !isFAQ.value) {
console.log('匹配当前知识库,开始刷新文件列表');
// 如果上传的文件属于当前知识库,使用 loadKnowledgeFiles 刷新文件列表
loadKnowledgeFiles(uploadedKbId);
@@ -95,7 +124,7 @@ const handleFileUploaded = (event: CustomEvent) => {
};
onMounted(() => {
getKnowled({ page: 1, page_size: pageSize });
loadKnowledgeBaseInfo(kbId.value);
// 监听文件上传事件
window.addEventListener('knowledgeFileUploaded', handleFileUploaded as EventListener);
@@ -105,6 +134,7 @@ onUnmounted(() => {
window.removeEventListener('knowledgeFileUploaded', handleFileUploaded as EventListener);
});
watch(() => cardList.value, (newValue) => {
if (isFAQ.value) return;
let analyzeList = [];
analyzeList = newValue.filter(item => {
return item.parse_status == 'pending' || item.parse_status == 'processing';
@@ -172,12 +202,13 @@ const delCard = (index: number, item: KnowledgeCard) => {
};
const manualEditorSuccess = ({ kbId: savedKbId }: { kbId: string; knowledgeId: string; status: 'draft' | 'publish' }) => {
if (savedKbId === kbId.value) {
if (savedKbId === kbId.value && !isFAQ.value) {
loadKnowledgeFiles(savedKbId);
}
};
const handleManualEdit = (index: number, item: KnowledgeCard) => {
if (isFAQ.value) return;
if (cardList.value[index]) {
cardList.value[index].isMore = false;
}
@@ -190,6 +221,7 @@ const handleManualEdit = (index: number, item: KnowledgeCard) => {
};
const handleScroll = () => {
if (isFAQ.value) return;
const element = knowledgeScroll.value;
if (element) {
let pageNum = Math.ceil(total.value / pageSize)
@@ -256,95 +288,103 @@ async function createNewSession(value: string): Promise<void> {
</script>
<template>
<div v-show="cardList.length" class="knowledge-card-box" style="position: relative">
<div class="knowledge-card-wrap" ref="knowledgeScroll" @scroll="handleScroll">
<div class="knowledge-card" v-for="(item, index) in cardList" :key="index" @click="openCardDetails(item)">
<div class="card-content">
<div class="card-content-nav">
<span class="card-content-title">{{ item.file_name }}</span>
<t-popup v-model="item.isMore" overlayClassName="card-more"
:on-visible-change="onVisibleChange" trigger="click" destroy-on-close placement="bottom-right">
<div variant="outline" class="more-wrap" @click.stop="openMore(index)"
:class="[moreIndex == index ? 'active-more' : '']">
<img class="more" src="@/assets/img/more.png" alt="" />
</div>
<template #content>
<div class="card-menu">
<div
v-if="item.type === 'manual'"
class="card-menu-item"
@click.stop="handleManualEdit(index, item)"
>
<t-icon class="icon" name="edit" />
<span>{{ t('knowledgeBase.editDocument') }}</span>
</div>
<div
class="card-menu-item danger"
@click.stop="delCard(index, item)"
>
<t-icon class="icon" name="delete" />
<span>{{ t('knowledgeBase.deleteDocument') }}</span>
</div>
<template v-if="!isFAQ">
<div v-show="cardList.length" class="knowledge-card-box" style="position: relative">
<div class="knowledge-card-wrap" ref="knowledgeScroll" @scroll="handleScroll">
<div class="knowledge-card" v-for="(item, index) in cardList" :key="index" @click="openCardDetails(item)">
<div class="card-content">
<div class="card-content-nav">
<span class="card-content-title">{{ item.file_name }}</span>
<t-popup v-model="item.isMore" overlayClassName="card-more"
:on-visible-change="onVisibleChange" trigger="click" destroy-on-close placement="bottom-right">
<div variant="outline" class="more-wrap" @click.stop="openMore(index)"
:class="[moreIndex == index ? 'active-more' : '']">
<img class="more" src="@/assets/img/more.png" alt="" />
</div>
</template>
</t-popup>
<template #content>
<div class="card-menu">
<div
v-if="item.type === 'manual'"
class="card-menu-item"
@click.stop="handleManualEdit(index, item)"
>
<t-icon class="icon" name="edit" />
<span>{{ t('knowledgeBase.editDocument') }}</span>
</div>
<div
class="card-menu-item danger"
@click.stop="delCard(index, item)"
>
<t-icon class="icon" name="delete" />
<span>{{ t('knowledgeBase.deleteDocument') }}</span>
</div>
</div>
</template>
</t-popup>
</div>
<div
v-if="item.parse_status === 'processing' || item.parse_status === 'pending'"
class="card-analyze"
>
<t-icon name="loading" class="card-analyze-loading"></t-icon>
<span class="card-analyze-txt">{{ t('knowledgeBase.parsingInProgress') }}</span>
</div>
<div
v-else-if="item.parse_status === 'failed'"
class="card-analyze failure"
>
<t-icon name="close-circle" class="card-analyze-loading failure"></t-icon>
<span class="card-analyze-txt failure">{{ t('knowledgeBase.parsingFailed') }}</span>
</div>
<div v-else-if="item.parse_status === 'draft'" class="card-draft">
<t-tag size="small" theme="warning" variant="light-outline">{{ t('knowledgeBase.draft') }}</t-tag>
<span class="card-draft-tip">{{ t('knowledgeBase.draftTip') }}</span>
</div>
<div v-else-if="item.parse_status === 'completed'" class="card-content-txt">
{{ item.description }}
</div>
</div>
<div
v-if="item.parse_status === 'processing' || item.parse_status === 'pending'"
class="card-analyze"
>
<t-icon name="loading" class="card-analyze-loading"></t-icon>
<span class="card-analyze-txt">{{ t('knowledgeBase.parsingInProgress') }}</span>
</div>
<div
v-else-if="item.parse_status === 'failed'"
class="card-analyze failure"
>
<t-icon name="close-circle" class="card-analyze-loading failure"></t-icon>
<span class="card-analyze-txt failure">{{ t('knowledgeBase.parsingFailed') }}</span>
</div>
<div v-else-if="item.parse_status === 'draft'" class="card-draft">
<t-tag size="small" theme="warning" variant="light-outline">{{ t('knowledgeBase.draft') }}</t-tag>
<span class="card-draft-tip">{{ t('knowledgeBase.draftTip') }}</span>
</div>
<div v-else-if="item.parse_status === 'completed'" class="card-content-txt">
{{ item.description }}
<div class="card-bottom">
<span class="card-time">{{ item.updated_at }}</span>
<div class="card-type">
<span>{{ item.file_type }}</span>
</div>
</div>
</div>
<div class="card-bottom">
<span class="card-time">{{ item.updated_at }}</span>
<div class="card-type">
<span>{{ item.file_type }}</span>
<t-dialog v-model:visible="delDialog" dialogClassName="del-knowledge" :closeBtn="false" :cancelBtn="null"
:confirmBtn="null">
<div class="circle-wrap">
<div class="header">
<img class="circle-img" src="@/assets/img/circle.png" alt="">
<span class="circle-title">{{ t('knowledgeBase.deleteConfirmation') }}</span>
</div>
<span class="del-circle-txt">
{{ t('knowledgeBase.confirmDeleteDocument', { fileName: knowledge.file_name || '' }) }}
</span>
<div class="circle-btn">
<span class="circle-btn-txt" @click="delDialog = false">{{ t('common.cancel') }}</span>
<span class="circle-btn-txt confirm" @click="delCardConfirm">{{ t('knowledgeBase.confirmDelete') }}</span>
</div>
</div>
</div>
</t-dialog>
</div>
<t-dialog v-model:visible="delDialog" dialogClassName="del-knowledge" :closeBtn="false" :cancelBtn="null"
:confirmBtn="null">
<div class="circle-wrap">
<div class="header">
<img class="circle-img" src="@/assets/img/circle.png" alt="">
<span class="circle-title">{{ t('knowledgeBase.deleteConfirmation') }}</span>
</div>
<span class="del-circle-txt">
{{ t('knowledgeBase.confirmDeleteDocument', { fileName: knowledge.file_name || '' }) }}
</span>
<div class="circle-btn">
<span class="circle-btn-txt" @click="delDialog = false">{{ t('common.cancel') }}</span>
<span class="circle-btn-txt confirm" @click="delCardConfirm">{{ t('knowledgeBase.confirmDelete') }}</span>
</div>
</div>
</t-dialog>
<InputField @send-msg="sendMsg"></InputField>
<DocContent :visible="isCardDetails" :details="details" @closeDoc="closeDoc" @getDoc="getDoc"></DocContent>
</div>
<InputField @send-msg="sendMsg"></InputField>
<DocContent :visible="isCardDetails" :details="details" @closeDoc="closeDoc" @getDoc="getDoc"></DocContent>
</div>
<EmptyKnowledge v-show="!cardList.length"></EmptyKnowledge>
<EmptyKnowledge v-show="!cardList.length"></EmptyKnowledge>
</template>
<template v-else>
<div class="faq-manager-wrapper">
<FAQEntryManager v-if="kbId" :kb-id="kbId" />
</div>
</template>
<!-- 知识库编辑器(创建/编辑统一组件) -->
<KnowledgeBaseEditorModal
:visible="uiStore.showKBEditorModal"
:mode="uiStore.kbEditorMode"
:kb-id="uiStore.currentKBId || undefined"
:initial-type="uiStore.kbEditorType"
@update:visible="(val) => val ? null : uiStore.closeKBEditor()"
@success="handleKBEditorSuccess"
/>
@@ -369,6 +409,12 @@ async function createNewSession(value: string): Promise<void> {
flex: 1;
}
.faq-manager-wrapper {
flex: 1;
padding: 24px 44px;
overflow-y: auto;
}
@media (max-width: 1250px) and (min-width: 1045px) {
.answers-input {
transform: translateX(-329px);
@@ -40,6 +40,18 @@
<p class="section-desc">{{ $t('knowledgeEditor.basic.description') }}</p>
</div>
<div class="section-body">
<div class="form-item">
<label class="form-label required">{{ $t('knowledgeEditor.basic.typeLabel') }}</label>
<t-radio-group
v-model="formData.type"
:disabled="mode === 'edit'"
variant="default-filled"
>
<t-radio-button value="document">{{ $t('knowledgeEditor.basic.typeDocument') }}</t-radio-button>
<t-radio-button value="faq">{{ $t('knowledgeEditor.basic.typeFAQ') }}</t-radio-button>
</t-radio-group>
<p class="form-tip">{{ $t('knowledgeEditor.basic.typeDescription') }}</p>
</div>
<div class="form-item">
<label class="form-label required">{{ $t('knowledgeEditor.basic.nameLabel') }}</label>
<t-input
@@ -73,8 +85,45 @@
/>
</div>
<!-- FAQ 配置 -->
<div v-if="isFAQ && formData" v-show="currentSection === 'faq'" class="section">
<div class="section-content">
<div class="section-header">
<h3 class="section-title">{{ $t('knowledgeEditor.faq.title') }}</h3>
<p class="section-desc">{{ $t('knowledgeEditor.faq.description') }}</p>
</div>
<div class="section-body">
<div class="form-item">
<label class="form-label required">{{ $t('knowledgeEditor.faq.indexModeLabel') }}</label>
<t-radio-group
v-model="formData.faqConfig.indexMode"
variant="default-filled"
>
<t-radio-button value="question_only">{{ $t('knowledgeEditor.faq.modes.questionOnly') }}</t-radio-button>
<t-radio-button value="question_answer">{{ $t('knowledgeEditor.faq.modes.questionAnswer') }}</t-radio-button>
</t-radio-group>
<p class="form-tip">{{ $t('knowledgeEditor.faq.indexModeDescription') }}</p>
</div>
<div class="form-item">
<label class="form-label required">{{ $t('knowledgeEditor.faq.questionIndexModeLabel') }}</label>
<t-radio-group
v-model="formData.faqConfig.questionIndexMode"
variant="default-filled"
>
<t-radio-button value="combined">{{ $t('knowledgeEditor.faq.modes.combined') }}</t-radio-button>
<t-radio-button value="separate">{{ $t('knowledgeEditor.faq.modes.separate') }}</t-radio-button>
</t-radio-group>
<p class="form-tip">{{ $t('knowledgeEditor.faq.questionIndexModeDescription') }}</p>
</div>
<div class="faq-guide">
<p>{{ $t('knowledgeEditor.faq.entryGuide') }}</p>
</div>
</div>
</div>
</div>
<!-- 分块设置 -->
<div v-show="currentSection === 'chunking'" class="section">
<div v-if="!isFAQ" v-show="currentSection === 'chunking'" class="section">
<KBChunkingSettings
v-if="formData"
:config="formData.chunkingConfig"
@@ -83,7 +132,7 @@
</div>
<!-- 高级设置 -->
<div v-show="currentSection === 'advanced'" class="section">
<div v-if="!isFAQ" v-show="currentSection === 'advanced'" class="section">
<KBAdvancedSettings
ref="advancedSettingsRef"
v-if="formData"
@@ -133,6 +182,7 @@ const props = defineProps<{
visible: boolean
mode: 'create' | 'edit'
kbId?: string
initialType?: 'document' | 'faq'
}>()
// Emits
@@ -147,12 +197,21 @@ const loading = ref(false)
const allModels = ref<any[]>([])
const hasFiles = ref(false)
const navItems = computed(() => [
{ key: 'basic', icon: 'info-circle', label: t('knowledgeEditor.sidebar.basic') },
{ key: 'models', icon: 'control-platform', label: t('knowledgeEditor.sidebar.models') },
{ key: 'chunking', icon: 'file-copy', label: t('knowledgeEditor.sidebar.chunking') },
{ key: 'advanced', icon: 'setting', label: t('knowledgeEditor.sidebar.advanced') }
])
const navItems = computed(() => {
const items = [
{ key: 'basic', icon: 'info-circle', label: t('knowledgeEditor.sidebar.basic') },
{ key: 'models', icon: 'control-platform', label: t('knowledgeEditor.sidebar.models') }
]
if (formData.value?.type === 'faq') {
items.push({ key: 'faq', icon: 'help-circle', label: t('knowledgeEditor.sidebar.faq') })
} else {
items.push(
{ key: 'chunking', icon: 'file-copy', label: t('knowledgeEditor.sidebar.chunking') },
{ key: 'advanced', icon: 'setting', label: t('knowledgeEditor.sidebar.advanced') }
)
}
return items
})
// 模型配置引用
const modelConfigRef = ref<InstanceType<typeof KBModelConfig>>()
@@ -160,12 +219,34 @@ const advancedSettingsRef = ref<InstanceType<typeof KBAdvancedSettings>>()
// 表单数据
const formData = ref<any>(null)
const isFAQ = computed(() => formData.value?.type === 'faq')
watch(
() => formData.value?.type,
(newType, oldType) => {
if (!formData.value) return
if (newType === 'faq') {
if (!formData.value.faqConfig) {
formData.value.faqConfig = { indexMode: 'question_answer', questionIndexMode: 'combined' }
}
if (!['basic', 'models', 'faq'].includes(currentSection.value)) {
currentSection.value = 'faq'
}
} else if (oldType === 'faq' && currentSection.value === 'faq') {
currentSection.value = 'basic'
}
}
)
// 初始化表单数据
const initFormData = () => {
const initFormData = (type: 'document' | 'faq' = 'document') => {
return {
type,
name: '',
description: '',
faqConfig: {
indexMode: 'question_answer'
},
modelConfig: {
llmModelId: '',
embeddingModelId: '',
@@ -234,9 +315,15 @@ const loadKBData = async () => {
hasFiles.value = (filesResult as any)?.total > 0
// 设置表单数据
const kbType = (kb.type as 'document' | 'faq') || 'document'
formData.value = {
type: kbType,
name: kb.name || '',
description: kb.description || '',
faqConfig: {
indexMode: kb.faq_config?.index_mode || 'question_answer',
questionIndexMode: kb.faq_config?.question_index_mode || 'combined'
},
modelConfig: {
llmModelId: kb.summary_model_id || '',
embeddingModelId: kb.embedding_model_id || '',
@@ -339,6 +426,12 @@ const validateForm = (): boolean => {
}
}
if (formData.value.type === 'faq' && !formData.value.faqConfig?.indexMode) {
MessagePlugin.warning(t('knowledgeEditor.messages.indexModeRequired'))
currentSection.value = 'faq'
return false
}
return true
}
@@ -349,6 +442,7 @@ const buildSubmitData = () => {
const data: any = {
name: formData.value.name,
description: formData.value.description,
type: formData.value.type,
chunking_config: {
chunk_size: formData.value.chunkingConfig.chunkSize,
chunk_overlap: formData.value.chunkingConfig.chunkOverlap,
@@ -400,6 +494,13 @@ const buildSubmitData = () => {
}
}
if (formData.value.type === 'faq') {
data.faq_config = {
index_mode: formData.value.faqConfig?.indexMode || 'question_answer',
question_index_mode: formData.value.faqConfig?.questionIndexMode || 'combined'
}
}
return data
}
@@ -430,11 +531,18 @@ const handleSubmit = async () => {
throw new Error(t('knowledgeEditor.messages.missingId'))
}
// 1. 更新基本信息(名称、描述)
// 1. 更新基本信息(名称、描述)和 FAQ 配置
const updateConfig: any = {}
if (formData.value.type === 'faq' && formData.value.faqConfig) {
updateConfig.faq_config = {
index_mode: formData.value.faqConfig.indexMode || 'question_answer',
question_index_mode: formData.value.faqConfig.questionIndexMode || 'combined'
}
}
await updateKnowledgeBase(props.kbId, {
name: data.name,
description: data.description,
config: {} // 空配置,只更新基本信息
config: updateConfig
})
// 2. 更新完整配置(模型、分块、多模态、知识图谱等)
@@ -524,7 +632,7 @@ watch(() => props.visible, async (newVal) => {
await loadKBData()
} else {
// 创建模式:初始化空表单
formData.value = initFormData()
formData.value = initFormData(props.initialType || 'document')
hasFiles.value = false
}
} else {
@@ -740,6 +848,22 @@ watch(
}
}
.form-tip {
margin-top: 6px;
font-size: 12px;
color: #00000066;
}
.faq-guide {
margin-top: 20px;
padding: 12px 16px;
border-radius: 8px;
background: #f5f5f5;
color: #00000099;
font-size: 13px;
line-height: 20px;
}
.settings-footer {
padding: 16px 32px;
border-top: 1px solid #e5e5e5;
@@ -763,5 +887,48 @@ watch(
transform: scale(0.95);
}
}
// Radio-group 样式优化,符合项目主题风格
:deep(.t-radio-group) {
.t-radio-group--filled {
background: #f5f5f5;
}
.t-radio-button {
border-color: #d9d9d9;
// color: #00000099;
&:hover:not(.t-is-disabled) {
border-color: #07c05f;
color: #07c05f;
}
&.t-is-checked {
background: #07c05f;
border-color: #07c05f;
color: #fff;
&:hover:not(.t-is-disabled) {
background: #05a04f;
border-color: #05a04f;
color: #fff;
}
}
// 禁用状态样式
&.t-is-disabled {
background: #f5f5f5;
border-color: #d9d9d9;
color: #00000040;
cursor: not-allowed;
opacity: 0.6;
&.t-is-checked {
background: #f0f0f0;
border-color: #d9d9d9;
color: #00000066;
}
}
}
}
</style>
@@ -3,10 +3,12 @@
<!-- 头部 -->
<div class="header">
<h2>{{ $t('knowledgeBase.title') }}</h2>
<button class="create-btn" @click="openCreateModal">
<t-icon name="add" size="16px" class="btn-icon" />
<span>{{ $t('knowledgeList.create') }}</span>
</button>
<div class="action-buttons">
<button class="create-btn ghost" @click="openCreateModal">
<t-icon name="add" size="16px" class="btn-icon" />
<span>{{ $t('knowledgeList.create') }}</span>
</button>
</div>
</div>
<!-- 未初始化知识库提示 -->
@@ -67,9 +69,8 @@
<!-- 卡片底部 -->
<div class="card-bottom">
<div class="status-badge" :class="{ 'initialized': isInitialized(kb), 'uninitialized': !isInitialized(kb) }">
<span v-if="!isInitialized(kb)" class="warning-icon">⚠</span>
<span>{{ isInitialized(kb) ? $t('knowledgeBase.initializedStatus') : $t('knowledgeBase.notInitializedStatus') }}</span>
<div class="type-badge" :class="{ 'document': (kb.type || 'document') === 'document', 'faq': kb.type === 'faq' }">
<span>{{ kb.type === 'faq' ? $t('knowledgeEditor.basic.typeFAQ') : $t('knowledgeEditor.basic.typeDocument') }}</span>
</div>
<span class="card-time">{{ kb.updated_at }}</span>
</div>
@@ -112,6 +113,7 @@
:visible="uiStore.showKBEditorModal"
:mode="uiStore.kbEditorMode"
:kb-id="uiStore.currentKBId || undefined"
:initial-type="uiStore.kbEditorType"
@update:visible="(val) => val ? null : uiStore.closeKBEditor()"
@success="handleKBEditorSuccess"
/>
@@ -143,6 +145,7 @@ interface KB {
updated_at?: string;
embedding_model_id?: string;
summary_model_id?: string;
type?: 'document' | 'faq';
showMore?: boolean;
}
@@ -278,15 +281,19 @@ const handleKBEditorSuccess = (kbId: string) => {
}
}
.action-buttons {
display: flex;
gap: 12px;
align-items: center;
}
.create-btn {
display: flex;
align-items: center;
gap: 6px;
padding: 8px 20px;
height: 36px;
background: #07c05f;
color: #fff;
border: none;
border: 1px solid transparent;
border-radius: 8px;
font-family: "PingFang SC";
font-size: 14px;
@@ -294,16 +301,38 @@ const handleKBEditorSuccess = (kbId: string) => {
cursor: pointer;
transition: all 0.2s ease;
.btn-icon {
flex-shrink: 0;
}
}
.create-btn.primary {
background: #07c05f;
color: #fff;
border-color: #07c05f;
&:hover {
background: #05a04f;
border-color: #05a04f;
}
&:active {
background: #048f45;
border-color: #048f45;
}
}
.create-btn.ghost {
background: transparent;
color: #07c05f;
border-color: #07c05f;
&:hover {
background: #07c05f1a;
}
.btn-icon {
flex-shrink: 0;
&:active {
background: #07c05f33;
}
}
@@ -428,7 +457,7 @@ const handleKBEditorSuccess = (kbId: string) => {
margin-top: auto;
}
.status-badge {
.type-badge {
display: flex;
align-items: center;
gap: 3px;
@@ -438,18 +467,14 @@ const handleKBEditorSuccess = (kbId: string) => {
font-size: 12px;
font-weight: 500;
&.initialized {
&.document {
background: #07c05f1a;
color: #07c05f;
}
&.uninitialized {
background: #fff7e6;
color: #d46b08;
}
.warning-icon {
font-size: 12px;
&.faq {
background: #1890ff1a;
color: #1890ff;
}
}
File diff suppressed because it is too large Load Diff
+10 -4
View File
@@ -29,6 +29,7 @@ require (
github.com/spf13/viper v1.20.1
github.com/stretchr/testify v1.11.1
github.com/tencentyun/cos-go-sdk-v5 v0.7.65
github.com/xuri/excelize/v2 v2.10.0
github.com/yanyiwu/gojieba v1.4.5
go.opentelemetry.io/otel v1.37.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.37.0
@@ -37,7 +38,7 @@ require (
go.opentelemetry.io/otel/sdk v1.37.0
go.opentelemetry.io/otel/trace v1.37.0
go.uber.org/dig v1.18.1
golang.org/x/crypto v0.42.0
golang.org/x/crypto v0.43.0
golang.org/x/sync v0.17.0
google.golang.org/grpc v1.73.0
google.golang.org/protobuf v1.36.9
@@ -105,6 +106,8 @@ require (
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/richardlehane/mscfb v1.0.4 // indirect
github.com/richardlehane/msoleps v1.0.4 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/rs/xid v1.6.0 // indirect
@@ -114,18 +117,21 @@ require (
github.com/spf13/cast v1.10.0 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tiendc/go-deepcopy v1.7.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
github.com/xuri/efp v0.0.1 // indirect
github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9 // indirect
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/otel/metric v1.37.0 // indirect
go.opentelemetry.io/proto/otlp v1.7.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.15.0 // indirect
golang.org/x/net v0.43.0 // indirect
golang.org/x/sys v0.36.0 // indirect
golang.org/x/text v0.29.0 // indirect
golang.org/x/net v0.46.0 // indirect
golang.org/x/sys v0.37.0 // indirect
golang.org/x/text v0.30.0 // indirect
golang.org/x/time v0.13.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20250603155806-513f23925822 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250603155806-513f23925822 // indirect
+25 -10
View File
@@ -235,6 +235,11 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.14.0 h1:u4tNCjXOyzfgeLN+vAZaW1xUooqWDqVEsZN0U01jfAE=
github.com/redis/go-redis/v9 v9.14.0/go.mod h1:huWgSWd8mW6+m0VPhJjSSQ+d6Nh1VICQ6Q5lHuCH/Iw=
github.com/richardlehane/mscfb v1.0.4 h1:WULscsljNPConisD5hR0+OyZjwK46Pfyr6mPu5ZawpM=
github.com/richardlehane/mscfb v1.0.4/go.mod h1:YzVpcZg9czvAuhk9T+a3avCpcFPMUWm7gK3DypaEsUk=
github.com/richardlehane/msoleps v1.0.1/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg=
github.com/richardlehane/msoleps v1.0.4 h1:WuESlvhX3gH2IHcd8UqyCuFY5yiq/GR/yqaSM/9/g00=
github.com/richardlehane/msoleps v1.0.4/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
@@ -279,6 +284,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.563/go.mod
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.563/go.mod h1:uom4Nvi9W+Qkom0exYiJ9VWJjXwyxtPYTkKkaLMlfE0=
github.com/tencentyun/cos-go-sdk-v5 v0.7.65 h1:+WBbfwThfZSbxpf1Dw6fyMwyzVtWBBExqfDJ5giiR2s=
github.com/tencentyun/cos-go-sdk-v5 v0.7.65/go.mod h1:8+hG+mQMuRP/OIS9d83syAvXvrMj9HhkND6Q1fLghw0=
github.com/tiendc/go-deepcopy v1.7.1 h1:LnubftI6nYaaMOcaz0LphzwraqN8jiWTwm416sitff4=
github.com/tiendc/go-deepcopy v1.7.1/go.mod h1:4bKjNC2r7boYOkD2IOuZpYjmlDdzjbpTRyCx+goBCJQ=
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo=
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
@@ -303,6 +310,12 @@ github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/xuri/efp v0.0.1 h1:fws5Rv3myXyYni8uwj2qKjVaRP30PdjeYe2Y6FDsCL8=
github.com/xuri/efp v0.0.1/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
github.com/xuri/excelize/v2 v2.10.0 h1:8aKsP7JD39iKLc6dH5Tw3dgV3sPRh8uRVXu/fMstfW4=
github.com/xuri/excelize/v2 v2.10.0/go.mod h1:SC5TzhQkaOsTWpANfm+7bJCldzcnU/jrhqkTi/iBHBU=
github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9 h1:+C0TIdyyYmzadGaL/HBLbf3WdLgC29pgyhTjAT/0nuE=
github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
github.com/yanyiwu/gojieba v1.4.5 h1:VyZogGtdFSnJbACHvDRvDreXPPVPCg8axKFUdblU/JI=
github.com/yanyiwu/gojieba v1.4.5/go.mod h1:JUq4DddFVGdHXJHxxepxRmhrKlDpaBxR8O28v6fKYLY=
github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=
@@ -344,8 +357,10 @@ golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliY
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/crypto v0.42.0 h1:chiH31gIWm57EkTXpwnqf8qeuMUi0yekh6mT2AvFlqI=
golang.org/x/crypto v0.42.0/go.mod h1:4+rDnOTJhQCx2q7/j6rAN5XDw8kPjeaXEUR2eL94ix8=
golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04=
golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0=
golang.org/x/image v0.25.0 h1:Y6uW6rH1y5y/LK1J8BPWZtr6yZ7hrsy6hFrXjgsc2fQ=
golang.org/x/image v0.25.0/go.mod h1:tCAmOEGthTtkalusGp1g3xa2gke8J6c2N565dTyl9Rs=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
@@ -360,8 +375,8 @@ golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/net v0.43.0 h1:lat02VYK2j4aLzMzecihNvTlJNQUq316m2Mr9rnM6YE=
golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg=
golang.org/x/net v0.46.0 h1:giFlY12I07fugqwPuWJi68oOnpfqFnJIJzaIIm2JVV4=
golang.org/x/net v0.46.0/go.mod h1:Q9BGdFy1y4nkUwiLvT5qtyhAnEHgnQ/zd8PfU6nc210=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -384,8 +399,8 @@ golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
@@ -395,8 +410,8 @@ golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
golang.org/x/term v0.35.0 h1:bZBVKBudEyhRcajGcNc3jIfWPqV4y/Kt2XcoigOWtDQ=
golang.org/x/term v0.35.0/go.mod h1:TPGtkTLesOwf2DE8CgVYiZinHAOuy5AYUYT1lENIZnA=
golang.org/x/term v0.36.0 h1:zMPR+aF8gfksFprF/Nc/rd1wRS1EI6nDBGyWAvDzx2Q=
golang.org/x/term v0.36.0/go.mod h1:Qu394IJq6V6dCBRgwqshf3mPF85AqzYEzofzRdZkWss=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
@@ -406,8 +421,8 @@ golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4=
golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k=
golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM=
golang.org/x/time v0.13.0 h1:eUlYslOIt32DgYD6utsuUeHs4d7AsEYLuIAdg7FlYgI=
golang.org/x/time v0.13.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+6
View File
@@ -201,6 +201,12 @@ Your pretraining data may be outdated or incorrect. Do NOT rely on any internal
<Thinking_and_Planning>
- IMPORTANT: Unless the user question is trivially simple (e.g., directly confirming visible information), you MUST use the thinking tool to break down complex problems, track thinking progress iteratively, and adjust the approach when retrieved content changes or exceptions block the original workflow.
- IMPORTANT: Record your KB-first compliance in the thinking step: briefly list the attempted KB strategies and why they were insufficient before you switch to web_search.
- CRITICAL - todo_write Tool Usage: The todo_write tool is MANDATORY and MUST be used frequently throughout your workflow. You MUST:
- Create a todo list at the START of any multi-step task (3+ steps) or complex problem-solving session.
- Update the todo list IMMEDIATELY after completing each task item (mark as completed).
- Add new todo items when you discover additional steps are needed.
- Mark items as in_progress when you start working on them.
- Use todo_write proactively to organize and track your progress; do NOT skip this tool even if you think you can handle the task without it. Regular todo management is essential for maintaining clarity and ensuring all tasks are completed.
- For multi-turn conversations, examine prior retrieved evidence first; if it cannot answer the new question, plan and execute fresh retrieval before responding.
- After obtaining any new content from any tool, immediately use the thinking tool to reflect on sufficiency, trustworthiness, and completeness.
- Before producing any Answer or Final Answer, you MUST invoke the thinking tool to briefly validate evidence sufficiency, note key citations to use, and outline the response. Do not emit the Answer until this thinking step is completed.
+122 -45
View File
@@ -609,6 +609,7 @@ func (t *KnowledgeSearchTool) rerankResults(
}
// rerankWithLLM uses LLM prompt to score and rerank search results
// Uses batch processing to handle large result sets efficiently
func (t *KnowledgeSearchTool) rerankWithLLM(
ctx context.Context,
query string,
@@ -616,75 +617,151 @@ func (t *KnowledgeSearchTool) rerankWithLLM(
) ([]*searchResultWithMeta, error) {
logger.Infof(ctx, "[Tool][KnowledgeSearch] Using LLM for reranking %d results", len(results))
// Build prompt with query and all passages
var passagesBuilder strings.Builder
for i, result := range results {
passagesBuilder.WriteString(fmt.Sprintf("Passage %d:\n%s\n\n", i+1, result.Content))
if len(results) == 0 {
return results, nil
}
prompt := fmt.Sprintf(`你是一个相关性评分专家。请根据查询问题,对以下每个段落进行相关性评分。
// Batch size: process 15 results at a time to balance quality and token usage
// This prevents token overflow and improves processing efficiency
const batchSize = 15
const maxContentLength = 800 // Maximum characters per passage to avoid excessive tokens
查询问题:%s
// Process in batches
allScores := make([]float64, len(results))
allReranked := make([]*searchResultWithMeta, 0, len(results))
评分要求:
1. 评分范围:0.0 到 1.0 之间的小数
2. 1.0 表示完全相关,0.0 表示完全不相关
3. 考虑语义相关性、信息完整性、准确性等因素
4. 必须为每个段落返回一个评分
5. 输出格式:每行一个评分,格式为 "Passage N: X.XX"(N 是段落编号,X.XX 是 0.0-1.0 之间的分数)
for batchStart := 0; batchStart < len(results); batchStart += batchSize {
batchEnd := batchStart + batchSize
if batchEnd > len(results) {
batchEnd = len(results)
}
段落列表:
batch := results[batchStart:batchEnd]
logger.Debugf(ctx, "[Tool][KnowledgeSearch] Processing rerank batch %d-%d of %d results",
batchStart+1, batchEnd, len(results))
// Build prompt with query and batch passages
var passagesBuilder strings.Builder
for i, result := range batch {
// Truncate content if too long to save tokens
content := result.Content
if len([]rune(content)) > maxContentLength {
runes := []rune(content)
content = string(runes[:maxContentLength]) + "..."
}
// Use clear separators to distinguish each passage
if i > 0 {
passagesBuilder.WriteString("\n")
}
passagesBuilder.WriteString("─────────────────────────────────────────────────────────────\n")
passagesBuilder.WriteString(fmt.Sprintf("Passage %d:\n", i+1))
passagesBuilder.WriteString("─────────────────────────────────────────────────────────────\n")
passagesBuilder.WriteString(content + "\n")
}
// Optimized prompt focused on retrieval matching and reranking
prompt := fmt.Sprintf(`You are a search result reranking expert. Your task is to evaluate how well each retrieved passage matches the user's search query and information need.
User Query: %s
Your task: Rerank these search results by evaluating their retrieval relevance - how well each passage answers or relates to the query.
Scoring Criteria (0.0 to 1.0):
- 1.0 (0.9-1.0): Directly answers the query, contains key information needed, highly relevant
- 0.8 (0.7-0.8): Strongly related, provides substantial relevant information
- 0.6 (0.5-0.6): Moderately related, contains some relevant information but may be incomplete
- 0.4 (0.3-0.4): Weakly related, minimal relevance to the query
- 0.2 (0.1-0.2): Barely related, mostly irrelevant
- 0.0 (0.0): Completely irrelevant, no relation to the query
Evaluation Factors:
1. Query-Answer Match: Does the passage directly address what the user is asking?
2. Information Completeness: Does it provide sufficient information to answer the query?
3. Semantic Relevance: Does the content semantically relate to the query intent?
4. Key Term Coverage: Does it cover important terms/concepts from the query?
5. Information Accuracy: Is the information accurate and trustworthy?
Retrieved Passages:
%s
请严格按照以下格式返回每个段落的评分(每行一个,共 %d 个):
IMPORTANT: Return exactly %d scores, one per line, in this exact format:
Passage 1: X.XX
Passage 2: X.XX
Passage 3: X.XX
...
Passage %d: X.XX`, query, passagesBuilder.String(), len(results), len(results))
Passage %d: X.XX
messages := []chat.Message{
{
Role: "system",
Content: "你是一个专业的文本相关性评分专家,能够准确评估文本与查询问题的相关性。",
},
{
Role: "user",
Content: prompt,
},
}
Output only the scores, no explanations or additional text.`, query, passagesBuilder.String(), len(batch), len(batch))
response, err := t.chatModel.Chat(ctx, messages, &chat.ChatOptions{
Temperature: 0.1, // Low temperature for consistent scoring
MaxTokens: 1024,
})
if err != nil {
return nil, fmt.Errorf("LLM rerank call failed: %w", err)
}
messages := []chat.Message{
{
Role: "system",
Content: "You are a professional search result reranking expert specializing in information retrieval. You evaluate how well retrieved passages match user queries in search scenarios. Focus on retrieval relevance: whether the passage answers the query, provides needed information, and matches the user's information need. Always respond with scores only, no explanations.",
},
{
Role: "user",
Content: prompt,
},
}
// Parse scores from response
scores, err := t.parseScoresFromResponse(response.Content, len(results))
if err != nil {
logger.Warnf(ctx, "[Tool][KnowledgeSearch] Failed to parse LLM scores: %v, using original scores", err)
return results, nil // Return original results if parsing fails
// Calculate appropriate max tokens based on batch size
// Each score line is ~15 tokens, add buffer for safety
maxTokens := len(batch)*20 + 100
response, err := t.chatModel.Chat(ctx, messages, &chat.ChatOptions{
Temperature: 0.1, // Low temperature for consistent scoring
MaxTokens: maxTokens,
})
if err != nil {
logger.Warnf(ctx, "[Tool][KnowledgeSearch] LLM rerank batch %d-%d failed: %v, using original scores",
batchStart+1, batchEnd, err)
// Use original scores for this batch on error
for i := batchStart; i < batchEnd; i++ {
allScores[i] = results[i].Score
}
continue
}
logger.Infof(ctx, "[Tool][KnowledgeSearch] LLM rerank batch %d-%d response: %s",
batchStart+1, batchEnd, response.Content)
// Parse scores from response
batchScores, err := t.parseScoresFromResponse(response.Content, len(batch))
if err != nil {
logger.Warnf(ctx, "[Tool][KnowledgeSearch] Failed to parse LLM scores for batch %d-%d: %v, using original scores",
batchStart+1, batchEnd, err)
// Use original scores for this batch on parsing error
for i := batchStart; i < batchEnd; i++ {
allScores[i] = results[i].Score
}
continue
}
// Store scores for this batch
for i, score := range batchScores {
if batchStart+i < len(allScores) {
allScores[batchStart+i] = score
}
}
}
// Create reranked results with new scores
reranked := make([]*searchResultWithMeta, 0, len(results))
for i, result := range results {
newResult := *result
if i < len(scores) {
newResult.Score = scores[i]
if i < len(allScores) {
newResult.Score = allScores[i]
}
reranked = append(reranked, &newResult)
allReranked = append(allReranked, &newResult)
}
// Sort by new scores (descending)
sort.Slice(reranked, func(i, j int) bool {
return reranked[i].Score > reranked[j].Score
sort.Slice(allReranked, func(i, j int) bool {
return allReranked[i].Score > allReranked[j].Score
})
logger.Infof(ctx, "[Tool][KnowledgeSearch] LLM reranked %d results from %d original results", len(reranked), len(results))
return reranked, nil
logger.Infof(ctx, "[Tool][KnowledgeSearch] LLM reranked %d results from %d original results (processed in batches)",
len(allReranked), len(results))
return allReranked, nil
}
// parseScoresFromResponse parses scores from LLM response text
+5 -5
View File
@@ -25,7 +25,7 @@ func (r *chunkRepository) CreateChunks(ctx context.Context, chunks []*types.Chun
for _, chunk := range chunks {
chunk.Content = common.CleanInvalidUTF8(chunk.Content)
}
return r.db.WithContext(ctx).CreateInBatches(chunks, 100).Error
return r.db.Debug().WithContext(ctx).CreateInBatches(chunks, 100).Error
}
// GetChunkByID retrieves a chunk by its ID and tenant ID
@@ -45,7 +45,7 @@ func (r *chunkRepository) ListChunksByID(
ctx context.Context, tenantID uint, ids []string,
) ([]*types.Chunk, error) {
var chunks []*types.Chunk
if err := r.db.WithContext(ctx).
if err := r.db.Debug().WithContext(ctx).
Where("tenant_id = ? AND id IN ?", tenantID, ids).
Find(&chunks).Error; err != nil {
return nil, err
@@ -77,15 +77,15 @@ func (r *chunkRepository) ListPagedChunksByKnowledgeID(
// First query the total count
if err := r.db.WithContext(ctx).Model(&types.Chunk{}).
Where("tenant_id = ? AND knowledge_id = ?", tenantID, knowledgeID).
Where("tenant_id = ? AND knowledge_id = ? AND chunk_type IN (?)", tenantID, knowledgeID, chunk_type).
Count(&total).Error; err != nil {
return nil, 0, err
}
// Then query the paginated data
if err := r.db.WithContext(ctx).
Select("id, content, knowledge_id, knowledge_base_id, start_at, end_at, chunk_index, is_enabled, chunk_type, parent_chunk_id, image_info").
Where("tenant_id = ? AND knowledge_id = ? and chunk_type in (?)", tenantID, knowledgeID, chunk_type).
Select("id, content, knowledge_id, knowledge_base_id, start_at, end_at, chunk_index, is_enabled, chunk_type, parent_chunk_id, image_info, metadata").
Where("tenant_id = ? AND knowledge_id = ? AND chunk_type IN (?)", tenantID, knowledgeID, chunk_type).
Order("chunk_index ASC").
Offset(page.Offset()).
Limit(page.Limit()).
+662
View File
@@ -2021,6 +2021,668 @@ func (s *knowledgeService) CloneChunk(ctx context.Context, src, dst *types.Knowl
return nil
}
// ListFAQEntries lists FAQ entries under a FAQ knowledge base.
func (s *knowledgeService) ListFAQEntries(ctx context.Context,
kbID string, page *types.Pagination,
) (*types.PageResult, error) {
if page == nil {
page = &types.Pagination{}
}
kb, err := s.validateFAQKnowledgeBase(ctx, kbID)
if err != nil {
return nil, err
}
tenantID := ctx.Value(types.TenantIDContextKey).(uint)
faqKnowledge, err := s.findFAQKnowledge(ctx, tenantID, kb.ID)
if err != nil {
return nil, err
}
if faqKnowledge == nil {
return types.NewPageResult(0, page, []*types.FAQEntry{}), nil
}
chunkType := []types.ChunkType{types.ChunkTypeFAQ}
chunks, total, err := s.chunkRepo.ListPagedChunksByKnowledgeID(
ctx, tenantID, faqKnowledge.ID, page, chunkType,
)
if err != nil {
return nil, err
}
kb.EnsureDefaults()
entries := make([]*types.FAQEntry, 0, len(chunks))
for _, chunk := range chunks {
entry, err := s.chunkToFAQEntry(chunk, kb)
if err != nil {
return nil, err
}
entries = append(entries, entry)
}
return types.NewPageResult(total, page, entries), nil
}
// UpsertFAQEntries imports or appends FAQ entries.
func (s *knowledgeService) UpsertFAQEntries(ctx context.Context,
kbID string, payload *types.FAQBatchUpsertPayload,
) error {
if payload == nil || len(payload.Entries) == 0 {
return werrors.NewBadRequestError("FAQ 条目不能为空")
}
if payload.Mode == "" {
payload.Mode = types.FAQBatchModeAppend
}
if payload.Mode != types.FAQBatchModeAppend && payload.Mode != types.FAQBatchModeReplace {
return werrors.NewBadRequestError("模式仅支持 append 或 replace")
}
kb, err := s.validateFAQKnowledgeBase(ctx, kbID)
if err != nil {
return err
}
kb.EnsureDefaults()
tenantID := ctx.Value(types.TenantIDContextKey).(uint)
faqKnowledge, err := s.ensureFAQKnowledge(ctx, tenantID, kb)
if err != nil {
return err
}
if payload.Mode == types.FAQBatchModeReplace {
if err := s.cleanupFAQKnowledge(ctx, faqKnowledge); err != nil {
return err
}
}
startIndex := 0
if payload.Mode == types.FAQBatchModeAppend {
_, total, err := s.chunkRepo.ListPagedChunksByKnowledgeID(ctx,
tenantID,
faqKnowledge.ID,
&types.Pagination{Page: 1, PageSize: 1},
[]types.ChunkType{types.ChunkTypeFAQ},
)
if err != nil {
return err
}
startIndex = int(total)
}
// 获取索引模式
indexMode := types.FAQIndexModeQuestionOnly
if kb.FAQConfig != nil && kb.FAQConfig.IndexMode != "" {
indexMode = kb.FAQConfig.IndexMode
}
chunks := make([]*types.Chunk, 0, len(payload.Entries))
for idx := range payload.Entries {
entry := payload.Entries[idx]
meta, err := sanitizeFAQEntryPayload(&entry)
if err != nil {
return err
}
chunk := &types.Chunk{
ID: uuid.New().String(),
TenantID: tenantID,
KnowledgeID: faqKnowledge.ID,
KnowledgeBaseID: kb.ID,
Content: buildFAQChunkContent(meta, indexMode),
ChunkIndex: startIndex + idx + 1,
IsEnabled: true,
ChunkType: types.ChunkTypeFAQ,
}
if err := chunk.SetFAQMetadata(meta); err != nil {
return err
}
chunks = append(chunks, chunk)
}
if err := s.chunkService.CreateChunks(ctx, chunks); err != nil {
return err
}
if err := s.indexFAQChunks(ctx, kb, faqKnowledge, chunks, true); err != nil {
for _, chunk := range chunks {
_ = s.chunkService.DeleteChunk(ctx, chunk.ID)
}
return err
}
return nil
}
// UpdateFAQEntry updates a single FAQ entry.
func (s *knowledgeService) UpdateFAQEntry(ctx context.Context,
kbID string, entryID string, payload *types.FAQEntryPayload,
) error {
if payload == nil {
return werrors.NewBadRequestError("请求体不能为空")
}
kb, err := s.validateFAQKnowledgeBase(ctx, kbID)
if err != nil {
return err
}
kb.EnsureDefaults()
tenantID := ctx.Value(types.TenantIDContextKey).(uint)
chunk, err := s.chunkRepo.GetChunkByID(ctx, tenantID, entryID)
if err != nil {
return err
}
if chunk.KnowledgeBaseID != kb.ID {
return werrors.NewForbiddenError("无权操作该 FAQ 条目")
}
if chunk.ChunkType != types.ChunkTypeFAQ {
return werrors.NewBadRequestError("仅支持更新 FAQ 条目")
}
meta, err := sanitizeFAQEntryPayload(payload)
if err != nil {
return err
}
if existing, err := chunk.FAQMetadata(); err == nil && existing != nil {
meta.Version = existing.Version + 1
}
if err := chunk.SetFAQMetadata(meta); err != nil {
return err
}
// 获取索引模式
indexMode := types.FAQIndexModeQuestionOnly
if kb.FAQConfig != nil && kb.FAQConfig.IndexMode != "" {
indexMode = kb.FAQConfig.IndexMode
}
chunk.Content = buildFAQChunkContent(meta, indexMode)
chunk.UpdatedAt = time.Now()
if err := s.chunkService.UpdateChunk(ctx, chunk); err != nil {
return err
}
faqKnowledge, err := s.repo.GetKnowledgeByID(ctx, tenantID, chunk.KnowledgeID)
if err != nil {
return err
}
return s.indexFAQChunks(ctx, kb, faqKnowledge, []*types.Chunk{chunk}, false)
}
// SearchFAQEntries searches FAQ entries using hybrid search.
func (s *knowledgeService) SearchFAQEntries(ctx context.Context,
kbID string, req *types.FAQSearchRequest,
) ([]*types.FAQEntry, error) {
// Validate FAQ knowledge base
kb, err := s.validateFAQKnowledgeBase(ctx, kbID)
if err != nil {
return nil, err
}
// Set default values
if req.VectorThreshold <= 0 {
req.VectorThreshold = 0.7
}
if req.MatchCount <= 0 {
req.MatchCount = 10
}
if req.MatchCount > 50 {
req.MatchCount = 50
}
// Prepare search parameters
searchParams := types.SearchParams{
QueryText: req.QueryText,
VectorThreshold: req.VectorThreshold,
MatchCount: req.MatchCount,
DisableKeywordsMatch: true,
}
// Call HybridSearch
searchResults, err := s.kbService.HybridSearch(ctx, kbID, searchParams)
if err != nil {
return nil, err
}
if len(searchResults) == 0 {
return []*types.FAQEntry{}, nil
}
// Extract chunk IDs and build score/match type maps
chunkIDs := make([]string, 0, len(searchResults))
chunkScores := make(map[string]float64)
chunkMatchTypes := make(map[string]types.MatchType)
for _, result := range searchResults {
// SearchResult.ID is the chunk ID
chunkID := result.ID
chunkIDs = append(chunkIDs, chunkID)
chunkScores[chunkID] = result.Score
chunkMatchTypes[chunkID] = result.MatchType
}
// Batch fetch chunks
tenantID := ctx.Value(types.TenantIDContextKey).(uint)
chunks, err := s.chunkRepo.ListChunksByID(ctx, tenantID, chunkIDs)
if err != nil {
return nil, err
}
// Filter FAQ chunks and convert to FAQEntry
kb.EnsureDefaults()
entries := make([]*types.FAQEntry, 0, len(chunks))
for _, chunk := range chunks {
// Only process FAQ type chunks
if chunk.ChunkType != types.ChunkTypeFAQ {
continue
}
entry, err := s.chunkToFAQEntry(chunk, kb)
if err != nil {
logger.Warnf(ctx, "Failed to convert chunk to FAQ entry: %v", err)
continue
}
// Preserve score and match type from search results
// Note: Negative question filtering is now handled in HybridSearch
if score, ok := chunkScores[chunk.ID]; ok {
entry.Score = score
}
if matchType, ok := chunkMatchTypes[chunk.ID]; ok {
entry.MatchType = matchType
}
entries = append(entries, entry)
}
return entries, nil
}
// DeleteFAQEntries deletes FAQ entries in batch.
func (s *knowledgeService) DeleteFAQEntries(ctx context.Context,
kbID string, entryIDs []string,
) error {
if len(entryIDs) == 0 {
return werrors.NewBadRequestError("请选择需要删除的 FAQ 条目")
}
kb, err := s.validateFAQKnowledgeBase(ctx, kbID)
if err != nil {
return err
}
tenantID := ctx.Value(types.TenantIDContextKey).(uint)
var faqKnowledge *types.Knowledge
chunksToRemove := make([]*types.Chunk, 0, len(entryIDs))
for _, id := range entryIDs {
if id == "" {
continue
}
chunk, err := s.chunkRepo.GetChunkByID(ctx, tenantID, id)
if err != nil {
return err
}
if chunk.KnowledgeBaseID != kb.ID || chunk.ChunkType != types.ChunkTypeFAQ {
return werrors.NewBadRequestError("包含无效的 FAQ 条目")
}
if err := s.chunkService.DeleteChunk(ctx, id); err != nil {
return err
}
if faqKnowledge == nil {
faqKnowledge, err = s.repo.GetKnowledgeByID(ctx, tenantID, chunk.KnowledgeID)
if err != nil {
return err
}
}
chunksToRemove = append(chunksToRemove, chunk)
}
if len(chunksToRemove) > 0 && faqKnowledge != nil {
if err := s.deleteFAQChunkVectors(ctx, kb, faqKnowledge, chunksToRemove); err != nil {
return err
}
}
return nil
}
func (s *knowledgeService) validateFAQKnowledgeBase(ctx context.Context, kbID string) (*types.KnowledgeBase, error) {
if kbID == "" {
return nil, werrors.NewBadRequestError("知识库 ID 不能为空")
}
kb, err := s.kbService.GetKnowledgeBaseByID(ctx, kbID)
if err != nil {
return nil, err
}
kb.EnsureDefaults()
if kb.Type != types.KnowledgeBaseTypeFAQ {
return nil, werrors.NewBadRequestError("仅 FAQ 知识库支持该操作")
}
return kb, nil
}
func (s *knowledgeService) findFAQKnowledge(ctx context.Context, tenantID uint, kbID string) (*types.Knowledge, error) {
knowledges, err := s.repo.ListKnowledgeByKnowledgeBaseID(ctx, tenantID, kbID)
if err != nil {
return nil, err
}
for _, knowledge := range knowledges {
if knowledge.Type == types.KnowledgeTypeFAQ {
return knowledge, nil
}
}
return nil, nil
}
func (s *knowledgeService) ensureFAQKnowledge(ctx context.Context, tenantID uint, kb *types.KnowledgeBase) (*types.Knowledge, error) {
existing, err := s.findFAQKnowledge(ctx, tenantID, kb.ID)
if err != nil {
return nil, err
}
if existing != nil {
return existing, nil
}
knowledge := &types.Knowledge{
TenantID: tenantID,
KnowledgeBaseID: kb.ID,
Type: types.KnowledgeTypeFAQ,
Title: fmt.Sprintf("%s - FAQ", kb.Name),
Description: "FAQ 条目容器",
Source: types.KnowledgeTypeFAQ,
ParseStatus: "completed",
EnableStatus: "enabled",
EmbeddingModelID: kb.EmbeddingModelID,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
if err := s.repo.CreateKnowledge(ctx, knowledge); err != nil {
return nil, err
}
return knowledge, nil
}
func (s *knowledgeService) chunkToFAQEntry(chunk *types.Chunk, kb *types.KnowledgeBase) (*types.FAQEntry, error) {
meta, err := chunk.FAQMetadata()
if err != nil {
return nil, err
}
if meta == nil {
meta = &types.FAQChunkMetadata{StandardQuestion: chunk.Content}
}
entry := &types.FAQEntry{
ID: chunk.ID,
ChunkID: chunk.ID,
KnowledgeID: chunk.KnowledgeID,
KnowledgeBaseID: chunk.KnowledgeBaseID,
StandardQuestion: meta.StandardQuestion,
SimilarQuestions: meta.SimilarQuestions,
NegativeQuestions: meta.NegativeQuestions,
Answers: meta.Answers,
IndexMode: kb.FAQConfig.IndexMode,
UpdatedAt: chunk.UpdatedAt,
CreatedAt: chunk.CreatedAt,
ChunkType: chunk.ChunkType,
}
return entry, nil
}
func buildFAQChunkContent(meta *types.FAQChunkMetadata, mode types.FAQIndexMode) string {
var builder strings.Builder
builder.WriteString(fmt.Sprintf("Q: %s\n", meta.StandardQuestion))
if len(meta.SimilarQuestions) > 0 {
builder.WriteString("Similar Questions:\n")
for _, q := range meta.SimilarQuestions {
builder.WriteString(fmt.Sprintf("- %s\n", q))
}
}
// 负例不应该包含在 Content 中,因为它们不应该被索引
// 答案根据索引模式决定是否包含
if mode == types.FAQIndexModeQuestionAnswer && len(meta.Answers) > 0 {
builder.WriteString("Answers:\n")
for _, ans := range meta.Answers {
builder.WriteString(fmt.Sprintf("- %s\n", ans))
}
}
return builder.String()
}
func sanitizeFAQEntryPayload(payload *types.FAQEntryPayload) (*types.FAQChunkMetadata, error) {
meta := &types.FAQChunkMetadata{
StandardQuestion: strings.TrimSpace(payload.StandardQuestion),
SimilarQuestions: payload.SimilarQuestions,
NegativeQuestions: payload.NegativeQuestions,
Answers: payload.Answers,
Version: 1,
Source: "faq",
}
meta.Normalize()
if meta.StandardQuestion == "" {
return nil, werrors.NewBadRequestError("标准问不能为空")
}
if len(meta.Answers) == 0 {
return nil, werrors.NewBadRequestError("至少提供一个答案")
}
return meta, nil
}
func buildFAQIndexContent(meta *types.FAQChunkMetadata, mode types.FAQIndexMode) string {
var builder strings.Builder
builder.WriteString(meta.StandardQuestion)
for _, q := range meta.SimilarQuestions {
builder.WriteString("\n")
builder.WriteString(q)
}
if mode == types.FAQIndexModeQuestionAnswer {
for _, ans := range meta.Answers {
builder.WriteString("\n")
builder.WriteString(ans)
}
}
return builder.String()
}
// buildFAQIndexInfoList 构建FAQ索引信息列表,支持分别索引模式
func (s *knowledgeService) buildFAQIndexInfoList(ctx context.Context, kb *types.KnowledgeBase, chunk *types.Chunk) ([]*types.IndexInfo, error) {
indexMode := types.FAQIndexModeQuestionAnswer
questionIndexMode := types.FAQQuestionIndexModeCombined
if kb.FAQConfig != nil {
if kb.FAQConfig.IndexMode != "" {
indexMode = kb.FAQConfig.IndexMode
}
if kb.FAQConfig.QuestionIndexMode != "" {
questionIndexMode = kb.FAQConfig.QuestionIndexMode
}
}
logger.Infof(ctx, "buildFAQIndexInfoList: indexMode: %s, questionIndexMode: %s", indexMode, questionIndexMode)
meta, err := chunk.FAQMetadata()
if err != nil {
return nil, err
}
if meta == nil {
meta = &types.FAQChunkMetadata{StandardQuestion: chunk.Content}
}
// 如果是一起索引模式,使用原有逻辑
if questionIndexMode == types.FAQQuestionIndexModeCombined {
content := buildFAQIndexContent(meta, indexMode)
return []*types.IndexInfo{
{
Content: content,
SourceID: chunk.ID,
SourceType: types.ChunkSourceType,
ChunkID: chunk.ID,
KnowledgeID: chunk.KnowledgeID,
KnowledgeBaseID: chunk.KnowledgeBaseID,
},
}, nil
}
// 分别索引模式:为每个问题创建独立的索引项
indexInfoList := make([]*types.IndexInfo, 0)
// 标准问索引项
standardContent := meta.StandardQuestion
if indexMode == types.FAQIndexModeQuestionAnswer && len(meta.Answers) > 0 {
var builder strings.Builder
builder.WriteString(meta.StandardQuestion)
for _, ans := range meta.Answers {
builder.WriteString("\n")
builder.WriteString(ans)
}
standardContent = builder.String()
}
indexInfoList = append(indexInfoList, &types.IndexInfo{
Content: standardContent,
SourceID: chunk.ID,
SourceType: types.ChunkSourceType,
ChunkID: chunk.ID,
KnowledgeID: chunk.KnowledgeID,
KnowledgeBaseID: chunk.KnowledgeBaseID,
})
// 每个相似问创建一个索引项
for i, similarQ := range meta.SimilarQuestions {
similarContent := similarQ
if indexMode == types.FAQIndexModeQuestionAnswer && len(meta.Answers) > 0 {
var builder strings.Builder
builder.WriteString(similarQ)
for _, ans := range meta.Answers {
builder.WriteString("\n")
builder.WriteString(ans)
}
similarContent = builder.String()
}
sourceID := fmt.Sprintf("%s-%d", chunk.ID, i)
indexInfoList = append(indexInfoList, &types.IndexInfo{
Content: similarContent,
SourceID: sourceID,
SourceType: types.ChunkSourceType,
ChunkID: chunk.ID,
KnowledgeID: chunk.KnowledgeID,
KnowledgeBaseID: chunk.KnowledgeBaseID,
})
}
logger.Infof(ctx, "buildFAQIndexInfoList: indexInfoList: %v", indexInfoList)
return indexInfoList, nil
}
func (s *knowledgeService) indexFAQChunks(ctx context.Context,
kb *types.KnowledgeBase, knowledge *types.Knowledge, chunks []*types.Chunk, adjustStorage bool,
) error {
if len(chunks) == 0 {
return nil
}
embeddingModel, err := s.modelService.GetEmbeddingModel(ctx, kb.EmbeddingModelID)
if err != nil {
return err
}
tenantInfo := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
retrieveEngine, err := retriever.NewCompositeRetrieveEngine(s.retrieveEngine, tenantInfo.RetrieverEngines.Engines)
if err != nil {
return err
}
indexInfo := make([]*types.IndexInfo, 0)
chunkIDs := make([]string, 0, len(chunks))
for _, chunk := range chunks {
infoList, err := s.buildFAQIndexInfoList(ctx, kb, chunk)
if err != nil {
return err
}
indexInfo = append(indexInfo, infoList...)
chunkIDs = append(chunkIDs, chunk.ID)
}
var size int64
if adjustStorage {
size = retrieveEngine.EstimateStorageSize(ctx, embeddingModel, indexInfo)
if tenantInfo.StorageQuota > 0 && tenantInfo.StorageUsed+size > tenantInfo.StorageQuota {
return types.NewStorageQuotaExceededError()
}
}
if err := retrieveEngine.DeleteByChunkIDList(ctx, chunkIDs, embeddingModel.GetDimensions()); err != nil {
logger.Warnf(ctx, "Delete FAQ vectors failed: %v", err)
}
if err := retrieveEngine.BatchIndex(ctx, embeddingModel, indexInfo); err != nil {
return err
}
if adjustStorage && size > 0 {
if err := s.tenantRepo.AdjustStorageUsed(ctx, tenantInfo.ID, size); err == nil {
tenantInfo.StorageUsed += size
}
knowledge.StorageSize += size
}
now := time.Now()
knowledge.UpdatedAt = now
knowledge.ProcessedAt = &now
return s.repo.UpdateKnowledge(ctx, knowledge)
}
func (s *knowledgeService) deleteFAQChunkVectors(ctx context.Context,
kb *types.KnowledgeBase, knowledge *types.Knowledge, chunks []*types.Chunk,
) error {
if len(chunks) == 0 {
return nil
}
embeddingModel, err := s.modelService.GetEmbeddingModel(ctx, kb.EmbeddingModelID)
if err != nil {
return err
}
tenantInfo := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
retrieveEngine, err := retriever.NewCompositeRetrieveEngine(s.retrieveEngine, tenantInfo.RetrieverEngines.Engines)
if err != nil {
return err
}
indexInfo := make([]*types.IndexInfo, 0)
chunkIDs := make([]string, 0, len(chunks))
for _, chunk := range chunks {
infoList, err := s.buildFAQIndexInfoList(ctx, kb, chunk)
if err != nil {
return err
}
indexInfo = append(indexInfo, infoList...)
chunkIDs = append(chunkIDs, chunk.ID)
}
size := retrieveEngine.EstimateStorageSize(ctx, embeddingModel, indexInfo)
if err := retrieveEngine.DeleteByChunkIDList(ctx, chunkIDs, embeddingModel.GetDimensions()); err != nil {
return err
}
if size > 0 {
if err := s.tenantRepo.AdjustStorageUsed(ctx, tenantInfo.ID, -size); err == nil {
tenantInfo.StorageUsed -= size
if tenantInfo.StorageUsed < 0 {
tenantInfo.StorageUsed = 0
}
}
if knowledge.StorageSize >= size {
knowledge.StorageSize -= size
} else {
knowledge.StorageSize = 0
}
}
knowledge.UpdatedAt = time.Now()
return s.repo.UpdateKnowledge(ctx, knowledge)
}
func (s *knowledgeService) cleanupFAQKnowledge(ctx context.Context, knowledge *types.Knowledge) error {
tenantInfo := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
if knowledge.EmbeddingModelID != "" {
retrieveEngine, err := retriever.NewCompositeRetrieveEngine(s.retrieveEngine, tenantInfo.RetrieverEngines.Engines)
if err == nil {
if embeddingModel, modelErr := s.modelService.GetEmbeddingModel(ctx, knowledge.EmbeddingModelID); modelErr == nil {
_ = retrieveEngine.DeleteByKnowledgeIDList(ctx, []string{knowledge.ID}, embeddingModel.GetDimensions())
}
}
}
if err := s.chunkService.DeleteChunksByKnowledgeID(ctx, knowledge.ID); err != nil {
return err
}
if knowledge.StorageSize > 0 {
if err := s.tenantRepo.AdjustStorageUsed(ctx, tenantInfo.ID, -knowledge.StorageSize); err == nil {
tenantInfo.StorageUsed -= knowledge.StorageSize
if tenantInfo.StorageUsed < 0 {
tenantInfo.StorageUsed = 0
}
}
knowledge.StorageSize = 0
}
knowledge.UpdatedAt = time.Now()
knowledge.ProcessedAt = nil
return s.repo.UpdateKnowledge(ctx, knowledge)
}
func ensureManualFileName(title string) string {
if title == "" {
return fmt.Sprintf("manual-%s%s", time.Now().Format("20060102-150405"), manualFileExtension)
+265 -5
View File
@@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"slices"
"strings"
"time"
"github.com/Tencent/WeKnora/internal/application/service/retriever"
@@ -55,6 +56,7 @@ func (s *knowledgeBaseService) CreateKnowledgeBase(ctx context.Context,
kb.CreatedAt = time.Now()
kb.TenantID = ctx.Value(types.TenantIDContextKey).(uint)
kb.UpdatedAt = time.Now()
kb.EnsureDefaults()
logger.Infof(ctx, "Creating knowledge base, ID: %s, tenant ID: %d, name: %s", kb.ID, kb.TenantID, kb.Name)
@@ -87,6 +89,8 @@ func (s *knowledgeBaseService) GetKnowledgeBaseByID(ctx context.Context, id stri
return nil, err
}
kb.EnsureDefaults()
logger.Infof(ctx, "Knowledge base retrieved successfully, ID: %s, name: %s", kb.ID, kb.Name)
return kb, nil
}
@@ -98,6 +102,10 @@ func (s *knowledgeBaseService) ListKnowledgeBases(ctx context.Context) ([]*types
kbs, err := s.repo.ListKnowledgeBasesByTenantID(ctx, tenantID)
if err != nil {
for _, kb := range kbs {
kb.EnsureDefaults()
}
logger.ErrorWithFields(ctx, err, map[string]interface{}{
"tenant_id": tenantID,
})
@@ -141,7 +149,12 @@ func (s *knowledgeBaseService) UpdateKnowledgeBase(ctx context.Context,
kb.Description = description
kb.ChunkingConfig = config.ChunkingConfig
kb.ImageProcessingConfig = config.ImageProcessingConfig
// Update FAQ config if provided
if config.FAQConfig != nil {
kb.FAQConfig = config.FAQConfig
}
kb.UpdatedAt = time.Now()
kb.EnsureDefaults()
logger.Info(ctx, "Saving knowledge base update")
if err := s.repo.UpdateKnowledgeBase(ctx, kb); err != nil {
@@ -232,6 +245,7 @@ func (s *knowledgeBaseService) CopyKnowledgeBase(ctx context.Context,
logger.Errorf(ctx, "Get source knowledge base failed: %v", err)
return nil, nil, err
}
sourceKB.EnsureDefaults()
tenantID := ctx.Value(types.TenantIDContextKey).(uint)
var targetKB *types.KnowledgeBase
if dstKB != "" {
@@ -240,9 +254,15 @@ func (s *knowledgeBaseService) CopyKnowledgeBase(ctx context.Context,
return nil, nil, err
}
} else {
var faqConfig *types.FAQConfig
if sourceKB.FAQConfig != nil {
cfg := *sourceKB.FAQConfig
faqConfig = &cfg
}
targetKB = &types.KnowledgeBase{
ID: uuid.New().String(),
Name: sourceKB.Name,
Type: sourceKB.Type,
Description: sourceKB.Description,
TenantID: tenantID,
ChunkingConfig: sourceKB.ChunkingConfig,
@@ -252,7 +272,9 @@ func (s *knowledgeBaseService) CopyKnowledgeBase(ctx context.Context,
RerankModelID: sourceKB.RerankModelID,
VLMModelID: sourceKB.VLMModelID,
StorageConfig: sourceKB.StorageConfig,
FAQConfig: faqConfig,
}
targetKB.EnsureDefaults()
if err := s.repo.CreateKnowledgeBase(ctx, targetKB); err != nil {
return nil, nil, err
}
@@ -282,7 +304,7 @@ func (s *knowledgeBaseService) HybridSearch(ctx context.Context,
var kb *types.KnowledgeBase
// Add vector retrieval params if supported
if retrieveEngine.SupportRetriever(types.VectorRetrieverType) {
if retrieveEngine.SupportRetriever(types.VectorRetrieverType) && !params.DisableVectorMatch {
logger.Info(ctx, "Vector retrieval supported, preparing vector retrieval parameters")
kb, err = s.repo.GetKnowledgeBaseByID(ctx, id)
@@ -322,7 +344,7 @@ func (s *knowledgeBaseService) HybridSearch(ctx context.Context,
}
// Add keyword retrieval params if supported
if retrieveEngine.SupportRetriever(types.KeywordsRetrieverType) {
if retrieveEngine.SupportRetriever(types.KeywordsRetrieverType) && !params.DisableKeywordsMatch {
logger.Info(ctx, "Keyword retrieval supported, preparing keyword retrieval parameters")
retrieveParams = append(retrieveParams, types.RetrieveParams{
Query: params.QueryText,
@@ -367,15 +389,251 @@ func (s *knowledgeBaseService) HybridSearch(ctx context.Context,
logger.Info(ctx, "No search results found")
return nil, nil
}
// Deduplicate results by chunk ID
logger.Infof(ctx, "Result count before deduplication: %d", len(matchResults))
deduplicatedChunks := common.Deduplicate(func(r *types.IndexWithScore) string { return r.ChunkID }, matchResults...)
for _, chunk := range matchResults {
logger.Infof(ctx, "Before deduplication: Chunk: %s, Content: %s, Score: %f, MatchType: %d", chunk.ChunkID, chunk.Content, chunk.Score, chunk.MatchType)
}
// First, try standard deduplication
deduplicatedChunks := common.DeduplicateWithScore(func(r *types.IndexWithScore) string { return r.ChunkID }, matchResults...)
logger.Infof(ctx, "Result count after deduplication: %d", len(deduplicatedChunks))
for _, chunk := range deduplicatedChunks {
logger.Infof(ctx, "After deduplication: Chunk: %s, Content: %s, Score: %f, MatchType: %d", chunk.ChunkID, chunk.Content, chunk.Score, chunk.MatchType)
}
kb.EnsureDefaults()
// Check if we need iterative retrieval for FAQ with separate indexing
// Only use iterative retrieval if we don't have enough unique chunks after first deduplication
needsIterativeRetrieval := len(deduplicatedChunks) < params.MatchCount &&
kb.Type == types.KnowledgeBaseTypeFAQ &&
kb.FAQConfig != nil &&
kb.FAQConfig.QuestionIndexMode == types.FAQQuestionIndexModeSeparate
if needsIterativeRetrieval {
logger.Infof(ctx, "Not enough unique chunks (%d < %d), using iterative retrieval for FAQ with separate indexing",
len(deduplicatedChunks), params.MatchCount)
// Use iterative retrieval to get more unique chunks (with negative question filtering inside)
deduplicatedChunks = s.iterativeRetrieveWithDeduplication(ctx, retrieveEngine, retrieveParams, params.MatchCount, params.QueryText, kb.Type == types.KnowledgeBaseTypeFAQ)
} else if kb.Type == types.KnowledgeBaseTypeFAQ {
// Filter by negative questions if not using iterative retrieval
deduplicatedChunks = s.filterByNegativeQuestions(ctx, deduplicatedChunks, params.QueryText)
logger.Infof(ctx, "Result count after negative question filtering: %d", len(deduplicatedChunks))
}
// Limit to MatchCount
if len(deduplicatedChunks) > params.MatchCount {
deduplicatedChunks = deduplicatedChunks[:params.MatchCount]
}
return s.processSearchResults(ctx, deduplicatedChunks)
}
// iterativeRetrieveWithDeduplication performs iterative retrieval until enough unique chunks are found
// This is used for FAQ knowledge bases with separate indexing mode
// Negative question filtering is applied after each iteration to ensure we have enough valid chunks
func (s *knowledgeBaseService) iterativeRetrieveWithDeduplication(ctx context.Context,
retrieveEngine *retriever.CompositeRetrieveEngine,
retrieveParams []types.RetrieveParams,
matchCount int,
queryText string,
isFAQ bool,
) []*types.IndexWithScore {
maxIterations := 5
currentTopK := matchCount
uniqueChunks := make(map[string]*types.IndexWithScore)
for i := 0; i < maxIterations; i++ {
logger.Infof(ctx, "Iterative retrieval iteration %d, TopK: %d", i+1, currentTopK)
// Update TopK in retrieve params
updatedParams := make([]types.RetrieveParams, len(retrieveParams))
for j := range retrieveParams {
updatedParams[j] = retrieveParams[j]
updatedParams[j].TopK = currentTopK
}
// Execute retrieval
retrieveResults, err := retrieveEngine.Retrieve(ctx, updatedParams)
if err != nil {
logger.Warnf(ctx, "Iterative retrieval failed at iteration %d: %v", i+1, err)
break
}
// Collect results
iterationResults := []*types.IndexWithScore{}
for _, retrieveResult := range retrieveResults {
iterationResults = append(iterationResults, retrieveResult.Results...)
}
if len(iterationResults) == 0 {
logger.Infof(ctx, "No results found at iteration %d", i+1)
break
}
// Check if we got fewer results than requested - means no more results available
totalRetrieved := len(iterationResults)
if totalRetrieved < currentTopK {
logger.Infof(ctx, "Retrieved %d results (less than TopK %d), no more results available", totalRetrieved, currentTopK)
}
// Deduplicate and merge (keep highest score for each chunk)
// Multiple similar questions hitting the same chunk will keep the highest score
for _, result := range iterationResults {
if existing, ok := uniqueChunks[result.ChunkID]; !ok || result.Score > existing.Score {
uniqueChunks[result.ChunkID] = result
}
}
// Convert to slice for filtering
chunksSlice := make([]*types.IndexWithScore, 0, len(uniqueChunks))
for _, chunk := range uniqueChunks {
chunksSlice = append(chunksSlice, chunk)
}
// Filter by negative questions if this is a FAQ knowledge base
if isFAQ {
chunksSlice = s.filterByNegativeQuestions(ctx, chunksSlice, queryText)
// Update uniqueChunks map with filtered results
uniqueChunks = make(map[string]*types.IndexWithScore, len(chunksSlice))
for _, chunk := range chunksSlice {
uniqueChunks[chunk.ChunkID] = chunk
}
}
logger.Infof(ctx, "After iteration %d: retrieved %d results, found %d unique chunks after filtering (target: %d)",
i+1, totalRetrieved, len(uniqueChunks), matchCount)
// Early stop: Check if we have enough unique chunks after deduplication and filtering
if len(uniqueChunks) >= matchCount {
logger.Infof(ctx, "Found enough unique chunks after %d iterations", i+1)
break
}
// Early stop: If we got fewer results than TopK, there are no more results to retrieve
if totalRetrieved < currentTopK {
logger.Infof(ctx, "No more results available, stopping iteration")
break
}
// Increase TopK for next iteration
currentTopK *= 2
}
// Convert map to slice and sort by score
result := make([]*types.IndexWithScore, 0, len(uniqueChunks))
for _, chunk := range uniqueChunks {
result = append(result, chunk)
}
// Sort by score descending
slices.SortFunc(result, func(a, b *types.IndexWithScore) int {
if a.Score > b.Score {
return -1
} else if a.Score < b.Score {
return 1
}
return 0
})
logger.Infof(ctx, "Iterative retrieval completed: %d unique chunks found after filtering", len(result))
return result
}
// filterByNegativeQuestions filters out chunks that match negative questions for FAQ knowledge bases.
func (s *knowledgeBaseService) filterByNegativeQuestions(ctx context.Context,
chunks []*types.IndexWithScore,
queryText string,
) []*types.IndexWithScore {
if len(chunks) == 0 {
return chunks
}
queryTextLower := strings.ToLower(strings.TrimSpace(queryText))
if queryTextLower == "" {
return chunks
}
tenantID := ctx.Value(types.TenantIDContextKey).(uint)
// Collect chunk IDs
chunkIDs := make([]string, 0, len(chunks))
for _, chunk := range chunks {
chunkIDs = append(chunkIDs, chunk.ChunkID)
}
// Batch fetch chunks to get negative questions
allChunks, err := s.chunkRepo.ListChunksByID(ctx, tenantID, chunkIDs)
if err != nil {
logger.Warnf(ctx, "Failed to fetch chunks for negative question filtering: %v", err)
// If we can't fetch chunks, return original results
return chunks
}
// Build chunk map for quick lookup
chunkMap := make(map[string]*types.Chunk, len(allChunks))
for _, chunk := range allChunks {
chunkMap[chunk.ID] = chunk
}
// Filter out chunks that match negative questions
filteredChunks := make([]*types.IndexWithScore, 0, len(chunks))
for _, chunk := range chunks {
chunkData, ok := chunkMap[chunk.ChunkID]
if !ok {
// If chunk not found, keep it (shouldn't happen, but be safe)
filteredChunks = append(filteredChunks, chunk)
continue
}
// Only filter FAQ type chunks
if chunkData.ChunkType != types.ChunkTypeFAQ {
filteredChunks = append(filteredChunks, chunk)
continue
}
// Get FAQ metadata and check negative questions
meta, err := chunkData.FAQMetadata()
if err != nil || meta == nil {
// If we can't parse metadata, keep the chunk
filteredChunks = append(filteredChunks, chunk)
continue
}
// Check if query matches any negative question
if s.matchesNegativeQuestions(queryTextLower, meta.NegativeQuestions) {
logger.Debugf(ctx, "Filtered FAQ chunk %s due to negative question match", chunk.ChunkID)
continue
}
// Keep the chunk
filteredChunks = append(filteredChunks, chunk)
}
return filteredChunks
}
// matchesNegativeQuestions checks if the query text matches any negative questions.
// Returns true if the query matches any negative question, false otherwise.
func (s *knowledgeBaseService) matchesNegativeQuestions(queryTextLower string, negativeQuestions []string) bool {
if len(negativeQuestions) == 0 {
return false
}
for _, negativeQ := range negativeQuestions {
negativeQLower := strings.ToLower(strings.TrimSpace(negativeQ))
if negativeQLower == "" {
continue
}
// Check if query text is exactly the same as the negative question
if queryTextLower == negativeQLower {
return true
}
}
return false
}
// processSearchResults handles the processing of search results, optimizing database queries
func (s *knowledgeBaseService) processSearchResults(ctx context.Context,
chunks []*types.IndexWithScore,
@@ -422,6 +680,7 @@ func (s *knowledgeBaseService) processSearchResults(ctx context.Context,
})
return nil, err
}
logger.Infof(ctx, "Chunk data fetched successfully, count: %d", len(allChunks))
// Build chunk map and collect additional IDs to fetch
chunkMap := make(map[string]*types.Chunk, len(allChunks))
@@ -555,6 +814,7 @@ func (s *knowledgeBaseService) buildSearchResult(chunk *types.Chunk,
func (s *knowledgeBaseService) isValidTextChunk(chunk *types.Chunk) bool {
return slices.Contains([]types.ChunkType{
types.ChunkTypeText, types.ChunkTypeSummary,
types.ChunkTypeFAQ,
}, chunk.ChunkType)
}
@@ -51,7 +51,7 @@ func (v *KeywordsVectorHybridRetrieveEngineService) Index(ctx context.Context,
if err != nil {
return err
}
embeddingMap[indexInfo.ChunkID] = embedding
embeddingMap[indexInfo.SourceID] = embedding
}
params["embedding"] = embeddingMap
return v.indexRepository.Save(ctx, indexInfo, params)
+4 -2
View File
@@ -645,14 +645,16 @@ func (s *sessionService) KnowledgeQAByEvent(ctx context.Context,
fallbackID := generateEventID("fallback")
if err := chatManage.EventBus.Emit(ctx, types.Event{
ID: fallbackID,
Type: types.EventType(event.EventAgentThought),
Type: types.EventType(event.EventAgentFinalAnswer),
SessionID: chatManage.SessionID,
Data: event.AgentThoughtData{
Data: event.AgentFinalAnswerData{
Content: chatManage.FallbackResponse,
Done: true,
},
}); err != nil {
logger.Errorf(ctx, "Failed to emit fallback answer event: %v", err)
} else {
logger.Infof(ctx, "Fallback answer event emitted successfully")
}
}
return nil
+34
View File
@@ -49,6 +49,40 @@ func Deduplicate[T any, K comparable](keyFunc func(T) K, items ...T) []T {
return slices.Collect(maps.Values(seen))
}
// ScoreComparable is an interface for types that have a Score method returning float64
type ScoreComparable interface {
GetScore() float64
}
// DeduplicateWithScore removes duplicates from a slice based on a key function,
// keeping the item with the highest score for each key, then sorts by score descending
// T: the type of elements in the slice (must implement ScoreComparable)
// K: the type of key used for deduplication
func DeduplicateWithScore[T ScoreComparable, K comparable](keyFunc func(T) K, items ...T) []T {
seen := make(map[K]T)
for _, item := range items {
key := keyFunc(item)
if existing, exists := seen[key]; !exists {
seen[key] = item
} else if item.GetScore() > existing.GetScore() {
seen[key] = item
}
}
result := slices.Collect(maps.Values(seen))
// Sort by score descending
slices.SortFunc(result, func(a, b T) int {
scoreA := a.GetScore()
scoreB := b.GetScore()
if scoreA > scoreB {
return -1
} else if scoreA < scoreB {
return 1
}
return 0
})
return result
}
// ParseLLMJsonResponse parses a JSON response from LLM, handling cases where JSON is wrapped in code blocks.
// This is useful when LLMs return responses like:
// ```json
+1
View File
@@ -148,6 +148,7 @@ func BuildContainer(container *dig.Container) *dig.Container {
must(container.Provide(handler.NewKnowledgeBaseHandler))
must(container.Provide(handler.NewKnowledgeHandler))
must(container.Provide(handler.NewChunkHandler))
must(container.Provide(handler.NewFAQHandler))
must(container.Provide(session.NewHandler))
must(container.Provide(handler.NewMessageHandler))
must(container.Provide(handler.NewModelHandler))
+135
View File
@@ -0,0 +1,135 @@
package handler
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/Tencent/WeKnora/internal/errors"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
)
// FAQHandler handles FAQ knowledge base operations.
type FAQHandler struct {
knowledgeService interfaces.KnowledgeService
}
// NewFAQHandler creates a new FAQ handler.
func NewFAQHandler(knowledgeService interfaces.KnowledgeService) *FAQHandler {
return &FAQHandler{knowledgeService: knowledgeService}
}
// ListEntries lists FAQ entries under a knowledge base.
func (h *FAQHandler) ListEntries(c *gin.Context) {
ctx := c.Request.Context()
var page types.Pagination
if err := c.ShouldBindQuery(&page); err != nil {
logger.Error(ctx, "Failed to bind pagination query", err)
c.Error(errors.NewBadRequestError("分页参数不合法").WithDetails(err.Error()))
return
}
result, err := h.knowledgeService.ListFAQEntries(ctx, c.Param("id"), &page)
if err != nil {
logger.ErrorWithFields(ctx, err, nil)
c.Error(err)
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": result,
})
}
// UpsertEntries appends or replaces FAQ entries in batch.
func (h *FAQHandler) UpsertEntries(c *gin.Context) {
ctx := c.Request.Context()
var req types.FAQBatchUpsertPayload
if err := c.ShouldBindJSON(&req); err != nil {
logger.Error(ctx, "Failed to bind FAQ upsert payload", err)
c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
return
}
if err := h.knowledgeService.UpsertFAQEntries(ctx, c.Param("id"), &req); err != nil {
logger.ErrorWithFields(ctx, err, nil)
c.Error(err)
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
})
}
// UpdateEntry updates a single FAQ entry.
func (h *FAQHandler) UpdateEntry(c *gin.Context) {
ctx := c.Request.Context()
var req types.FAQEntryPayload
if err := c.ShouldBindJSON(&req); err != nil {
logger.Error(ctx, "Failed to bind FAQ entry payload", err)
c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
return
}
if err := h.knowledgeService.UpdateFAQEntry(ctx, c.Param("id"), c.Param("entry_id"), &req); err != nil {
logger.ErrorWithFields(ctx, err, nil)
c.Error(err)
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
})
}
type faqDeleteRequest struct {
IDs []string `json:"ids" binding:"required,min=1,dive,required"`
}
// DeleteEntries deletes FAQ entries in batch.
func (h *FAQHandler) DeleteEntries(c *gin.Context) {
ctx := c.Request.Context()
var req faqDeleteRequest
if err := c.ShouldBindJSON(&req); err != nil {
logger.Error(ctx, "Failed to bind FAQ delete payload", err)
c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
return
}
if err := h.knowledgeService.DeleteFAQEntries(ctx, c.Param("id"), req.IDs); err != nil {
logger.ErrorWithFields(ctx, err, nil)
c.Error(err)
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
})
}
// SearchFAQ searches FAQ entries using hybrid search.
func (h *FAQHandler) SearchFAQ(c *gin.Context) {
ctx := c.Request.Context()
var req types.FAQSearchRequest
if err := c.ShouldBindJSON(&req); err != nil {
logger.Error(ctx, "Failed to bind FAQ search payload", err)
c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
return
}
entries, err := h.knowledgeService.SearchFAQEntries(ctx, c.Param("id"), &req)
if err != nil {
logger.ErrorWithFields(ctx, err, nil)
c.Error(err)
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": entries,
})
}
+17
View File
@@ -41,6 +41,7 @@ type RouterParams struct {
SystemHandler *handler.SystemHandler
MCPServiceHandler *handler.MCPServiceHandler
WebSearchHandler *handler.WebSearchHandler
FAQHandler *handler.FAQHandler
}
// NewRouter 创建新的路由
@@ -79,6 +80,7 @@ func NewRouter(params RouterParams) *gin.Engine {
RegisterTenantRoutes(v1, params.TenantHandler)
RegisterKnowledgeBaseRoutes(v1, params.KBHandler)
RegisterKnowledgeRoutes(v1, params.KnowledgeHandler)
RegisterFAQRoutes(v1, params.FAQHandler)
RegisterChunkRoutes(v1, params.ChunkHandler)
RegisterSessionRoutes(v1, params.SessionHandler)
RegisterChatRoutes(v1, params.SessionHandler)
@@ -147,6 +149,21 @@ func RegisterKnowledgeRoutes(r *gin.RouterGroup, handler *handler.KnowledgeHandl
}
}
// RegisterFAQRoutes 注册 FAQ 相关路由
func RegisterFAQRoutes(r *gin.RouterGroup, handler *handler.FAQHandler) {
if handler == nil {
return
}
faq := r.Group("/knowledge-bases/:id/faq")
{
faq.GET("/entries", handler.ListEntries)
faq.POST("/entries", handler.UpsertEntries)
faq.PUT("/entries/:entry_id", handler.UpdateEntry)
faq.DELETE("/entries", handler.DeleteEntries)
faq.POST("/search", handler.SearchFAQ)
}
}
// RegisterKnowledgeBaseRoutes 注册知识库相关的路由
func RegisterKnowledgeBaseRoutes(r *gin.RouterGroup, handler *handler.KnowledgeBaseHandler) {
// 知识库路由组
+4
View File
@@ -24,6 +24,8 @@ const (
ChunkTypeEntity ChunkType = "entity"
// ChunkTypeRelationship 表示关系类型的 Chunk
ChunkTypeRelationship ChunkType = "relationship"
// ChunkTypeFAQ 表示 FAQ 条目 Chunk
ChunkTypeFAQ ChunkType = "faq"
)
// ImageInfo 表示与 Chunk 关联的图片信息
@@ -79,6 +81,8 @@ type Chunk struct {
RelationChunks JSON `json:"relation_chunks" gorm:"type:json"`
// 间接关系 Chunk ID,用于关联间接关系 Chunk 和原始文本 Chunk
IndirectRelationChunks JSON `json:"indirect_relation_chunks" gorm:"type:json"`
// Metadata 存储 chunk 级别的扩展信息,例如 FAQ 元数据
Metadata JSON `json:"metadata" gorm:"type:json"`
// 图片信息,存储为 JSON
ImageInfo string `json:"image_info" gorm:"type:text"`
// Chunk creation time
+130
View File
@@ -0,0 +1,130 @@
package types
import (
"encoding/json"
"strings"
"time"
)
// FAQChunkMetadata 定义 FAQ 条目在 Chunk.Metadata 中的结构
type FAQChunkMetadata struct {
StandardQuestion string `json:"standard_question"`
SimilarQuestions []string `json:"similar_questions,omitempty"`
NegativeQuestions []string `json:"negative_questions,omitempty"`
Answers []string `json:"answers,omitempty"`
Version int `json:"version,omitempty"`
Source string `json:"source,omitempty"`
}
// Normalize 清理空白与重复项
func (m *FAQChunkMetadata) Normalize() {
if m == nil {
return
}
m.StandardQuestion = strings.TrimSpace(m.StandardQuestion)
m.SimilarQuestions = normalizeStrings(m.SimilarQuestions)
m.NegativeQuestions = normalizeStrings(m.NegativeQuestions)
m.Answers = normalizeStrings(m.Answers)
if m.Version <= 0 {
m.Version = 1
}
}
// FAQMetadata 解析 Chunk 中的 FAQ 元数据
func (c *Chunk) FAQMetadata() (*FAQChunkMetadata, error) {
if c == nil || len(c.Metadata) == 0 {
return nil, nil
}
var meta FAQChunkMetadata
if err := json.Unmarshal(c.Metadata, &meta); err != nil {
return nil, err
}
meta.Normalize()
return &meta, nil
}
// SetFAQMetadata 设置 Chunk 的 FAQ 元数据
func (c *Chunk) SetFAQMetadata(meta *FAQChunkMetadata) error {
if c == nil {
return nil
}
if meta == nil {
c.Metadata = nil
return nil
}
meta.Normalize()
bytes, err := json.Marshal(meta)
if err != nil {
return err
}
c.Metadata = JSON(bytes)
return nil
}
// FAQEntry 表示返回给前端的 FAQ 条目
type FAQEntry struct {
ID string `json:"id"`
ChunkID string `json:"chunk_id"`
KnowledgeID string `json:"knowledge_id"`
KnowledgeBaseID string `json:"knowledge_base_id"`
StandardQuestion string `json:"standard_question"`
SimilarQuestions []string `json:"similar_questions"`
NegativeQuestions []string `json:"negative_questions"`
Answers []string `json:"answers"`
IndexMode FAQIndexMode `json:"index_mode"`
UpdatedAt time.Time `json:"updated_at"`
CreatedAt time.Time `json:"created_at"`
Score float64 `json:"score,omitempty"`
MatchType MatchType `json:"match_type,omitempty"`
ChunkType ChunkType `json:"chunk_type"`
}
// FAQEntryPayload 用于创建/更新 FAQ 条目的 payload
type FAQEntryPayload struct {
StandardQuestion string `json:"standard_question" binding:"required"`
SimilarQuestions []string `json:"similar_questions"`
NegativeQuestions []string `json:"negative_questions"`
Answers []string `json:"answers" binding:"required"`
}
const (
FAQBatchModeAppend = "append"
FAQBatchModeReplace = "replace"
)
// FAQBatchUpsertPayload 批量导入 FAQ 条目
type FAQBatchUpsertPayload struct {
Entries []FAQEntryPayload `json:"entries" binding:"required"`
Mode string `json:"mode" binding:"oneof=append replace"`
KnowledgeID string `json:"knowledge_id"`
}
// FAQSearchRequest FAQ检索请求参数
type FAQSearchRequest struct {
QueryText string `json:"query_text" binding:"required"`
VectorThreshold float64 `json:"vector_threshold"`
MatchCount int `json:"match_count"`
}
func normalizeStrings(values []string) []string {
if len(values) == 0 {
return nil
}
dedup := make([]string, 0, len(values))
seen := make(map[string]struct{}, len(values))
for _, v := range values {
trimmed := strings.TrimSpace(v)
if trimmed == "" {
continue
}
if _, exists := seen[trimmed]; exists {
continue
}
seen[trimmed] = struct{}{}
dedup = append(dedup, trimmed)
}
if len(dedup) == 0 {
return nil
}
return dedup
}
+10
View File
@@ -50,6 +50,16 @@ type KnowledgeService interface {
CloneKnowledgeBase(ctx context.Context, srcID, dstID string) error
// UpdateImageInfo updates image information for a knowledge chunk.
UpdateImageInfo(ctx context.Context, knowledgeID string, chunkID string, imageInfo string) error
// ListFAQEntries lists FAQ entries under a FAQ knowledge base.
ListFAQEntries(ctx context.Context, kbID string, page *types.Pagination) (*types.PageResult, error)
// UpsertFAQEntries imports or appends FAQ entries.
UpsertFAQEntries(ctx context.Context, kbID string, payload *types.FAQBatchUpsertPayload) error
// UpdateFAQEntry updates a single FAQ entry.
UpdateFAQEntry(ctx context.Context, kbID string, entryID string, payload *types.FAQEntryPayload) error
// DeleteFAQEntries deletes FAQ entries in batch.
DeleteFAQEntries(ctx context.Context, kbID string, entryIDs []string) error
// SearchFAQEntries searches FAQ entries using hybrid search.
SearchFAQEntries(ctx context.Context, kbID string, req *types.FAQSearchRequest) ([]*types.FAQEntry, error)
}
// KnowledgeRepository defines the interface for knowledge repositories.
+1
View File
@@ -11,6 +11,7 @@ import (
const (
KnowledgeTypeManual = "manual"
KnowledgeTypeFAQ = "faq"
)
const (
+81
View File
@@ -12,12 +12,39 @@ const (
InitDefaultKnowledgeBaseID = "kb-00000001"
)
const (
KnowledgeBaseTypeDocument = "document"
KnowledgeBaseTypeFAQ = "faq"
)
// FAQIndexMode 表示 FAQ 索引策略:仅索引标准问还是同时索引问答内容
type FAQIndexMode string
const (
// FAQIndexModeQuestionOnly 仅使用标准问及相似问向量化
FAQIndexModeQuestionOnly FAQIndexMode = "question_only"
// FAQIndexModeQuestionAnswer 将标准问与答案拼接后向量化
FAQIndexModeQuestionAnswer FAQIndexMode = "question_answer"
)
// FAQQuestionIndexMode 表示 FAQ 问题索引方式:一起索引还是分别索引
type FAQQuestionIndexMode string
const (
// FAQQuestionIndexModeCombined 将标准问和相似问一起索引(当前方式)
FAQQuestionIndexModeCombined FAQQuestionIndexMode = "combined"
// FAQQuestionIndexModeSeparate 将标准问和相似问分别索引
FAQQuestionIndexModeSeparate FAQQuestionIndexMode = "separate"
)
// KnowledgeBase represents a knowledge base
type KnowledgeBase struct {
// Unique identifier of the knowledge base
ID string `yaml:"id" json:"id" gorm:"type:varchar(36);primaryKey"`
// Name of the knowledge base
Name string `yaml:"name" json:"name"`
// Type of the knowledge base (document, faq, etc.)
Type string `yaml:"type" json:"type" gorm:"type:varchar(32);default:'document'"`
// Whether this knowledge base is temporary (ephemeral) and should be hidden from UI
IsTemporary bool `yaml:"is_temporary" json:"is_temporary" gorm:"default:false"`
// Description of the knowledge base
@@ -42,6 +69,8 @@ type KnowledgeBase struct {
StorageConfig StorageConfig `yaml:"cos_config" json:"cos_config" gorm:"column:cos_config;type:json"`
// Extract config
ExtractConfig *ExtractConfig `yaml:"extract_config" json:"extract_config" gorm:"column:extract_config;type:json"`
// FAQConfig stores FAQ specific configuration such as indexing strategy
FAQConfig *FAQConfig `yaml:"faq_config" json:"faq_config" gorm:"column:faq_config;type:json"`
// Creation time of the knowledge base
CreatedAt time.Time `yaml:"created_at" json:"created_at"`
// Last updated time of the knowledge base
@@ -56,6 +85,8 @@ type KnowledgeBaseConfig struct {
ChunkingConfig ChunkingConfig `yaml:"chunking_config" json:"chunking_config"`
// Image processing configuration
ImageProcessingConfig ImageProcessingConfig `yaml:"image_processing_config" json:"image_processing_config"`
// FAQ configuration (only for FAQ type knowledge bases)
FAQConfig *FAQConfig `yaml:"faq_config" json:"faq_config"`
}
// ChunkingConfig represents the document splitting configuration
@@ -195,3 +226,53 @@ func (e *ExtractConfig) Scan(value interface{}) error {
}
return json.Unmarshal(b, e)
}
// FAQConfig 存储 FAQ 知识库的特有配置
type FAQConfig struct {
IndexMode FAQIndexMode `yaml:"index_mode" json:"index_mode"`
QuestionIndexMode FAQQuestionIndexMode `yaml:"question_index_mode" json:"question_index_mode"`
}
// Value implements driver.Valuer
func (f FAQConfig) Value() (driver.Value, error) {
return json.Marshal(f)
}
// Scan implements sql.Scanner
func (f *FAQConfig) Scan(value interface{}) error {
if value == nil {
return nil
}
b, ok := value.([]byte)
if !ok {
return nil
}
return json.Unmarshal(b, f)
}
// EnsureDefaults 确保类型与配置具备默认值
func (kb *KnowledgeBase) EnsureDefaults() {
if kb == nil {
return
}
if kb.Type == "" {
kb.Type = KnowledgeBaseTypeDocument
}
if kb.Type != KnowledgeBaseTypeFAQ {
kb.FAQConfig = nil
return
}
if kb.FAQConfig == nil {
kb.FAQConfig = &FAQConfig{
IndexMode: FAQIndexModeQuestionAnswer,
QuestionIndexMode: FAQQuestionIndexModeCombined,
}
return
}
if kb.FAQConfig.IndexMode == "" {
kb.FAQConfig.IndexMode = FAQIndexModeQuestionAnswer
}
if kb.FAQConfig.QuestionIndexMode == "" {
kb.FAQConfig.QuestionIndexMode = FAQQuestionIndexModeCombined
}
}
+5
View File
@@ -73,6 +73,11 @@ type IndexWithScore struct {
MatchType MatchType
}
// GetScore returns the score for ScoreComparable interface
func (i *IndexWithScore) GetScore() float64 {
return i.Score
}
// RetrieveResult represents the result of retrieval
type RetrieveResult struct {
Results []*IndexWithScore // Retrieval results
+6 -4
View File
@@ -50,10 +50,12 @@ type SearchResult struct {
// SearchParams represents the search parameters
type SearchParams struct {
QueryText string `json:"query_text"`
VectorThreshold float64 `json:"vector_threshold"`
KeywordThreshold float64 `json:"keyword_threshold"`
MatchCount int `json:"match_count"`
QueryText string `json:"query_text"`
VectorThreshold float64 `json:"vector_threshold"`
KeywordThreshold float64 `json:"keyword_threshold"`
MatchCount int `json:"match_count"`
DisableKeywordsMatch bool `json:"disable_keywords_match"`
DisableVectorMatch bool `json:"disable_vector_match"`
}
// Value implements the driver.Valuer interface, used to convert SearchResult to database value
+10
View File
@@ -0,0 +1,10 @@
-- Add FAQ knowledge base type and chunk metadata storage
ALTER TABLE knowledge_bases
ADD COLUMN type VARCHAR(32) NOT NULL DEFAULT 'document' AFTER name,
ADD COLUMN faq_config JSON NULL AFTER extract_config;
UPDATE knowledge_bases SET type = 'document' WHERE type IS NULL OR type = '';
ALTER TABLE chunks
ADD COLUMN metadata JSON NULL AFTER indirect_relation_chunks;
@@ -0,0 +1,12 @@
-- Add FAQ knowledge base type and chunk metadata support
ALTER TABLE knowledge_bases
ADD COLUMN IF NOT EXISTS type VARCHAR(32) NOT NULL DEFAULT 'document',
ADD COLUMN IF NOT EXISTS faq_config JSONB;
UPDATE knowledge_bases
SET type = 'document'
WHERE type IS NULL OR type = '';
ALTER TABLE chunks
ADD COLUMN IF NOT EXISTS metadata JSONB;
@@ -0,0 +1,12 @@
-- 08-add-faq-support rollback
BEGIN;
ALTER TABLE chunks
DROP COLUMN IF EXISTS metadata;
ALTER TABLE knowledge_bases
DROP COLUMN IF EXISTS faq_config,
DROP COLUMN IF EXISTS type;
COMMIT;
@@ -0,0 +1,17 @@
-- 08-add-faq-support.sql
-- Introduce FAQ knowledge base type and chunk metadata storage
BEGIN;
ALTER TABLE knowledge_bases
ADD COLUMN IF NOT EXISTS type VARCHAR(32) NOT NULL DEFAULT 'document',
ADD COLUMN IF NOT EXISTS faq_config JSONB;
UPDATE knowledge_bases
SET type = 'document'
WHERE type IS NULL OR type = '';
ALTER TABLE chunks
ADD COLUMN IF NOT EXISTS metadata JSONB;
COMMIT;