Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: support openai endpoint for private deployment (#5204)

Danny Xu85715b2a4ca1
diff --git a/backend/api/v1/setting_service.go b/backend/api/v1/setting_service.go
index 8edce2d1a0..d3009c24b4 100644
--- a/backend/api/v1/setting_service.go
+++ b/backend/api/v1/setting_service.go
@@ -36,6 +36,7 @@ var whitelistSettings = []api.SettingName{
api.SettingAppIM,
api.SettingWatermark,
api.SettingPluginOpenAIKey,
+ api.SettingPluginOpenAIEndpoint,
api.SettingWorkspaceApproval,
}
diff --git a/backend/legacyapi/setting.go b/backend/legacyapi/setting.go
index 17b174863c..ddcb8e3967 100644
--- a/backend/legacyapi/setting.go
+++ b/backend/legacyapi/setting.go
@@ -29,6 +29,8 @@ const (
// SettingPluginOpenAIKey is used for OpenAI's API key.
// For AI-related features.
SettingPluginOpenAIKey SettingName = "bb.plugin.openai.key"
+ // SettingPluginOpenAIEndpoint is used for OpenAI's API endpoint.
+ SettingPluginOpenAIEndpoint SettingName = "bb.plugin.openai.endpoint"
// SettingPluginAgent is the setting name for the internal agent API.
// For now we will call the hub to fetch the subscription license.
SettingPluginAgent SettingName = "bb.plugin.agent"
diff --git a/backend/server/server.go b/backend/server/server.go
index 5fa17b2202..0ec2dc5507 100644
--- a/backend/server/server.go
+++ b/backend/server/server.go
@@ -708,6 +708,13 @@ func (s *Server) getInitSetting(ctx context.Context, datastore *store.Store) (*w
}, api.SystemBotID); err != nil {
return nil, err
}
+ if _, _, err := datastore.CreateSettingIfNotExistV2(ctx, &store.SettingMessage{
+ Name: api.SettingPluginOpenAIEndpoint,
+ Value: "",
+ Description: "API Endpoint for OpenAI",
+ }, api.SystemBotID); err != nil {
+ return nil, err
+ }
// initial workspace approval setting
approvalSettingValue, err := protojson.Marshal(&storepb.WorkspaceApprovalSetting{})
diff --git a/backend/server/setting.go b/backend/server/setting.go
index 84d36959bf..9cb379daf2 100644
--- a/backend/server/setting.go
+++ b/backend/server/setting.go
@@ -22,6 +22,7 @@ var whitelistSettings = []api.SettingName{
api.SettingWatermark,
api.SettingWorkspaceProfile,
api.SettingPluginOpenAIKey,
+ api.SettingPluginOpenAIEndpoint,
}
func (s *Server) registerSettingRoutes(g *echo.Group) {
diff --git a/frontend/src/components/GeneralSetting/AIAugmentationSetting.vue b/frontend/src/components/GeneralSetting/AIAugmentationSetting.vue
index 7a41f6f64f..4f3e3340d6 100644
--- a/frontend/src/components/GeneralSetting/AIAugmentationSetting.vue
+++ b/frontend/src/components/GeneralSetting/AIAugmentationSetting.vue
@@ -55,12 +55,45 @@
"
@input="handleOpenAIKeyChange"
/>
+ <label
+ class="flex items-center gap-x-2 tooltip-wrapper"
+ :class="[allowEdit ? 'cursor-pointer' : 'cursor-not-allowed']"
+ >
+ <span class="font-medium">{{
+ $t("settings.general.workspace.plugin.openai.openai-endpoint.self")
+ }}</span>
+
+ <FeatureBadge
+ feature="bb.feature.plugin.openai"
+ class="text-accent"
+ />
+
+ <span
+ v-if="!allowEdit"
+ class="text-sm text-gray-400 -translate-y-2 tooltip"
+ >
+ {{ $t("settings.general.workspace.only-owner-can-edit") }}
+ </span>
+ </label>
+ <div class="mb-3 text-sm text-gray-400">
+ {{
+ $t(
+ "settings.general.workspace.plugin.openai.openai-endpoint.description"
+ )
+ }}
+ </div>
+ <BBTextField
+ class="mb-5 w-full"
+ :disabled="!allowEdit"
+ :value="state.openAIEndpoint"
+ @input="handleOpenAIEndpointChange"
+ />
<div class="flex">
<button
type="button"
class="btn-primary ml-auto"
:disabled="!allowSave"
- @click.prevent="updateOpenAIKey"
+ @click.prevent="updateOpenAIKeyEndpoint"
>
{{ $t("common.update") }}
</button>
@@ -93,6 +126,7 @@ import scrollIntoView from "scroll-into-view-if-needed";
interface LocalState {
openAIKey: string;
+ openAIEndpoint: string;
showFeatureModal: boolean;
}
@@ -103,16 +137,17 @@ const containerRef = ref<HTMLDivElement>();
const state = reactive<LocalState>({
openAIKey: "",
+ openAIEndpoint: "",
showFeatureModal: false,
});
const openAIKeySetting = useSettingByName("bb.plugin.openai.key");
+const openAIEndpointSetting = useSettingByName("bb.plugin.openai.endpoint");
watchEffect(() => {
- state.openAIKey = openAIKeySetting.value?.value
- ? openAIKeySetting.value?.value.slice(0, 3) +
- "***" +
- openAIKeySetting.value?.value.slice(-4)
+ state.openAIKey = maskKey(openAIKeySetting.value?.value);
+ state.openAIEndpoint = openAIEndpointSetting.value?.value
+ ? openAIEndpointSetting.value?.value
: "";
});
@@ -124,14 +159,29 @@ const allowEdit = computed((): boolean => {
});
const allowSave = computed((): boolean => {
- return allowEdit.value && state.openAIKey !== openAIKeySetting.value?.value;
+ const openAIKeyUpdated =
+ state.openAIKey !== maskKey(openAIKeySetting.value?.value) ||
+ !state.openAIKey.includes("***");
+ return (
+ allowEdit.value &&
+ (openAIKeyUpdated ||
+ state.openAIEndpoint !== openAIEndpointSetting.value?.value)
+ );
});
+function maskKey(key: string | undefined): string {
+ return key ? key.slice(0, 3) + "***" + key.slice(-4) : "";
+}
+
const handleOpenAIKeyChange = (event: InputEvent) => {
state.openAIKey = (event.target as HTMLInputElement).value;
};
-const updateOpenAIKey = async () => {
+const handleOpenAIEndpointChange = (event: InputEvent) => {
+ state.openAIEndpoint = (event.target as HTMLInputElement).value;
+};
+
+const updateOpenAIKeyEndpoint = async () => {
if (!hasFeature("bb.feature.plugin.openai")) {
state.showFeatureModal = true;
return;
@@ -141,10 +191,21 @@ const updateOpenAIKey = async () => {
return;
}
- await settingStore.updateSettingByName({
- name: "bb.plugin.openai.key",
- value: state.openAIKey,
- });
+ if (
+ state.openAIKey !== maskKey(openAIKeySetting.value?.value) ||
+ !state.openAIKey.includes("***")
+ ) {
+ await settingStore.updateSettingByName({
+ name: "bb.plugin.openai.key",
+ value: state.openAIKey,
+ });
+ }
+ if (state.openAIEndpoint !== openAIEndpointSetting.value?.value) {
+ await settingStore.updateSettingByName({
+ name: "bb.plugin.openai.endpoint",
+ value: state.openAIEndpoint,
+ });
+ }
pushNotification({
module: "bytebase",
style: "SUCCESS",
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index c2a329b5b7..b0481f12b0 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -495,6 +495,10 @@
"self": "OpenAI API Key",
"description": "Supply OpenAI API Key to enable ChatSQL capability in SQL Editor. {viewDoc}",
"placeholder": "An OpenAI API Key usually starts with sk-*****"
+ },
+ "openai-endpoint": {
+ "self": "OpenAI API Endpoint",
+ "description": "Supply private deployment of OpenAI API Endpoint."
}
}
}
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 67576ab154..c6f5a5d262 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -493,6 +493,10 @@
"self": "OpenAI API Key",
"description": "提供 OpenAI API Key 以在 SQL 编辑器中开启 ChatSQL 的能力。{viewDoc}",
"placeholder": "OpenAI API Key 通常以 sk-***** 开头"
+ },
+ "openai-endpoint": {
+ "self": "OpenAI API Endpoint",
+ "description": "提供私有化部署的 OpenAI API Endpoint。"
}
}
}
diff --git a/frontend/src/plugins/ai/components/AIChatToSQL.vue b/frontend/src/plugins/ai/components/AIChatToSQL.vue
index b3cbf7d31b..49c18c516c 100644
--- a/frontend/src/plugins/ai/components/AIChatToSQL.vue
+++ b/frontend/src/plugins/ai/components/AIChatToSQL.vue
@@ -38,7 +38,9 @@ const state = reactive<LocalState>({
});
const openAIKeySetting = useSettingByName("bb.plugin.openai.key");
+const openAIEndpointSetting = useSettingByName("bb.plugin.openai.endpoint");
const openAIKey = computed(() => openAIKeySetting.value?.value ?? "");
+const openAIEndpoint = computed(() => openAIEndpointSetting.value?.value ?? "");
const tab = useCurrentTab();
const instance = useInstanceById(
@@ -62,6 +64,7 @@ const chat = useChatByTab();
provideAIContext({
openAIKey,
+ openAIEndpoint,
engineType: computed(() => instance.value.engine),
databaseMetadata: databaseMetadata,
autoRun,
diff --git a/frontend/src/plugins/ai/components/ChatPanel.vue b/frontend/src/plugins/ai/components/ChatPanel.vue
index 5fb1706c25..9df37b860c 100644
--- a/frontend/src/plugins/ai/components/ChatPanel.vue
+++ b/frontend/src/plugins/ai/components/ChatPanel.vue
@@ -53,7 +53,8 @@ const store = useConversationStore();
const isChatMode = computed(() => tab.value.editMode === "CHAT-TO-SQL");
const context = useAIContext();
-const { events, openAIKey, autoRun, showHistoryDialog } = context;
+const { events, openAIKey, openAIEndpoint, autoRun, showHistoryDialog } =
+ context;
const {
list: conversationList,
ready,
@@ -124,7 +125,10 @@ const requestAI = async (query: string) => {
conversation_id: conversation.id,
status: "LOADING",
});
- const url = "https://api.openai.com/v1/chat/completions";
+ const url =
+ openAIEndpoint.value === ""
+ ? "https://api.openai.com/v1/chat/completions"
+ : openAIEndpoint.value + "/v1/chat/completions";
const messages: OpenAIMessage[] = [];
conversation.messageList.forEach((message) => {
diff --git a/frontend/src/plugins/ai/logic/useDynamicSuggestions.ts b/frontend/src/plugins/ai/logic/useDynamicSuggestions.ts
index 6fa55681b0..0bbba65104 100644
--- a/frontend/src/plugins/ai/logic/useDynamicSuggestions.ts
+++ b/frontend/src/plugins/ai/logic/useDynamicSuggestions.ts
@@ -56,7 +56,10 @@ export const useDynamicSuggestions = () => {
"Content-Type": "application/json",
Authorization: `Bearer ${context.openAIKey.value}`,
};
- const url = "https://api.openai.com/v1/chat/completions";
+ const url =
+ context.openAIEndpoint.value === ""
+ ? "https://api.openai.com/v1/chat/completions"
+ : context.openAIEndpoint.value + "/v1/chat/completions";
try {
const response: AxiosResponse<string> = await axios.post(
url,
diff --git a/frontend/src/plugins/ai/types/context.ts b/frontend/src/plugins/ai/types/context.ts
index 3d045f0627..24f4218106 100644
--- a/frontend/src/plugins/ai/types/context.ts
+++ b/frontend/src/plugins/ai/types/context.ts
@@ -18,6 +18,7 @@ export type AIChatInfo = {
export type AIContext = {
openAIKey: Ref<string>;
+ openAIEndpoint: Ref<string>;
engineType: Ref<EngineType | undefined>;
databaseMetadata: Ref<DatabaseMetadata | undefined>;
autoRun: Ref<boolean>;
diff --git a/frontend/src/types/setting.ts b/frontend/src/types/setting.ts
index 7b2f780cb4..e279aeea78 100644
--- a/frontend/src/types/setting.ts
+++ b/frontend/src/types/setting.ts
@@ -6,7 +6,8 @@ export type SettingName =
| "bb.workspace.watermark"
| "bb.workspace.profile"
| "bb.workspace.approval"
- | "bb.plugin.openai.key";
+ | "bb.plugin.openai.key"
+ | "bb.plugin.openai.endpoint";
export type Setting = {
id: SettingId;