Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support alibaba cloud dashscope ai #1233

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ Unused:
> noopai
> googlevertexai
> watsonxai
> alicloud
```

For detailed documentation on how to configure and use each provider see [here](https://docs.k8sgpt.ai/reference/providers/backend/).
Expand Down
3 changes: 3 additions & 0 deletions cmd/auth/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ var addCmd = &cobra.Command{
if strings.ToLower(backend) == "amazonbedrock" {
_ = cmd.MarkFlagRequired("providerRegion")
}
if strings.ToLower(backend) == "alicloud" {
_ = cmd.MarkFlagRequired("model")
}
if strings.ToLower(backend) == "watsonxai" {
_ = cmd.MarkFlagRequired("providerId")
}
Expand Down
80 changes: 80 additions & 0 deletions pkg/ai/aliclouddashscope.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Copyright 2023 The K8sGPT 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 (
"context"
"errors"

//Due to the temporary absence of a Golang client, the OpenAI client is being used for compatibility.
"github.com/sashabaranov/go-openai"
)

const alicloudAIClientName = "alicloud"

type DashScopeClient struct {
nopCloser

client *openai.Client
model string
temprature float32
topP float32
topK int32

Check failure on line 33 in pkg/ai/aliclouddashscope.go

View workflow job for this annotation

GitHub Actions / golangci-lint

[golangci] reported by reviewdog 🐢 field `topK` is unused (unused) Raw Output: pkg/ai/aliclouddashscope.go:33:2: field `topK` is unused (unused) topK int32 ^
}

func (c *DashScopeClient) Configure(config IAIConfig) error {
const defaultBaseURL = "https://dashscope.aliyuncs.com/compatible-mode/v1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest using a custom base url for the openAI provider rather than writing a new one


apiKey := config.GetPassword()
defaultConfig := openai.DefaultConfig(apiKey)
defaultConfig.BaseURL = defaultBaseURL

baseURL := config.GetBaseURL()
if baseURL != "" {
defaultConfig.BaseURL = baseURL
}

client := openai.NewClientWithConfig(defaultConfig)
if client == nil {
return errors.New("error create DashScope client")
}
c.client = client
c.temprature = config.GetTemperature()
c.topP = config.GetTopP()
c.model = config.GetModel()

return nil
}

func (c *DashScopeClient) GetCompletion(ctx context.Context, prompt string) (string, error) {
resp, err := c.client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{
Model: c.model,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: prompt,
},
},
Temperature: c.temprature,
TopP: c.topP,
})
if err != nil {
return "", err
}
return resp.Choices[0].Message.Content, nil
}

func (c *DashScopeClient) GetName() string {
return alicloudAIClientName
}
4 changes: 3 additions & 1 deletion pkg/ai/iai.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (
&GoogleVertexAIClient{},
&OCIGenAIClient{},
&WatsonxAIClient{},
&DashScopeClient{},
}
Backends = []string{
openAIClientName,
Expand All @@ -48,6 +49,7 @@ var (
googleVertexAIClientName,
ociClientName,
watsonxAIClientName,
alicloudAIClientName,
}
)

Expand Down Expand Up @@ -181,7 +183,7 @@ func (p *AIProvider) GetCustomHeaders() []http.Header {
return p.CustomHeaders
}

var passwordlessProviders = []string{"localai", "ollama", "amazonsagemaker", "amazonbedrock", "googlevertexai", "oci"}
var passwordlessProviders = []string{"localai", "ollama", "amazonsagemaker", "amazonbedrock", "googlevertexai", "oci", "alicloud"}

func NeedPassword(backend string) bool {
for _, b := range passwordlessProviders {
Expand Down
Loading