Files
ragflow/web/src/interfaces/database/chat.ts

227 lines
4.5 KiB
TypeScript
Raw Normal View History

import { MessageType } from '@/constants/chat';
import { IAttachment } from '@/hooks/use-send-message';
export interface IDocumentDownloadInfo {
doc_id: string;
filename: string;
mime_type: string;
size?: number;
}
export interface PromptConfig {
empty_response: string;
parameters: Parameter[];
prologue: string;
system: string;
tts?: boolean;
quote: boolean;
keyword: boolean;
refine_multiturn: boolean;
use_kg: boolean;
reasoning?: boolean;
cross_languages?: Array<string>;
tavily_api_key?: string;
toc_enhance?: boolean;
Feat/configurable metadata display (#13464) ### What problem does this PR solve? Currently, RAGFlow's Search and Chat interfaces display only raw vectorized text chunks during retrieval, without contextual information about their source documents. Users cannot see document titles, page numbers, upload dates, or custom metadata fields that would help them understand and trust the retrieved results. This PR introduces an **optional metadata display feature** that enriches retrieved chunks with document-level metadata in both the Search tab and Chatbot interface. **Key improvements:** - **Search results**: Display document metadata as styled badges beneath chunk snippets - **Chat citations**: Show metadata in citation popovers and reference lists for better source context - **LLM context**: Metadata is injected into the LLM prompt to enable more accurate, citation-aware responses - **External API support**: Applications using RAGFlow's SDK retrieval endpoints (`/v1/retrieval`, `/v1/searchbots/retrieval_test`) can opt-in via request parameters - **User control**: Multi-select dropdown UI allows users to choose which metadata fields to display **Implementation approach:** - ✅ Reuses existing `DocMetadataService` infrastructure (no new database tables or indices) - ✅ Settings stored in existing JSON configuration fields (`search_config.reference_metadata`, `prompt_config.reference_metadata`) - ✅ No database migrations required - ✅ Disabled by default (fully opt-in and backward-compatible) - ✅ Dynamic metadata field selection populated from actual document metadata keys - ✅ Fixed critical bug where Python's builtin `set()` was shadowed by a route handler function **Modified endpoints (all backward-compatible):** - `POST /v1/retrieval` (Public SDK) - `POST /v1/searchbots/retrieval_test` (Searchbots) - `POST /v1/chunk/retrieval_test` (UI/Internal) - Chat completions endpoints (via `extra_body.reference_metadata` or `prompt_config`) ### Type of change - [x] New Feature (non-breaking change which adds functionality) ###Images - <img width="879" height="1275" alt="image" src="https://github.com/user-attachments/assets/95b2d731-31ae-45a1-b081-bf5893f52aeb" /> <br><br> <br><br> <img width="1532" height="362" alt="image" src="https://github.com/user-attachments/assets/9cebc65b-b7a7-459f-b25e-3b13fa9b638e" /> <br><br> <br><br> <img width="2586" height="1320" alt="image" src="https://github.com/user-attachments/assets/2153d493-d899-461f-a7a9-041391e07776" /> --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: Attili-sys <Attili-sys@users.noreply.github.com> Co-authored-by: Ahmad Intisar <ahmadintisar@Ahmads-MacBook-M4-Pro.local>
2026-04-30 18:13:27 +03:00
reference_metadata?: {
include?: boolean;
fields?: string[];
};
}
export interface Parameter {
key: string;
optional: boolean;
}
export interface LlmSetting {
Creative: Variable;
Custom: Variable;
Evenly: Variable;
Precise: Variable;
}
export interface Variable {
frequency_penalty?: number;
max_tokens?: number;
presence_penalty?: number;
temperature?: number;
top_p?: number;
tenant_llm_id?: string;
model_type?: string;
}
export interface IDialog {
create_date: string;
create_time: number;
description: string;
icon: string;
id: string;
dialog_id?: string;
dataset_ids: string[];
kb_names: string[];
language: string;
llm_id: string;
tenant_llm_id?: string;
llm_setting: Variable;
llm_setting_type?: string;
name: string;
prompt_config: PromptConfig;
prompt_type: string;
status: string;
tenant_id: string;
update_date: string;
update_time: number;
vector_similarity_weight: number;
similarity_threshold: number;
top_k: number;
top_n: number;
rerank_id?: string;
meta_data_filter: MetaDataFilter;
}
interface MetaDataFilter {
manual: Manual[];
method: string;
}
interface Manual {
key: string;
op: string;
value: string;
}
export interface IConversation {
create_date: string;
create_time: number;
chat_id: string;
id: string;
avatar: string;
messages: Message[];
reference: IReference[];
name: string;
update_date: string;
update_time: number;
is_new: true;
}
export interface Message {
content: string;
role: MessageType;
doc_ids?: string[];
prompt?: string;
id?: string;
audio_binary?: string;
data?: any;
files?: (File | UploadResponseDataType)[];
chatBoxId?: string;
attachment?: IAttachment;
downloads?: IDocumentDownloadInfo[];
}
export interface IReferenceChunk {
id: string;
content: null;
document_id: string;
document_name: string;
dataset_id: string;
image_id: string;
similarity: number;
vector_similarity: number;
term_similarity: number;
positions: number[];
doc_type?: string;
Feat/configurable metadata display (#13464) ### What problem does this PR solve? Currently, RAGFlow's Search and Chat interfaces display only raw vectorized text chunks during retrieval, without contextual information about their source documents. Users cannot see document titles, page numbers, upload dates, or custom metadata fields that would help them understand and trust the retrieved results. This PR introduces an **optional metadata display feature** that enriches retrieved chunks with document-level metadata in both the Search tab and Chatbot interface. **Key improvements:** - **Search results**: Display document metadata as styled badges beneath chunk snippets - **Chat citations**: Show metadata in citation popovers and reference lists for better source context - **LLM context**: Metadata is injected into the LLM prompt to enable more accurate, citation-aware responses - **External API support**: Applications using RAGFlow's SDK retrieval endpoints (`/v1/retrieval`, `/v1/searchbots/retrieval_test`) can opt-in via request parameters - **User control**: Multi-select dropdown UI allows users to choose which metadata fields to display **Implementation approach:** - ✅ Reuses existing `DocMetadataService` infrastructure (no new database tables or indices) - ✅ Settings stored in existing JSON configuration fields (`search_config.reference_metadata`, `prompt_config.reference_metadata`) - ✅ No database migrations required - ✅ Disabled by default (fully opt-in and backward-compatible) - ✅ Dynamic metadata field selection populated from actual document metadata keys - ✅ Fixed critical bug where Python's builtin `set()` was shadowed by a route handler function **Modified endpoints (all backward-compatible):** - `POST /v1/retrieval` (Public SDK) - `POST /v1/searchbots/retrieval_test` (Searchbots) - `POST /v1/chunk/retrieval_test` (UI/Internal) - Chat completions endpoints (via `extra_body.reference_metadata` or `prompt_config`) ### Type of change - [x] New Feature (non-breaking change which adds functionality) ###Images - <img width="879" height="1275" alt="image" src="https://github.com/user-attachments/assets/95b2d731-31ae-45a1-b081-bf5893f52aeb" /> <br><br> <br><br> <img width="1532" height="362" alt="image" src="https://github.com/user-attachments/assets/9cebc65b-b7a7-459f-b25e-3b13fa9b638e" /> <br><br> <br><br> <img width="2586" height="1320" alt="image" src="https://github.com/user-attachments/assets/2153d493-d899-461f-a7a9-041391e07776" /> --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: Attili-sys <Attili-sys@users.noreply.github.com> Co-authored-by: Ahmad Intisar <ahmadintisar@Ahmads-MacBook-M4-Pro.local>
2026-04-30 18:13:27 +03:00
document_metadata?: Record<string, any>;
}
export interface IReference {
chunks: IReferenceChunk[];
doc_aggs: Docagg[];
total: number;
}
export interface IReferenceObject {
chunks: Record<string, IReferenceChunk>;
doc_aggs: Record<string, Docagg>;
}
export interface IAnswer {
answer: string;
attachment?: IAttachment;
downloads?: IDocumentDownloadInfo[];
reference?: IReference;
conversationId?: string;
prompt?: string;
id?: string;
audio_binary?: string;
data?: any;
chatBoxId?: string;
}
export interface Docagg {
count: number;
doc_id: string;
doc_name: string;
url?: string;
}
// interface Chunk {
// chunk_id: string;
// content_ltks: string;
// content_with_weight: string;
// doc_id: string;
// docnm_kwd: string;
// img_id: string;
// important_kwd: any[];
// kb_id: string;
// similarity: number;
// term_similarity: number;
// vector_similarity: number;
// }
export interface IToken {
create_date: string;
create_time: number;
tenant_id: string;
token: string;
update_date?: any;
update_time?: any;
beta: string;
}
export interface IStats {
pv: [string, number][];
uv: [string, number][];
speed: [string, number][];
tokens: [string, number][];
round: [string, number][];
thumb_up: [string, number][];
}
export interface IExternalChatInfo {
avatar?: string;
title: string;
prologue?: string;
has_tavily_key?: boolean;
}
export interface IMessage extends Message {
id: string;
reference?: IReference; // the latest news has reference
conversationId?: string; // To distinguish which conversation the message belongs to
}
export interface IClientConversation extends IConversation {
messages: IMessage[];
}
export interface UploadResponseDataType {
created_at: number;
created_by: string;
extension: string;
id: string;
mime_type: string;
name: string;
preview_url: null;
size: number;
}