|
| 1 | +<template> |
| 2 | + <el-form |
| 3 | + ref="FormCom" |
| 4 | + label-width="164px" |
| 5 | + class="provider-form" |
| 6 | + label-position="right" |
| 7 | + :rules="rules" |
| 8 | + :model="record" |
| 9 | + :validate-on-rule-change="false" |
| 10 | + @submit.prevent |
| 11 | + > |
| 12 | + <CustomFormItem prop="input" :label="t('flow.input')" :readonly="readonly"> |
| 13 | + <el-autocomplete |
| 14 | + v-model="record.input" |
| 15 | + :fetch-suggestions="getFieldList" |
| 16 | + clearable |
| 17 | + popper-class="is-wider" |
| 18 | + /> |
| 19 | + </CustomFormItem> |
| 20 | + <CustomFormItem prop="system_prompt" :readonly="readonly"> |
| 21 | + <template #label> |
| 22 | + {{ t('flow.systemPrompt') }} |
| 23 | + <component :is="tipComponent" :content="t('flow.systemPromptDesc')" /> |
| 24 | + </template> |
| 25 | + <InputWithTextEditDialog v-model="record.system_prompt" :title="t('flow.systemPrompt')" /> |
| 26 | + </CustomFormItem> |
| 27 | + <CustomFormItem prop="base_url" :readonly="readonly"> |
| 28 | + <template #label> |
| 29 | + {{ t('flow.baseURL') }} |
| 30 | + <component |
| 31 | + :is="tipComponent" |
| 32 | + :content=" |
| 33 | + isGemini |
| 34 | + ? t('flow.geminiBaseUrlTips') |
| 35 | + : t('flow.baseURLDesc', { url: defaultBaseURL, name: title }) |
| 36 | + " |
| 37 | + /> |
| 38 | + </template> |
| 39 | + <el-input v-model="baseUrlProxy" :placeholder="defaultBaseURL"> </el-input> |
| 40 | + </CustomFormItem> |
| 41 | + <CustomFormItem prop="api_key" :label="t('flow.apiKey')" :readonly="readonly"> |
| 42 | + <CustomInputPassword v-model="record.api_key" /> |
| 43 | + </CustomFormItem> |
| 44 | + <CustomFormItem prop="model" :label="t('flow.model')" :readonly="readonly"> |
| 45 | + <InputWithOptions v-model="record.model" :options="modelOpts" :filterable="false" /> |
| 46 | + </CustomFormItem> |
| 47 | + <template v-if="isAnthropicProfile(record)"> |
| 48 | + <CustomFormItem prop="max_tokens" :label="t('flow.maxTokens')" :readonly="readonly"> |
| 49 | + <component :is="inputNumberComponent" v-model="record.max_tokens" /> |
| 50 | + </CustomFormItem> |
| 51 | + <CustomFormItem |
| 52 | + prop="anthropic_version" |
| 53 | + :label="t('flow.anthropicVersion')" |
| 54 | + :readonly="readonly" |
| 55 | + > |
| 56 | + <el-select v-model="record.anthropic_version"> |
| 57 | + <el-option v-for="item in anthropicVersionOpts" :key="item" :label="item" :value="item" /> |
| 58 | + </el-select> |
| 59 | + </CustomFormItem> |
| 60 | + </template> |
| 61 | + <CustomFormItem prop="alias" :readonly="readonly"> |
| 62 | + <template #label> |
| 63 | + {{ t('flow.aiOutputAlias') }} |
| 64 | + <component |
| 65 | + :is="tipComponent" |
| 66 | + :content="`${t('flow.aiOutputAliasDesc')}<br />${t('flow.aliasDesc')}`" |
| 67 | + :is-html="true" |
| 68 | + :desc-marked="true" |
| 69 | + /> |
| 70 | + </template> |
| 71 | + <el-input v-model="record.alias" /> |
| 72 | + </CustomFormItem> |
| 73 | + <slot name="advancedSettings"></slot> |
| 74 | + </el-form> |
| 75 | +</template> |
| 76 | + |
| 77 | +<script setup lang="ts"> |
| 78 | +import { ElForm, ElAutocomplete, ElSelect, ElOption, ElInput, type FormRules } from 'element-plus' |
| 79 | +import { |
| 80 | + GEMINI_DEFAULT_BASE_URL, |
| 81 | + ProcessingType, |
| 82 | + ANTHROPIC_VERSION_MAP, |
| 83 | + AIProviderType, |
| 84 | +} from '@emqx/shared-ui-constants' |
| 85 | +import { Component, computed, ref } from 'vue' |
| 86 | +import aiModels from './aiModels.json' |
| 87 | +import { useFlowLocale } from '../../composables/useFlowLocale' |
| 88 | +import useFlowNode from '../../composables/useFlowNode' |
| 89 | +import CustomFormItem from '../../../common/CustomFormItem.vue' |
| 90 | +import CustomInputPassword from '../../../common/CustomInputPassword.vue' |
| 91 | +import InputWithOptions from '../../../common/InputWithOptions.vue' |
| 92 | +import InputWithTextEditDialog from '../../../common/InputWithTextEditDialog.vue' |
| 93 | +import type { Node } from '@vue-flow/core' |
| 94 | +import type { AIAnthropicConfig, AIConfig } from '../../types' |
| 95 | +
|
| 96 | +const defaultModelOptsMap = new Map([ |
| 97 | + [ProcessingType.AIOpenAI, aiModels.openai], |
| 98 | + [ProcessingType.AIAnthropic, aiModels.anthropic], |
| 99 | + [ProcessingType.AIGemini, aiModels.gemini], |
| 100 | +]) |
| 101 | +
|
| 102 | +const props = defineProps<{ |
| 103 | + modelValue: AIConfig |
| 104 | + nodeSpecificType: ProcessingType |
| 105 | + readonly: boolean |
| 106 | + availableFields: Array<{ value: string }> |
| 107 | + rules: FormRules |
| 108 | + inputNumberComponent: Component |
| 109 | + tipComponent: Component |
| 110 | + modelOptsMap?: Map<ProcessingType, Array<string>> |
| 111 | + nodes?: Array<Node> |
| 112 | +}>() |
| 113 | +const emit = defineEmits<{ |
| 114 | + (e: 'update:modelValue', val: AIConfig): void |
| 115 | +}>() |
| 116 | +
|
| 117 | +const { t } = useFlowLocale() |
| 118 | +const { getCommonTypeLabel } = useFlowNode() |
| 119 | +
|
| 120 | +const FormCom = ref() |
| 121 | +
|
| 122 | +const record = computed({ |
| 123 | + get() { |
| 124 | + return props.modelValue |
| 125 | + }, |
| 126 | + set(val) { |
| 127 | + emit('update:modelValue', val) |
| 128 | + }, |
| 129 | +}) |
| 130 | +
|
| 131 | +const getFieldList = (queryString: string, cb: any) => { |
| 132 | + if (!queryString) { |
| 133 | + cb(props.availableFields) |
| 134 | + } |
| 135 | + const ret = props.availableFields.filter(({ value }) => value.includes(queryString)) |
| 136 | + cb(ret) |
| 137 | +} |
| 138 | +
|
| 139 | +const anthropicVersionOpts = Object.values(ANTHROPIC_VERSION_MAP) |
| 140 | +
|
| 141 | +const isAnthropicProfile = (profile: AIConfig): profile is AIAnthropicConfig => { |
| 142 | + return profile.type === AIProviderType.Anthropic |
| 143 | +} |
| 144 | +
|
| 145 | +const modelOpts = computed( |
| 146 | + () => (props.modelOptsMap ?? defaultModelOptsMap).get(props.nodeSpecificType) ?? [], |
| 147 | +) |
| 148 | +const isGemini = computed(() => props.nodeSpecificType === ProcessingType.AIGemini) |
| 149 | +const baseUrlProxy = computed({ |
| 150 | + get() { |
| 151 | + const { base_url } = record.value |
| 152 | + if (isGemini.value && base_url === GEMINI_DEFAULT_BASE_URL) { |
| 153 | + return '' |
| 154 | + } |
| 155 | + return base_url |
| 156 | + }, |
| 157 | + set(val: string) { |
| 158 | + if (isGemini.value) { |
| 159 | + if (!val) { |
| 160 | + record.value.base_url = GEMINI_DEFAULT_BASE_URL |
| 161 | + } else { |
| 162 | + record.value.base_url = val |
| 163 | + } |
| 164 | + } else { |
| 165 | + record.value.base_url = val |
| 166 | + } |
| 167 | + }, |
| 168 | +}) |
| 169 | +const defaultBaseURL = computed(() => |
| 170 | + isGemini.value ? GEMINI_DEFAULT_BASE_URL : `https://api.${props.modelValue.type}.com/v1`, |
| 171 | +) |
| 172 | +const title = computed(() => getCommonTypeLabel(props.nodeSpecificType)) |
| 173 | +
|
| 174 | +defineExpose({ validate: () => FormCom.value.validate(), defaultBaseURL }) |
| 175 | +</script> |
0 commit comments