zhangxiao
2024-08-16 5efb195a3e0c956c4b254408a8868368135eb773
src/views/dmx/model/components/addPageModel.vue
@@ -17,54 +17,57 @@
      :style="{ width: '90%', margin: '0 auto' }"
      layout="vertical"
    >
      <div v-if="isType !== 1">
        <a-form-item field="model_type" label="模型类型">
          <a-select v-model="form.model_type" placeholder="请选择">
            <a-option value="chat">chat</a-option>
            <a-option value="embedding">embedding</a-option>
          </a-select>
        </a-form-item>
        <a-form-item field="llm_name" label="模型名称">
          <a-input v-model="form.llm_name" placeholder="请输入模型名称" />
        </a-form-item>
        <!-- <a-form-item field="volc_ak" label="火山 ACCESS_KEY">
          <a-input v-model="form.volc_ak" placeholder="请输入火山 ACCESS_KEY" />
          <a-button
            type="primary"
            @click="handleClick"
            style="margin-left: 10px"
          >
            <template #icon>
              <icon-plus />
            </template>
          </a-button>
        </a-form-item>
        <a-form-item field="volc_sk" label="火山 SECRET_KEY">
          <a-input v-model="form.volc_sk" placeholder="请输入火山 SECRET_KEY" />
        </a-form-item> -->
        <a-form-item field="api_base" label="基础URL">
          <a-input v-model="form.api_base" placeholder="请输入基础URL" />
        </a-form-item>
        <a-form-item
      <div>
        <div v-for="(item, index) in form.formData.params" :key="index">
          <div v-if="item.value_type === 'select'">
            <a-form-item
              :field="item.key"
              :label="item.name"
              :required="item.required"
            >
              <a-select v-model="form[item.key]" placeholder="请选择">
                <a-option
                  v-for="(list, v) in item.options"
                  :value="v"
                  :label="v"
                ></a-option>
              </a-select>
            </a-form-item>
          </div>
          <div v-else-if="item.value_type === 'input'">
            <a-form-item
              :field="item.key"
              :label="item.name"
              :required="item.required"
            >
              <a-input v-model="form[item.key]" placeholder="请输入" />
            </a-form-item>
          </div>
        </div>
        <!-- <a-form-item
          field="raptor"
          label="是否支持 Vision"
          v-if="form.model_type === 'chat'"
        >
          <a-switch v-model="form.raptor" />
        </a-form-item>
        </a-form-item> -->
        <a-form-item field="logo" label="模型图片">
          <a-space direction="vertical" :style="{ width: '100%' }">
          <a-space
            direction="vertical"
            v-model="form.logo"
            :style="{ width: '100%' }"
          >
            <Upload
              :action="uploadAction"
              :limit="1"
              :url="form.urlS"
              @handleRemove="deleteRemove"
              @update:fileList="updateFileList"
              @success="handleSuccess"
            ></Upload>
          </a-space>
        </a-form-item>
        <a-form-item field="all_params" label="增加其它参数配置">
        <!-- <a-form-item field="all_params" label="增加其它参数配置">
          <a-button
            type="primary"
            @click="handleAddClick"
@@ -74,7 +77,7 @@
              <icon-plus />
            </template>
          </a-button>
        </a-form-item>
        </a-form-item> -->
        <a-form-item v-for="(item, index) in form.addFomList" :key="index">
          <div class="addInput">
@@ -98,25 +101,6 @@
          </div>
        </a-form-item>
      </div>
      <div v-else>
        <a-form-item field="name" label="API-Key">
          <a-input v-model="form.name" placeholder="API-Key" />
        </a-form-item>
        <a-form-item field="describe" label="Base-Url">
          <a-input v-model="form.name" placeholder="Base-Url" />
        </a-form-item>
        <a-form-item field="section" label="模型图片">
          <a-space direction="vertical" :style="{ width: '100%' }">
            <Upload
              :action="uploadAction"
              :limit="1"
              @update:fileList="updateFileList"
              @success="handleSuccess"
            ></Upload>
          </a-space>
        </a-form-item>
      </div>
      <a-form-item>
        <div style="width: 100%; text-align: right">
@@ -131,11 +115,14 @@
</template>
<script lang="ts" setup>
  import { reactive, ref, computed, watch } from 'vue';
  import { addLlm, getLlmDetail } from '@/api/model';
  import { reactive, ref, computed, watch, watchEffect, onMounted } from 'vue';
  import { addLlm, getLlmDetail, editLlm, getFactoryDetail } from '@/api/model';
  import { Modal, Message } from '@arco-design/web-vue';
  import message from '@arco-design/web-vue/es/message';
  import { resolveUnref } from '@vueuse/core';
  import { userModelState } from '@/store';
  const modelStore = userModelState();
  const addPageModelVisible = defineModel('show');
  const loading = ref(false);
