CitationsModal.svelte 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <script lang="ts">
  2. import { getContext, onMount, tick } from 'svelte';
  3. import Modal from '$lib/components/common/Modal.svelte';
  4. import Tooltip from '$lib/components/common/Tooltip.svelte';
  5. import { WEBUI_API_BASE_URL } from '$lib/constants';
  6. const i18n = getContext('i18n');
  7. export let show = false;
  8. export let citation;
  9. export let showPercentage = false;
  10. export let showRelevance = true;
  11. let mergedDocuments = [];
  12. function calculatePercentage(distance: number) {
  13. if (distance < 0) return 0;
  14. if (distance > 1) return 100;
  15. return Math.round(distance * 10000) / 100;
  16. }
  17. function getRelevanceColor(percentage: number) {
  18. if (percentage >= 80)
  19. return 'bg-green-200 dark:bg-green-800 text-green-800 dark:text-green-200';
  20. if (percentage >= 60)
  21. return 'bg-yellow-200 dark:bg-yellow-800 text-yellow-800 dark:text-yellow-200';
  22. if (percentage >= 40)
  23. return 'bg-orange-200 dark:bg-orange-800 text-orange-800 dark:text-orange-200';
  24. return 'bg-red-200 dark:bg-red-800 text-red-800 dark:text-red-200';
  25. }
  26. $: if (citation) {
  27. mergedDocuments = citation.document?.map((c, i) => {
  28. return {
  29. source: citation.source,
  30. document: c,
  31. metadata: citation.metadata?.[i],
  32. distance: citation.distances?.[i]
  33. };
  34. });
  35. if (mergedDocuments.every((doc) => doc.distance !== undefined)) {
  36. mergedDocuments = mergedDocuments.sort(
  37. (a, b) => (b.distance ?? Infinity) - (a.distance ?? Infinity)
  38. );
  39. }
  40. }
  41. </script>
  42. <Modal size="lg" bind:show>
  43. <div>
  44. <div class=" flex justify-between dark:text-gray-300 px-5 pt-4 pb-2">
  45. <div class=" text-lg font-medium self-center capitalize">
  46. {$i18n.t('Citation')}
  47. </div>
  48. <button
  49. class="self-center"
  50. on:click={() => {
  51. show = false;
  52. }}
  53. >
  54. <svg
  55. xmlns="http://www.w3.org/2000/svg"
  56. viewBox="0 0 20 20"
  57. fill="currentColor"
  58. class="w-5 h-5"
  59. >
  60. <path
  61. d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"
  62. />
  63. </svg>
  64. </button>
  65. </div>
  66. <div class="flex flex-col md:flex-row w-full px-6 pb-5 md:space-x-4">
  67. <div
  68. class="flex flex-col w-full dark:text-gray-200 overflow-y-scroll max-h-[22rem] scrollbar-hidden"
  69. >
  70. {#each mergedDocuments as document, documentIdx}
  71. <div class="flex flex-col w-full">
  72. <div class="text-sm font-medium dark:text-gray-300">
  73. {$i18n.t('Source')}
  74. </div>
  75. {#if document.source?.name}
  76. <Tooltip
  77. className="w-fit"
  78. content={$i18n.t('Open file')}
  79. placement="top-start"
  80. tippyOptions={{ duration: [500, 0] }}
  81. >
  82. <div class="text-sm dark:text-gray-400 flex items-center gap-2 w-fit">
  83. <a
  84. class="hover:text-gray-500 dark:hover:text-gray-100 underline grow"
  85. href={document?.metadata?.file_id
  86. ? `${WEBUI_API_BASE_URL}/files/${document?.metadata?.file_id}/content${document?.metadata?.page !== undefined ? `#page=${document.metadata.page + 1}` : ''}`
  87. : document.source?.url?.includes('http')
  88. ? document.source.url
  89. : `#`}
  90. target="_blank"
  91. >
  92. {decodeURIComponent(document?.metadata?.name) ?? decodeURIComponent(document.source.name)}
  93. </a>
  94. {#if document?.metadata?.page}
  95. <span class="text-xs text-gray-500 dark:text-gray-400">
  96. ({$i18n.t('page')}
  97. {document.metadata.page + 1})
  98. </span>
  99. {/if}
  100. </div>
  101. </Tooltip>
  102. {#if showRelevance}
  103. <div class="text-sm font-medium dark:text-gray-300 mt-2">
  104. {$i18n.t('Relevance')}
  105. </div>
  106. {#if document.distance !== undefined}
  107. <Tooltip
  108. className="w-fit"
  109. content={$i18n.t('Semantic distance to query')}
  110. placement="top-start"
  111. tippyOptions={{ duration: [500, 0] }}
  112. >
  113. <div class="text-sm my-1 dark:text-gray-400 flex items-center gap-2 w-fit">
  114. {#if showPercentage}
  115. {@const percentage = calculatePercentage(document.distance)}
  116. <span
  117. class={`px-1 rounded-sm font-medium ${getRelevanceColor(percentage)}`}
  118. >
  119. {percentage.toFixed(2)}%
  120. </span>
  121. <span class="text-gray-500 dark:text-gray-500">
  122. ({document.distance.toFixed(4)})
  123. </span>
  124. {:else}
  125. <span class="text-gray-500 dark:text-gray-500">
  126. {document.distance.toFixed(4)}
  127. </span>
  128. {/if}
  129. </div>
  130. </Tooltip>
  131. {:else}
  132. <div class="text-sm dark:text-gray-400">
  133. {$i18n.t('No distance available')}
  134. </div>
  135. {/if}
  136. {/if}
  137. {:else}
  138. <div class="text-sm dark:text-gray-400">
  139. {$i18n.t('No source available')}
  140. </div>
  141. {/if}
  142. </div>
  143. <div class="flex flex-col w-full">
  144. <div class=" text-sm font-medium dark:text-gray-300 mt-2">
  145. {$i18n.t('Content')}
  146. </div>
  147. {#if document.metadata?.html}
  148. <iframe
  149. class="w-full border-0 h-auto rounded-none"
  150. sandbox="allow-scripts allow-forms allow-same-origin"
  151. srcdoc={document.document}
  152. title={$i18n.t('Content')}
  153. ></iframe>
  154. {:else}
  155. <pre class="text-sm dark:text-gray-400 whitespace-pre-line">
  156. {document.document}
  157. </pre>
  158. {/if}
  159. </div>
  160. {#if documentIdx !== mergedDocuments.length - 1}
  161. <hr class="border-gray-100 dark:border-gray-850 my-3" />
  162. {/if}
  163. {/each}
  164. </div>
  165. </div>
  166. </div>
  167. </Modal>