ModelSelect.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <!--
  2. - Copyright (c) 2024 LangChat. TyCoding All Rights Reserved.
  3. -
  4. - Licensed under the GNU Affero General Public License, Version 3 (the "License");
  5. - you may not use this file except in compliance with the License.
  6. - You may obtain a copy of the License at
  7. -
  8. - https://www.gnu.org/licenses/agpl-3.0.html
  9. -
  10. - Unless required by applicable law or agreed to in writing, software
  11. - distributed under the License is distributed on an "AS IS" BASIS,
  12. - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. - See the License for the specific language governing permissions and
  14. - limitations under the License.
  15. -->
  16. <script lang="ts" setup>
  17. import { onMounted, ref, toRaw } from 'vue';
  18. import { list as getModels } from '@/api/aigc/model';
  19. import { LLMProviders } from '@/views/aigc/model/components/chat/data';
  20. import { ModelTypeEnum } from '@/api/models';
  21. import { useChatStore } from '@/views/chat/store/useChatStore';
  22. const chatStore = useChatStore();
  23. const props = defineProps<{
  24. id: any;
  25. }>();
  26. const emit = defineEmits(['update', 'load']);
  27. const options = ref([]);
  28. const modelId = ref('');
  29. onMounted(async () => {
  30. const providers = await getModels({ type: ModelTypeEnum.CHAT });
  31. if (chatStore.modelId === '' || chatStore.modelId === null) {
  32. if (providers != null && providers.length != 0) {
  33. const item = providers[0];
  34. chatStore.modelId = item.id;
  35. chatStore.modelName = item.model;
  36. chatStore.modelProvider = item.provider;
  37. if (props.id == null) {
  38. modelId.value = item.id;
  39. emit('load', item);
  40. } else {
  41. modelId.value = props.id;
  42. }
  43. }
  44. }
  45. const data: any = [];
  46. LLMProviders.forEach((i) => {
  47. const children = providers.filter((m) => m.provider == i.model);
  48. if (children.length === 0) {
  49. return;
  50. }
  51. data.push({
  52. type: 'group',
  53. name: i.name,
  54. id: i.id,
  55. children: children,
  56. });
  57. });
  58. options.value = data;
  59. modelId.value = chatStore.modelId;
  60. });
  61. function onUpdate(val: any, opt) {
  62. const obj = toRaw(opt);
  63. emit('update', {
  64. id: obj.id,
  65. modelName: obj.model,
  66. modelProvider: obj.provider,
  67. });
  68. chatStore.modelId = obj.id;
  69. chatStore.modelName = obj.model;
  70. chatStore.modelProvider = obj.provider;
  71. }
  72. </script>
  73. <template>
  74. <n-select
  75. v-model:value="modelId"
  76. :consistent-menu-width="false"
  77. :label-field="'name'"
  78. :options="options"
  79. :value-field="'id'"
  80. placeholder="请选择关联模型"
  81. @update:value="onUpdate"
  82. />
  83. </template>
  84. <style lang="less" scoped>
  85. ::v-deep(
  86. .n-base-selection .n-base-selection__border,
  87. .n-base-selection .n-base-selection__state-border
  88. ) {
  89. border: none !important;
  90. }
  91. ::v-deep(.n-base-selection-label) {
  92. font-weight: 600 !important;
  93. gap: 6px !important;
  94. display: flex !important;
  95. }
  96. ::v-deep(.n-base-selection-input) {
  97. margin-right: 4px;
  98. }
  99. </style>