Skip to content

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

chore: require ai key, endpoint and model (#15613)

ecmadaoa01c19230fbe
diff --git a/backend/api/v1/setting_service.go b/backend/api/v1/setting_service.go
index 8e8ee3388f..5b9375f327 100644
--- a/backend/api/v1/setting_service.go
+++ b/backend/api/v1/setting_service.go
@@ -568,6 +568,9 @@ func (s *SettingService) UpdateSetting(ctx context.Context, request *v1pb.Update
if err := convertProtoToProto(request.Setting.Value.GetAiSetting(), aiSetting); err != nil {
return nil, status.Errorf(codes.Internal, "failed to unmarshal setting value for %s with error: %v", apiSettingName, err)
}
+ if aiSetting.ApiKey == "" || aiSetting.Endpoint == "" || aiSetting.Model == "" {
+ return nil, status.Errorf(codes.InvalidArgument, "both api key, endpoint and model are required")
+ }
bytes, err := protojson.Marshal(aiSetting)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to marshal setting for %s with error: %v", apiSettingName, err)
diff --git a/frontend/src/components/GeneralSetting/AIAugmentationSetting.vue b/frontend/src/components/GeneralSetting/AIAugmentationSetting.vue
index 68cd56a14b..cae6021877 100644
--- a/frontend/src/components/GeneralSetting/AIAugmentationSetting.vue
+++ b/frontend/src/components/GeneralSetting/AIAugmentationSetting.vue
@@ -46,9 +46,10 @@
</div>
<NTooltip placement="top-start" :disabled="allowEdit">
<template #trigger>
- <NInput
+ <BBTextField
v-model:value="state.openAIKey"
class="mb-4 w-full"
+ :required="true"
:disabled="!allowEdit || !hasAIFeature"
:placeholder="
$t(
@@ -77,9 +78,10 @@
</div>
<NTooltip placement="top-start" :disabled="allowEdit">
<template #trigger>
- <NInput
+ <BBTextField
v-model:value="state.openAIEndpoint"
class="mb-4 w-full"
+ :required="true"
:disabled="!allowEdit || !hasAIFeature"
/>
</template>
@@ -102,9 +104,10 @@
</div>
<NTooltip placement="top-start" :disabled="allowEdit">
<template #trigger>
- <NInput
+ <BBTextField
v-model:value="state.openAIModel"
class="mb-4 w-full"
+ :required="true"
:disabled="!allowEdit || !hasAIFeature"
/>
</template>
@@ -119,9 +122,10 @@
</template>
<script lang="ts" setup>
-import { NInput, NTooltip } from "naive-ui";
+import { NTooltip } from "naive-ui";
import scrollIntoView from "scroll-into-view-if-needed";
import { computed, onMounted, reactive, ref, watchEffect } from "vue";
+import { BBTextField } from "@/bbkit";
import LearnMoreLink from "@/components/LearnMoreLink.vue";
import { hasFeature } from "@/store";
import { useSettingV1Store } from "@/store/modules/v1/setting";
@@ -171,12 +175,13 @@ watchEffect(() => {
});
const allowSave = computed((): boolean => {
+ const initValue = getInitialState();
const openAIKeyUpdated =
- state.openAIKey !== maskKey(aiSetting.value?.apiKey) ||
+ state.openAIKey !== initValue.openAIKey ||
(state.openAIKey && !state.openAIKey.includes("***"));
const openAIEndpointUpdated =
- state.openAIEndpoint !== aiSetting.value?.endpoint;
- const openAIModelUpdated = state.openAIModel !== aiSetting.value?.model;
+ state.openAIEndpoint !== initValue.openAIEndpoint;
+ const openAIModelUpdated = state.openAIModel !== initValue.openAIModel;
return openAIKeyUpdated || openAIEndpointUpdated || openAIModelUpdated;
});
diff --git a/frontend/src/plugins/ai/components/ChatPanel.vue b/frontend/src/plugins/ai/components/ChatPanel.vue
index b1804541da..9becc56ed6 100644
--- a/frontend/src/plugins/ai/components/ChatPanel.vue
+++ b/frontend/src/plugins/ai/components/ChatPanel.vue
@@ -11,7 +11,10 @@
class="pt-2"
@enter="requestAI"
/>
- <div v-else class="flex-1 overflow-hidden relative flex items-center justify-center">
+ <div
+ v-else
+ class="flex-1 overflow-hidden relative flex items-center justify-center"
+ >
<NSpin size="small" />
</div>
@@ -37,7 +40,6 @@ import { onConnectionChanged, useAIContext, useCurrentChat } from "../logic";
import * as promptUtils from "../logic/prompt";
import { useConversationStore } from "../store";
import type { OpenAIMessage, OpenAIResponse } from "../types";
-import { OPENAI_DEFAULT_MODEL } from "@/types";
import ActionBar from "./ActionBar.vue";
import ChatView from "./ChatView";
import DynamicSuggestions from "./DynamicSuggestions.vue";
@@ -56,8 +58,13 @@ const { currentTab: tab } = storeToRefs(useSQLEditorTabStore());
const store = useConversationStore();
const context = useAIContext();
-const { openAIKey, openAIEndpoint, openAIModel, showHistoryDialog, pendingSendChat } =
- context;
+const {
+ openAIKey,
+ openAIEndpoint,
+ openAIModel,
+ showHistoryDialog,
+ pendingSendChat,
+} = context;
const {
list: conversationList,
ready,
@@ -110,15 +117,7 @@ const requestAI = async (query: string) => {
conversation_id: conversation.id,
status: "LOADING",
});
- const url =
- openAIEndpoint.value === ""
- ? "https://api.openai.com/v1/chat/completions"
- : openAIEndpoint.value + "/v1/chat/completions";
const messages: OpenAIMessage[] = [];
- const model =
- openAIModel.value === ""
- ? OPENAI_DEFAULT_MODEL
- : openAIModel.value;
conversation.messageList.forEach((message) => {
const { author, prompt } = message;
messages.push({
@@ -127,7 +126,7 @@ const requestAI = async (query: string) => {
});
});
const body = {
- model: model,
+ model: openAIModel.value,
messages,
temperature: 0,
stop: ["#", ";"],
@@ -146,7 +145,7 @@ const requestAI = async (query: string) => {
};
try {
const response: AxiosResponse<string> = await axios.post(
- url,
+ openAIEndpoint.value,
JSON.stringify(body),
{
headers,
diff --git a/frontend/src/plugins/ai/logic/useDynamicSuggestions.ts b/frontend/src/plugins/ai/logic/useDynamicSuggestions.ts
index b053ada44a..3eb0b141dd 100644
--- a/frontend/src/plugins/ai/logic/useDynamicSuggestions.ts
+++ b/frontend/src/plugins/ai/logic/useDynamicSuggestions.ts
@@ -4,7 +4,6 @@ import { computed, reactive, ref } from "vue";
import { hashCode } from "@/bbkit/BBUtil";
import { WebStorageHelper } from "@/utils";
import type { OpenAIMessage, OpenAIResponse } from "../types";
-import { OPENAI_DEFAULT_MODEL } from "@/types";
import { useAIContext } from "./context";
import * as promptUtils from "./prompt";
@@ -43,12 +42,8 @@ export const useDynamicSuggestions = () => {
const requestAI = async (messages: OpenAIMessage[]) => {
await new Promise((resolve) => setTimeout(resolve, 1000));
- const modelName =
- context.openAIModel.value === ""
- ? OPENAI_DEFAULT_MODEL
- : context.openAIModel.value
const body = {
- model: modelName,
+ model: context.openAIModel.value,
messages,
temperature: 0,
stop: ["#", ";"],
@@ -64,13 +59,9 @@ export const useDynamicSuggestions = () => {
"Content-Type": "application/json",
Authorization: `Bearer ${context.openAIKey.value}`,
};
- 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,
+ context.openAIEndpoint.value,
JSON.stringify(body),
{
headers,
diff --git a/frontend/src/types/common.ts b/frontend/src/types/common.ts
index bdd84c448d..e2b17a4576 100644
--- a/frontend/src/types/common.ts
+++ b/frontend/src/types/common.ts
@@ -14,7 +14,6 @@ export const TEXT_VALIDATION_DELAY = 1000;
export const ENTERPRISE_INQUIRE_LINK =
"https://www.bytebase.com/contact-us?source=console";
-export const OPENAI_DEFAULT_MODEL = "gpt-3.5-turbo";
// Router
export type RouterSlug = {
issueSlug?: string;