SplitStep.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <template>
  2. <div>
  3. <!-- 上部分段设置部分 -->
  4. <div class="mb-20px">
  5. <div class="mb-20px flex justify-between items-center">
  6. <div class="text-16px font-bold flex items-center">
  7. 分段设置
  8. <el-tooltip
  9. content="系统会自动将文档内容分割成多个段落,您可以根据需要调整分段方式和内容。"
  10. placement="top"
  11. >
  12. <Icon icon="ep:warning" class="ml-5px text-gray-400" />
  13. </el-tooltip>
  14. </div>
  15. <div>
  16. <el-button type="primary" plain size="small" @click="handleAutoSegment">
  17. 预览分段
  18. </el-button>
  19. </div>
  20. </div>
  21. <div class="segment-settings mb-20px">
  22. <el-form label-width="120px">
  23. <el-form-item label="最大 Token 数">
  24. <el-input-number v-model="modelData.segmentMaxTokens" :min="1" :max="2048" />
  25. </el-form-item>
  26. </el-form>
  27. </div>
  28. </div>
  29. <!-- 下部文件预览部分 -->
  30. <div class="mb-10px">
  31. <div class="text-16px font-bold mb-10px">分段预览</div>
  32. <!-- 文件选择器 -->
  33. <div class="file-selector mb-10px">
  34. <el-dropdown v-if="modelData.list && modelData.list.length > 0" trigger="click">
  35. <div class="flex items-center cursor-pointer">
  36. <Icon icon="ep:document" class="text-danger mr-5px" />
  37. <span>{{ currentFile?.name || '请选择文件' }}</span>
  38. <span v-if="currentFile?.segments" class="ml-5px text-gray-500 text-12px">
  39. ({{ currentFile.segments.length }}个分片)
  40. </span>
  41. <Icon icon="ep:arrow-down" class="ml-5px" />
  42. </div>
  43. <template #dropdown>
  44. <el-dropdown-menu>
  45. <el-dropdown-item
  46. v-for="(file, index) in modelData.list"
  47. :key="index"
  48. @click="selectFile(index)"
  49. >
  50. {{ file.name }}
  51. <span v-if="file.segments" class="ml-5px text-gray-500 text-12px">
  52. ({{ file.segments.length }}个分片)
  53. </span>
  54. </el-dropdown-item>
  55. </el-dropdown-menu>
  56. </template>
  57. </el-dropdown>
  58. <div v-else class="text-gray-400">暂无上传文件</div>
  59. </div>
  60. <!-- 文件内容预览 -->
  61. <div class="file-preview bg-gray-50 p-15px rounded-md max-h-600px overflow-y-auto">
  62. <div v-if="splitLoading" class="flex justify-center items-center py-20px">
  63. <Icon icon="ep:loading" class="is-loading" />
  64. <span class="ml-10px">正在加载分段内容...</span>
  65. </div>
  66. <template
  67. v-else-if="currentFile && currentFile.segments && currentFile.segments.length > 0"
  68. >
  69. <div v-for="(segment, index) in currentFile.segments" :key="index" class="mb-10px">
  70. <div class="text-gray-500 text-12px mb-5px">
  71. 分片-{{ index + 1 }} · {{ segment.contentLength || 0 }} 字符数 ·
  72. {{ segment.tokens || 0 }} Token
  73. </div>
  74. <div class="bg-white p-10px rounded-md">{{ segment.content }}</div>
  75. </div>
  76. </template>
  77. <el-empty v-else description="暂无预览内容" />
  78. </div>
  79. </div>
  80. <!-- 添加底部按钮 -->
  81. <div class="mt-20px flex justify-between">
  82. <div>
  83. <el-button v-if="!modelData.id" @click="handlePrevStep">上一步</el-button>
  84. </div>
  85. <div>
  86. <el-button type="primary" :loading="submitLoading" @click="handleSave">
  87. 保存并处理
  88. </el-button>
  89. </div>
  90. </div>
  91. </div>
  92. </template>
  93. <script lang="ts" setup>
  94. import { computed, getCurrentInstance, inject, onMounted, PropType, ref } from 'vue'
  95. import { Icon } from '@/components/Icon'
  96. import { KnowledgeSegmentApi } from '@/api/ai/knowledge/segment'
  97. import { useMessage } from '@/hooks/web/useMessage'
  98. import { KnowledgeDocumentApi } from '@/api/ai/knowledge/document'
  99. const props = defineProps({
  100. modelValue: {
  101. type: Object as PropType<any>,
  102. required: true
  103. }
  104. })
  105. const emit = defineEmits(['update:modelValue'])
  106. const message = useMessage() // 消息提示
  107. const parent = inject('parent', null) // 获取父组件实例
  108. const modelData = computed({
  109. get: () => props.modelValue,
  110. set: (val) => emit('update:modelValue', val)
  111. }) // 表单数据
  112. const splitLoading = ref(false) // 分段加载状态
  113. const currentFile = ref<any>(null) // 当前选中的文件
  114. const submitLoading = ref(false) // 提交按钮加载状态
  115. /** 选择文件 */
  116. const selectFile = async (index: number) => {
  117. currentFile.value = modelData.value.list[index]
  118. await splitContent(currentFile.value)
  119. }
  120. /** 获取文件分段内容 */
  121. const splitContent = async (file: any) => {
  122. if (!file || !file.url) {
  123. message.warning('文件 URL 不存在')
  124. return
  125. }
  126. splitLoading.value = true
  127. try {
  128. // 调用后端分段接口,获取文档的分段内容、字符数和 Token 数
  129. file.segments = await KnowledgeSegmentApi.splitContent(
  130. file.url,
  131. modelData.value.segmentMaxTokens
  132. )
  133. } catch (error) {
  134. console.error('获取分段内容失败:', file, error)
  135. message.error('获取分段内容失败')
  136. } finally {
  137. splitLoading.value = false
  138. }
  139. }
  140. /** 处理预览分段 */
  141. const handleAutoSegment = async () => {
  142. // 如果没有选中文件,默认选中第一个
  143. if (!currentFile.value && modelData.value.list && modelData.value.list.length > 0) {
  144. currentFile.value = modelData.value.list[0]
  145. }
  146. // 如果没有选中文件,提示请先选择文件
  147. if (!currentFile.value) {
  148. message.warning('请先选择文件')
  149. return
  150. }
  151. // 获取分段内容
  152. await splitContent(currentFile.value)
  153. }
  154. /** 上一步按钮处理 */
  155. const handlePrevStep = () => {
  156. const parentEl = parent || getCurrentInstance()?.parent
  157. if (parentEl && typeof parentEl.exposed?.goToPrevStep === 'function') {
  158. parentEl.exposed.goToPrevStep()
  159. }
  160. }
  161. /** 保存操作 */
  162. const handleSave = async () => {
  163. // 保存前验证
  164. if (!currentFile?.value?.segments || currentFile.value.segments.length === 0) {
  165. message.warning('请先预览分段内容')
  166. return
  167. }
  168. // 设置按钮加载状态
  169. submitLoading.value = true
  170. try {
  171. if (modelData.value.id) {
  172. // 修改场景
  173. modelData.value.ids = await KnowledgeDocumentApi.updateKnowledgeDocument({
  174. id: modelData.value.id,
  175. segmentMaxTokens: modelData.value.segmentMaxTokens
  176. })
  177. } else {
  178. // 新增场景
  179. modelData.value.ids = await KnowledgeDocumentApi.createKnowledgeDocumentList({
  180. knowledgeId: modelData.value.knowledgeId,
  181. segmentMaxTokens: modelData.value.segmentMaxTokens,
  182. list: modelData.value.list.map((item: any) => ({
  183. name: item.name,
  184. url: item.url
  185. }))
  186. })
  187. }
  188. // 进入下一步
  189. const parentEl = parent || getCurrentInstance()?.parent
  190. if (parentEl && typeof parentEl.exposed?.goToNextStep === 'function') {
  191. parentEl.exposed.goToNextStep()
  192. }
  193. } catch (error: any) {
  194. console.error('保存失败:', modelData.value, error)
  195. message.error(error.message)
  196. } finally {
  197. // 关闭按钮加载状态
  198. submitLoading.value = false
  199. }
  200. }
  201. /** 初始化 */
  202. onMounted(async () => {
  203. // 确保 segmentMaxTokens 存在
  204. if (!modelData.value.segmentMaxTokens) {
  205. modelData.value.segmentMaxTokens = 500
  206. }
  207. // 如果没有选中文件,默认选中第一个
  208. if (!currentFile.value && modelData.value.list && modelData.value.list.length > 0) {
  209. currentFile.value = modelData.value.list[0]
  210. }
  211. // 如果有选中的文件,获取分段内容
  212. if (currentFile.value) {
  213. await splitContent(currentFile.value)
  214. }
  215. })
  216. </script>