Skip to content

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

chore: support gemini & claude (#17298)

ecmadao8a1661fdc34f
diff --git a/backend/api/v1/sql_service_ai.go b/backend/api/v1/sql_service_ai.go
index b1fa5c70c6..ca61fb0f5a 100644
--- a/backend/api/v1/sql_service_ai.go
+++ b/backend/api/v1/sql_service_ai.go
@@ -39,6 +39,64 @@ type openAIResponse struct {
} `json:"choices"`
}
+// geminiRequest represents the payload for Gemini API requests.
+type geminiRequest struct {
+ Contents []geminiContent `json:"contents"`
+ GenerationConfig geminiGenerationConfig `json:"generationConfig"`
+}
+
+type geminiContent struct {
+ Parts []geminiPart `json:"parts"`
+ Role string `json:"role"`
+}
+
+type geminiPart struct {
+ Text string `json:"text"`
+}
+
+type geminiGenerationConfig struct {
+ Temperature float64 `json:"temperature"`
+ TopP float64 `json:"topP"`
+ TopK int `json:"topK"`
+ MaxOutputTokens int `json:"maxOutputTokens"`
+ StopSequences []string `json:"stopSequences,omitempty"`
+}
+
+// geminiResponse represents the response from Gemini API.
+type geminiResponse struct {
+ Candidates []struct {
+ Content struct {
+ Parts []struct {
+ Text string `json:"text"`
+ } `json:"parts"`
+ } `json:"content"`
+ } `json:"candidates"`
+}
+
+// claudeRequest represents the payload for Claude API requests.
+type claudeRequest struct {
+ Model string `json:"model"`
+ Messages []claudeMessage `json:"messages"`
+ MaxTokens int `json:"max_tokens"`
+ Temperature float64 `json:"temperature"`
+ TopP float64 `json:"top_p"`
+ TopK int `json:"top_k"`
+ StopSequences []string `json:"stop_sequences,omitempty"`
+}
+
+type claudeMessage struct {
+ Role string `json:"role"`
+ Content string `json:"content"`
+}
+
+// claudeResponse represents the response from Claude API.
+type claudeResponse struct {
+ Content []struct {
+ Type string `json:"type"`
+ Text string `json:"text"`
+ } `json:"content"`
+}
+
// AICompletion is the mixer for AI completion.
func (s *SQLService) AICompletion(ctx context.Context, req *connect.Request[v1pb.AICompletionRequest]) (*connect.Response[v1pb.AICompletionResponse], error) {
request := req.Msg
@@ -53,6 +111,10 @@ func (s *SQLService) AICompletion(ctx context.Context, req *connect.Request[v1pb
switch aiSetting.Provider {
case storepb.AISetting_OPEN_AI, storepb.AISetting_AZURE_OPENAI:
return callOpenAI(ctx, aiSetting, request)
+ case storepb.AISetting_GEMINI:
+ return callGemini(ctx, aiSetting, request)
+ case storepb.AISetting_CLAUDE:
+ return callClaude(ctx, aiSetting, request)
default:
return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("unsupported AI provider %s", aiSetting.Provider))
}
@@ -123,3 +185,171 @@ func callOpenAI(ctx context.Context, aiSetting *storepb.AISetting, request *v1pb
}
return connect.NewResponse(resp), nil
}
+
+// Gemini API docs: https://ai.google.dev/gemini-api/docs
+func callGemini(ctx context.Context, aiSetting *storepb.AISetting, request *v1pb.AICompletionRequest) (*connect.Response[v1pb.AICompletionResponse], error) {
+ // Convert messages to Gemini format
+ var contents []geminiContent
+ for _, m := range request.Messages {
+ // Gemini uses "user" and "model" as roles
+ role := m.Role
+ if role == "assistant" {
+ role = "model"
+ }
+ contents = append(contents, geminiContent{
+ Role: role,
+ Parts: []geminiPart{
+ {Text: m.Content},
+ },
+ })
+ }
+
+ payload := geminiRequest{
+ Contents: contents,
+ GenerationConfig: geminiGenerationConfig{
+ Temperature: 0.7,
+ TopP: 0.95,
+ TopK: 40,
+ MaxOutputTokens: 2048,
+ StopSequences: []string{"#", ";"},
+ },
+ }
+
+ payloadBytes, err := json.Marshal(payload)
+ if err != nil {
+ return nil, errors.Errorf("failed to marshal Gemini request payload: %s", err)
+ }
+
+ // Gemini API endpoint format: https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent?key={apiKey}
+ url := fmt.Sprintf("%s/models/%s:generateContent?key=%s", aiSetting.Endpoint, aiSetting.Model, aiSetting.ApiKey)
+
+ httpReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(payloadBytes))
+ if err != nil {
+ return nil, errors.Errorf("failed to create HTTP request: %s", err)
+ }
+
+ httpReq.Header.Set("Content-Type", "application/json")
+
+ client := &http.Client{}
+ httpResp, err := client.Do(httpReq)
+ if err != nil {
+ return nil, errors.Errorf("failed to send HTTP request: %s", err)
+ }
+ defer httpResp.Body.Close()
+
+ body, err := io.ReadAll(httpResp.Body)
+ if err != nil {
+ return nil, errors.Errorf("failed to read response body: %s", err)
+ }
+
+ if httpResp.StatusCode != http.StatusOK {
+ return nil, errors.Errorf("Gemini API returned status %d: %s", httpResp.StatusCode, string(body))
+ }
+
+ var geminiResp geminiResponse
+ if err := json.Unmarshal(body, &geminiResp); err != nil {
+ return nil, errors.Errorf("failed to unmarshal Gemini response: %s", err)
+ }
+
+ resp := &v1pb.AICompletionResponse{}
+ for _, candidate := range geminiResp.Candidates {
+ var textContent string
+ for _, part := range candidate.Content.Parts {
+ textContent += part.Text
+ }
+ resp.Candidates = append(resp.Candidates, &v1pb.AICompletionResponse_Candidate{
+ Content: &v1pb.AICompletionResponse_Candidate_Content{
+ Parts: []*v1pb.AICompletionResponse_Candidate_Content_Part{
+ {
+ Text: textContent,
+ },
+ },
+ },
+ })
+ }
+ return connect.NewResponse(resp), nil
+}
+
+// Claude API docs: https://docs.anthropic.com/en/api/getting-started
+func callClaude(ctx context.Context, aiSetting *storepb.AISetting, request *v1pb.AICompletionRequest) (*connect.Response[v1pb.AICompletionResponse], error) {
+ // Convert messages to Claude format
+ var messages []claudeMessage
+ for _, m := range request.Messages {
+ messages = append(messages, claudeMessage{
+ Role: m.Role,
+ Content: m.Content,
+ })
+ }
+
+ payload := claudeRequest{
+ Model: aiSetting.Model,
+ Messages: messages,
+ MaxTokens: 4096,
+ Temperature: 0.7,
+ TopP: 0.95,
+ TopK: 0,
+ StopSequences: []string{"#", ";"},
+ }
+
+ payloadBytes, err := json.Marshal(payload)
+ if err != nil {
+ return nil, errors.Errorf("failed to marshal Claude request payload: %s", err)
+ }
+
+ httpReq, err := http.NewRequestWithContext(ctx, "POST", aiSetting.Endpoint, bytes.NewBuffer(payloadBytes))
+ if err != nil {
+ return nil, errors.Errorf("failed to create HTTP request: %s", err)
+ }
+
+ httpReq.Header.Set("Content-Type", "application/json")
+ httpReq.Header.Set("x-api-key", aiSetting.ApiKey)
+ // Claude API requires anthropic-version header
+ if aiSetting.Version != "" {
+ httpReq.Header.Set("anthropic-version", aiSetting.Version)
+ } else {
+ httpReq.Header.Set("anthropic-version", "2023-06-01")
+ }
+
+ client := &http.Client{}
+ httpResp, err := client.Do(httpReq)
+ if err != nil {
+ return nil, errors.Errorf("failed to send HTTP request: %s", err)
+ }
+ defer httpResp.Body.Close()
+
+ body, err := io.ReadAll(httpResp.Body)
+ if err != nil {
+ return nil, errors.Errorf("failed to read response body: %s", err)
+ }
+
+ if httpResp.StatusCode != http.StatusOK {
+ return nil, errors.Errorf("Claude API returned status %d: %s", httpResp.StatusCode, string(body))
+ }
+
+ var claudeResp claudeResponse
+ if err := json.Unmarshal(body, &claudeResp); err != nil {
+ return nil, errors.Errorf("failed to unmarshal Claude response: %s", err)
+ }
+
+ resp := &v1pb.AICompletionResponse{}
+ var textContent string
+ for _, content := range claudeResp.Content {
+ if content.Type == "text" {
+ textContent += content.Text
+ }
+ }
+
+ if textContent != "" {
+ resp.Candidates = append(resp.Candidates, &v1pb.AICompletionResponse_Candidate{
+ Content: &v1pb.AICompletionResponse_Candidate_Content{
+ Parts: []*v1pb.AICompletionResponse_Candidate_Content_Part{
+ {
+ Text: textContent,
+ },
+ },
+ },
+ })
+ }
+
+ return connect.NewResponse(resp), nil
+}
diff --git a/frontend/src/components/GeneralSetting/AIAugmentationSetting.vue b/frontend/src/components/GeneralSetting/AIAugmentationSetting.vue
index f6f3dd734d..a4ae3b70e1 100644
--- a/frontend/src/components/GeneralSetting/AIAugmentationSetting.vue
+++ b/frontend/src/components/GeneralSetting/AIAugmentationSetting.vue
@@ -212,25 +212,34 @@ const getInitialState = (): LocalState => {
};
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,
- };
+ [
+ AISetting_Provider.OPEN_AI,
+ AISetting_Provider.AZURE_OPENAI,
+ AISetting_Provider.GEMINI,
+ AISetting_Provider.CLAUDE,
+ ].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;
+ case AISetting_Provider.GEMINI:
+ label = t("settings.general.workspace.ai-assistant.provider.gemini");
+ break;
+ case AISetting_Provider.CLAUDE:
+ label = t("settings.general.workspace.ai-assistant.provider.claude");
+ break;
}
- )
+ return {
+ label,
+ value: provider,
+ };
+ })
);
watchEffect(() => {
@@ -268,6 +277,20 @@ const providerDefault = computed(() => {
"https://{resource name}.openai.azure.com/openai/deployments/{deployment id}/chat/completions?api-version=2024-06-0",
model: "gpt-4o",
};
+ case AISetting_Provider.GEMINI:
+ return {
+ apiKey: "",
+ apiKeyDoc: "https://ai.google.dev/gemini-api/docs",
+ endpoint: "https://generativelanguage.googleapis.com/v1beta",
+ model: "gemini-2.0-flash",
+ };
+ case AISetting_Provider.CLAUDE:
+ return {
+ apiKey: "",
+ apiKeyDoc: "https://docs.anthropic.com/en/api/getting-started",
+ endpoint: "https://api.anthropic.com/v1/messages",
+ model: "claude-3-opus-20240229",
+ };
default:
return {
apiKey: "",
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index b50266923c..967eb6f6e2 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -660,7 +660,9 @@
"provider": {
"self": "AI Provider",
"open_ai": "Open AI",
- "azure_open_ai": "Azure Open AI"
+ "azure_open_ai": "Azure Open AI",
+ "gemini": "Gemini",
+ "claude": "Claude"
},
"api-key": {
"self": "API Key",
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index 3395416b54..19fcb3467f 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -660,7 +660,9 @@
"provider": {
"self": "Proveedor de IA",
"open_ai": "Open AI",
- "azure_open_ai": "Azure Open AI"
+ "azure_open_ai": "Azure Open AI",
+ "gemini": "Gemini",
+ "claude": "Claude"
},
"api-key": {
"self": "Clave de API",
@@ -2835,8 +2837,8 @@
},
"file-type": {
"versioned": "Versión",
- "unspecified": "No especificado",
- "declarative": "Declarativo"
+ "declarative": "Declarativo",
+ "unspecified": "No especificado"
},
"vcs-type": {
"unspecified": "No especificado"
diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.json
index 735bfe72b3..9efa1330cd 100644
--- a/frontend/src/locales/ja-JP.json
+++ b/frontend/src/locales/ja-JP.json
@@ -660,7 +660,9 @@
"provider": {
"self": "AIプロバイダー",
"open_ai": "Open AI",
- "azure_open_ai": "Azure Open AI"
+ "azure_open_ai": "Azure Open AI",
+ "gemini": "Gemini",
+ "claude": "Claude"
},
"api-key": {
"self": "APIキー",
@@ -2835,8 +2837,8 @@
},
"file-type": {
"versioned": "バージョン",
- "unspecified": "未指定",
- "declarative": "宣言"
+ "declarative": "宣言",
+ "unspecified": "未指定"
},
"vcs-type": {
"unspecified": "未指定"
diff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.json
index 68bbbb8d2a..98a7187685 100644
--- a/frontend/src/locales/vi-VN.json
+++ b/frontend/src/locales/vi-VN.json
@@ -660,7 +660,9 @@
"provider": {
"self": "Nhà cung cấp AI",
"open_ai": "Open AI",
- "azure_open_ai": "Azure Open AI"
+ "azure_open_ai": "Azure Open AI",
+ "gemini": "Gemini",
+ "claude": "Claude"
},
"api-key": {
"self": "Khóa API",
@@ -2835,8 +2837,8 @@
},
"file-type": {
"versioned": "Phiên bản",
- "unspecified": "Không xác định",
- "declarative": "Khai báo"
+ "declarative": "Khai báo",
+ "unspecified": "Không xác định"
},
"vcs-type": {
"unspecified": "Không xác định"
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index f3b878c4e7..f004ac862a 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -660,7 +660,9 @@
"provider": {
"self": "AI 提供方",
"open_ai": "Open AI",
- "azure_open_ai": "Azure Open AI"
+ "azure_open_ai": "Azure Open AI",
+ "gemini": "Gemini",
+ "claude": "Claude"
},
"api-key": {
"self": "API Key",
@@ -2835,8 +2837,8 @@
},
"file-type": {
"versioned": "版本化",
- "unspecified": "未指定",
- "declarative": "声明式"
+ "declarative": "声明式",
+ "unspecified": "未指定"
},
"vcs-type": {
"unspecified": "未指定"