Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: support switch ai provider (#15618)
ecmadao5e799656c0e1
diff --git a/frontend/src/components/GeneralSetting/AIAugmentationSetting.vue b/frontend/src/components/GeneralSetting/AIAugmentationSetting.vueindex 3d3e07704d..40b895970e 100644--- a/frontend/src/components/GeneralSetting/AIAugmentationSetting.vue+++ b/frontend/src/components/GeneralSetting/AIAugmentationSetting.vue@@ -38,24 +38,37 @@</div></div><template v-if="state.enabled">+ <div>+ <label class="flex items-center gap-x-2 mb-2">+ <span class="font-medium">{{+ $t("settings.general.workspace.ai-assistant.provider.self")+ }}</span>+ </label>+ <NSelect+ style="width: 12rem"+ v-model:value="state.provider"+ :options="providerOptions"+ :consistent-menu-width="true"+ />+ </div><div><label class="flex items-center gap-x-2"><span class="font-medium">{{- $t("settings.general.workspace.ai-assistant.openai-key.self")+ $t("settings.general.workspace.ai-assistant.api-key.self")}}</span></label><div class="mb-3 text-sm text-gray-400"><i18n-t- keypath="settings.general.workspace.ai-assistant.openai-key.description"+ keypath="settings.general.workspace.ai-assistant.api-key.description"><template #viewDoc><a- href="https://platform.openai.com/account/api-keys"+ :href="providerDefault.apiKeyDoc"class="normal-link"target="_blank">{{$t(- "settings.general.workspace.ai-assistant.openai-key.find-my-key"+ "settings.general.workspace.ai-assistant.api-key.find-my-key")}}</a>@@ -65,12 +78,12 @@<NTooltip placement="top-start" :disabled="allowEdit"><template #trigger><BBTextField- v-model:value="state.openAIKey"+ v-model:value="state.apiKey":required="true":disabled="!allowEdit || !hasAIFeature":placeholder="$t(- 'settings.general.workspace.ai-assistant.openai-key.placeholder'+ 'settings.general.workspace.ai-assistant.api-key.placeholder')"/>@@ -84,24 +97,23 @@<div><label class="flex items-center gap-x-2"><span class="font-medium">{{- $t(- "settings.general.workspace.ai-assistant.openai-endpoint.self"- )+ $t("settings.general.workspace.ai-assistant.endpoint.self")}}</span></label><div class="mb-3 text-sm text-gray-400">{{$t(- "settings.general.workspace.ai-assistant.openai-endpoint.description"+ "settings.general.workspace.ai-assistant.endpoint.description")}}</div><NTooltip placement="top-start" :disabled="allowEdit"><template #trigger><BBTextField- v-model:value="state.openAIEndpoint"+ v-model:value="state.endpoint":required="true":disabled="!allowEdit || !hasAIFeature"+ :placeholder="providerDefault.endpoint"/></template><span class="text-sm text-gray-400 -translate-y-2">@@ -113,20 +125,18 @@<div><label class="flex items-center gap-x-2"><span class="font-medium">{{- $t("settings.general.workspace.ai-assistant.openai-model.self")+ $t("settings.general.workspace.ai-assistant.model.self")}}</span></label><div class="mb-3 text-sm text-gray-400">{{- $t(- "settings.general.workspace.ai-assistant.openai-model.description"- )+ $t("settings.general.workspace.ai-assistant.model.description")}}</div><NTooltip placement="top-start" :disabled="allowEdit"><template #trigger><BBTextField- v-model:value="state.openAIModel"+ v-model:value="state.model":required="true":disabled="!allowEdit || !hasAIFeature"/>@@ -143,9 +153,10 @@</template><script lang="ts" setup>-import { NTooltip } from "naive-ui";+import { NTooltip, NSelect } from "naive-ui";import scrollIntoView from "scroll-into-view-if-needed";-import { computed, onMounted, reactive, ref, watchEffect } from "vue";+import { computed, onMounted, reactive, ref, watchEffect, watch } from "vue";+import { useI18n } from "vue-i18n";import { BBTextField } from "@/bbkit";import LearnMoreLink from "@/components/LearnMoreLink.vue";import { Switch } from "@/components/v2";@@ -159,9 +170,10 @@ import { FeatureBadge } from "../FeatureGuard";interface LocalState {enabled: boolean;- openAIKey: string;- openAIEndpoint: string;- openAIModel: string;+ apiKey: string;+ endpoint: string;+ model: string;+ provider: AISetting_Provider;}const props = defineProps<{@@ -171,12 +183,14 @@ const props = defineProps<{const settingV1Store = useSettingV1Store();const containerRef = ref<HTMLDivElement>();+const { t } = useI18n();const state = reactive<LocalState>({enabled: false,- openAIKey: "",- openAIEndpoint: "",- openAIModel: "",+ apiKey: "",+ endpoint: "",+ model: "",+ provider: AISetting_Provider.OPEN_AI,});const aiSetting = computed(@@ -188,12 +202,35 @@ const hasAIFeature = computed(() => hasFeature("bb.feature.ai-assistant"));const getInitialState = (): LocalState => {return {enabled: aiSetting.value?.enabled ?? false,- openAIKey: maskKey(aiSetting.value?.apiKey),- openAIEndpoint: aiSetting.value?.endpoint ?? "",- openAIModel: aiSetting.value?.model ?? "",+ apiKey: maskKey(aiSetting.value?.apiKey),+ endpoint: aiSetting.value?.endpoint ?? "",+ model: aiSetting.value?.model ?? "",+ provider: aiSetting.value?.provider ?? AISetting_Provider.OPEN_AI,};};+const providerOptions = computed(() =>+ [AISetting_Provider.OPEN_AI, AISetting_Provider.AZURE_OPENAI].map(+ (provider) => {+ let label = "";+ switch (provider) {+ case AISetting_Provider.OPEN_AI:+ label = t("settings.general.workspace.ai-assistant.provider.open_ai");+ break;+ case AISetting_Provider.AZURE_OPENAI:+ label = t(+ "settings.general.workspace.ai-assistant.provider.azure_open_ai"+ );+ break;+ }+ return {+ label,+ value: provider,+ };+ }+ )+);+watchEffect(() => {Object.assign(state, getInitialState());});@@ -202,11 +239,10 @@ const allowSave = computed((): boolean => {const initValue = getInitialState();const enabledUpdated = state.enabled !== initValue.enabled;const openAIKeyUpdated =- state.openAIKey !== initValue.openAIKey ||- (state.openAIKey && !state.openAIKey.includes("***"));- const openAIEndpointUpdated =- state.openAIEndpoint !== initValue.openAIEndpoint;- const openAIModelUpdated = state.openAIModel !== initValue.openAIModel;+ state.apiKey !== initValue.apiKey ||+ (state.apiKey && !state.apiKey.includes("***"));+ const openAIEndpointUpdated = state.endpoint !== initValue.endpoint;+ const openAIModelUpdated = state.model !== initValue.model;return (enabledUpdated ||openAIKeyUpdated ||@@ -219,16 +255,48 @@ function maskKey(key: string | undefined): string {return key ? key.slice(0, 3) + "***" + key.slice(-4) : "";}+const providerDefault = computed(() => {+ switch (state.provider) {+ case AISetting_Provider.OPEN_AI:+ return {+ apiKey: "",+ apiKeyDoc: "https://platform.openai.com/account/api-keys",+ endpoint: "https://api.openai.com/v1/chat/completions",+ model: "gpt-3.5-turbo",+ };+ case AISetting_Provider.AZURE_OPENAI:+ return {+ apiKey: "",+ apiKeyDoc: "https://ai.azure.com/",+ endpoint:+ "https://{resource name}.openai.azure.com/openai/deployments/{deployment id}/chat/completions?api-version=2024-06-0",+ model: "gpt-4o",+ };+ default:+ return {+ apiKey: "",+ apiKeyDoc: "",+ endpoint: "",+ model: "",+ };+ }+});++watch(+ () => state.provider,+ () => {+ Object.assign(state, providerDefault.value);+ }+);+const toggleAIEnabled = (on: boolean) => {if (!on) {return;}- if (state.openAIEndpoint === "") {- state.openAIEndpoint = "https://api.openai.com/v1/chat/completions";- }- if (state.openAIModel === "") {- state.openAIModel = "gpt-3.5-turbo";- }+ Object.assign(state, {+ endpoint: state.endpoint || providerDefault.value.endpoint,+ model: state.model || providerDefault.value.model,+ });};const updateAISetting = async () => {@@ -238,11 +306,10 @@ const updateAISetting = async () => {aiSetting: AISetting.fromPartial({...(aiSetting.value ?? {}),enabled: state.enabled,- apiKey: state.openAIKey,- endpoint: state.openAIEndpoint,- model: state.openAIModel,- // TODO(ed): support change provider.- provider: AISetting_Provider.OPEN_AI,+ apiKey: state.apiKey,+ endpoint: state.endpoint,+ model: state.model,+ provider: state.provider,}),},});diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.jsonindex 9f6ba91f3f..68813c8669 100644--- a/frontend/src/locales/en-US.json+++ b/frontend/src/locales/en-US.json@@ -556,20 +556,25 @@"ai-assistant": {"self": "AI Assistant","enable-ai-assistant": "Enable AI Assistant",- "description": "Use OpenAPI to assist the database development tasks.",- "openai-key": {- "self": "OpenAI API Key",- "description": "Supply OpenAI API Key. {viewDoc}",+ "description": "Use AI to assist the database development tasks.",+ "provider": {+ "self": "AI Provider",+ "open_ai": "Open AI",+ "azure_open_ai": "Azure Open AI"+ },+ "api-key": {+ "self": "API Key",+ "description": "Supply API Key. {viewDoc}","find-my-key": "Find my key",- "placeholder": "An OpenAI API Key usually starts with sk-*****"+ "placeholder": "An API Key usually starts with sk-*****"},- "openai-endpoint": {- "self": "OpenAI API Endpoint",- "description": "Supply private deployment of OpenAI API Endpoint."+ "endpoint": {+ "self": "API Endpoint",+ "description": "Supply private deployment of API Endpoint."},- "openai-model": {- "self": "OpenAI Model Name",- "description": "Supply private deployment of OpenAI Model name."+ "model": {+ "self": "Model Name",+ "description": "Supply private deployment of model name."}},"announcement": {diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.jsonindex a34e8f37e0..4250a1684a 100644--- a/frontend/src/locales/es-ES.json+++ b/frontend/src/locales/es-ES.json@@ -556,20 +556,25 @@"ai-assistant": {"self": "Asistente de AI","enable-ai-assistant": "Habilitar el Asistente de IA",- "description": "Utilice OpenAPI para ayudar en las tareas de desarrollo de bases de datos.",- "openai-key": {- "self": "Clave de API de OpenAI",- "description": "Proporcione una clave de API de OpenAI para habilitar la capacidad Text2SQL en el Editor de SQL. {viewDoc}",+ "description": "Utilice AI para ayudar en las tareas de desarrollo de bases de datos.",+ "provider": {+ "self": "Proveedor de IA",+ "open_ai": "Open AI",+ "azure_open_ai": "Azure Open AI"+ },+ "api-key": {+ "self": "Clave de API",+ "description": "Proporcione una clave de API para habilitar la capacidad Text2SQL en el Editor de SQL. {viewDoc}","find-my-key": "Encuentra mi llave",- "placeholder": "Una clave de API de OpenAI generalmente comienza con sk-*****"+ "placeholder": "Una clave de API generalmente comienza con sk-*****"},- "openai-endpoint": {- "self": "Punto de conexión de API de OpenAI",- "description": "Proporcione una implementación privada del Punto de conexión de API de OpenAI."+ "endpoint": {+ "self": "Punto de conexión de API",+ "description": "Proporcione una implementación privada del Punto de conexión de API."},- "openai-model": {- "self": "Nombre del modelo de OpenAI",- "description": "Suministro de implementación privada del nombre del modelo de OpenAI."+ "model": {+ "self": "Nombre del modelo",+ "description": "Suministro de implementación privada del nombre del modelo."}},"announcement": {@@ -2590,4 +2595,4 @@"last-visit": "Última visita","changelog-for-version": "Registro de cambios para {version}"}-}\ No newline at end of file+}diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.jsonindex 19529f54c0..b252820d86 100644--- a/frontend/src/locales/ja-JP.json+++ b/frontend/src/locales/ja-JP.json@@ -556,20 +556,25 @@"ai-assistant": {"self": "AI アシスタント","enable-ai-assistant": "AIアシスタントを有効にする",- "description": "OpenAPI を使用してデータベース開発タスクを支援します。",- "openai-key": {- "self": "OpenAI APIキー",- "description": "SQL エディターで Text2SQL を開く機能を有効にする OpenAI API キーを提供します。 {viewDoc}",+ "description": "AI を使用してデータベース開発タスクを支援します。",+ "provider": {+ "self": "AIプロバイダー",+ "open_ai": "Open AI",+ "azure_open_ai": "Azure Open AI"+ },+ "api-key": {+ "self": "APIキー",+ "description": "SQL エディターで Text2SQL を開く機能を有効にする API キーを提供します。 {viewDoc}","find-my-key": "鍵を探す",- "placeholder": "OpenAI API キーは通常 sk-***** で始まります"+ "placeholder": "API キーは通常 sk-***** で始まります"},- "openai-endpoint": {- "self": "OpenAI API エンドポイント",- "description": "プライベート展開用の OpenAI API エンドポイントを提供します。"+ "endpoint": {+ "self": "API エンドポイント",+ "description": "プライベート展開用の API エンドポイントを提供します。"},- "openai-model": {- "self": "OpenAIモデル名",- "description": "供給プライベートデプロイメントのOpenAIモデル名。"+ "model": {+ "self": "モデル名",+ "description": "供給プライベートデプロイメントのモデル名。"}},"announcement": {@@ -2590,4 +2595,4 @@"last-visit": "最終訪問","changelog-for-version": "{version} の変更履歴"}-}\ No newline at end of file+}diff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.jsonindex e072fb2777..71d54718fd 100644--- a/frontend/src/locales/vi-VN.json+++ b/frontend/src/locales/vi-VN.json@@ -556,20 +556,25 @@"ai-assistant": {"self": "Trợ lý AI","enable-ai-assistant": "Bật Trợ lý AI",- "description": "Sử dụng OpenAPI để hỗ trợ các tác vụ phát triển cơ sở dữ liệu.",- "openai-key": {- "self": "Khóa API OpenAI",- "description": "Cung cấp Khóa API OpenAI. {viewDoc}",+ "description": "Sử dụng AI để hỗ trợ các tác vụ phát triển cơ sở dữ liệu.",+ "provider": {+ "self": "Nhà cung cấp AI",+ "open_ai": "Open AI",+ "azure_open_ai": "Azure Open AI"+ },+ "api-key": {+ "self": "Khóa API",+ "description": "Cung cấp Khóa API. {viewDoc}","find-my-key": "Tìm khóa của tôi",- "placeholder": "Khóa API OpenAI thường bắt đầu bằng sk-*****"+ "placeholder": "Khóa API thường bắt đầu bằng sk-*****"},- "openai-endpoint": {- "self": "Điểm cuối API OpenAI",- "description": "Cung cấp triển khai riêng của Điểm cuối API OpenAI."+ "endpoint": {+ "self": "Điểm cuối API",+ "description": "Cung cấp triển khai riêng của Điểm cuối API."},- "openai-model": {- "self": "Tên mô hình OpenAI",- "description": "Cung cấp triển khai riêng của tên mô hình OpenAI."+ "model": {+ "self": "Tên mô hình",+ "description": "Cung cấp triển khai riêng của tên mô hình."}},"announcement": {@@ -2590,4 +2595,4 @@"last-visit": "Lần truy cập cuối","changelog-for-version": "Nhật ký thay đổi cho phiên bản {version}"}-}\ No newline at end of file+}diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.jsonindex b2dd07804c..b2d8bfff59 100644--- a/frontend/src/locales/zh-CN.json+++ b/frontend/src/locales/zh-CN.json@@ -556,20 +556,25 @@"ai-assistant": {"self": "AI 助手","enable-ai-assistant": "启用 AI 助手",- "description": "使用 OpenAPI 来辅助数据库开发任务。",- "openai-key": {- "self": "OpenAI API Key",- "description": "提供 OpenAI API Key 以开启 AI 助手。{viewDoc}",+ "description": "使用 AI 来辅助数据库开发任务。",+ "provider": {+ "self": "AI 提供方",+ "open_ai": "Open AI",+ "azure_open_ai": "Azure Open AI"+ },+ "api-key": {+ "self": "API Key",+ "description": "提供 API Key 以开启 AI 助手。{viewDoc}","find-my-key": "找到我的 Key",- "placeholder": "OpenAI API Key 通常以 sk-***** 开头"+ "placeholder": "API Key 通常以 sk-***** 开头"},- "openai-endpoint": {- "self": "OpenAI API Endpoint",- "description": "提供私有化部署的 OpenAI API Endpoint。"+ "endpoint": {+ "self": "API Endpoint",+ "description": "提供私有化部署的 API Endpoint。"},- "openai-model": {- "self": "OpenAI Model Name",- "description": "提供私有化部署的 OpenAI 模型名称。"+ "model": {+ "self": "Model Name",+ "description": "提供私有化部署的模型名称。"}},"announcement": {diff --git a/frontend/src/plugins/ai/components/ChatPanel.vue b/frontend/src/plugins/ai/components/ChatPanel.vueindex b414ae9aa6..7cf1d2a50a 100644--- a/frontend/src/plugins/ai/components/ChatPanel.vue+++ b/frontend/src/plugins/ai/components/ChatPanel.vue@@ -28,14 +28,15 @@</template><script lang="ts" setup>-import { useSQLEditorTabStore } from "@/store";-import { nextAnimationFrame } from "@/utils";import type { AxiosResponse } from "axios";import { Axios } from "axios";import { head } from "lodash-es";import { NSpin } from "naive-ui";import { storeToRefs } from "pinia";import { reactive, watch } from "vue";+import { useSQLEditorTabStore } from "@/store";+import { AISetting_Provider } from "@/types/proto/v1/setting_service";+import { nextAnimationFrame } from "@/utils";import { onConnectionChanged, useAIContext, useCurrentChat } from "../logic";import * as promptUtils from "../logic/prompt";import { useConversationStore } from "../store";@@ -58,11 +59,7 @@ const { currentTab: tab } = storeToRefs(useSQLEditorTabStore());const store = useConversationStore();const context = useAIContext();-const {- aiSetting,- showHistoryDialog,- pendingSendChat,-} = context;+const { aiSetting, showHistoryDialog, pendingSendChat } = context;const {list: conversationList,ready,@@ -140,7 +137,7 @@ const requestAI = async (query: string) => {const headers: { [key: string]: string } = {"Content-Type": "application/json",};- if (context.aiSetting.value.provider === "AZURE_OPENAI") {+ if (context.aiSetting.value.provider === AISetting_Provider.AZURE_OPENAI) {headers["api-key"] = context.aiSetting.value.apiKey;} else {headers["Authorization"] = `Bearer ${context.aiSetting.value.apiKey}`;diff --git a/frontend/src/plugins/ai/logic/useDynamicSuggestions.ts b/frontend/src/plugins/ai/logic/useDynamicSuggestions.tsindex 9fed8f1ee7..ea49c83446 100644--- a/frontend/src/plugins/ai/logic/useDynamicSuggestions.ts+++ b/frontend/src/plugins/ai/logic/useDynamicSuggestions.ts@@ -1,8 +1,9 @@-import { hashCode } from "@/bbkit/BBUtil";-import { WebStorageHelper } from "@/utils";import { Axios, type AxiosResponse } from "axios";import { head, uniq, values } from "lodash-es";import { computed, reactive, ref } from "vue";+import { hashCode } from "@/bbkit/BBUtil";+import { AISetting_Provider } from "@/types/proto/v1/setting_service";+import { WebStorageHelper } from "@/utils";import type { OpenAIMessage, OpenAIResponse } from "../types";import { useAIContext } from "./context";import * as promptUtils from "./prompt";@@ -58,7 +59,7 @@ export const useDynamicSuggestions = () => {const headers: { [key: string]: string } = {"Content-Type": "application/json",};- if (context.aiSetting.value.provider === "AZURE_OPENAI") {+ if (context.aiSetting.value.provider === AISetting_Provider.AZURE_OPENAI) {headers["api-key"] = context.aiSetting.value.apiKey;} else {headers["Authorization"] = `Bearer ${context.aiSetting.value.apiKey}`;