SplitStep.vue 7.3 KB

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