From ce63821bebbd87b2e058f5cf58a2cdd474b8fb58 Mon Sep 17 00:00:00 2001 From: Jin Song Wang <64242752+jinsongo@users.noreply.github.com> Date: Mon, 1 Jul 2024 02:20:44 -0700 Subject: [PATCH 1/2] feat: add watsonx ai provider (#1163) Signed-off-by: JINSONG WANG --- README.md | 1 + go.mod | 1 + go.sum | 2 ++ pkg/ai/iai.go | 4 ++- pkg/ai/watsonxai.go | 84 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 pkg/ai/watsonxai.go diff --git a/README.md b/README.md index aa48c53fe7..63caf26ca9 100644 --- a/README.md +++ b/README.md @@ -322,6 +322,7 @@ Unused: > huggingface > noopai > googlevertexai +> watsonxai ``` For detailed documentation on how to configure and use each provider see [here](https://docs.k8sgpt.ai/reference/providers/backend/). diff --git a/go.mod b/go.mod index 533075d733..f20d94b064 100644 --- a/go.mod +++ b/go.mod @@ -62,6 +62,7 @@ require ( github.com/Azure/azure-sdk-for-go/sdk/azcore v1.11.1 // indirect github.com/Azure/azure-sdk-for-go/sdk/internal v1.8.0 // indirect github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 // indirect + github.com/IBM/watsonx-go v1.0.0 // indirect github.com/Microsoft/hcsshim v0.12.4 // indirect github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 // indirect github.com/anchore/go-struct-converter v0.0.0-20230627203149-c72ef8859ca9 // indirect diff --git a/go.sum b/go.sum index 9c702a3541..9e864c6aac 100644 --- a/go.sum +++ b/go.sum @@ -1245,6 +1245,8 @@ github.com/Code-Hex/go-generics-cache v1.3.1 h1:i8rLwyhoyhaerr7JpjtYjJZUcCbWOdiY github.com/Code-Hex/go-generics-cache v1.3.1/go.mod h1:qxcC9kRVrct9rHeiYpFWSoW1vxyillCVzX13KZG8dl4= github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU= github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU= +github.com/IBM/watsonx-go v1.0.0 h1:xG7xA2W9N0RsiztR26dwBI8/VxIX4wTBhdYmEis2Yl8= +github.com/IBM/watsonx-go v1.0.0/go.mod h1:8lzvpe/158JkrzvcoIcIj6OdNty5iC9co5nQHfkhRtM= github.com/JohnCGriffin/overflow v0.0.0-20211019200055-46fa312c352c/go.mod h1:X0CRv0ky0k6m906ixxpzmDRLvX58TFUKS2eePweuyxk= github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ= github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE= diff --git a/pkg/ai/iai.go b/pkg/ai/iai.go index 08caa02079..3e2ee96bde 100644 --- a/pkg/ai/iai.go +++ b/pkg/ai/iai.go @@ -30,6 +30,7 @@ var ( &HuggingfaceClient{}, &GoogleVertexAIClient{}, &OCIGenAIClient{}, + &WatsonxAIClient{}, } Backends = []string{ openAIClientName, @@ -43,6 +44,7 @@ var ( huggingfaceAIClientName, googleVertexAIClientName, ociClientName, + watsonxAIClientName, } ) @@ -170,7 +172,7 @@ func (p *AIProvider) GetOrganizationId() string { return p.OrganizationId } -var passwordlessProviders = []string{"localai", "amazonsagemaker", "amazonbedrock", "googlevertexai", "oci"} +var passwordlessProviders = []string{"localai", "amazonsagemaker", "amazonbedrock", "googlevertexai", "oci", "watsonxai"} func NeedPassword(backend string) bool { for _, b := range passwordlessProviders { diff --git a/pkg/ai/watsonxai.go b/pkg/ai/watsonxai.go new file mode 100644 index 0000000000..f6ce81c1a6 --- /dev/null +++ b/pkg/ai/watsonxai.go @@ -0,0 +1,84 @@ +package ai + +import ( + "os" + "fmt" + "context" + "errors" + + wx "github.com/IBM/watsonx-go/pkg/models" +) + +const watsonxAIClientName = "watsonxai" + +type WatsonxAIClient struct { + nopCloser + + client *wx.Client + model string + temperature float32 + topP float32 + topK int32 + maxNewTokens int +} + +const ( + modelMetallama = "ibm/granite-13b-chat-v2" +) + +func (c *WatsonxAIClient) Configure(config IAIConfig) error { + if(config.GetModel() == "") { + c.model = config.GetModel() + } else { + c.model = modelMetallama + } + c.temperature = config.GetTemperature() + c.topP = config.GetTopP() + c.topK = config.GetTopK() + c.maxNewTokens = config.GetMaxTokens() + + // WatsonxAPIKeyEnvVarName = "WATSONX_API_KEY" + // WatsonxProjectIDEnvVarName = "WATSONX_PROJECT_ID" + apiKey, projectID := os.Getenv(wx.WatsonxAPIKeyEnvVarName), os.Getenv(wx.WatsonxProjectIDEnvVarName) + + if apiKey == "" { + return errors.New("No watsonx API key provided") + } + if projectID == "" { + return errors.New("No watsonx project ID provided") + } + + client, err := wx.NewClient( + wx.WithWatsonxAPIKey(apiKey), + wx.WithWatsonxProjectID(projectID), + ) + if err != nil { + return fmt.Errorf("Failed to create client for testing. Error: %v", err) + } + c.client = client + + return nil +} + +func (c *WatsonxAIClient) GetCompletion(ctx context.Context, prompt string) (string, error) { + result, err := c.client.GenerateText( + c.model, + prompt, + wx.WithTemperature((float64)(c.temperature)), + wx.WithTopP((float64)(c.topP)), + wx.WithTopK((uint)(c.topK)), + wx.WithMaxNewTokens((uint)(c.maxNewTokens)), + ) + if err != nil { + return "", fmt.Errorf("Expected no error, but got an error: %v", err) + } + if result.Text == "" { + return "", errors.New("Expected a result, but got an empty string") + } + + return result.Text, nil +} + +func (c *WatsonxAIClient) GetName() string { + return watsonxAIClientName +} From a0757921191205398539a6ccc8dbfaa503db595f Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Mon, 1 Jul 2024 10:31:27 +0100 Subject: [PATCH 2/2] fix(deps): update module github.com/spf13/cobra to v1.8.1 (#1161) Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> --- go.mod | 4 ++-- go.sum | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index f20d94b064..1e23ce24dd 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/mittwald/go-helm-client v0.12.9 github.com/sashabaranov/go-openai v1.23.0 github.com/schollz/progressbar/v3 v3.14.2 - github.com/spf13/cobra v1.8.0 + github.com/spf13/cobra v1.8.1 github.com/spf13/viper v1.18.2 github.com/stretchr/testify v1.9.0 golang.org/x/term v0.21.0 @@ -34,6 +34,7 @@ require ( cloud.google.com/go/vertexai v0.7.1 github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.6.0 github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.3.2 + github.com/IBM/watsonx-go v1.0.0 github.com/aws/aws-sdk-go v1.53.21 github.com/cohere-ai/cohere-go/v2 v2.7.3 github.com/google/generative-ai-go v0.11.0 @@ -62,7 +63,6 @@ require ( github.com/Azure/azure-sdk-for-go/sdk/azcore v1.11.1 // indirect github.com/Azure/azure-sdk-for-go/sdk/internal v1.8.0 // indirect github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 // indirect - github.com/IBM/watsonx-go v1.0.0 // indirect github.com/Microsoft/hcsshim v0.12.4 // indirect github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 // indirect github.com/anchore/go-struct-converter v0.0.0-20230627203149-c72ef8859ca9 // indirect diff --git a/go.sum b/go.sum index 9e864c6aac..5b04427000 100644 --- a/go.sum +++ b/go.sum @@ -1397,7 +1397,7 @@ github.com/containerd/errdefs v0.1.0 h1:m0wCRBiu1WJT/Fr+iOoQHMQS/eP5myQ8lCv4Dz5Z github.com/containerd/errdefs v0.1.0/go.mod h1:YgWiiHtLmSeBrvpw+UfPijzbLaB77mEG1WwJTDETIV0= github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I= github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo= -github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY= github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= @@ -2061,8 +2061,8 @@ github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNo github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= -github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= -github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= +github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= +github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ=