test.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <template>
  2. <div class="main-container">
  3. <div class="main-container-lf">
  4. <div class="main-container-lf-top">
  5. <!-- 检索测试 -->
  6. {{ $t('knowledgeLib.test_title') }}
  7. </div>
  8. <div class="main-container-lf-down">
  9. <div style="color: #999">
  10. <!-- 最后一步! 成功后,剩下的就交给Infiniflow AI吧。 -->
  11. {{ $t('knowledgeLib.test_tips') }}
  12. </div>
  13. <a-divider style="margin-top: 10px" />
  14. <a-form ref="formRef" :model="form" :style="{ width: '100%' }" layout="vertical" @submit="handleSubmit">
  15. <!-- 相似度阈值 -->
  16. <a-form-item field="similarity_threshold" :label="$t('knowledgeLib.similarity_threshold')">
  17. <a-slider v-model="form.similarity_threshold" :step="0.01" :min="0" :max="1" />
  18. </a-form-item>
  19. <!-- 关键字相似度权重 -->
  20. <a-form-item field="slider" :label="$t('knowledgeLib.similarity_weight')">
  21. <a-slider v-model="form.vector_similarity_weight" :step="0.01" :min="0" :max="1" />
  22. </a-form-item>
  23. <!-- Rerank模型 -->
  24. <a-form-item field="section" :label="$t('knowledgeLib.rerank_model')">
  25. <a-space direction="vertical" size="large">
  26. <a-select
  27. :size="'large'"
  28. v-model="form.rerank_id"
  29. :style="{ width: '100%' }"
  30. :placeholder="$t('knowledgeLib.select_placeholder')"
  31. allow-clear
  32. >
  33. <a-optgroup :label="index" v-for="(item, index) in modelList" :key="index">
  34. <a-option v-for="obj in item" :key="obj.fid" :disabled="!obj.available" :value="obj.llm_name">
  35. {{ obj.llm_name }}
  36. </a-option>
  37. </a-optgroup>
  38. </a-select>
  39. </a-space>
  40. </a-form-item>
  41. <a-form-item field="slider" label="Top-K" v-if="form.rerank_id">
  42. <a-slider v-model="form.top_k" :min="1" :max="2048" />
  43. </a-form-item>
  44. <a-form-item>
  45. <div class="main-container-form-item-extra">
  46. <div class="main-container-form-item-extra-top">
  47. <!-- 测试文本 -->
  48. {{ $t('knowledgeLib.test_content') }}
  49. </div>
  50. <div class="main-container-form-item-extra-down">
  51. <a-textarea
  52. v-model="form.question"
  53. style="height: 10rem; border: 1px solid var(--color-fill-3); border-radius: 4px"
  54. placeholder=""
  55. allow-clear
  56. />
  57. </div>
  58. <div class="main-container-form-item-extra-btn">
  59. <a-button :disabled="!form.question" :loading="loading" type="primary" html-type="submit">
  60. <!-- 测试 -->
  61. {{ $t('knowledgeLib.test_btn') }}
  62. </a-button>
  63. </div>
  64. </div>
  65. </a-form-item>
  66. </a-form>
  67. </div>
  68. </div>
  69. <div class="main-container-rt">
  70. <a-collapse style="width: 96%; margin-top: 1rem; margin-left: 2%" @change="handleCollapseChange">
  71. <!-- 选定的文件 -->
  72. <a-collapse-item :header="check_count + '/' + doc_total + $t('knowledgeLib.test_result_head')" key="3">
  73. <a-checkbox-group v-model="checked_list" @change="handleCheckedChange" :style="{ width: '100%' }">
  74. <a-list :bordered="false" :pagination-props="doc_pagination_props" style="background: var(--color-bg-1)">
  75. <a-list-item v-for="doc in docList" :key="doc.doc_id" style="border-left: none; border-right: none">
  76. <a-list-item-meta :title="doc.doc_name">
  77. <template #avatar>
  78. <a-checkbox :value="doc.doc_id"></a-checkbox>
  79. </template>
  80. </a-list-item-meta>
  81. <template #actions>
  82. {{ doc.count }}
  83. <!-- 预览 -->
  84. <a-tooltip :content="$t('knowledgeLib.test_preview')">
  85. <div class="action-icon">
  86. <icon-eye @click="handlePreview(doc.doc_id)" />
  87. </div>
  88. </a-tooltip>
  89. </template>
  90. </a-list-item>
  91. </a-list>
  92. </a-checkbox-group>
  93. </a-collapse-item>
  94. </a-collapse>
  95. <div
  96. class="main-container-rt-down"
  97. :style="collapse_status ? 'height: calc(100% - 350px);overflow-y: auto' : 'height: calc(100% - 140px)'"
  98. >
  99. <a-card :bordered="false" v-for="item in chunks" :key="item.chunk_id">
  100. <template #title>
  101. <div class="main-container-rt-down-title">
  102. <!-- 混合相似度 -->
  103. <div>{{ (item.similarity * 100).toFixed(2) }}&nbsp; {{ $t('knowledgeLib.hybrid_similarity') }}</div>
  104. <!-- 关键词相似度 -->
  105. <div>{{ (item.term_similarity * 100).toFixed(2) }}&nbsp; {{ $t('knowledgeLib.keyword_similarity') }}</div>
  106. <!-- 向量相似度 -->
  107. <div>{{ (item.vector_similarity * 100).toFixed(2) }}&nbsp;{{ $t('knowledgeLib.vector_similarity') }}</div>
  108. </div>
  109. </template>
  110. <div class="main-container-rt-down-content">
  111. {{ item.content_with_weight }}
  112. </div>
  113. </a-card>
  114. </div>
  115. <!-- <div class="footer-page">
  116. <a-pagination :total="chunk_total" show-total />
  117. </div> -->
  118. </div>
  119. </div>
  120. </template>
  121. <script setup>
  122. import { onMounted, onBeforeMount, reactive, ref, watch, computed } from 'vue';
  123. // import { Message } from '@arco-design/web-vue';
  124. import { Message } from '../../../../3rd-libs/arco-vue-libs';
  125. import { kbretrievalTest, queryModelList } from '../../../../apis/rgflow-dify';
  126. import { useI18n } from 'vue-i18n';
  127. const { t } = useI18n();
  128. let visible = ref(false);
  129. let loading = ref(false);
  130. const formRef = ref(null);
  131. const kbid = ref('');
  132. const modelList = ref({});
  133. let form = reactive({
  134. page: 1,
  135. question: '',
  136. rerank_id: '',
  137. similarity_threshold: 0.2,
  138. size: 10,
  139. top_k: 1024,
  140. vector_similarity_weight: 0.7
  141. });
  142. const chunks = ref([]);
  143. const all_chunks = ref([]);
  144. const docList = ref([]);
  145. const checked_list = ref([]);
  146. const doc_total = ref(0);
  147. const chunk_total = ref(0);
  148. const collapse_status = ref(false);
  149. const check_count = computed(() => {
  150. return checked_list.value.length || 0;
  151. }); // 选中的文件数
  152. const doc_pagination_props = reactive({
  153. defaultPageSize: 3,
  154. total: 0
  155. });
  156. const formatter = (value) => {
  157. // return String(Math.round(value / 100))
  158. };
  159. const handleCollapseChange = (key) => {
  160. key.length ? (collapse_status.value = true) : (collapse_status.value = false);
  161. };
  162. const handleSubmit = async ({ values, errors }) => {
  163. if (!form.question) {
  164. return;
  165. }
  166. let formObj = {
  167. ...form,
  168. kb_id: kbid.value,
  169. vector_similarity_weight: parseFloat((1 - form.vector_similarity_weight).toFixed(2))
  170. };
  171. if (!formObj.rerank_id) {
  172. delete formObj.top_k;
  173. delete formObj.rerank_id;
  174. }
  175. try {
  176. loading.value = true;
  177. const data = await kbretrievalTest(formObj);
  178. if (data.code == 0) {
  179. Message.success(t('knowledgeLib.test_success'));
  180. all_chunks.value = data.data.chunks;
  181. docList.value = data.data.doc_aggs;
  182. if (docList.value.length) {
  183. checked_list.value.push(docList.value[0].doc_id);
  184. }
  185. if (all_chunks.value.length) {
  186. chunks.value = all_chunks.value?.filter((item) => item?.doc_id == checked_list.value[0]);
  187. }
  188. doc_pagination_props.total = docList.value.length;
  189. doc_total.value = data.data.doc_aggs.length;
  190. chunk_total.value = data.data.chunks.length;
  191. } else {
  192. Message.error(data.retmsg);
  193. }
  194. loading.value = false;
  195. } catch (err) {
  196. console.log(err);
  197. Message.error(err.message);
  198. loading.value = false;
  199. }
  200. };
  201. const handleClick = () => {
  202. visible.value = true;
  203. };
  204. const handleCancel = () => {
  205. visible.value = false;
  206. };
  207. const handleOpened = (el) => {
  208. Object.assign(form, {});
  209. formRef.value.resetFields();
  210. };
  211. const changekbid = (value) => {
  212. kbid.value = value;
  213. };
  214. const queryModel = async (params) => {
  215. try {
  216. const data = await queryModelList(params);
  217. modelList.value = {
  218. BAAI: [data.data.BAAI[1]],
  219. Jina: data.data.Jina,
  220. youdao: data.data.youdao
  221. };
  222. } catch (err) {
  223. // you can report use errorHandler or other
  224. } finally {
  225. }
  226. };
  227. const handlePreview = (doc_id) => {
  228. window.open(`${window.RGFLOW_BASE_URL}/document/${doc_id}?ext=docx&prefix=document`);
  229. };
  230. const handleCheckedChange = (doc_ids) => {
  231. chunks.value = [];
  232. doc_ids.forEach((doc_id) => {
  233. chunks.value.push(...all_chunks.value.filter((item) => item.doc_id == doc_id));
  234. });
  235. };
  236. // watch(()=>)
  237. defineExpose({
  238. changekbid
  239. });
  240. onBeforeMount(() => {
  241. queryModel({});
  242. });
  243. onMounted(() => {});
  244. </script>
  245. <script>
  246. export default {
  247. name: 'config',
  248. methods: {}
  249. };
  250. </script>
  251. <style scoped lang="css">
  252. .main-container {
  253. display: flex;
  254. justify-content: space-between;
  255. height: 100%;
  256. .main-container-lf {
  257. width: 30%;
  258. height: 100%;
  259. background: var(--color-bg-2);
  260. border-radius: 10px;
  261. overflow: hidden;
  262. .main-container-lf-top {
  263. width: 100%;
  264. height: 60px;
  265. line-height: 60px;
  266. background: rgb(var(--primary-6));
  267. color: #ffffff;
  268. text-align: center;
  269. }
  270. .main-container-lf-down {
  271. width: calc(100% - 40px);
  272. padding: 20px;
  273. }
  274. }
  275. .main-container-form-item-extra {
  276. width: 100%;
  277. height: 20rem;
  278. border: 1px solid var(--color-fill-3);
  279. border-radius: 6px;
  280. overflow: hidden;
  281. .main-container-form-item-extra-top {
  282. width: 100%;
  283. height: 40px;
  284. line-height: 40px;
  285. border-bottom: 1px solid var(--color-fill-3);
  286. color: #999999;
  287. text-align: center;
  288. }
  289. .main-container-form-item-extra-down {
  290. width: 92%;
  291. margin-left: 4%;
  292. margin-top: 30px;
  293. }
  294. .main-container-form-item-extra-btn {
  295. width: 96%;
  296. text-align: right;
  297. margin-top: 1rem;
  298. }
  299. }
  300. .main-container-rt {
  301. position: relative;
  302. width: 69%;
  303. height: 100%;
  304. overflow: hidden;
  305. background: var(--color-bg-2);
  306. border-radius: 10px;
  307. .main-container-rt-down {
  308. padding: 25px;
  309. .main-container-rt-down-title {
  310. display: flex;
  311. gap: 10px;
  312. font-size: 14px;
  313. }
  314. .main-container-rt-down-content {
  315. max-height: 300px;
  316. position: relative;
  317. padding: 20px 0;
  318. font-size: 16px;
  319. .footer-page {
  320. position: absolute;
  321. bottom: 10px;
  322. left: 10px;
  323. }
  324. }
  325. }
  326. .footer-page {
  327. padding-top: 10px;
  328. position: absolute;
  329. bottom: 20px;
  330. left: 20px;
  331. width: 100%;
  332. background: var(--color-bg-2);
  333. }
  334. }
  335. }
  336. .action-icon {
  337. width: 20px;
  338. height: 20px;
  339. display: flex;
  340. justify-content: center;
  341. align-items: center;
  342. border-radius: 2px;
  343. }
  344. .action-icon:hover {
  345. cursor: pointer;
  346. background-color: var(--color-neutral-4);
  347. }
  348. :deep(.arco-collapse-item-content) {
  349. padding: 0;
  350. }
  351. :deep(.arco-collapse-item-content-box) {
  352. padding: 0;
  353. }
  354. </style>