dify
This commit is contained in:
168
schoolNewsWeb/src/apis/ai/document-segment.ts
Normal file
168
schoolNewsWeb/src/apis/ai/document-segment.ts
Normal file
@@ -0,0 +1,168 @@
|
||||
/**
|
||||
* @description Dify 文档分段管理 API
|
||||
* @author AI Assistant
|
||||
* @since 2025-11-04
|
||||
*/
|
||||
|
||||
import { api } from '@/apis/index';
|
||||
import type { ResultDomain } from '@/types';
|
||||
import type {
|
||||
DifySegmentListResponse,
|
||||
DifyChildChunkListResponse,
|
||||
DifyChildChunkResponse,
|
||||
SegmentUpdateRequest,
|
||||
SegmentCreateRequest
|
||||
} from '@/types/ai';
|
||||
|
||||
/**
|
||||
* 文档分段管理 API
|
||||
*/
|
||||
export const documentSegmentApi = {
|
||||
/**
|
||||
* 获取文档的所有分段(父级)
|
||||
* @param datasetId Dify数据集ID
|
||||
* @param documentId Dify文档ID
|
||||
* @returns Promise<ResultDomain<DifySegmentListResponse>>
|
||||
*/
|
||||
async getDocumentSegments(
|
||||
datasetId: string,
|
||||
documentId: string
|
||||
): Promise<ResultDomain<DifySegmentListResponse>> {
|
||||
const response = await api.get<DifySegmentListResponse>(
|
||||
`/ai/dify/datasets/${datasetId}/documents/${documentId}/segments`
|
||||
);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取分段的子块列表
|
||||
* @param datasetId Dify数据集ID
|
||||
* @param documentId Dify文档ID
|
||||
* @param segmentId 分段ID
|
||||
* @returns Promise<ResultDomain<DifyChildChunkListResponse>>
|
||||
*/
|
||||
async getChildChunks(
|
||||
datasetId: string,
|
||||
documentId: string,
|
||||
segmentId: string
|
||||
): Promise<ResultDomain<DifyChildChunkListResponse>> {
|
||||
const response = await api.get<DifyChildChunkListResponse>(
|
||||
`/ai/dify/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}/child_chunks`
|
||||
);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* 更新子块内容
|
||||
* @param datasetId Dify数据集ID
|
||||
* @param documentId Dify文档ID
|
||||
* @param segmentId 分段ID
|
||||
* @param childChunkId 子块ID
|
||||
* @param content 新内容
|
||||
* @returns Promise<ResultDomain<DifyChildChunkResponse>>
|
||||
*/
|
||||
async updateChildChunk(
|
||||
datasetId: string,
|
||||
documentId: string,
|
||||
segmentId: string,
|
||||
childChunkId: string,
|
||||
content: string
|
||||
): Promise<ResultDomain<DifyChildChunkResponse>> {
|
||||
const requestBody: SegmentUpdateRequest = { content };
|
||||
const response = await api.patch<DifyChildChunkResponse>(
|
||||
`/ai/dify/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}/child_chunks/${childChunkId}`,
|
||||
requestBody
|
||||
);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* 创建新的子块
|
||||
* @param datasetId Dify数据集ID
|
||||
* @param documentId Dify文档ID
|
||||
* @param segmentId 分段ID
|
||||
* @param content 分段内容
|
||||
* @returns Promise<ResultDomain<DifyChildChunkResponse>>
|
||||
*/
|
||||
async createChildChunk(
|
||||
datasetId: string,
|
||||
documentId: string,
|
||||
segmentId: string,
|
||||
content: string
|
||||
): Promise<ResultDomain<DifyChildChunkResponse>> {
|
||||
const requestBody: SegmentCreateRequest = { content };
|
||||
const response = await api.post<DifyChildChunkResponse>(
|
||||
`/ai/dify/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}/child_chunks`,
|
||||
requestBody
|
||||
);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* 删除子块
|
||||
* @param datasetId Dify数据集ID
|
||||
* @param documentId Dify文档ID
|
||||
* @param segmentId 分段ID
|
||||
* @param childChunkId 子块ID
|
||||
* @returns Promise<ResultDomain<void>>
|
||||
*/
|
||||
async deleteChildChunk(
|
||||
datasetId: string,
|
||||
documentId: string,
|
||||
segmentId: string,
|
||||
childChunkId: string
|
||||
): Promise<ResultDomain<void>> {
|
||||
const response = await api.delete<void>(
|
||||
`/ai/dify/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}/child_chunks/${childChunkId}`
|
||||
);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* 批量获取所有分段和子块
|
||||
* @param datasetId Dify数据集ID
|
||||
* @param documentId Dify文档ID
|
||||
* @returns Promise<DifyChildChunk[]> 所有子块的扁平列表
|
||||
*/
|
||||
async getAllSegmentsWithChunks(
|
||||
datasetId: string,
|
||||
documentId: string
|
||||
): Promise<any[]> {
|
||||
// 1. 获取所有父级分段
|
||||
const segmentsResult = await this.getDocumentSegments(datasetId, documentId);
|
||||
|
||||
if (!segmentsResult.success || !segmentsResult.data?.data) {
|
||||
throw new Error('获取分段列表失败');
|
||||
}
|
||||
|
||||
// 2. 对每个父级分段,获取其子块
|
||||
const allChunks: any[] = [];
|
||||
|
||||
for (const segment of segmentsResult.data.data) {
|
||||
try {
|
||||
const chunksResult = await this.getChildChunks(
|
||||
datasetId,
|
||||
documentId,
|
||||
segment.id
|
||||
);
|
||||
|
||||
if (chunksResult.success && chunksResult.data?.data) {
|
||||
// 为每个子块添加父级分段信息(用于显示)
|
||||
const chunksWithSegmentInfo = chunksResult.data.data.map(chunk => ({
|
||||
...chunk,
|
||||
parentSegmentId: segment.id,
|
||||
parentPosition: segment.position,
|
||||
parentKeywords: segment.keywords
|
||||
}));
|
||||
allChunks.push(...chunksWithSegmentInfo);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`获取分段 ${segment.id} 的子块失败:`, error);
|
||||
// 继续处理其他分段
|
||||
}
|
||||
}
|
||||
|
||||
return allChunks;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user