Merge branch 'PingChuan' into shiy-login
# Conflicts: # app/config/api-config.ts
This commit is contained in:
@@ -121,7 +121,7 @@ export async function toggleDocumentStatus(
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件到知识库
|
||||
* 上传文件到知识库(使用默认配置)
|
||||
*
|
||||
* @param datasetId - 知识库 ID
|
||||
* @param file - 文件对象
|
||||
@@ -132,19 +132,44 @@ export async function uploadDocument(
|
||||
datasetId: string,
|
||||
file: File,
|
||||
onProgress?: (percent: number) => void
|
||||
): Promise<any> {
|
||||
): Promise<UploadDocumentResponse> {
|
||||
return uploadDocumentWithConfig(
|
||||
datasetId,
|
||||
file,
|
||||
{
|
||||
indexing_technique: 'high_quality',
|
||||
process_rule: { mode: 'automatic' },
|
||||
},
|
||||
onProgress
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件到知识库(使用自定义配置)
|
||||
*
|
||||
* @param datasetId - 知识库 ID
|
||||
* @param file - 文件对象
|
||||
* @param config - 上传配置(索引模式、分段规则等)
|
||||
* @param onProgress - 上传进度回调
|
||||
* @returns 创建的文档信息,包含 batch 用于查询索引进度
|
||||
*/
|
||||
export async function uploadDocumentWithConfig(
|
||||
datasetId: string,
|
||||
file: File,
|
||||
config: UploadDocumentConfig,
|
||||
onProgress?: (percent: number) => void
|
||||
): Promise<UploadDocumentResponse> {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
formData.append('data', JSON.stringify({
|
||||
indexing_technique: 'high_quality',
|
||||
process_rule: {
|
||||
mode: 'automatic',
|
||||
},
|
||||
}));
|
||||
formData.append('data', JSON.stringify(config));
|
||||
|
||||
console.log('[Dataset Client] 上传文档:', { datasetId, fileName: file.name });
|
||||
console.log('[Dataset Client] 上传文档:', {
|
||||
datasetId,
|
||||
fileName: file.name,
|
||||
config,
|
||||
});
|
||||
|
||||
const response = await axios.post(
|
||||
const response = await axios.post<UploadDocumentResponse>(
|
||||
`${API_URL}/datasets/${datasetId}/documents`,
|
||||
formData,
|
||||
{
|
||||
@@ -200,21 +225,41 @@ export async function fetchUploadFileInfo(
|
||||
return response.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 预处理规则 ID
|
||||
*/
|
||||
export type PreProcessingRuleId = 'remove_extra_spaces' | 'remove_urls_emails';
|
||||
|
||||
/**
|
||||
* 预处理规则配置
|
||||
*/
|
||||
export interface PreProcessingRule {
|
||||
id: PreProcessingRuleId;
|
||||
enabled: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分段配置
|
||||
*/
|
||||
export interface SegmentationConfig {
|
||||
separator: string;
|
||||
max_tokens: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义处理规则
|
||||
*/
|
||||
export interface CustomRules {
|
||||
pre_processing_rules?: PreProcessingRule[];
|
||||
segmentation?: SegmentationConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* 文档处理规则配置
|
||||
*/
|
||||
export interface ProcessRule {
|
||||
mode: 'automatic' | 'custom';
|
||||
rules?: {
|
||||
pre_processing_rules?: Array<{
|
||||
id: 'remove_extra_spaces' | 'remove_urls_emails';
|
||||
enabled: boolean;
|
||||
}>;
|
||||
segmentation?: {
|
||||
separator: string;
|
||||
max_tokens: number;
|
||||
};
|
||||
};
|
||||
rules?: CustomRules;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -225,6 +270,45 @@ export interface UpdateDocumentSettings {
|
||||
process_rule?: ProcessRule;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文档配置参数
|
||||
*/
|
||||
export interface UploadDocumentConfig {
|
||||
indexing_technique: 'high_quality' | 'economy';
|
||||
process_rule: ProcessRule;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文档响应
|
||||
*/
|
||||
export interface UploadDocumentResponse {
|
||||
document: {
|
||||
id: string;
|
||||
position: number;
|
||||
data_source_type: string;
|
||||
data_source_info: {
|
||||
upload_file_id: string;
|
||||
};
|
||||
dataset_process_rule_id: string;
|
||||
name: string;
|
||||
created_from: string;
|
||||
created_by: string;
|
||||
created_at: number;
|
||||
tokens: number;
|
||||
indexing_status: string;
|
||||
error: string | null;
|
||||
enabled: boolean;
|
||||
disabled_at: number | null;
|
||||
disabled_by: string | null;
|
||||
archived: boolean;
|
||||
display_status: string;
|
||||
word_count: number;
|
||||
hit_count: number;
|
||||
doc_form: string;
|
||||
};
|
||||
batch: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新文档设置并重新处理
|
||||
* 注意:Dify API 不直接支持修改已有文档的分段设置
|
||||
@@ -252,3 +336,50 @@ export async function updateDocumentWithSettings(
|
||||
);
|
||||
return response.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过文件更新文档
|
||||
* Dify API: POST /datasets/{dataset_id}/documents/{document_id}/update-by-file
|
||||
*
|
||||
* 用于在用户修改分段参数后,使用同一文件重新处理文档
|
||||
*
|
||||
* @param datasetId - 知识库 ID
|
||||
* @param documentId - 文档 ID
|
||||
* @param file - 文件对象(使用原上传的文件)
|
||||
* @param config - 新的分段配置
|
||||
* @param onProgress - 上传进度回调
|
||||
* @returns 更新后的文档信息,包含新的 batch 用于查询索引进度
|
||||
*/
|
||||
export async function updateDocumentByFile(
|
||||
datasetId: string,
|
||||
documentId: string,
|
||||
file: File,
|
||||
config: UploadDocumentConfig,
|
||||
onProgress?: (percent: number) => void
|
||||
): Promise<UploadDocumentResponse> {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
formData.append('data', JSON.stringify(config));
|
||||
|
||||
console.log('[Dataset Client] 通过文件更新文档:', {
|
||||
datasetId,
|
||||
documentId,
|
||||
fileName: file.name,
|
||||
config,
|
||||
});
|
||||
|
||||
const response = await axios.post<UploadDocumentResponse>(
|
||||
`${API_URL}/datasets/${datasetId}/documents/${documentId}/update-by-file`,
|
||||
formData,
|
||||
{
|
||||
withCredentials: true,
|
||||
onUploadProgress: (progressEvent) => {
|
||||
if (progressEvent.total && onProgress) {
|
||||
const percent = Math.round((progressEvent.loaded * 100) / progressEvent.total);
|
||||
onProgress(percent);
|
||||
}
|
||||
},
|
||||
}
|
||||
);
|
||||
return response.data;
|
||||
}
|
||||
|
||||
@@ -18,10 +18,19 @@ export {
|
||||
deleteDocument,
|
||||
toggleDocumentStatus,
|
||||
uploadDocument,
|
||||
uploadDocumentWithConfig,
|
||||
updateDocumentByFile,
|
||||
fetchIndexingStatus,
|
||||
fetchUploadFileInfo,
|
||||
} from './documentApi';
|
||||
|
||||
// 文档 API 类型
|
||||
export type {
|
||||
ProcessRule,
|
||||
UploadDocumentConfig,
|
||||
UploadDocumentResponse,
|
||||
} from './documentApi';
|
||||
|
||||
// 分段、子分段、检索 API
|
||||
export {
|
||||
fetchSegments,
|
||||
|
||||
@@ -332,7 +332,7 @@ export async function deleteChildChunk(
|
||||
*
|
||||
* @param datasetId - 知识库 ID
|
||||
* @param query - 检索关键词
|
||||
* @param retrievalModel - 检索模型配置
|
||||
* @param retrievalModel - 检索模型配置(完整的 Dify API 格式)
|
||||
* @returns 检索结果
|
||||
*/
|
||||
export async function retrieveDataset(
|
||||
@@ -340,7 +340,7 @@ export async function retrieveDataset(
|
||||
query: string,
|
||||
retrievalModel?: RetrieveRequest['retrieval_model']
|
||||
): Promise<RetrieveResponse> {
|
||||
console.log('[Dataset Client] 检索知识库:', { datasetId, query });
|
||||
console.log('[Dataset Client] 检索知识库:', { datasetId, query, retrievalModel });
|
||||
|
||||
const requestBody: RetrieveRequest = {
|
||||
query,
|
||||
|
||||
@@ -42,6 +42,7 @@ export type {
|
||||
MetadataFilterCondition,
|
||||
MetadataFilteringConditions,
|
||||
RetrieveRequest,
|
||||
RetrieveSegment,
|
||||
RetrieveRecord,
|
||||
RetrieveResponse,
|
||||
} from './segmentTypes';
|
||||
|
||||
@@ -4,6 +4,11 @@
|
||||
* @module api/dify-dataset/type/segmentTypes
|
||||
*/
|
||||
|
||||
import type { RetrievalModel } from './datasetTypes';
|
||||
|
||||
// 重新导出以便其他模块使用
|
||||
export type { RetrievalModel };
|
||||
|
||||
// ============================================================================
|
||||
// 分段类型
|
||||
// ============================================================================
|
||||
@@ -154,25 +159,27 @@ export interface MetadataFilteringConditions {
|
||||
*/
|
||||
export interface RetrieveRequest {
|
||||
query: string;
|
||||
retrieval_model?: {
|
||||
search_method: 'keyword_search' | 'semantic_search' | 'full_text_search' | 'hybrid_search';
|
||||
reranking_enable?: boolean;
|
||||
reranking_model?: {
|
||||
reranking_provider_name: string;
|
||||
reranking_model_name: string;
|
||||
};
|
||||
top_k?: number;
|
||||
score_threshold_enabled?: boolean;
|
||||
score_threshold?: number;
|
||||
};
|
||||
retrieval_model?: RetrievalModel;
|
||||
metadata_filtering_conditions?: MetadataFilteringConditions;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检索结果中的分段信息(包含关联文档)
|
||||
*/
|
||||
export interface RetrieveSegment extends Segment {
|
||||
document?: {
|
||||
id: string;
|
||||
data_source_type: string;
|
||||
name: string;
|
||||
doc_type: string | null;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 检索结果记录
|
||||
*/
|
||||
export interface RetrieveRecord {
|
||||
segment: Segment;
|
||||
segment: RetrieveSegment;
|
||||
score: number;
|
||||
tsne_position?: {
|
||||
x: number;
|
||||
|
||||
Reference in New Issue
Block a user