Evaluations.svelte 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import { models, user } from '$lib/stores';
  4. import { createEventDispatcher, onMount, getContext, tick } from 'svelte';
  5. const dispatch = createEventDispatcher();
  6. import { getModels } from '$lib/apis';
  7. import Switch from '$lib/components/common/Switch.svelte';
  8. import Spinner from '$lib/components/common/Spinner.svelte';
  9. import Tooltip from '$lib/components/common/Tooltip.svelte';
  10. import Plus from '$lib/components/icons/Plus.svelte';
  11. import Model from './Evaluations/Model.svelte';
  12. import ArenaModelModal from './Evaluations/ArenaModelModal.svelte';
  13. import { getConfig, updateConfig } from '$lib/apis/evaluations';
  14. const i18n = getContext('i18n');
  15. let config = null;
  16. let showAddModel = false;
  17. const submitHandler = async () => {
  18. config = await updateConfig(localStorage.token, config).catch((err) => {
  19. toast.error(err);
  20. return null;
  21. });
  22. if (config) {
  23. toast.success('Settings saved successfully');
  24. }
  25. };
  26. const addModelHandler = async (model) => {
  27. config.EVALUATION_ARENA_MODELS.push(model);
  28. config.EVALUATION_ARENA_MODELS = [...config.EVALUATION_ARENA_MODELS];
  29. await submitHandler();
  30. models.set(await getModels(localStorage.token));
  31. };
  32. const editModelHandler = async (model, modelIdx) => {
  33. config.EVALUATION_ARENA_MODELS[modelIdx] = model;
  34. config.EVALUATION_ARENA_MODELS = [...config.EVALUATION_ARENA_MODELS];
  35. await submitHandler();
  36. models.set(await getModels(localStorage.token));
  37. };
  38. const deleteModelHandler = async (modelIdx) => {
  39. config.EVALUATION_ARENA_MODELS = config.EVALUATION_ARENA_MODELS.filter(
  40. (m, mIdx) => mIdx !== modelIdx
  41. );
  42. await submitHandler();
  43. models.set(await getModels(localStorage.token));
  44. };
  45. onMount(async () => {
  46. if ($user.role === 'admin') {
  47. config = await getConfig(localStorage.token).catch((err) => {
  48. toast.error(err);
  49. return null;
  50. });
  51. }
  52. });
  53. </script>
  54. <ArenaModelModal
  55. bind:show={showAddModel}
  56. on:submit={async (e) => {
  57. addModelHandler(e.detail);
  58. }}
  59. />
  60. <form
  61. class="flex flex-col h-full justify-between text-sm"
  62. on:submit|preventDefault={() => {
  63. submitHandler();
  64. dispatch('save');
  65. }}
  66. >
  67. <div class="overflow-y-scroll scrollbar-hidden h-full">
  68. {#if config !== null}
  69. <div class="">
  70. <div class="text-sm font-medium mb-2">{$i18n.t('General Settings')}</div>
  71. <div class=" mb-2">
  72. <div class="flex justify-between items-center text-xs">
  73. <div class=" text-xs font-medium">{$i18n.t('Arena Models')}</div>
  74. <Tooltip content={$i18n.t(`Message rating should be enabled to use this feature`)}>
  75. <Switch bind:state={config.ENABLE_EVALUATION_ARENA_MODELS} />
  76. </Tooltip>
  77. </div>
  78. </div>
  79. {#if config.ENABLE_EVALUATION_ARENA_MODELS}
  80. <hr class=" border-gray-50 dark:border-gray-700/10 my-2" />
  81. <div class="flex justify-between items-center mb-2">
  82. <div class="text-sm font-medium">{$i18n.t('Manage Arena Models')}</div>
  83. <div>
  84. <Tooltip content={$i18n.t('Add Arena Model')}>
  85. <button
  86. class="p-1"
  87. type="button"
  88. on:click={() => {
  89. showAddModel = true;
  90. }}
  91. >
  92. <Plus />
  93. </button>
  94. </Tooltip>
  95. </div>
  96. </div>
  97. <div class="flex flex-col gap-2">
  98. {#if (config?.EVALUATION_ARENA_MODELS ?? []).length > 0}
  99. {#each config.EVALUATION_ARENA_MODELS as model, index}
  100. <Model
  101. {model}
  102. on:edit={(e) => {
  103. editModelHandler(e.detail, index);
  104. }}
  105. on:delete={(e) => {
  106. deleteModelHandler(index);
  107. }}
  108. />
  109. {/each}
  110. {:else}
  111. <div class=" text-center text-xs text-gray-500">
  112. {$i18n.t(
  113. `Using the default arena model with all models. Click the plus button to add custom models.`
  114. )}
  115. </div>
  116. {/if}
  117. </div>
  118. {/if}
  119. </div>
  120. {:else}
  121. <div class="flex h-full justify-center">
  122. <div class="my-auto">
  123. <Spinner className="size-6" />
  124. </div>
  125. </div>
  126. {/if}
  127. </div>
  128. <div class="flex justify-end pt-3 text-sm font-medium">
  129. <button
  130. class="px-3.5 py-1.5 text-sm font-medium bg-black hover:bg-gray-900 text-white dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full"
  131. type="submit"
  132. >
  133. {$i18n.t('Save')}
  134. </button>
  135. </div>
  136. </form>