From 563b868860162984fff1e15a1c1ea9edbe838ca1 Mon Sep 17 00:00:00 2001 From: jueli12 Date: Tue, 3 Sep 2024 11:53:43 +0800 Subject: [PATCH 01/10] blank --- pkg/infra/ai/azureopenai.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/infra/ai/azureopenai.go b/pkg/infra/ai/azureopenai.go index d48aae38..0e074e7b 100644 --- a/pkg/infra/ai/azureopenai.go +++ b/pkg/infra/ai/azureopenai.go @@ -48,7 +48,6 @@ func (c *AzureAIClient) Configure(cfg AIConfig) error { } func (c *AzureAIClient) Generate(ctx context.Context, prompt string) (string, error) { - resp, err := c.client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{ Model: c.model, Messages: []openai.ChatCompletionMessage{ From c3fe88821c1f6e44e80b38700d1d4250064b45e7 Mon Sep 17 00:00:00 2001 From: jueli12 Date: Sun, 25 Aug 2024 17:19:12 +0800 Subject: [PATCH 02/10] feat: add nl interface --- pkg/core/handler/search/search.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/core/handler/search/search.go b/pkg/core/handler/search/search.go index df91bbdb..e2da6b22 100644 --- a/pkg/core/handler/search/search.go +++ b/pkg/core/handler/search/search.go @@ -52,8 +52,6 @@ func SearchForResource(searchMgr *search.SearchManager, aiMgr *ai.AIManager, sea ctx := r.Context() logger := ctxutil.GetLogger(ctx) - //res, nil := aiMgr.ConvertTextToSQL("搜索集群cluster中kind为namespace的") - // Extract URL query parameters with default value searchQuery := r.URL.Query().Get("query") searchPattern := r.URL.Query().Get("pattern") From 1906899c64823bce14e4e3c98a07dea96be099fa Mon Sep 17 00:00:00 2001 From: jueli12 Date: Thu, 29 Aug 2024 18:23:37 +0800 Subject: [PATCH 03/10] fix: format the sql --- pkg/core/handler/search/search.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/core/handler/search/search.go b/pkg/core/handler/search/search.go index e2da6b22..e4eb53c2 100644 --- a/pkg/core/handler/search/search.go +++ b/pkg/core/handler/search/search.go @@ -82,7 +82,11 @@ func SearchForResource(searchMgr *search.SearchManager, aiMgr *ai.AIManager, sea res, err := searchStorage.Search(ctx, searchQuery, searchPattern, &storage.Pagination{Page: searchPage, PageSize: searchPageSize}) if err != nil { if searchPattern == storage.NLPatternType { +<<<<<<< HEAD //logger.Info(err.Error()) +======= + logger.Info("-------------" + err.Error() + "-------------") +>>>>>>> 1eac1aa (fix: format the sql) fixedQuery, fixErr := aiMgr.FixSQL(query, searchQuery, err.Error()) if fixErr != nil { handler.FailureRender(ctx, w, r, err) From 3969536161dfe8726174ce0464e4584a295af681 Mon Sep 17 00:00:00 2001 From: jueli12 Date: Thu, 29 Aug 2024 18:34:24 +0800 Subject: [PATCH 04/10] fix: format --- pkg/core/handler/search/search.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pkg/core/handler/search/search.go b/pkg/core/handler/search/search.go index e4eb53c2..e2da6b22 100644 --- a/pkg/core/handler/search/search.go +++ b/pkg/core/handler/search/search.go @@ -82,11 +82,7 @@ func SearchForResource(searchMgr *search.SearchManager, aiMgr *ai.AIManager, sea res, err := searchStorage.Search(ctx, searchQuery, searchPattern, &storage.Pagination{Page: searchPage, PageSize: searchPageSize}) if err != nil { if searchPattern == storage.NLPatternType { -<<<<<<< HEAD //logger.Info(err.Error()) -======= - logger.Info("-------------" + err.Error() + "-------------") ->>>>>>> 1eac1aa (fix: format the sql) fixedQuery, fixErr := aiMgr.FixSQL(query, searchQuery, err.Error()) if fixErr != nil { handler.FailureRender(ctx, w, r, err) From 957b3e3c806fb14ed26a258350dcb964b98f89f9 Mon Sep 17 00:00:00 2001 From: jueli12 Date: Wed, 18 Sep 2024 19:41:03 +0800 Subject: [PATCH 05/10] rename the AI configs --- cmd/karpor/app/options/ai.go | 36 ++++++++++++++++---------------- pkg/core/manager/ai/manager.go | 2 +- pkg/infra/ai/types.go | 12 +++++------ pkg/kubernetes/registry/types.go | 12 +++++------ 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/cmd/karpor/app/options/ai.go b/cmd/karpor/app/options/ai.go index f47d5592..2603e5b2 100644 --- a/cmd/karpor/app/options/ai.go +++ b/cmd/karpor/app/options/ai.go @@ -20,12 +20,12 @@ import ( ) type AIOptions struct { - Backend string - AuthToken string - BaseURL string - Model string - Temperature float32 - TopP float32 + AIBackend string + AIAuthToken string + AIBaseURL string + AIModel string + AITemperature float32 + AITopP float32 } const ( @@ -45,12 +45,12 @@ func (o *AIOptions) Validate() []error { func (o *AIOptions) ApplyTo(config *registry.ExtraConfig) error { // Apply the AIOptions to the provided config - config.Backend = o.Backend - config.AuthToken = o.AuthToken - config.BaseURL = o.BaseURL - config.Model = o.Model - config.Temperature = o.Temperature - config.TopP = o.TopP + config.AIBackend = o.AIBackend + config.AIAuthToken = o.AIAuthToken + config.AIBaseURL = o.AIBaseURL + config.AIModel = o.AIModel + config.AITemperature = o.AITemperature + config.AITopP = o.AITopP return nil } @@ -60,10 +60,10 @@ func (o *AIOptions) AddFlags(fs *pflag.FlagSet) { return } - fs.StringVar(&o.Backend, "ai-backend", defaultBackend, "The ai backend") - fs.StringVar(&o.AuthToken, "ai-auth-token", "", "The ai auth token") - fs.StringVar(&o.BaseURL, "ai-base-url", "", "The ai base url") - fs.StringVar(&o.Model, "ai-model", defaultModel, "The ai model") - fs.Float32Var(&o.Temperature, "ai-temperature", defaultTemperature, "The ai temperature") - fs.Float32Var(&o.TopP, "ai-top-p", defaultTopP, "The ai top-p") + fs.StringVar(&o.AIBackend, "ai-backend", defaultBackend, "The ai backend") + fs.StringVar(&o.AIAuthToken, "ai-auth-token", "", "The ai auth token") + fs.StringVar(&o.AIBaseURL, "ai-base-url", "", "The ai base url") + fs.StringVar(&o.AIModel, "ai-model", defaultModel, "The ai model") + fs.Float32Var(&o.AITemperature, "ai-temperature", defaultTemperature, "The ai temperature") + fs.Float32Var(&o.AITopP, "ai-top-p", defaultTopP, "The ai top-p") } diff --git a/pkg/core/manager/ai/manager.go b/pkg/core/manager/ai/manager.go index 699a4bc8..0665c510 100644 --- a/pkg/core/manager/ai/manager.go +++ b/pkg/core/manager/ai/manager.go @@ -25,7 +25,7 @@ type AIManager struct { // NewAIManager returns a new AIManager object func NewAIManager(c registry.ExtraConfig) (*AIManager, error) { - aiClient := ai.NewClient(c.Backend) + aiClient := ai.NewClient(c.AIBackend) if err := aiClient.Configure(ai.ConvertToAIConfig(c)); err != nil { return nil, err } diff --git a/pkg/infra/ai/types.go b/pkg/infra/ai/types.go index 4437506e..e18ad67c 100644 --- a/pkg/infra/ai/types.go +++ b/pkg/infra/ai/types.go @@ -57,12 +57,12 @@ type AIConfig struct { func ConvertToAIConfig(c registry.ExtraConfig) AIConfig { return AIConfig{ - Name: c.Backend, - AuthToken: c.AuthToken, - BaseURL: c.BaseURL, - Model: c.Model, - Temperature: c.Temperature, - TopP: c.TopP, + Name: c.AIBackend, + AuthToken: c.AIAuthToken, + BaseURL: c.AIBaseURL, + Model: c.AIModel, + Temperature: c.AITemperature, + TopP: c.AITopP, } } diff --git a/pkg/kubernetes/registry/types.go b/pkg/kubernetes/registry/types.go index 51f4fa91..36549eeb 100644 --- a/pkg/kubernetes/registry/types.go +++ b/pkg/kubernetes/registry/types.go @@ -47,10 +47,10 @@ type ExtraConfig struct { ExtendExpiration bool // AI configs - Backend string - AuthToken string - BaseURL string - Model string - Temperature float32 - TopP float32 + AIBackend string + AIAuthToken string + AIBaseURL string + AIModel string + AITemperature float32 + AITopP float32 } From 9f624c8cb46a6cc76970fdf7930dfa4ce948c38b Mon Sep 17 00:00:00 2001 From: jueli12 Date: Wed, 18 Sep 2024 21:58:55 +0800 Subject: [PATCH 06/10] feat: change the AI command line argument to optional --- pkg/core/handler/search/search.go | 7 ++++++- pkg/core/manager/ai/manager.go | 3 +++ pkg/infra/ai/azureopenai.go | 3 --- pkg/infra/ai/openai.go | 3 --- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/pkg/core/handler/search/search.go b/pkg/core/handler/search/search.go index e2da6b22..5d7cc786 100644 --- a/pkg/core/handler/search/search.go +++ b/pkg/core/handler/search/search.go @@ -15,6 +15,7 @@ package search import ( + "errors" "github.com/KusionStack/karpor/pkg/core/manager/ai" "net/http" "strconv" @@ -67,7 +68,11 @@ func SearchForResource(searchMgr *search.SearchManager, aiMgr *ai.AIManager, sea query := searchQuery if searchPattern == storage.NLPatternType { - //logger.Info(searchQuery) + // logger.Info(searchQuery) + if aiMgr == nil { + handler.FailureRender(ctx, w, r, errors.New("auth token was not provided")) + return + } res, err := aiMgr.ConvertTextToSQL(searchQuery) if err != nil { handler.FailureRender(ctx, w, r, err) diff --git a/pkg/core/manager/ai/manager.go b/pkg/core/manager/ai/manager.go index 0665c510..c5e043e6 100644 --- a/pkg/core/manager/ai/manager.go +++ b/pkg/core/manager/ai/manager.go @@ -25,6 +25,9 @@ type AIManager struct { // NewAIManager returns a new AIManager object func NewAIManager(c registry.ExtraConfig) (*AIManager, error) { + if c.AIAuthToken == "" { + return nil, nil + } aiClient := ai.NewClient(c.AIBackend) if err := aiClient.Configure(ai.ConvertToAIConfig(c)); err != nil { return nil, err diff --git a/pkg/infra/ai/azureopenai.go b/pkg/infra/ai/azureopenai.go index 0e074e7b..235b7205 100644 --- a/pkg/infra/ai/azureopenai.go +++ b/pkg/infra/ai/azureopenai.go @@ -27,9 +27,6 @@ type AzureAIClient struct { } func (c *AzureAIClient) Configure(cfg AIConfig) error { - if cfg.AuthToken == "" { - return errors.New("auth token was not provided") - } if cfg.BaseURL == "" { return errors.New("base url was not provided") } diff --git a/pkg/infra/ai/openai.go b/pkg/infra/ai/openai.go index 8de36a2d..9ec3f98e 100644 --- a/pkg/infra/ai/openai.go +++ b/pkg/infra/ai/openai.go @@ -28,9 +28,6 @@ type OpenAIClient struct { } func (c *OpenAIClient) Configure(cfg AIConfig) error { - if cfg.AuthToken == "" { - return errors.New("auth token was not provided") - } defaultConfig := openai.DefaultConfig(cfg.AuthToken) if cfg.BaseURL != "" { defaultConfig.BaseURL = cfg.BaseURL From 6eab885df12d0d419f3821888ff8b32300c79589 Mon Sep 17 00:00:00 2001 From: jueli12 Date: Wed, 18 Sep 2024 22:19:39 +0800 Subject: [PATCH 07/10] validate ai manager --- pkg/core/handler/search/search.go | 6 +++--- pkg/core/manager/ai/manager.go | 7 +++++++ pkg/core/manager/ai/types.go | 21 +++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 pkg/core/manager/ai/types.go diff --git a/pkg/core/handler/search/search.go b/pkg/core/handler/search/search.go index 5d7cc786..e97937c2 100644 --- a/pkg/core/handler/search/search.go +++ b/pkg/core/handler/search/search.go @@ -15,7 +15,6 @@ package search import ( - "errors" "github.com/KusionStack/karpor/pkg/core/manager/ai" "net/http" "strconv" @@ -69,10 +68,11 @@ func SearchForResource(searchMgr *search.SearchManager, aiMgr *ai.AIManager, sea if searchPattern == storage.NLPatternType { // logger.Info(searchQuery) - if aiMgr == nil { - handler.FailureRender(ctx, w, r, errors.New("auth token was not provided")) + if err := ai.ValidateAIManager(aiMgr); err != nil { + handler.FailureRender(ctx, w, r, err) return } + res, err := aiMgr.ConvertTextToSQL(searchQuery) if err != nil { handler.FailureRender(ctx, w, r, err) diff --git a/pkg/core/manager/ai/manager.go b/pkg/core/manager/ai/manager.go index c5e043e6..36eb44f8 100644 --- a/pkg/core/manager/ai/manager.go +++ b/pkg/core/manager/ai/manager.go @@ -37,3 +37,10 @@ func NewAIManager(c registry.ExtraConfig) (*AIManager, error) { client: aiClient, }, nil } + +func ValidateAIManager(aiMgr *AIManager) error { + if aiMgr == nil { + return ErrMissingAuthToken + } + return nil +} diff --git a/pkg/core/manager/ai/types.go b/pkg/core/manager/ai/types.go new file mode 100644 index 00000000..39e8b501 --- /dev/null +++ b/pkg/core/manager/ai/types.go @@ -0,0 +1,21 @@ +// Copyright The Karpor Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ai + +import "errors" + +var ( + ErrMissingAuthToken = errors.New("auth token is required") +) From 624a9b60cebcca8e405d3ce12a2ab2c328e35b81 Mon Sep 17 00:00:00 2001 From: jueli12 Date: Wed, 18 Sep 2024 22:22:24 +0800 Subject: [PATCH 08/10] check ai manager --- pkg/core/handler/search/search.go | 2 +- pkg/core/manager/ai/manager.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/core/handler/search/search.go b/pkg/core/handler/search/search.go index e97937c2..7938a202 100644 --- a/pkg/core/handler/search/search.go +++ b/pkg/core/handler/search/search.go @@ -68,7 +68,7 @@ func SearchForResource(searchMgr *search.SearchManager, aiMgr *ai.AIManager, sea if searchPattern == storage.NLPatternType { // logger.Info(searchQuery) - if err := ai.ValidateAIManager(aiMgr); err != nil { + if err := ai.CheckAIManager(aiMgr); err != nil { handler.FailureRender(ctx, w, r, err) return } diff --git a/pkg/core/manager/ai/manager.go b/pkg/core/manager/ai/manager.go index 36eb44f8..d07355e4 100644 --- a/pkg/core/manager/ai/manager.go +++ b/pkg/core/manager/ai/manager.go @@ -38,7 +38,8 @@ func NewAIManager(c registry.ExtraConfig) (*AIManager, error) { }, nil } -func ValidateAIManager(aiMgr *AIManager) error { +// CheckAIManager check if the AI manager is created +func CheckAIManager(aiMgr *AIManager) error { if aiMgr == nil { return ErrMissingAuthToken } From 2609f10aa5fd66deae62a0ff328d1a4eb0b0d3f6 Mon Sep 17 00:00:00 2001 From: jueli12 Date: Tue, 24 Sep 2024 11:27:48 +0800 Subject: [PATCH 09/10] fix: remove useless comments --- pkg/core/handler/search/search.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/pkg/core/handler/search/search.go b/pkg/core/handler/search/search.go index 7938a202..db94a495 100644 --- a/pkg/core/handler/search/search.go +++ b/pkg/core/handler/search/search.go @@ -67,7 +67,6 @@ func SearchForResource(searchMgr *search.SearchManager, aiMgr *ai.AIManager, sea query := searchQuery if searchPattern == storage.NLPatternType { - // logger.Info(searchQuery) if err := ai.CheckAIManager(aiMgr); err != nil { handler.FailureRender(ctx, w, r, err) return @@ -81,13 +80,11 @@ func SearchForResource(searchMgr *search.SearchManager, aiMgr *ai.AIManager, sea searchQuery = res } - //logger.Info(searchQuery) logger.Info("Searching for resources...", "page", searchPage, "pageSize", searchPageSize) res, err := searchStorage.Search(ctx, searchQuery, searchPattern, &storage.Pagination{Page: searchPage, PageSize: searchPageSize}) if err != nil { if searchPattern == storage.NLPatternType { - //logger.Info(err.Error()) fixedQuery, fixErr := aiMgr.FixSQL(query, searchQuery, err.Error()) if fixErr != nil { handler.FailureRender(ctx, w, r, err) From 295bc9efa514b106d2c56b7ae18b6817ae1f31fc Mon Sep 17 00:00:00 2001 From: jueli12 Date: Tue, 24 Sep 2024 14:13:28 +0800 Subject: [PATCH 10/10] fix: warning --- pkg/core/manager/ai/manager.go | 2 +- pkg/core/route/route.go | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/pkg/core/manager/ai/manager.go b/pkg/core/manager/ai/manager.go index d07355e4..2333abfa 100644 --- a/pkg/core/manager/ai/manager.go +++ b/pkg/core/manager/ai/manager.go @@ -26,7 +26,7 @@ type AIManager struct { // NewAIManager returns a new AIManager object func NewAIManager(c registry.ExtraConfig) (*AIManager, error) { if c.AIAuthToken == "" { - return nil, nil + return nil, ErrMissingAuthToken } aiClient := ai.NewClient(c.AIBackend) if err := aiClient.Configure(ai.ConvertToAIConfig(c)); err != nil { diff --git a/pkg/core/route/route.go b/pkg/core/route/route.go index 50401d67..fffc06ef 100644 --- a/pkg/core/route/route.go +++ b/pkg/core/route/route.go @@ -15,8 +15,8 @@ package route import ( + "errors" "expvar" - docs "github.com/KusionStack/karpor/api/openapispec" authnhandler "github.com/KusionStack/karpor/pkg/core/handler/authn" clusterhandler "github.com/KusionStack/karpor/pkg/core/handler/cluster" @@ -44,6 +44,7 @@ import ( "github.com/go-chi/chi/v5/middleware" httpswagger "github.com/swaggo/http-swagger/v2" genericapiserver "k8s.io/apiserver/pkg/server" + "k8s.io/klog/v2" ) // NewCoreRoute creates and configures an instance of chi.Mux with the given @@ -92,7 +93,11 @@ func NewCoreRoute( } aiMgr, err := aimanager.NewAIManager(*extraConfig) if err != nil { - return nil, err + if errors.Is(err, aimanager.ErrMissingAuthToken) { + klog.Warning("Auth token is empty.") + } else { + return nil, err + } } clusterMgr := clustermanager.NewClusterManager()