@@ -161,107 +148,94 @@
  const isType = computed(() => props.type);
  const editList = computed(() => props.editList);
  watch(
    () => editList.value.name,
    (newValue) => {
      getDetail();
    }
  );
  const form = reactive({
    size: 'medium',
    name: '',
    formData: { logo: '' },
    llm_factory: '',
    model_type: '',
    province: 'haidian',
    options: [],
    llm_name: '',
    api_base: '',
    volc_ak: '',
    volc_sk: '',
    logo: '',
    radio: 'radio one',
    urlS: '',
    slider: 5,
    score: 5,
    switch: false,
    multiSelect: ['section one'],
    treeSelect: '',
    raptor: false,
    addFomList: [],
    addFomList: <any[]>[],
  });
  const form_ref = ref(null);
  const form_ref = ref();
  const rules = {
    model_type: [
    logo: [
      {
        required: true,
        message: '请选择模型类型',
      },
    ],
    llm_name: [
      {
        required: true,
        message: '名称不允许为空',
      },
    ],
    volc_sk: [
      {
        required: true,
        message: '请输入volc_sk',
      },
    ],
    volc_ak: [
      {
        required: true,
        message: '请输入volc_ak',
      },
    ],
    api_base: [
      {
        required: true,
        message: '请输入api_base',
        message: '模型图片不能为空',
      },
    ],
  };
  const uploadAction = '/api/v1/llm/upload'; // 替换为你的上传API
  const fileList = ref([]);
  const imageUrls = ref([]);
  const imageUrls = ref<any[]>([]);
  const uploadUrl = ref([]);
  const httpUrl = modelStore.hrefUrl;
  const updateFileList = (newFileList) => {
    fileList.value = newFileList;
    console.log(newFileList, 88);
  };
  const handleSuccess = (urls) => {
    imageUrls.value = urls; // 拿到上传的图片地址
    uploadUrl.value = urls;
    form.logo = urls[0];
    const urlsArr = urls.map((url) => {
      return httpUrl + url;
    });
    imageUrls.value = urlsArr; // 拿到上传的图片地址
  };
  const deleteRemove = () => {
    form.logo = '';
    uploadUrl.value = [];
    imageUrls.value = [];
  };
  const emit = defineEmits(['refresh-parent']);
  const handleSubmit = ({ values, errors }) => {
    const all_params = form.addFomList.map((item) => {
      console.log(item, 8888);
      return item.name + ':' + item.value;
    });
    console.log(form, 9999);
    form_ref.value
      ?.validate()
      .then(async (res) => {
        if (!res) {
          const data = await addLlm({
            llm_factory: editList.value.llm_factory,
            llm_name: form.llm_name,
            model_type: form.model_type,
            volc_ak: form.volc_ak,
            volc_sk: form.volc_sk,
            api_base: form.api_base,
            raptor: form.raptor,
            all_params: all_params,
            logo: imageUrls.value[0],
          });
          if ((data as any).retmsg == 'success') {
            message.success('添加成功');
            emit('refresh-parent');
          form.formData.logo = uploadUrl.value[0] || '';
          form.llm_factory = editList.value.llm_factory;
          delete form.formData;
          if (props.title == '编辑') {
            const data = await editLlm(form);
            if ((data as any).code == 200) {
              message.success('添加成功');
              addPageModelVisible.value = false;
              emit('refresh-parent');
            } else {
              message.error('添加失败');
            }
          } else {
            message.error('添加失败');
            const data = await addLlm(form);
            if ((data as any).code == 200) {
              message.success('添加成功');
              addPageModelVisible.value = false;
              emit('refresh-parent');
            } else {
              message.error('添加失败');
            }
          }
        }
      })
@@ -330,18 +304,37 @@
      editList.value.llm_factory,
      editList.value.name
    );
    console.log(data.data, 8888);
    form.llm_name = data.data.llm_name;
    form.model_type = data.data.model_type;
    form.volc_ak = data.data.volc_ak;
    form.volc_sk = data.data.volc_sk;
    form.api_base = data.data.api_base;
    form.raptor = data.data.raptor;
    form.addFomList = data.data.all_params.map((item) => {
      return { name: item.split(':')[0], value: item.split(':')[1] };
    });
    imageUrls.value = data.data.logo;
    form.urlS = data.data.logo;
    if (JSON.parse(data.data.all_params).length > 0) {
      form.addFomList = JSON.parse(data.data.all_params).map((item) => {
        return { name: item.split(':')[0], value: item.split(':')[1] };
      });
    }
    imageUrls.value.push(httpUrl + data.data.logo);
  };
  const getFactory = async () => {
    const { data } = await getFactoryDetail({
      factory_name: editList.value.llm_factory,
    });
    form.formData = data;
  };
  onMounted(() => {
    getFactory();
  });
  watchEffect(() => {
    if (props.editList.name) {
      getDetail();
    }
  });
</script>
<style scoped lang="less">
  .addInput {