Skip to content

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

chore: aisetting refactor and use api-key for azure openai (#15617)

Danny Xu50717d97fd2c
diff --git a/frontend/src/plugins/ai/components/AIChatToSQL.vue b/frontend/src/plugins/ai/components/AIChatToSQL.vue
index 563269b8b4..6a8edf3a1e 100644
--- a/frontend/src/plugins/ai/components/AIChatToSQL.vue
+++ b/frontend/src/plugins/ai/components/AIChatToSQL.vue
@@ -1,10 +1,10 @@
<template>
- <ChatPanel v-if="openAIKey" />
+ <ChatPanel v-if="aiSetting.enabled" />
</template>
<script lang="ts" setup>
import { useAIContext } from "../logic";
const [{ default: ChatPanel }] = await Promise.all([import("./ChatPanel.vue")]);
-const { openAIKey } = useAIContext();
+const { aiSetting } = useAIContext();
</script>
diff --git a/frontend/src/plugins/ai/components/ChatPanel.vue b/frontend/src/plugins/ai/components/ChatPanel.vue
index 9becc56ed6..b414ae9aa6 100644
--- a/frontend/src/plugins/ai/components/ChatPanel.vue
+++ b/frontend/src/plugins/ai/components/ChatPanel.vue
@@ -1,6 +1,6 @@
<template>
<div
- v-if="openAIKey"
+ v-if="aiSetting.enabled"
class="w-full h-full flex-1 flex flex-col overflow-hidden"
>
<ActionBar />
@@ -28,14 +28,14 @@
</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 { nextAnimationFrame } from "@/utils";
import { onConnectionChanged, useAIContext, useCurrentChat } from "../logic";
import * as promptUtils from "../logic/prompt";
import { useConversationStore } from "../store";
@@ -59,9 +59,7 @@ const store = useConversationStore();
const context = useAIContext();
const {
- openAIKey,
- openAIEndpoint,
- openAIModel,
+ aiSetting,
showHistoryDialog,
pendingSendChat,
} = context;
@@ -126,7 +124,7 @@ const requestAI = async (query: string) => {
});
});
const body = {
- model: openAIModel.value,
+ model: aiSetting.value.model,
messages,
temperature: 0,
stop: ["#", ";"],
@@ -139,13 +137,17 @@ const requestAI = async (query: string) => {
timeout: 300 * 1000,
responseType: "json",
});
- const headers = {
+ const headers: { [key: string]: string } = {
"Content-Type": "application/json",
- Authorization: `Bearer ${openAIKey.value}`,
};
+ if (context.aiSetting.value.provider === "AZURE_OPENAI") {
+ headers["api-key"] = context.aiSetting.value.apiKey;
+ } else {
+ headers["Authorization"] = `Bearer ${context.aiSetting.value.apiKey}`;
+ }
try {
const response: AxiosResponse<string> = await axios.post(
- openAIEndpoint.value,
+ aiSetting.value.endpoint,
JSON.stringify(body),
{
headers,
diff --git a/frontend/src/plugins/ai/components/ProvideAIContext.vue b/frontend/src/plugins/ai/components/ProvideAIContext.vue
index d143973467..1708926462 100644
--- a/frontend/src/plugins/ai/components/ProvideAIContext.vue
+++ b/frontend/src/plugins/ai/components/ProvideAIContext.vue
@@ -3,9 +3,6 @@
</template>
<script lang="ts" setup>
-import Emittery from "emittery";
-import { storeToRefs } from "pinia";
-import { computed, reactive, ref, toRef } from "vue";
import { useEmitteryEventListener } from "@/composables/useEmitteryEventListener";
import {
useConnectionOfCurrentSQLEditorTab,
@@ -13,7 +10,11 @@ import {
useSettingV1Store,
useSQLEditorTabStore,
} from "@/store";
+import { AISetting } from "@/types/proto/v1/setting_service";
import { wrapRefAsPromise } from "@/utils";
+import Emittery from "emittery";
+import { storeToRefs } from "pinia";
+import { computed, reactive, ref, toRef } from "vue";
import { provideAIContext, useChatByTab, useCurrentChat } from "../logic";
import { useConversationStore } from "../store";
import type { AIContext, AIContextEvents } from "../types";
@@ -27,12 +28,7 @@ const state = reactive<LocalState>({
});
const settingV1Store = useSettingV1Store();
-const aiSetting = settingV1Store.getSettingByName("bb.ai");
-const openAIKey = computed(() => aiSetting?.value?.aiSetting?.apiKey ?? "");
-const openAIEndpoint = computed(
- () => aiSetting?.value?.aiSetting?.endpoint ?? ""
-);
-const openAIModel = computed(() => aiSetting?.value?.aiSetting?.model ?? "");
+const aiSetting = computed(() => settingV1Store.getSettingByName("bb.ai")?.value?.aiSetting ?? AISetting.create());
const { instance, database } = useConnectionOfCurrentSQLEditorTab();
const databaseMetadata = useMetadata(
@@ -51,9 +47,7 @@ const schema = computed(() => tab.value?.connection.schema);
const store = useConversationStore();
const context: AIContext = {
- openAIKey,
- openAIEndpoint,
- openAIModel,
+ aiSetting: aiSetting,
engine: computed(() => instance.value.engine),
databaseMetadata,
schema,
diff --git a/frontend/src/plugins/ai/components/editor-actions.ts b/frontend/src/plugins/ai/components/editor-actions.ts
index cd74bb365e..626526ec79 100644
--- a/frontend/src/plugins/ai/components/editor-actions.ts
+++ b/frontend/src/plugins/ai/components/editor-actions.ts
@@ -1,6 +1,3 @@
-import type monaco from "monaco-editor";
-import type { MaybeRef } from "vue";
-import { computed, shallowRef, unref, watchEffect } from "vue";
import type { MonacoModule } from "@/components/MonacoEditor";
import {
useSelectedContent,
@@ -8,6 +5,9 @@ import {
} from "@/components/MonacoEditor/composables";
import { useTextModelLanguage } from "@/components/MonacoEditor/composables/common";
import { useEditorContextKey } from "@/components/MonacoEditor/utils";
+import type monaco from "monaco-editor";
+import type { MaybeRef } from "vue";
+import { computed, shallowRef, unref, watchEffect } from "vue";
import type { AIContext, ChatAction } from "../types";
type AIActionsOptions = {
@@ -56,7 +56,7 @@ export const useAIActions = async (
});
actions.value = [];
- if (!context.openAIKey.value) {
+ if (!context.aiSetting.value.enabled) {
return;
}
diff --git a/frontend/src/plugins/ai/logic/useDynamicSuggestions.ts b/frontend/src/plugins/ai/logic/useDynamicSuggestions.ts
index 3eb0b141dd..9fed8f1ee7 100644
--- a/frontend/src/plugins/ai/logic/useDynamicSuggestions.ts
+++ b/frontend/src/plugins/ai/logic/useDynamicSuggestions.ts
@@ -1,8 +1,8 @@
+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 { WebStorageHelper } from "@/utils";
import type { OpenAIMessage, OpenAIResponse } from "../types";
import { useAIContext } from "./context";
import * as promptUtils from "./prompt";
@@ -43,7 +43,7 @@ export const useDynamicSuggestions = () => {
const requestAI = async (messages: OpenAIMessage[]) => {
await new Promise((resolve) => setTimeout(resolve, 1000));
const body = {
- model: context.openAIModel.value,
+ model: context.aiSetting.value.model,
messages,
temperature: 0,
stop: ["#", ";"],
@@ -55,13 +55,17 @@ export const useDynamicSuggestions = () => {
timeout: 300 * 1000,
responseType: "json",
});
- const headers = {
+ const headers: { [key: string]: string } = {
"Content-Type": "application/json",
- Authorization: `Bearer ${context.openAIKey.value}`,
};
+ if (context.aiSetting.value.provider === "AZURE_OPENAI") {
+ headers["api-key"] = context.aiSetting.value.apiKey;
+ } else {
+ headers["Authorization"] = `Bearer ${context.aiSetting.value.apiKey}`;
+ }
try {
const response: AxiosResponse<string> = await axios.post(
- context.openAIEndpoint.value,
+ context.aiSetting.value.endpoint,
JSON.stringify(body),
{
headers,
diff --git a/frontend/src/plugins/ai/types/context.ts b/frontend/src/plugins/ai/types/context.ts
index 267bb92a52..95067333c9 100644
--- a/frontend/src/plugins/ai/types/context.ts
+++ b/frontend/src/plugins/ai/types/context.ts
@@ -1,7 +1,8 @@
-import type Emittery from "emittery";
-import type { Ref } from "vue";
import type { Engine } from "@/types/proto/v1/common";
import type { DatabaseMetadata } from "@/types/proto/v1/database_service";
+import type { AISetting } from "@/types/proto/v1/setting_service";
+import type Emittery from "emittery";
+import type { Ref } from "vue";
import type { Conversation } from "./conversation";
export type AIContextEvents = Emittery<{
@@ -18,9 +19,7 @@ export type AIChatInfo = {
};
export type AIContext = {
- openAIKey: Ref<string>;
- openAIEndpoint: Ref<string>;
- openAIModel: Ref<string>;
+ aiSetting: Ref<AISetting>;
engine: Ref<Engine | undefined>;
databaseMetadata: Ref<DatabaseMetadata | undefined>;
schema: Ref<string | undefined>;