-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
/kind feature support an AI backend service that other modules can use to access AI language models.
- Loading branch information
Showing
13 changed files
with
445 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// 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 options | ||
|
||
import ( | ||
"github.com/KusionStack/karpor/pkg/kubernetes/registry" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
type AIOptions struct { | ||
Backend string | ||
AuthToken string | ||
BaseURL string | ||
Model string | ||
Temperature float32 | ||
TopP float32 | ||
} | ||
|
||
const ( | ||
defaultBackend = "openai" | ||
defaultModel = "gpt-3.5-turbo" | ||
defaultTemperature = 1 | ||
defaultTopP = 1 | ||
) | ||
|
||
func NewAIOptions() *AIOptions { | ||
return &AIOptions{} | ||
} | ||
|
||
func (o *AIOptions) Validate() []error { | ||
return nil | ||
} | ||
|
||
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 | ||
return nil | ||
} | ||
|
||
// AddFlags adds flags for a specific Option to the specified FlagSet | ||
func (o *AIOptions) AddFlags(fs *pflag.FlagSet) { | ||
if o == nil { | ||
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// 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 ( | ||
"github.com/KusionStack/karpor/pkg/infra/ai" | ||
"github.com/KusionStack/karpor/pkg/kubernetes/registry" | ||
) | ||
|
||
type AIManager struct { | ||
client ai.AIProvider | ||
} | ||
|
||
// NewAIManager returns a new AIManager object | ||
func NewAIManager(c registry.ExtraConfig) (*AIManager, error) { | ||
aiClient := ai.NewClient(c.Backend) | ||
if err := aiClient.Configure(ai.ConvertToAIConfig(c)); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &AIManager{ | ||
client: aiClient, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// 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 ( | ||
"context" | ||
"errors" | ||
"github.com/sashabaranov/go-openai" | ||
) | ||
|
||
type AzureAIClient struct { | ||
client *openai.Client | ||
model string | ||
temperature float32 | ||
} | ||
|
||
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") | ||
} | ||
|
||
defaultConfig := openai.DefaultAzureConfig(cfg.AuthToken, cfg.BaseURL) | ||
|
||
client := openai.NewClientWithConfig(defaultConfig) | ||
if client == nil { | ||
return errors.New("error creating Azure OpenAI client") | ||
} | ||
|
||
c.client = client | ||
c.model = cfg.Model | ||
c.temperature = cfg.Temperature | ||
return nil | ||
} | ||
|
||
func (c *AzureAIClient) Generate(ctx context.Context, prompt string, serviceType string) (string, error) { | ||
servicePrompt := ServicePromptMap[serviceType] | ||
|
||
resp, err := c.client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{ | ||
Model: c.model, | ||
Messages: []openai.ChatCompletionMessage{ | ||
{ | ||
Role: openai.ChatMessageRoleSystem, | ||
Content: servicePrompt, | ||
}, | ||
{ | ||
Role: openai.ChatMessageRoleUser, | ||
Content: prompt, | ||
}, | ||
}, | ||
Temperature: c.temperature, | ||
}) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if len(resp.Choices) == 0 { | ||
return "", errors.New("no completion choices returned from response") | ||
} | ||
return resp.Choices[0].Message.Content, nil | ||
} |
Oops, something went wrong.