From c1cddb4d4dbedff160e7753cbcb924289537aeb4 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Mon, 13 Jul 2026 11:57:30 +0800 Subject: [PATCH] Add instruction fields to knowledge base configuration --- frontend/src/api/initialization/index.ts | 5 + frontend/src/api/knowledge-base/index.ts | 6 + frontend/src/i18n/locales/en-US.ts | 23 ++++ frontend/src/i18n/locales/ko-KR.ts | 23 ++++ frontend/src/i18n/locales/ru-RU.ts | 25 +++- frontend/src/i18n/locales/zh-CN.ts | 23 ++++ frontend/src/types/knowledgeProcess.ts | 5 + .../knowledge/KnowledgeBaseEditorModal.vue | 113 +++++++++++++++--- .../src/views/knowledge/KnowledgeBaseList.vue | 4 +- .../components/UploadConfirmDialog.vue | 27 ++++- .../knowledge/settings/GraphSettings.vue | 26 +++- .../knowledge/settings/KBAdvancedSettings.vue | 57 ++++++++- internal/agent/finalize.go | 2 +- internal/application/service/extract.go | 27 ++++- .../application/service/image_multimodal.go | 13 +- .../service/image_multimodal_prompt_test.go | 29 +++++ .../application/service/knowledge_process.go | 16 ++- .../service/knowledge_process_config.go | 17 +++ .../service/knowledge_process_config_test.go | 28 +++++ internal/application/service/wiki_ingest.go | 28 +++-- .../application/service/wiki_ingest_batch.go | 47 +++++--- .../application/service/wiki_ingest_cite.go | 2 + internal/handler/initialization.go | 55 ++++++--- internal/handler/knowledgebase.go | 42 +++++++ internal/types/knowledgebase.go | 16 +++ internal/types/prompt_instructions.go | 25 ++++ internal/types/prompt_instructions_test.go | 24 ++++ internal/types/wiki_page.go | 7 +- 28 files changed, 634 insertions(+), 81 deletions(-) create mode 100644 internal/application/service/image_multimodal_prompt_test.go create mode 100644 internal/types/prompt_instructions.go create mode 100644 internal/types/prompt_instructions_test.go diff --git a/frontend/src/api/initialization/index.ts b/frontend/src/api/initialization/index.ts index e03971a4..de8cace9 100644 --- a/frontend/src/api/initialization/index.ts +++ b/frontend/src/api/initialization/index.ts @@ -109,6 +109,8 @@ export interface KBModelConfigRequest { vlm_config?: { enabled: boolean model_id?: string + description_language?: string + custom_instructions?: string } asr_config?: { enabled: boolean @@ -134,6 +136,7 @@ export interface KBModelConfigRequest { tokenLimit?: number // Language hints for heuristic patterns. Empty array = auto-detect. languages?: string[] + tableMetadataInstructions?: string } multimodal: { enabled: boolean @@ -146,10 +149,12 @@ export interface KBModelConfigRequest { tags: string[] nodes: Node[] relations: Relation[] + customInstructions?: string } questionGeneration?: { enabled: boolean questionCount: number + customInstructions?: string } } diff --git a/frontend/src/api/knowledge-base/index.ts b/frontend/src/api/knowledge-base/index.ts index 44e5f55a..48a3fe71 100644 --- a/frontend/src/api/knowledge-base/index.ts +++ b/frontend/src/api/knowledge-base/index.ts @@ -68,6 +68,8 @@ export function createKnowledgeBase(data: { vlm_config?: { enabled: boolean; model_id?: string; + description_language?: string; + custom_instructions?: string; }; storage_provider_config?: { provider: string }; storage_config?: any; // legacy, kept for backward compat (dual-write) @@ -82,6 +84,8 @@ export function createKnowledgeBase(data: { synthesis_model_id?: string; max_pages_per_ingest?: number; extraction_granularity?: 'focused' | 'standard' | 'exhaustive'; + content_instructions?: string; + extraction_instructions?: string; }; indexing_strategy?: { vector_enabled: boolean; @@ -111,6 +115,8 @@ export function updateKnowledgeBase(id: string, data: { synthesis_model_id?: string; max_pages_per_ingest?: number; extraction_granularity?: 'focused' | 'standard' | 'exhaustive'; + content_instructions?: string; + extraction_instructions?: string; }; indexing_strategy?: { vector_enabled: boolean; diff --git a/frontend/src/i18n/locales/en-US.ts b/frontend/src/i18n/locales/en-US.ts index 0aa2e348..fc5700b2 100755 --- a/frontend/src/i18n/locales/en-US.ts +++ b/frontend/src/i18n/locales/en-US.ts @@ -1637,6 +1637,9 @@ export default { sampleTextLabel: 'Sample Text', sampleTextDescription: 'Sample text for testing entity-relationship extraction', sampleTextPlaceholder: 'Enter text containing entities and relationships...', + customInstructionsLabel: 'Additional Extraction Instructions', + customInstructionsDescription: 'Add domain scope, entity filtering, or attribute guidance while the system retains the structured output contract', + customInstructionsPlaceholder: 'For example: focus on contract parties, amounts, performance dates, and liabilities…', generateRandomText: 'Generate Random Text', entityListLabel: 'Entity List', entityListDescription: 'Entities and their attributes extracted from text', @@ -2560,6 +2563,12 @@ export default { maxPagesLabel: 'Max Pages Per Ingest', maxPagesTip: 'Maximum number of pages to create/update per ingest (0 = no limit)', extractionGranularityLabel: 'Extraction Granularity', + contentInstructionsLabel: 'Wiki Content Instructions', + contentInstructionsTip: 'Control emphasis and tone for summaries, pages, and the index. Citation, merge, and factuality rules remain system-owned. Reprocess existing content to apply changes.', + contentInstructionsPlaceholder: 'For example: use a legal-review tone and emphasize owners, timelines, and risks…', + extractionInstructionsLabel: 'Wiki Extraction Focus', + extractionInstructionsTip: 'Describe domain entities and concepts to prioritize without replacing the system JSON and citation protocol.', + extractionInstructionsPlaceholder: 'For example: prioritize products, versions, organizations, owners, and core technical concepts…', extractionGranularityTip: 'Controls how many Wiki entities/concepts are extracted per document. Finer = tighter index, coarser = more comprehensive', granularityFocused: 'Focused', granularityStandard: 'Standard', @@ -2986,6 +2995,14 @@ export default { description: 'Generate related questions for each chunk using LLM during document parsing to improve retrieval recall. Enabling this will increase document parsing time.', countLabel: 'Question Count', countDescription: 'Number of questions to generate per document chunk (1-10)', + instructionsLabel: 'Question Generation Instructions', + instructionsDescription: 'Specify audience, scenario, and wording while the system retains the stable output format', + instructionsPlaceholder: 'For example: generate natural customer-support questions and avoid exam-style wording…', + }, + tableMetadataInstructions: { + label: 'Table Metadata Instructions', + description: 'Add business context and field semantics to CSV/Excel summaries for better retrieval', + placeholder: 'For example: this is a sales-order table, amounts are in CNY, and status uses internal codes…', }, multimodal: { label: 'Multimodal Feature', @@ -2993,6 +3010,12 @@ export default { vllmLabel: 'VLLM Vision Model', vllmDescription: 'Vision-language model required for multimodal understanding', vllmPlaceholder: 'Select a VLLM model (required)', + descriptionLanguageLabel: 'Image Description Language', + descriptionLanguageDescription: 'Leave empty to follow the document language instead of forcing Chinese', + descriptionLanguageAuto: 'Follow document language', + customInstructionsLabel: 'Image Processing Instructions', + customInstructionsDescription: 'Add visual priorities while OCR and Markdown output contracts remain fixed', + customInstructionsPlaceholder: 'For example: prioritize nameplates, model numbers, alarm codes, and table units…', storageTitle: 'Storage Configuration', storageTypeLabel: 'Storage Type', storageTypeDescription: 'Choose the storage solution for multimodal files (MinIO or Tencent Cloud COS)', diff --git a/frontend/src/i18n/locales/ko-KR.ts b/frontend/src/i18n/locales/ko-KR.ts index 7d644ba5..33b0aa7d 100755 --- a/frontend/src/i18n/locales/ko-KR.ts +++ b/frontend/src/i18n/locales/ko-KR.ts @@ -1494,6 +1494,9 @@ export default { sampleTextLabel: "샘플 텍스트", sampleTextDescription: "엔티티 관계 추출 테스트용 샘플 텍스트", sampleTextPlaceholder: "엔티티와 관계가 포함된 텍스트 입력...", + customInstructionsLabel: "추가 추출 지침", + customInstructionsDescription: "구조화된 출력 규칙은 유지하면서 도메인 범위와 엔티티 속성 지침을 추가합니다", + customInstructionsPlaceholder: "예: 계약 당사자, 금액, 이행 기한 및 책임을 중점적으로 추출…", generateRandomText: "랜덤 텍스트 생성", entityListLabel: "엔티티 목록", entityListDescription: "텍스트에서 추출한 엔티티 및 속성", @@ -3559,6 +3562,12 @@ export default { maxPagesLabel: "Ingest당 최대 페이지 수", maxPagesTip: "Ingest당 생성/업데이트할 최대 페이지 수 (0 = 제한 없음)", extractionGranularityLabel: "추출 세분화 수준", + contentInstructionsLabel: "Wiki 콘텐츠 생성 지침", + contentInstructionsTip: "요약, 페이지 및 인덱스의 강조점과 문체를 지정합니다. 인용 및 병합 규칙은 시스템에서 유지합니다.", + contentInstructionsPlaceholder: "예: 책임자, 일정 및 위험을 우선적으로 표시…", + extractionInstructionsLabel: "Wiki 추출 중점", + extractionInstructionsTip: "시스템 JSON 및 인용 규칙을 바꾸지 않고 우선할 엔티티와 개념을 지정합니다.", + extractionInstructionsPlaceholder: "예: 제품, 버전, 조직, 담당자 및 핵심 기술 개념을 우선…", extractionGranularityTip: "문서당 추출되는 Wiki 엔티티/개념 수를 제어합니다. 세밀할수록 인덱스가 간결하고, 거칠수록 포괄적입니다", granularityFocused: "집중", granularityStandard: "표준", @@ -3995,6 +4004,14 @@ export default { "문서 파싱 시 대규모 모델을 호출하여 각 청크에 대한 관련 질문을 생성하여 검색 재현율을 향상시킵니다. 활성화하면 문서 파싱 시간이 증가합니다.", countLabel: "생성 질문 수", countDescription: "각 문서 청크에서 생성할 질문 수 (1-10)", + instructionsLabel: "질문 생성 지침", + instructionsDescription: "안정적인 출력 형식을 유지하면서 대상, 상황 및 표현 방식을 지정합니다", + instructionsPlaceholder: "예: 시험 문제 형식을 피하고 자연스러운 고객 지원 질문을 생성…", + }, + tableMetadataInstructions: { + label: "테이블 메타데이터 지침", + description: "CSV/Excel 요약에 비즈니스 배경과 필드 의미를 추가합니다", + placeholder: "예: 판매 주문 테이블이며 금액 단위와 내부 상태 코드를 설명…", }, multimodal: { label: "멀티모달 기능", @@ -4002,6 +4019,12 @@ export default { vllmLabel: "VLLM 비전 모델", vllmDescription: "멀티모달 이해를 위한 비전 언어 모델 (필수)", vllmPlaceholder: "VLLM 모델을 선택해주세요 (필수)", + descriptionLanguageLabel: "이미지 설명 언어", + descriptionLanguageDescription: "비워 두면 문서 언어를 따릅니다", + descriptionLanguageAuto: "문서 언어 자동 사용", + customInstructionsLabel: "이미지 처리 지침", + customInstructionsDescription: "OCR 및 Markdown 형식은 유지하면서 시각적 우선순위를 추가합니다", + customInstructionsPlaceholder: "예: 명판, 모델 번호, 경고 코드 및 표 단위를 중점적으로 인식…", storageTitle: "저장소 설정", storageTypeLabel: "저장소 유형", storageTypeDescription: diff --git a/frontend/src/i18n/locales/ru-RU.ts b/frontend/src/i18n/locales/ru-RU.ts index 2ebf5e12..e54543cc 100755 --- a/frontend/src/i18n/locales/ru-RU.ts +++ b/frontend/src/i18n/locales/ru-RU.ts @@ -1546,6 +1546,9 @@ export default { sampleTextLabel: 'Образец текста', sampleTextDescription: 'Образец текста для тестирования извлечения сущностей и отношений', sampleTextPlaceholder: 'Введите текст, содержащий сущности и отношения...', + customInstructionsLabel: 'Дополнительные инструкции извлечения', + customInstructionsDescription: 'Добавьте предметную область и правила атрибутов, сохраняя системный формат вывода', + customInstructionsPlaceholder: 'Например: уделять внимание сторонам договора, суммам, срокам и ответственности…', generateRandomText: 'Сгенерировать случайный текст', entityListLabel: 'Список сущностей', entityListDescription: 'Сущности и их атрибуты, извлечённые из текста', @@ -3042,6 +3045,12 @@ export default { maxPagesLabel: 'Макс. страниц за Ingest', maxPagesTip: 'Максимальное количество страниц для создания/обновления за одну операцию (0 = без ограничения)', extractionGranularityLabel: 'Детализация извлечения', + contentInstructionsLabel: 'Инструкции для Wiki-контента', + contentInstructionsTip: 'Задайте акценты и стиль сводок, страниц и индекса. Правила цитирования и слияния остаются системными.', + contentInstructionsPlaceholder: 'Например: выделять ответственных, сроки и риски…', + extractionInstructionsLabel: 'Фокус извлечения Wiki', + extractionInstructionsTip: 'Укажите приоритетные сущности и концепции без замены системного JSON и протокола цитирования.', + extractionInstructionsPlaceholder: 'Например: продукты, версии, организации, владельцы и ключевые технические концепции…', extractionGranularityTip: 'Управляет количеством извлекаемых сущностей/концепций на документ. Точнее — компактный индекс, грубее — более полный', granularityFocused: 'Фокус', granularityStandard: 'Стандарт', @@ -3457,7 +3466,15 @@ export default { label: 'AI генерация вопросов', description: 'Генерация связанных вопросов для каждого фрагмента с помощью LLM при парсинге документа для улучшения полноты поиска. Включение увеличит время парсинга документа.', countLabel: 'Количество вопросов', - countDescription: 'Количество вопросов для генерации на фрагмент документа (1-10)' + countDescription: 'Количество вопросов для генерации на фрагмент документа (1-10)', + instructionsLabel: 'Инструкции генерации вопросов', + instructionsDescription: 'Задайте аудиторию, сценарий и формулировки, сохраняя стабильный формат вывода', + instructionsPlaceholder: 'Например: естественные вопросы службы поддержки без экзаменационного стиля…' + }, + tableMetadataInstructions: { + label: 'Инструкции метаданных таблиц', + description: 'Добавьте бизнес-контекст и семантику полей к сводкам CSV/Excel', + placeholder: 'Например: таблица заказов продаж, суммы в CNY, status использует внутренние коды…' }, multimodal: { label: 'Мультимодальная функция', @@ -3465,6 +3482,12 @@ export default { vllmLabel: 'VLLM модель для зрения', vllmDescription: 'Визуально-языковая модель, необходимая для мультимодального понимания', vllmPlaceholder: 'Выберите VLLM модель (обязательно)', + descriptionLanguageLabel: 'Язык описания изображений', + descriptionLanguageDescription: 'Оставьте пустым, чтобы использовать язык документа', + descriptionLanguageAuto: 'Следовать языку документа', + customInstructionsLabel: 'Инструкции обработки изображений', + customInstructionsDescription: 'Добавьте визуальные приоритеты, сохраняя правила OCR и Markdown', + customInstructionsPlaceholder: 'Например: распознавать шильдики, модели, коды ошибок и единицы таблиц…', storageTitle: 'Конфигурация хранилища', storageTypeLabel: 'Тип хранилища', storageTypeDescription: 'Выберите способ хранения мультимодальных файлов (MinIO или Tencent Cloud COS)', diff --git a/frontend/src/i18n/locales/zh-CN.ts b/frontend/src/i18n/locales/zh-CN.ts index a86b2f86..c51fd234 100755 --- a/frontend/src/i18n/locales/zh-CN.ts +++ b/frontend/src/i18n/locales/zh-CN.ts @@ -1503,6 +1503,9 @@ export default { sampleTextLabel: "示例文本", sampleTextDescription: "用于测试实体关系提取的示例文本", sampleTextPlaceholder: "输入一段包含实体和关系的文本...", + customInstructionsLabel: "额外提取要求", + customInstructionsDescription: "补充领域范围、实体筛选和属性提取要求;系统仍负责结构化输出协议", + customInstructionsPlaceholder: "例如:重点提取合同主体、金额、履约期限和违约责任…", generateRandomText: "生成随机文本", entityListLabel: "实体列表", entityListDescription: "从文本中提取的实体及其属性", @@ -3588,6 +3591,12 @@ export default { maxPagesLabel: "单次最大页面数", maxPagesTip: "每次 Ingest 最多创建/更新的页面数(0 表示不限制)", extractionGranularityLabel: "提取粒度", + contentInstructionsLabel: "Wiki 内容生成要求", + contentInstructionsTip: "控制摘要、页面和首页的表达重点;引用、合并与防幻觉规则由系统固定维护。修改后需重新解析才能影响已有内容。", + contentInstructionsPlaceholder: "例如:使用法务审阅口吻,优先展示责任主体、时间线和风险提示…", + extractionInstructionsLabel: "Wiki 提取重点", + extractionInstructionsTip: "说明应重点识别的领域实体和概念,不会替换系统的 JSON 与引用协议。", + extractionInstructionsPlaceholder: "例如:重点识别产品、版本、组织、负责人和关键技术概念…", extractionGranularityTip: "控制每篇文档抽取的 Wiki 实体/概念数量。粒度越细,索引越聚焦;粒度越粗,索引越详尽", granularityFocused: "聚焦", granularityStandard: "标准", @@ -4010,6 +4019,14 @@ export default { description: "解析文档时调用大模型为每个分块生成相关问题,提高检索召回率。启用后会增加文档解析耗时。", countLabel: "生成问题数量", countDescription: "每个文档分块生成的问题数量(1-10)", + instructionsLabel: "问题生成要求", + instructionsDescription: "指定问题面向的人群、场景和表达方式,系统仍维护稳定输出格式", + instructionsPlaceholder: "例如:生成客服用户常问的自然语言问题,避免考试题式表达…", + }, + tableMetadataInstructions: { + label: "表格元数据生成要求", + description: "为 CSV/Excel 摘要补充业务背景和字段语义,帮助后续检索理解表格内容", + placeholder: "例如:这是销售订单表,金额单位为元,status 字段使用公司内部状态码…", }, multimodal: { label: "多模态功能", @@ -4017,6 +4034,12 @@ export default { vllmLabel: "VLLM 视觉模型", vllmDescription: "用于多模态理解的视觉语言模型(必选)", vllmPlaceholder: "请选择 VLLM 模型(必选)", + descriptionLanguageLabel: "图片描述语言", + descriptionLanguageDescription: "留空时跟随文档语言;不再强制使用中文", + descriptionLanguageAuto: "自动跟随文档语言", + customInstructionsLabel: "图片解析要求", + customInstructionsDescription: "补充需要重点识别的视觉信息,OCR 和 Markdown 格式协议保持不变", + customInstructionsPlaceholder: "例如:重点识别设备铭牌、型号、告警代码和表格中的单位…", storageTitle: "存储配置", storageTypeLabel: "存储类型", storageTypeDescription: diff --git a/frontend/src/types/knowledgeProcess.ts b/frontend/src/types/knowledgeProcess.ts index 86613536..3f9a94a8 100644 --- a/frontend/src/types/knowledgeProcess.ts +++ b/frontend/src/types/knowledgeProcess.ts @@ -16,11 +16,14 @@ export interface ChunkingConfigOverride { strategy?: string token_limit?: number languages?: string[] + table_metadata_instructions?: string } export interface VLMConfigOverride { enabled?: boolean model_id?: string + description_language?: string + custom_instructions?: string } export interface ASRConfigOverride { @@ -32,6 +35,7 @@ export interface ASRConfigOverride { export interface QuestionGenerationConfigOverride { enabled?: boolean question_count?: number + custom_instructions?: string } export interface GraphNodeOverride { @@ -51,6 +55,7 @@ export interface ExtractConfigOverride { tags?: string[] nodes?: GraphNodeOverride[] relations?: GraphRelationOverride[] + custom_instructions?: string } export interface KnowledgeProcessOverrides { diff --git a/frontend/src/views/knowledge/KnowledgeBaseEditorModal.vue b/frontend/src/views/knowledge/KnowledgeBaseEditorModal.vue index 1406e93e..52fe6f9a 100644 --- a/frontend/src/views/knowledge/KnowledgeBaseEditorModal.vue +++ b/frontend/src/views/knowledge/KnowledgeBaseEditorModal.vue @@ -135,6 +135,28 @@

{{ granularityHint }}

+
+ +

{{ $t('knowledgeEditor.wiki.contentInstructionsTip') }}

+ +
+ +
+ +

{{ $t('knowledgeEditor.wiki.extractionInstructionsTip') }}

+ +
+
+ +
+
+ +

{{ $t('knowledgeEditor.advanced.multimodal.descriptionLanguageDescription') }}

+
+
+ + + + + + +
+
+ +
+
+ +

{{ $t('knowledgeEditor.advanced.multimodal.customInstructionsDescription') }}

+
+
+ +
+
@@ -356,7 +406,9 @@ :question-generation="formData.questionGenerationConfig" :rag-enabled="formData.indexingStrategy?.vectorEnabled || formData.indexingStrategy?.keywordEnabled" :all-models="allModels" + :table-metadata-instructions="formData.chunkingConfig.tableMetadataInstructions" @update:question-generation="handleQuestionGenerationUpdate" + @update:table-metadata-instructions="(value: string) => { if (formData) formData.chunkingConfig.tableMetadataInstructions = value }" /> @@ -656,12 +708,15 @@ const initFormData = (type: 'document' | 'faq' = 'document') => { // New KBs default to the adaptive auto-strategy. User can change in the UI. strategy: 'auto' as string, tokenLimit: 0, - languages: [] as string[] + languages: [] as string[], + tableMetadataInstructions: '' }, storageProvider: '' as string, multimodalConfig: { enabled: false, - vllmModelId: '' + vllmModelId: '', + descriptionLanguage: '', + customInstructions: '' }, asrConfig: { enabled: false, @@ -680,16 +735,20 @@ const initFormData = (type: 'document' | 'faq' = 'document') => { node1: string node2: string type: string - }> + }>, + customInstructions: '' }, questionGenerationConfig: { enabled: true, - questionCount: 3 + questionCount: 3, + customInstructions: '' }, wikiConfig: { synthesisModelId: '', maxPagesPerIngest: 0, extractionGranularity: 'standard' as 'focused' | 'standard' | 'exhaustive', + contentInstructions: '', + extractionInstructions: '', }, indexingStrategy: { vectorEnabled: true, @@ -770,12 +829,15 @@ const loadKBData = async () => { // The user has to actively pick a value to opt in to the new tiers. strategy: kb.chunking_config?.strategy || '', tokenLimit: kb.chunking_config?.token_limit || 0, - languages: kb.chunking_config?.languages || [] + languages: kb.chunking_config?.languages || [], + tableMetadataInstructions: kb.chunking_config?.table_metadata_instructions || '' }, storageProvider: (kb.storage_provider_config?.provider || kb.storage_config?.provider || 'local') as string, multimodalConfig: { enabled: !!kb.vlm_config?.enabled, - vllmModelId: kb.vlm_config?.model_id || '' + vllmModelId: kb.vlm_config?.model_id || '', + descriptionLanguage: kb.vlm_config?.description_language || '', + customInstructions: kb.vlm_config?.custom_instructions || '' }, asrConfig: { enabled: !!kb.asr_config?.enabled, @@ -790,11 +852,13 @@ const loadKBData = async () => { name: node.name, attributes: node.attributes || [] })), - relations: kb.extract_config?.relations || [] + relations: kb.extract_config?.relations || [], + customInstructions: kb.extract_config?.custom_instructions || '' }, questionGenerationConfig: { enabled: kb.question_generation_config?.enabled || false, - questionCount: kb.question_generation_config?.question_count || 3 + questionCount: kb.question_generation_config?.question_count || 3, + customInstructions: kb.question_generation_config?.custom_instructions || '' }, wikiConfig: { synthesisModelId: kb.wiki_config?.synthesis_model_id || '', @@ -805,6 +869,8 @@ const loadKBData = async () => { ? kb.wiki_config.extraction_granularity : 'standard' ) as 'focused' | 'standard' | 'exhaustive', + contentInstructions: kb.wiki_config?.content_instructions || '', + extractionInstructions: kb.wiki_config?.extraction_instructions || '', }, indexingStrategy: { vectorEnabled: kb.indexing_strategy?.vector_enabled ?? true, @@ -1067,6 +1133,7 @@ const buildSubmitData = () => { strategy: formData.value.chunkingConfig.strategy ?? '', token_limit: formData.value.chunkingConfig.tokenLimit ?? 0, languages: formData.value.chunkingConfig.languages ?? [], + table_metadata_instructions: formData.value.chunkingConfig.tableMetadataInstructions || '', ...(formData.value.chunkingConfig.parserEngineRules?.length ? { parser_engine_rules: formData.value.chunkingConfig.parserEngineRules } : {}) @@ -1089,7 +1156,9 @@ const buildSubmitData = () => { enabled: formData.value.multimodalConfig.enabled, model_id: formData.value.multimodalConfig.enabled ? (formData.value.multimodalConfig.vllmModelId || '') - : '' + : '', + description_language: formData.value.multimodalConfig.descriptionLanguage || '', + custom_instructions: formData.value.multimodalConfig.customInstructions || '' } // 添加ASR语音识别配置 @@ -1118,7 +1187,14 @@ const buildSubmitData = () => { if (formData.value.questionGenerationConfig?.enabled) { data.question_generation_config = { enabled: true, - question_count: formData.value.questionGenerationConfig.questionCount || 3 + question_count: formData.value.questionGenerationConfig.questionCount || 3, + custom_instructions: formData.value.questionGenerationConfig.customInstructions || '' + } + } else { + data.question_generation_config = { + enabled: false, + question_count: 3, + custom_instructions: formData.value.questionGenerationConfig?.customInstructions || '' } } @@ -1136,6 +1212,8 @@ const buildSubmitData = () => { synthesis_model_id: formData.value.modelConfig?.wikiSynthesisModelId || '', max_pages_per_ingest: formData.value.wikiConfig?.maxPagesPerIngest || 0, extraction_granularity: formData.value.wikiConfig?.extractionGranularity || 'standard', + content_instructions: formData.value.wikiConfig?.contentInstructions || '', + extraction_instructions: formData.value.wikiConfig?.extractionInstructions || '', } } @@ -1157,7 +1235,8 @@ const buildSubmitData = () => { text: formData.value.nodeExtractConfig.text || '', tags: formData.value.nodeExtractConfig.tags || [], nodes: formData.value.nodeExtractConfig.nodes || [], - relations: formData.value.nodeExtractConfig.relations || [] + relations: formData.value.nodeExtractConfig.relations || [], + custom_instructions: formData.value.nodeExtractConfig.customInstructions || '' } } @@ -1233,6 +1312,8 @@ const doSubmit = async () => { synthesis_model_id: formData.value.modelConfig?.wikiSynthesisModelId || '', max_pages_per_ingest: formData.value.wikiConfig.maxPagesPerIngest || 0, extraction_granularity: formData.value.wikiConfig.extractionGranularity || 'standard', + content_instructions: formData.value.wikiConfig.contentInstructions || '', + extraction_instructions: formData.value.wikiConfig.extractionInstructions || '', } } if (formData.value.type !== 'faq') { @@ -1268,7 +1349,8 @@ const doSubmit = async () => { // payload to let users reset back to defaults. strategy: formData.value?.chunkingConfig.strategy ?? '', tokenLimit: formData.value?.chunkingConfig.tokenLimit ?? 0, - languages: formData.value?.chunkingConfig.languages ?? [] + languages: formData.value?.chunkingConfig.languages ?? [], + tableMetadataInstructions: formData.value?.chunkingConfig.tableMetadataInstructions ?? '' }, multimodal: { enabled: !!data.vlm_config?.enabled @@ -1279,11 +1361,13 @@ const doSubmit = async () => { text: data.extract_config?.text || '', tags: data.extract_config?.tags || [], nodes: data.extract_config?.nodes || [], - relations: data.extract_config?.relations || [] + relations: data.extract_config?.relations || [], + customInstructions: data.extract_config?.custom_instructions || '' }, questionGeneration: { enabled: data.question_generation_config?.enabled || false, - questionCount: data.question_generation_config?.question_count || 3 + questionCount: data.question_generation_config?.question_count || 3, + customInstructions: data.question_generation_config?.custom_instructions || '' } } @@ -1896,4 +1980,3 @@ watch( } } - diff --git a/frontend/src/views/knowledge/KnowledgeBaseList.vue b/frontend/src/views/knowledge/KnowledgeBaseList.vue index 4924aa2c..2dade925 100644 --- a/frontend/src/views/knowledge/KnowledgeBaseList.vue +++ b/frontend/src/views/knowledge/KnowledgeBaseList.vue @@ -1559,8 +1559,8 @@ const isInitialized = (kb: KB) => { return true } -const isWikiKb = (kb: { indexing_strategy?: { wiki_enabled?: boolean } } | null | undefined) => - !!kb?.indexing_strategy?.wiki_enabled +const isWikiKb = (kb: unknown) => + !!(kb as { indexing_strategy?: { wiki_enabled?: boolean } } | null | undefined)?.indexing_strategy?.wiki_enabled // 计算是否有未初始化的知识库 const hasUninitializedKbs = computed(() => { diff --git a/frontend/src/views/knowledge/components/UploadConfirmDialog.vue b/frontend/src/views/knowledge/components/UploadConfirmDialog.vue index b26a06b7..e3ac130a 100644 --- a/frontend/src/views/knowledge/components/UploadConfirmDialog.vue +++ b/frontend/src/views/knowledge/components/UploadConfirmDialog.vue @@ -286,19 +286,21 @@ interface ChunkingUIConfig { strategy?: string tokenLimit?: number languages?: string[] + tableMetadataInstructions?: string } interface UploadUIState { chunkingConfig: ChunkingUIConfig - multimodalConfig: { enabled: boolean; vllmModelId: string } + multimodalConfig: { enabled: boolean; vllmModelId: string; descriptionLanguage?: string; customInstructions?: string } asrConfig: { enabled: boolean; modelId: string; language: string } - questionGenerationConfig: { enabled: boolean; questionCount: number } + questionGenerationConfig: { enabled: boolean; questionCount: number; customInstructions?: string } nodeExtractConfig: { enabled: boolean text: string tags: string[] nodes: Array<{ name: string; attributes: string[] }> relations: Array<{ node1: string; node2: string; type: string }> + customInstructions?: string } graphEnabled: boolean pdfForceScanned: boolean @@ -635,16 +637,18 @@ function createDefaultUIState(): UploadUIState { strategy: 'auto', tokenLimit: 0, languages: [], + tableMetadataInstructions: '', }, - multimodalConfig: { enabled: false, vllmModelId: '' }, + multimodalConfig: { enabled: false, vllmModelId: '', descriptionLanguage: '', customInstructions: '' }, asrConfig: { enabled: false, modelId: '', language: '' }, - questionGenerationConfig: { enabled: true, questionCount: 3 }, + questionGenerationConfig: { enabled: true, questionCount: 3, customInstructions: '' }, nodeExtractConfig: { enabled: false, text: '', tags: [], nodes: [], relations: [], + customInstructions: '', }, graphEnabled: false, pdfForceScanned: false, @@ -669,10 +673,13 @@ function initFromKbInfo(kb: any) { strategy: kb.chunking_config?.strategy || 'auto', tokenLimit: kb.chunking_config?.token_limit || 0, languages: kb.chunking_config?.languages || [], + tableMetadataInstructions: kb.chunking_config?.table_metadata_instructions || '', }, multimodalConfig: { enabled: !!kb.vlm_config?.enabled, vllmModelId: kb.vlm_config?.model_id || '', + descriptionLanguage: kb.vlm_config?.description_language || '', + customInstructions: kb.vlm_config?.custom_instructions || '', }, asrConfig: { enabled: !!kb.asr_config?.enabled, @@ -682,6 +689,7 @@ function initFromKbInfo(kb: any) { questionGenerationConfig: { enabled: kb.question_generation_config?.enabled ?? true, questionCount: kb.question_generation_config?.question_count || 3, + customInstructions: kb.question_generation_config?.custom_instructions || '', }, nodeExtractConfig: { enabled: kb.extract_config?.enabled || false, @@ -692,6 +700,7 @@ function initFromKbInfo(kb: any) { attributes: node.attributes || [], })), relations: kb.extract_config?.relations || [], + customInstructions: kb.extract_config?.custom_instructions || '', }, graphEnabled: kb.indexing_strategy?.graph_enabled ?? false, pdfForceScanned: false, @@ -714,11 +723,14 @@ function buildProcessOverrides(): KnowledgeProcessOverrides { strategy: chunking.strategy, token_limit: chunking.tokenLimit, languages: chunking.languages, + table_metadata_instructions: chunking.tableMetadataInstructions, }, enable_multimodel: state.multimodalConfig.enabled, vlm_config: { enabled: state.multimodalConfig.enabled, model_id: state.multimodalConfig.vllmModelId, + description_language: state.multimodalConfig.descriptionLanguage, + custom_instructions: state.multimodalConfig.customInstructions, }, asr_config: { enabled: state.asrConfig.enabled, @@ -728,6 +740,7 @@ function buildProcessOverrides(): KnowledgeProcessOverrides { question_generation_config: { enabled: state.questionGenerationConfig.enabled, question_count: state.questionGenerationConfig.questionCount, + custom_instructions: state.questionGenerationConfig.customInstructions, }, graph_enabled: state.nodeExtractConfig.enabled && state.graphEnabled, extract_config: { @@ -736,6 +749,7 @@ function buildProcessOverrides(): KnowledgeProcessOverrides { tags: state.nodeExtractConfig.tags, nodes: state.nodeExtractConfig.nodes, relations: state.nodeExtractConfig.relations, + custom_instructions: state.nodeExtractConfig.customInstructions, }, } @@ -762,6 +776,7 @@ function applyOverridesToState(o?: KnowledgeProcessOverrides | null) { if (cc.strategy != null) s.chunkingConfig.strategy = cc.strategy if (cc.token_limit != null) s.chunkingConfig.tokenLimit = cc.token_limit if (cc.languages) s.chunkingConfig.languages = cc.languages + if (cc.table_metadata_instructions != null) s.chunkingConfig.tableMetadataInstructions = cc.table_metadata_instructions if (cc.parser_engine_rules) s.chunkingConfig.parserEngineRules = cc.parser_engine_rules } if (o.parser_engine_rules) s.chunkingConfig.parserEngineRules = o.parser_engine_rules @@ -769,6 +784,8 @@ function applyOverridesToState(o?: KnowledgeProcessOverrides | null) { if (o.vlm_config) { if (o.vlm_config.enabled != null) s.multimodalConfig.enabled = o.vlm_config.enabled if (o.vlm_config.model_id != null) s.multimodalConfig.vllmModelId = o.vlm_config.model_id + if (o.vlm_config.description_language != null) s.multimodalConfig.descriptionLanguage = o.vlm_config.description_language + if (o.vlm_config.custom_instructions != null) s.multimodalConfig.customInstructions = o.vlm_config.custom_instructions } if (o.asr_config) { if (o.asr_config.enabled != null) s.asrConfig.enabled = o.asr_config.enabled @@ -779,6 +796,7 @@ function applyOverridesToState(o?: KnowledgeProcessOverrides | null) { if (qg) { if (qg.enabled != null) s.questionGenerationConfig.enabled = qg.enabled if (qg.question_count != null) s.questionGenerationConfig.questionCount = qg.question_count + if (qg.custom_instructions != null) s.questionGenerationConfig.customInstructions = qg.custom_instructions } const ec = o.extract_config if (ec) { @@ -787,6 +805,7 @@ function applyOverridesToState(o?: KnowledgeProcessOverrides | null) { if (ec.tags) s.nodeExtractConfig.tags = ec.tags if (ec.nodes) s.nodeExtractConfig.nodes = ec.nodes.map(n => ({ name: n.name, attributes: n.attributes || [] })) if (ec.relations) s.nodeExtractConfig.relations = ec.relations + if (ec.custom_instructions != null) s.nodeExtractConfig.customInstructions = ec.custom_instructions } if (o.graph_enabled != null) s.graphEnabled = o.graph_enabled if (o.parser_engine_overrides && o.parser_engine_overrides.pdf_force_scanned === 'true') { diff --git a/frontend/src/views/knowledge/settings/GraphSettings.vue b/frontend/src/views/knowledge/settings/GraphSettings.vue index 2988930d..e7b36b21 100644 --- a/frontend/src/views/knowledge/settings/GraphSettings.vue +++ b/frontend/src/views/knowledge/settings/GraphSettings.vue @@ -42,6 +42,22 @@ +
+
+ +

{{ t('graphSettings.customInstructionsDescription') }}

+
+
+ +
+
+
@@ -325,6 +341,7 @@ interface GraphExtractConfig { tags: string[] nodes: Node[] relations: Relation[] + customInstructions?: string } interface Props { @@ -352,7 +369,8 @@ const modelStatus = computed(() => ({ const localGraphExtract = ref({ ...props.graphExtract, nodes: props.graphExtract.nodes || [], - relations: props.graphExtract.relations || [] + relations: props.graphExtract.relations || [], + customInstructions: props.graphExtract.customInstructions || '' }) // 加载状态 @@ -373,7 +391,8 @@ watch(() => props.graphExtract, (newVal) => { localGraphExtract.value = { ...newVal, nodes: newVal.nodes || [], - relations: newVal.relations || [] + relations: newVal.relations || [], + customInstructions: newVal.customInstructions || '' } }, { deep: true }) @@ -390,6 +409,7 @@ const handleEnabledChange = () => { localGraphExtract.value.tags = [] localGraphExtract.value.nodes = [] localGraphExtract.value.relations = [] + localGraphExtract.value.customInstructions = '' } handleConfigChange() } @@ -800,4 +820,4 @@ onMounted(async () => { align-self: flex-start; } } - \ No newline at end of file + diff --git a/frontend/src/views/knowledge/settings/KBAdvancedSettings.vue b/frontend/src/views/knowledge/settings/KBAdvancedSettings.vue index a511239f..1b487d3e 100644 --- a/frontend/src/views/knowledge/settings/KBAdvancedSettings.vue +++ b/frontend/src/views/knowledge/settings/KBAdvancedSettings.vue @@ -41,9 +41,40 @@ />
+
+
+ +

{{ $t('knowledgeEditor.advanced.questionGeneration.instructionsDescription') }}

+
+
+ +
+
+
+
+ +

{{ $t('knowledgeEditor.advanced.tableMetadataInstructions.description') }}

+
+
+ +
+
+ @@ -54,6 +85,7 @@ import { ref, watch } from 'vue' interface QuestionGenerationConfig { enabled: boolean questionCount: number + customInstructions?: string } interface Props { @@ -61,6 +93,7 @@ interface Props { ragEnabled?: boolean allModels?: any[] embedded?: boolean + tableMetadataInstructions?: string } const props = withDefaults(defineProps(), { @@ -69,15 +102,18 @@ const props = withDefaults(defineProps(), { const emit = defineEmits<{ 'update:questionGeneration': [value: QuestionGenerationConfig] + 'update:tableMetadataInstructions': [value: string] }>() const localQuestionGeneration = ref( - props.questionGeneration || { enabled: false, questionCount: 3 } + props.questionGeneration + ? { ...props.questionGeneration, customInstructions: props.questionGeneration.customInstructions || '' } + : { enabled: false, questionCount: 3, customInstructions: '' } ) watch(() => props.questionGeneration, (newVal) => { if (newVal) { - localQuestionGeneration.value = { ...newVal } + localQuestionGeneration.value = { customInstructions: '', ...newVal } } }, { deep: true }) @@ -170,6 +206,23 @@ const handleQuestionGenerationChange = () => { align-items: center; } +.setting-row-vertical { + flex-direction: column; + gap: 12px; + + .setting-info, + .setting-control { + flex: none; + width: 100%; + max-width: none; + padding-right: 0; + } + + .setting-control { + display: block; + } +} + .subsection { padding: 16px 20px; margin: 12px 0 0 0; diff --git a/internal/agent/finalize.go b/internal/agent/finalize.go index c80344b0..c787a9b7 100644 --- a/internal/agent/finalize.go +++ b/internal/agent/finalize.go @@ -63,7 +63,7 @@ User question: %s Requirements: 1. Answer based on the actually retrieved content -2. Clearly cite information sources (chunk_id, document name) +2. Cite sources by document title or name; never expose chunk_id, knowledge_id, tool names, or other internal identifiers 3. Organize the answer in a structured format 4. If information is insufficient, honestly state so 5. IMPORTANT: Respond in the same language as the user's question diff --git a/internal/application/service/extract.go b/internal/application/service/extract.go index b14d5f27..c6667946 100644 --- a/internal/application/service/extract.go +++ b/internal/application/service/extract.go @@ -319,8 +319,9 @@ func (s *ChunkExtractService) Handle(ctx context.Context, t *asynq.Task) error { } template := &types.PromptTemplateStructured{ - Description: s.template.Description, - Tags: extractCfg.Tags, + Description: types.AppendCustomPromptInstructions( + s.template.Description, extractCfg.CustomInstructions, "graph_extraction"), + Tags: extractCfg.Tags, Examples: []types.GraphData{ { Text: extractCfg.Text, @@ -473,6 +474,7 @@ func (s *DataTableSummaryService) Handle(ctx context.Context, t *asynq.Task) err // extractionResources 封装提取过程所需的所有资源 type extractionResources struct { knowledge *types.Knowledge + knowledgeBase *types.KnowledgeBase tenant *types.Tenant chatModel chat.Chat embeddingModel embedding.Embedder @@ -543,6 +545,7 @@ func (s *DataTableSummaryService) prepareResources(ctx context.Context, payload return &extractionResources{ knowledge: knowledge, + knowledgeBase: kb, tenant: tenantInfo, chatModel: chatModel, embeddingModel: embeddingModel, @@ -621,14 +624,20 @@ func (s *DataTableSummaryService) processTableData(ctx context.Context, resource sampleDesc := s.buildSampleDataDescription(sampleResult, 10) // 使用AI生成表格摘要和列描述 - tableDescription, err := s.generateTableDescription(ctx, resources.chatModel, tableSchema.TableName, schemaDesc, sampleDesc) + customInstructions := "" + if resources.knowledgeBase != nil { + customInstructions = resources.knowledgeBase.ChunkingConfig.TableMetadataInstructions + } + tableDescription, err := s.generateTableDescription(ctx, resources.chatModel, tableSchema.TableName, + schemaDesc, sampleDesc, customInstructions) if err != nil { logger.Errorf(ctx, "failed to generate table description: %v", err) return nil, err } logger.Debugf(ctx, "table describe of knowledge %s: %s", resources.knowledge.ID, tableDescription) - columnDescription, err := s.generateColumnDescriptions(ctx, resources.chatModel, tableSchema.TableName, schemaDesc, sampleDesc) + columnDescription, err := s.generateColumnDescriptions(ctx, resources.chatModel, tableSchema.TableName, + schemaDesc, sampleDesc, customInstructions) if err != nil { logger.Errorf(ctx, "failed to generate column descriptions: %v", err) return nil, err @@ -771,8 +780,11 @@ func (s *DataTableSummaryService) cleanupOnFailure(ctx context.Context, resource } // generateTableDescription generates a summary description for the entire table -func (s *DataTableSummaryService) generateTableDescription(ctx context.Context, chatModel chat.Chat, tableName, schemaDesc, sampleDesc string) (string, error) { +func (s *DataTableSummaryService) generateTableDescription(ctx context.Context, chatModel chat.Chat, + tableName, schemaDesc, sampleDesc, customInstructions string, +) (string, error) { prompt := fmt.Sprintf(tableDescriptionPromptTemplate, tableName, schemaDesc, sampleDesc) + prompt = types.AppendCustomPromptInstructions(prompt, customInstructions, "table_metadata") // logger.Debugf(ctx, "generateTableDescription prompt: %s", prompt) thinking := false @@ -791,9 +803,12 @@ func (s *DataTableSummaryService) generateTableDescription(ctx context.Context, } // generateColumnDescriptions generates descriptions for each column in batch -func (s *DataTableSummaryService) generateColumnDescriptions(ctx context.Context, chatModel chat.Chat, tableName, schemaDesc, sampleDesc string) (string, error) { +func (s *DataTableSummaryService) generateColumnDescriptions(ctx context.Context, chatModel chat.Chat, + tableName, schemaDesc, sampleDesc, customInstructions string, +) (string, error) { // Build batch prompt for all columns prompt := fmt.Sprintf(columnDescriptionsPromptTemplate, tableName, schemaDesc, sampleDesc) + prompt = types.AppendCustomPromptInstructions(prompt, customInstructions, "table_metadata") // logger.Debugf(ctx, "generateColumnDescriptions prompt: %s", prompt) // Call LLM once for all columns diff --git a/internal/application/service/image_multimodal.go b/internal/application/service/image_multimodal.go index f56b5353..47adc444 100644 --- a/internal/application/service/image_multimodal.go +++ b/internal/application/service/image_multimodal.go @@ -47,9 +47,17 @@ const ( "5. Output ONLY the extracted text content. Do NOT include any HTML tags, reasoning, or unrelated comments.\n" + "6. If there is absolutely no recognizable text content in the image, reply ONLY with: No text content.\n" + "" - vlmCaptionPrompt = "Provide a brief and concise description of the main content of the image in Chinese" ) +func buildVLMCaptionPrompt(ctx context.Context, cfg types.VLMConfig) string { + language := strings.TrimSpace(cfg.DescriptionLanguage) + if language == "" { + language = types.LanguageNameFromContext(ctx) + } + prompt := fmt.Sprintf("Provide a brief and concise description of the main content of the image in %s.", language) + return types.AppendCustomPromptInstructions(prompt, cfg.CustomInstructions, "image_description") +} + // ImageMultimodalService handles image:multimodal asynq tasks. // It reads images from storage (via FileService for provider:// URLs), // performs OCR and VLM caption, and creates child chunks. @@ -243,6 +251,7 @@ func (s *ImageMultimodalService) Handle(ctx context.Context, task *asynq.Task) e } else { imgOut["ocr_prompt"] = "default" } + prompt = types.AppendCustomPromptInstructions(prompt, vlmCfg.CustomInstructions, "image_ocr") ocrText, ocrErr := vlmModel.Predict(ctx, [][]byte{imgBytes}, prompt) if ocrErr != nil { @@ -262,7 +271,7 @@ func (s *ImageMultimodalService) Handle(ctx context.Context, task *asynq.Task) e } } - caption, capErr := vlmModel.Predict(ctx, [][]byte{imgBytes}, vlmCaptionPrompt) + caption, capErr := vlmModel.Predict(ctx, [][]byte{imgBytes}, buildVLMCaptionPrompt(ctx, vlmCfg)) if capErr != nil { logger.Warnf(ctx, "[ImageMultimodal] Caption failed for %s: %v", payload.ImageURL, capErr) imgOut["caption_error"] = capErr.Error() diff --git a/internal/application/service/image_multimodal_prompt_test.go b/internal/application/service/image_multimodal_prompt_test.go new file mode 100644 index 00000000..4eaa1eaf --- /dev/null +++ b/internal/application/service/image_multimodal_prompt_test.go @@ -0,0 +1,29 @@ +package service + +import ( + "context" + "strings" + "testing" + + "github.com/Tencent/WeKnora/internal/types" +) + +func TestBuildVLMCaptionPrompt(t *testing.T) { + t.Run("uses configured language and custom instructions", func(t *testing.T) { + got := buildVLMCaptionPrompt(context.Background(), types.VLMConfig{ + DescriptionLanguage: "English", + CustomInstructions: "Focus on alarm codes.", + }) + if !strings.Contains(got, "in English") || !strings.Contains(got, "Focus on alarm codes.") { + t.Fatalf("unexpected prompt: %s", got) + } + }) + + t.Run("defaults to context language", func(t *testing.T) { + ctx := context.WithValue(context.Background(), types.LanguageContextKey, "ko-KR") + got := buildVLMCaptionPrompt(ctx, types.VLMConfig{}) + if !strings.Contains(got, "in Korean") { + t.Fatalf("unexpected prompt: %s", got) + } + }) +} diff --git a/internal/application/service/knowledge_process.go b/internal/application/service/knowledge_process.go index a754e452..ea784ab0 100644 --- a/internal/application/service/knowledge_process.go +++ b/internal/application/service/knowledge_process.go @@ -1490,7 +1490,12 @@ func (s *knowledgeService) processQuestionGenerationForKnowledge(ctx context.Con } llmCallAttempts++ - questions, err := s.generateQuestionsWithContext(ctx, chatModel, enrichContent(chunk), prevContent, nextContent, knowledge.Title, questionCount) + customInstructions := "" + if kb.QuestionGenerationConfig != nil { + customInstructions = kb.QuestionGenerationConfig.CustomInstructions + } + questions, err := s.generateQuestionsWithContext(ctx, chatModel, enrichContent(chunk), prevContent, nextContent, + knowledge.Title, questionCount, customInstructions) if err != nil { llmCallFailed++ logger.Warnf(ctx, "Failed to generate questions for chunk %s: %v", chunk.ID, err) @@ -1815,8 +1820,13 @@ func (s *knowledgeService) processQuestionGenerationForChunks(ctx context.Contex continue } + customInstructions := "" + if kb.QuestionGenerationConfig != nil { + customInstructions = kb.QuestionGenerationConfig.CustomInstructions + } questions, gerr := s.generateQuestionsWithContext( - ctx, chatModel, enrich(chunk), prevContentAt(i), nextContentAt(i), knowledge.Title, questionCount) + ctx, chatModel, enrich(chunk), prevContentAt(i), nextContentAt(i), knowledge.Title, questionCount, + customInstructions) if gerr != nil { llmCallFailed++ logger.Warnf(ctx, "Failed to generate questions for chunk %s: %v", chunk.ID, gerr) @@ -1878,6 +1888,7 @@ func (s *knowledgeService) processQuestionGenerationForChunks(ctx context.Contex // generateQuestionsWithContext generates questions for a chunk with surrounding context func (s *knowledgeService) generateQuestionsWithContext(ctx context.Context, chatModel chat.Chat, content, prevContent, nextContent, docName string, questionCount int, + customInstructions string, ) ([]string, error) { if content == "" || questionCount <= 0 { return nil, nil @@ -1909,6 +1920,7 @@ func (s *knowledgeService) generateQuestionsWithContext(ctx context.Context, "doc_name": docName, "language": langName, }) + prompt = types.AppendCustomPromptInstructions(prompt, customInstructions, "question_generation") thinking := false response, err := chatModel.Chat(ctx, []chat.Message{ diff --git a/internal/application/service/knowledge_process_config.go b/internal/application/service/knowledge_process_config.go index c1b6bb94..13329e7a 100644 --- a/internal/application/service/knowledge_process_config.go +++ b/internal/application/service/knowledge_process_config.go @@ -34,13 +34,24 @@ func ResolveProcessConfig(kb *types.KnowledgeBase, overrides *types.KnowledgePro eff.EnableMultimodel = *overrides.EnableMultimodel } if overrides.VLMConfig != nil { + base := eff.VLMConfig eff.VLMConfig = *overrides.VLMConfig + if eff.VLMConfig.DescriptionLanguage == "" { + eff.VLMConfig.DescriptionLanguage = base.DescriptionLanguage + } + if eff.VLMConfig.CustomInstructions == "" { + eff.VLMConfig.CustomInstructions = base.CustomInstructions + } } if overrides.ASRConfig != nil { eff.ASRConfig = *overrides.ASRConfig } if overrides.QuestionGenerationConfig != nil { + base := eff.QuestionGenerationConfig eff.QuestionGenerationConfig = *overrides.QuestionGenerationConfig + if eff.QuestionGenerationConfig.CustomInstructions == "" { + eff.QuestionGenerationConfig.CustomInstructions = base.CustomInstructions + } } if overrides.GraphEnabled != nil { eff.GraphEnabled = *overrides.GraphEnabled @@ -189,6 +200,9 @@ func mergeChunkingConfig(base types.ChunkingConfig, override *types.ChunkingConf if len(override.Languages) > 0 { result.Languages = override.Languages } + if override.TableMetadataInstructions != "" { + result.TableMetadataInstructions = override.TableMetadataInstructions + } return result } @@ -210,6 +224,9 @@ func mergeExtractConfig(base types.ExtractConfig, override *types.ExtractConfig) if len(override.Relations) > 0 { result.Relations = override.Relations } + if override.CustomInstructions != "" { + result.CustomInstructions = override.CustomInstructions + } return result } diff --git a/internal/application/service/knowledge_process_config_test.go b/internal/application/service/knowledge_process_config_test.go index 41a10b27..3bef90aa 100644 --- a/internal/application/service/knowledge_process_config_test.go +++ b/internal/application/service/knowledge_process_config_test.go @@ -199,6 +199,34 @@ func TestResolveProcessConfig_ExtractConfigFieldMerge(t *testing.T) { require.Equal(t, []string{"override-tag"}, eff.ExtractConfig.Tags) } +func TestResolveProcessConfig_PreservesKnowledgeBasePromptInstructions(t *testing.T) { + t.Parallel() + + kb := &types.KnowledgeBase{ + ChunkingConfig: types.ChunkingConfig{TableMetadataInstructions: "table context"}, + VLMConfig: types.VLMConfig{ + Enabled: true, ModelID: "vlm-1", DescriptionLanguage: "English", CustomInstructions: "read labels", + }, + QuestionGenerationConfig: &types.QuestionGenerationConfig{ + Enabled: true, QuestionCount: 3, CustomInstructions: "customer questions", + }, + ExtractConfig: &types.ExtractConfig{Enabled: true, CustomInstructions: "contract entities"}, + } + overrides := &types.KnowledgeProcessOverrides{ + ChunkingConfig: &types.ChunkingConfig{ChunkSize: 256}, + VLMConfig: &types.VLMConfig{Enabled: true, ModelID: "vlm-2"}, + QuestionGenerationConfig: &types.QuestionGenerationConfig{Enabled: true, QuestionCount: 5}, + ExtractConfig: &types.ExtractConfig{Enabled: true}, + } + + eff := ResolveProcessConfig(kb, overrides) + require.Equal(t, "table context", eff.ChunkingConfig.TableMetadataInstructions) + require.Equal(t, "English", eff.VLMConfig.DescriptionLanguage) + require.Equal(t, "read labels", eff.VLMConfig.CustomInstructions) + require.Equal(t, "customer questions", eff.QuestionGenerationConfig.CustomInstructions) + require.Equal(t, "contract entities", eff.ExtractConfig.CustomInstructions) +} + func TestValidateProcessOverrides_NilOverrides(t *testing.T) { t.Parallel() diff --git a/internal/application/service/wiki_ingest.go b/internal/application/service/wiki_ingest.go index c7ca9a10..b30ae0a3 100644 --- a/internal/application/service/wiki_ingest.go +++ b/internal/application/service/wiki_ingest.go @@ -1087,6 +1087,12 @@ type WikiBatchContext struct { // three valid values. ExtractionGranularity types.WikiExtractionGranularity + // ContentInstructions and ExtractionInstructions are KB-scoped business + // guidance. Stable citation, merge, taxonomy and JSON rules remain in the + // system templates and cannot be replaced by these fields. + ContentInstructions string + ExtractionInstructions string + // PlannedFolderID holds the per-slug wiki_folders.id assigned by the batch // taxonomy planning pass (planBatchTaxonomy + folder resolution), keyed by // page slug. Reduce applies it only to pages that aren't already filed @@ -1729,6 +1735,7 @@ func formatExistingTaxonomyForPrompt(paths [][]string) string { } return strings.TrimSpace(buf.String()) } + // getExistingPageSlugsForKnowledge returns all page slugs that currently // reference a given knowledge ID in their source_refs. Used to snapshot // state before re-ingest so the reduce phase can reconcile additions vs @@ -1848,7 +1855,9 @@ const indexIntroSummaryCap = 200 // // The intro is written to both Content and Summary so legacy readers // that fall through to Summary stay in sync. -func (s *wikiIngestService) rebuildIndexPage(ctx context.Context, chatModel chat.Chat, payload WikiIngestPayload, changeDesc, lang string) error { +func (s *wikiIngestService) rebuildIndexPage(ctx context.Context, chatModel chat.Chat, payload WikiIngestPayload, + changeDesc, lang, customInstructions string, +) error { indexPage, _ := s.wikiService.GetIndex(ctx, payload.KnowledgeBaseID) if indexPage == nil { return nil @@ -1902,8 +1911,10 @@ func (s *wikiIngestService) rebuildIndexPage(ctx context.Context, chatModel chat docSummaries.WriteString("(no documents yet)") } generatedIntro, genErr := s.generateWithTemplate(ctx, chatModel, agent.WikiIndexIntroPrompt, map[string]string{ - "DocumentSummaries": framing + docSummaries.String(), - "Language": lang, + "DocumentSummaries": framing + docSummaries.String(), + "Language": lang, + "CustomInstructions": customInstructions, + "InstructionScope": "wiki_content", }) if genErr != nil { intro = "# Wiki Index\n\nThis wiki contains knowledge extracted from uploaded documents.\n" @@ -1918,10 +1929,12 @@ func (s *wikiIngestService) rebuildIndexPage(ctx context.Context, chatModel chat // change-description block already encodes the "what just // changed" signal the prompt is asking for. updatedIntro, genErr := s.generateWithTemplate(ctx, chatModel, agent.WikiIndexIntroUpdatePrompt, map[string]string{ - "ExistingIntro": existingIntro, - "ChangeDescription": changeDesc, - "DocumentSummaries": "", - "Language": lang, + "ExistingIntro": existingIntro, + "ChangeDescription": changeDesc, + "DocumentSummaries": "", + "Language": lang, + "CustomInstructions": customInstructions, + "InstructionScope": "wiki_content", }) if genErr != nil { intro = existingIntro // keep existing on error @@ -2233,6 +2246,7 @@ func (s *wikiIngestService) generateWithTemplate(ctx context.Context, chatModel } prompt := buf.String() + prompt = types.AppendCustomPromptInstructions(prompt, data["CustomInstructions"], data["InstructionScope"]) thinking := false var lastErr error diff --git a/internal/application/service/wiki_ingest_batch.go b/internal/application/service/wiki_ingest_batch.go index c5bbf35a..3ac75f83 100644 --- a/internal/application/service/wiki_ingest_batch.go +++ b/internal/application/service/wiki_ingest_batch.go @@ -70,7 +70,7 @@ func (s *wikiIngestService) scheduleFollowUp(ctx context.Context, payload WikiIn // isolated view. func (s *wikiIngestService) newWikiBatchContext( kbID string, - granularity types.WikiExtractionGranularity, + wikiConfig *types.WikiConfig, ) *WikiBatchContext { var ( fetchMu sync.Mutex @@ -165,6 +165,14 @@ func (s *wikiIngestService) newWikiBatchContext( return out } + granularity := types.WikiExtractionStandard + contentInstructions := "" + extractionInstructions := "" + if wikiConfig != nil { + granularity = wikiConfig.ExtractionGranularity.Normalize() + contentInstructions = wikiConfig.ContentInstructions + extractionInstructions = wikiConfig.ExtractionInstructions + } return &WikiBatchContext{ SlugTitle: func(ctx context.Context, slug string) string { m := resolveSlugs(ctx, []string{slug}) @@ -175,7 +183,9 @@ func (s *wikiIngestService) newWikiBatchContext( m := resolveSummaries(ctx, []string{kid}) return m[kid] }, - ExtractionGranularity: granularity, + ExtractionGranularity: granularity, + ContentInstructions: contentInstructions, + ExtractionInstructions: extractionInstructions, } } @@ -387,12 +397,12 @@ func (s *wikiIngestService) ProcessWikiIngest(ctx context.Context, t *asynq.Task // empty/unknown values fall back to Standard via Normalize(). Failures // to load the KB (unlikely since we're already acting on it) also // degrade gracefully to Standard. - granularity := types.WikiExtractionStandard + var wikiConfig *types.WikiConfig if kb, kbErr := s.kbService.GetKnowledgeBaseByID(ctx, payload.KnowledgeBaseID); kbErr == nil && kb != nil && kb.WikiConfig != nil { - granularity = kb.WikiConfig.ExtractionGranularity.Normalize() + wikiConfig = kb.WikiConfig } - batchCtx := s.newWikiBatchContext(payload.KnowledgeBaseID, granularity) + batchCtx := s.newWikiBatchContext(payload.KnowledgeBaseID, wikiConfig) // 1. MAP PHASE (Parallel extraction and generation of updates) var mapMu sync.Mutex @@ -1025,11 +1035,7 @@ func (s *wikiIngestService) ProcessWikiFinalize(ctx context.Context, t *asynq.Ta logger.Warnf(ctx, "wiki finalize: no synthesis model for KB %s, skipping index rebuild", payload.KnowledgeBaseID) } - granularity := types.WikiExtractionStandard - if kb.WikiConfig != nil { - granularity = kb.WikiConfig.ExtractionGranularity.Normalize() - } - batchCtx := s.newWikiBatchContext(payload.KnowledgeBaseID, granularity) + batchCtx := s.newWikiBatchContext(payload.KnowledgeBaseID, kb.WikiConfig) lang := types.LanguageNameFromContext(ctx) indexRebuilt := false @@ -1037,7 +1043,8 @@ func (s *wikiIngestService) ProcessWikiFinalize(ctx context.Context, t *asynq.Ta chatModel, mErr := s.modelService.GetChatModel(ctx, synthesisModelID) if mErr != nil { logger.Warnf(ctx, "wiki finalize: get chat model failed: %v", mErr) - } else if err := s.rebuildIndexPage(ctx, chatModel, payload, changeDesc.String(), lang); err != nil { + } else if err := s.rebuildIndexPage(ctx, chatModel, payload, changeDesc.String(), lang, + batchCtx.ContentInstructions); err != nil { logger.Warnf(ctx, "wiki finalize: rebuild index failed: %v", err) } else { indexRebuilt = true @@ -1244,9 +1251,11 @@ func (s *wikiIngestService) mapOneDocument( go func() { defer wg.Done() summaryContent, summaryErr = s.generateWithTemplate(ctx, chatModel, agent.WikiSummaryPrompt, map[string]string{ - "Content": content, - "Language": lang, - "ExtractedSlugs": slugListing, + "Content": content, + "Language": lang, + "ExtractedSlugs": slugListing, + "CustomInstructions": batchCtx.ContentInstructions, + "InstructionScope": "wiki_content", }) if summaryErr != nil { s.tracker().FailSpan(ctx, summarySpan, "SUMMARY_FAILED", summaryErr.Error(), summaryErr) @@ -1541,9 +1550,11 @@ func (s *wikiIngestService) extractEntitiesAndConceptsNoUpsert( } extractionJSON, err := s.generateWithTemplate(ctx, chatModel, agent.WikiKnowledgeExtractPrompt, map[string]string{ - "Content": content, - "Language": lang, - "PreviousSlugs": prevSlugsText, + "Content": content, + "Language": lang, + "PreviousSlugs": prevSlugsText, + "CustomInstructions": batchCtx.ExtractionInstructions, + "InstructionScope": "wiki_extraction", }) if err != nil { return nil, nil, nil, fmt.Errorf("combined extraction failed: %w", err) @@ -1908,6 +1919,8 @@ func (s *wikiIngestService) reduceSlugUpdates( "RemainingSourcesContent": remainingSourcesContent.String(), "AvailableSlugs": relatedSlugs.String(), "Language": language, + "CustomInstructions": batchCtx.ContentInstructions, + "InstructionScope": "wiki_content", }) if err == nil && updatedContent != "" { diff --git a/internal/application/service/wiki_ingest_cite.go b/internal/application/service/wiki_ingest_cite.go index 48c68440..ea112ef4 100644 --- a/internal/application/service/wiki_ingest_cite.go +++ b/internal/application/service/wiki_ingest_cite.go @@ -98,6 +98,8 @@ func (s *wikiIngestService) extractCandidateSlugs( "PreviousSlugs": prevSlugsText, "Granularity": string(granularity), "GranularityGuidance": agent.WikiGranularityGuidance(string(granularity)), + "CustomInstructions": batchCtx.ExtractionInstructions, + "InstructionScope": "wiki_extraction", }) if err != nil { return nil, nil, nil, fmt.Errorf("candidate slug extraction failed: %w", err) diff --git a/internal/handler/initialization.go b/internal/handler/initialization.go index ad8c9d3d..b4bf8a61 100644 --- a/internal/handler/initialization.go +++ b/internal/handler/initialization.go @@ -111,9 +111,10 @@ type KBModelConfigRequest struct { // from "field present with empty/zero value" (clear / disable). // Without that distinction, users could set strategy="auto" once // but never reset it back to legacy / unset. - Strategy *string `json:"strategy,omitempty"` - TokenLimit *int `json:"tokenLimit,omitempty"` - Languages *[]string `json:"languages,omitempty"` + Strategy *string `json:"strategy,omitempty"` + TokenLimit *int `json:"tokenLimit,omitempty"` + Languages *[]string `json:"languages,omitempty"` + TableMetadataInstructions *string `json:"tableMetadataInstructions,omitempty"` } `json:"documentSplitting"` // 多模态配置(仅模型相关;存储引擎在 storageProvider 中配置) @@ -126,17 +127,19 @@ type KBModelConfigRequest struct { // 知识图谱配置 NodeExtract struct { - Enabled bool `json:"enabled"` - Text string `json:"text"` - Tags []string `json:"tags"` - Nodes []types.GraphNode `json:"nodes"` - Relations []types.GraphRelation `json:"relations"` + Enabled bool `json:"enabled"` + Text string `json:"text"` + Tags []string `json:"tags"` + Nodes []types.GraphNode `json:"nodes"` + Relations []types.GraphRelation `json:"relations"` + CustomInstructions string `json:"customInstructions"` } `json:"nodeExtract"` // 问题生成配置 QuestionGeneration struct { - Enabled bool `json:"enabled"` - QuestionCount int `json:"questionCount"` + Enabled bool `json:"enabled"` + QuestionCount int `json:"questionCount"` + CustomInstructions string `json:"customInstructions"` } `json:"questionGeneration"` } @@ -344,6 +347,9 @@ func (h *InitializationHandler) UpdateKBConfig(c *gin.Context) { if req.DocumentSplitting.Languages != nil { kb.ChunkingConfig.Languages = *req.DocumentSplitting.Languages } + if req.DocumentSplitting.TableMetadataInstructions != nil { + kb.ChunkingConfig.TableMetadataInstructions = strings.TrimSpace(*req.DocumentSplitting.TableMetadataInstructions) + } // 更新多模态配置 if req.Multimodal.Enabled { @@ -351,6 +357,10 @@ func (h *InitializationHandler) UpdateKBConfig(c *gin.Context) { } else { kb.VLMConfig.ModelID = "" } + if req.VLMConfig != nil { + kb.VLMConfig.DescriptionLanguage = strings.TrimSpace(req.VLMConfig.DescriptionLanguage) + kb.VLMConfig.CustomInstructions = strings.TrimSpace(req.VLMConfig.CustomInstructions) + } // 存储引擎:仅写入 provider 到新字段,参数从租户全局 StorageEngineConfig 读取 provider := strings.ToLower(strings.TrimSpace(req.StorageProvider)) @@ -387,11 +397,12 @@ func (h *InitializationHandler) UpdateKBConfig(c *gin.Context) { } kb.ExtractConfig = &types.ExtractConfig{ - Enabled: req.NodeExtract.Enabled, - Text: req.NodeExtract.Text, - Tags: req.NodeExtract.Tags, - Nodes: nodes, - Relations: relations, + Enabled: req.NodeExtract.Enabled, + Text: req.NodeExtract.Text, + Tags: req.NodeExtract.Tags, + Nodes: nodes, + Relations: relations, + CustomInstructions: strings.TrimSpace(req.NodeExtract.CustomInstructions), } } else { kb.ExtractConfig = &types.ExtractConfig{Enabled: false} @@ -412,11 +423,19 @@ func (h *InitializationHandler) UpdateKBConfig(c *gin.Context) { questionCount = 10 } kb.QuestionGenerationConfig = &types.QuestionGenerationConfig{ - Enabled: true, - QuestionCount: questionCount, + Enabled: true, + QuestionCount: questionCount, + CustomInstructions: strings.TrimSpace(req.QuestionGeneration.CustomInstructions), } } else { - kb.QuestionGenerationConfig = &types.QuestionGenerationConfig{Enabled: false} + kb.QuestionGenerationConfig = &types.QuestionGenerationConfig{ + Enabled: false, + CustomInstructions: strings.TrimSpace(req.QuestionGeneration.CustomInstructions), + } + } + if err := validateKnowledgeBasePromptInstructions(kb); err != nil { + c.Error(err) + return } // 保存更新后的知识库 diff --git a/internal/handler/knowledgebase.go b/internal/handler/knowledgebase.go index 9c84a305..009f648b 100644 --- a/internal/handler/knowledgebase.go +++ b/internal/handler/knowledgebase.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" stderrors "errors" + "fmt" "net/http" "strconv" "strings" @@ -361,6 +362,10 @@ func (h *KnowledgeBaseHandler) CreateKnowledgeBase(c *gin.Context) { c.Error(err) return } + if err := validateKnowledgeBasePromptInstructions(&req); err != nil { + c.Error(err) + return + } provider := strings.ToLower(strings.TrimSpace(req.GetStorageProvider())) if provider != "" && !isStorageProviderAllowed(provider) { c.Error(apperrors.NewBadRequestError("Storage provider is not allowed by STORAGE_ALLOW_LIST")) @@ -853,6 +858,17 @@ func (h *KnowledgeBaseHandler) UpdateKnowledgeBase(c *gin.Context) { c.Error(apperrors.NewBadRequestError("Invalid request parameters").WithDetails(err.Error())) return } + if req.Config != nil { + probe := &types.KnowledgeBase{ + ChunkingConfig: req.Config.ChunkingConfig, + ImageProcessingConfig: req.Config.ImageProcessingConfig, + WikiConfig: req.Config.WikiConfig, + } + if err := validateKnowledgeBasePromptInstructions(probe); err != nil { + c.Error(err) + return + } + } logger.Infof(ctx, "Updating knowledge base, ID: %s, name: %s", secutils.SanitizeForLog(id), secutils.SanitizeForLog(req.Name)) @@ -1293,6 +1309,32 @@ func validateExtractConfig(config *types.ExtractConfig) error { return nil } +func validateKnowledgeBasePromptInstructions(kb *types.KnowledgeBase) error { + if kb == nil { + return nil + } + fields := map[string]string{ + "table metadata instructions": kb.ChunkingConfig.TableMetadataInstructions, + "image instructions": kb.VLMConfig.CustomInstructions, + } + if kb.WikiConfig != nil { + fields["wiki content instructions"] = kb.WikiConfig.ContentInstructions + fields["wiki extraction instructions"] = kb.WikiConfig.ExtractionInstructions + } + if kb.QuestionGenerationConfig != nil { + fields["question generation instructions"] = kb.QuestionGenerationConfig.CustomInstructions + } + if kb.ExtractConfig != nil { + fields["graph extraction instructions"] = kb.ExtractConfig.CustomInstructions + } + for name, value := range fields { + if len([]rune(value)) > types.MaxCustomPromptInstructionsLength { + return apperrors.NewBadRequestError(fmt.Sprintf("%s exceeds %d characters", name, types.MaxCustomPromptInstructionsLength)) + } + } + return nil +} + // ListMoveTargets returns knowledge bases eligible as move targets for the given source KB. // Filters: same Type, same EmbeddingModelID, different ID, not temporary. // diff --git a/internal/types/knowledgebase.go b/internal/types/knowledgebase.go index f26986ee..d96d4960 100644 --- a/internal/types/knowledgebase.go +++ b/internal/types/knowledgebase.go @@ -178,6 +178,10 @@ type ChunkingConfig struct { // Languages hints the heuristic patterns. Empty = auto-detect from content. // Examples: ["de"], ["en", "zh"]. Languages []string `yaml:"languages,omitempty" json:"languages,omitempty"` + // TableMetadataInstructions contains optional business guidance used when + // generating searchable summaries for CSV/Excel tables. The system-owned + // output contract remains fixed; these instructions only add domain context. + TableMetadataInstructions string `yaml:"table_metadata_instructions,omitempty" json:"table_metadata_instructions,omitempty"` } // ResolveParserEngine returns the engine name for the given file type @@ -387,6 +391,12 @@ func (c *ImageProcessingConfig) Scan(value interface{}) error { type VLMConfig struct { Enabled bool `yaml:"enabled" json:"enabled"` ModelID string `yaml:"model_id" json:"model_id"` + // DescriptionLanguage controls the language used for generated image + // captions. Empty means follow the document/request language. + DescriptionLanguage string `yaml:"description_language,omitempty" json:"description_language,omitempty"` + // CustomInstructions adds KB-specific image interpretation guidance without + // replacing the system-owned OCR and Markdown output contract. + CustomInstructions string `yaml:"custom_instructions,omitempty" json:"custom_instructions,omitempty"` // 兼容老版本 // Model Name @@ -421,6 +431,9 @@ type QuestionGenerationConfig struct { Enabled bool `yaml:"enabled" json:"enabled"` // Number of questions to generate per chunk (default: 3, max: 10) QuestionCount int `yaml:"question_count" json:"question_count"` + // CustomInstructions describes the intended audience or question style. + // It is appended to the stable system question-generation template. + CustomInstructions string `yaml:"custom_instructions,omitempty" json:"custom_instructions,omitempty"` } // Value implements the driver.Valuer interface @@ -493,6 +506,9 @@ type ExtractConfig struct { Tags []string `yaml:"tags" json:"tags,omitempty"` Nodes []*GraphNode `yaml:"nodes" json:"nodes,omitempty"` Relations []*GraphRelation `yaml:"relations" json:"relations,omitempty"` + // CustomInstructions adds domain-specific extraction guidance while the + // system keeps ownership of the structured graph output protocol. + CustomInstructions string `yaml:"custom_instructions,omitempty" json:"custom_instructions,omitempty"` } // Value implements the driver.Valuer interface, used to convert ExtractConfig to database value diff --git a/internal/types/prompt_instructions.go b/internal/types/prompt_instructions.go new file mode 100644 index 00000000..6ba5973c --- /dev/null +++ b/internal/types/prompt_instructions.go @@ -0,0 +1,25 @@ +package types + +import ( + "fmt" + "strings" +) + +// MaxCustomPromptInstructionsLength bounds user-authored business guidance. +// These fields are intentionally much smaller than full prompt templates. +const MaxCustomPromptInstructionsLength = 4000 + +// AppendCustomPromptInstructions appends user-authored business guidance to a +// system-owned prompt. Stable output, safety and citation rules always win. +func AppendCustomPromptInstructions(prompt, instructions, label string) string { + instructions = strings.TrimSpace(instructions) + if instructions == "" { + return prompt + } + if label == "" { + label = "custom" + } + return fmt.Sprintf("%s\n\n<%s_business_instructions>\n%s\n\n"+ + "Apply these business instructions only when they do not conflict with the system-owned output format, citation, safety, or factuality rules.", + strings.TrimSpace(prompt), label, instructions, label) +} diff --git a/internal/types/prompt_instructions_test.go b/internal/types/prompt_instructions_test.go new file mode 100644 index 00000000..6c167a20 --- /dev/null +++ b/internal/types/prompt_instructions_test.go @@ -0,0 +1,24 @@ +package types + +import ( + "strings" + "testing" +) + +func TestAppendCustomPromptInstructions(t *testing.T) { + t.Run("empty preserves prompt", func(t *testing.T) { + if got := AppendCustomPromptInstructions("base", " ", "wiki"); got != "base" { + t.Fatalf("got %q", got) + } + }) + + t.Run("appends bounded business guidance after base", func(t *testing.T) { + got := AppendCustomPromptInstructions("base", " Focus on contracts. ", "wiki") + if !strings.HasPrefix(got, "base\n\n") { + t.Fatalf("unexpected prefix: %q", got) + } + if !strings.Contains(got, "Focus on contracts.") || !strings.Contains(got, "do not conflict") { + t.Fatalf("missing guidance or precedence rule: %q", got) + } + }) +} diff --git a/internal/types/wiki_page.go b/internal/types/wiki_page.go index 90943b24..a8251351 100644 --- a/internal/types/wiki_page.go +++ b/internal/types/wiki_page.go @@ -347,6 +347,12 @@ type WikiConfig struct { // ExtractionGranularity controls how many candidate slugs Pass 0 extracts // per document. Empty / unknown value is treated as WikiExtractionStandard. ExtractionGranularity WikiExtractionGranularity `yaml:"extraction_granularity" json:"extraction_granularity,omitempty"` + // ContentInstructions controls tone, structure and emphasis for generated + // summary/entity/index prose. Citation and merge rules remain system-owned. + ContentInstructions string `yaml:"content_instructions,omitempty" json:"content_instructions,omitempty"` + // ExtractionInstructions tells candidate extraction which domain concepts + // to emphasize without replacing the stable JSON/citation protocol. + ExtractionInstructions string `yaml:"extraction_instructions,omitempty" json:"extraction_instructions,omitempty"` // Wiki ingest concurrency is two-level: // 1. batch-level: multiple batches per KB run concurrently in the wiki @@ -465,7 +471,6 @@ type WikiPageListResponse struct { TotalPages int `json:"total_pages"` } - // WikiGraphMode enumerates the graph query modes exposed to the API. const ( // WikiGraphModeOverview returns the top-N most-connected pages as an