CitationsModal.svelte 5.4 KB

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