mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2025-04-27 11:11:31 +00:00
* fix: [Bug] Make lint command is not working Signed-off-by: Milap Jhumkhawala <milap.jhumkhawala@gmail.com> * forgot to sign commit Signed-off-by: Milap Jhumkhawala <milap.jhumkhawala@gmail.com> --------- Signed-off-by: Milap Jhumkhawala <milap.jhumkhawala@gmail.com>
88 lines
1.9 KiB
Go
88 lines
1.9 KiB
Go
package ai
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"github.com/sashabaranov/go-openai"
|
|
)
|
|
|
|
const azureAIClientName = "azureopenai"
|
|
|
|
type AzureAIClient struct {
|
|
nopCloser
|
|
|
|
client *openai.Client
|
|
model string
|
|
temperature float32
|
|
// organizationId string
|
|
}
|
|
|
|
func (c *AzureAIClient) Configure(config IAIConfig) error {
|
|
token := config.GetPassword()
|
|
baseURL := config.GetBaseURL()
|
|
engine := config.GetEngine()
|
|
proxyEndpoint := config.GetProxyEndpoint()
|
|
defaultConfig := openai.DefaultAzureConfig(token, baseURL)
|
|
orgId := config.GetOrganizationId()
|
|
|
|
defaultConfig.AzureModelMapperFunc = func(model string) string {
|
|
// If you use a deployment name different from the model name, you can customize the AzureModelMapperFunc function
|
|
azureModelMapping := map[string]string{
|
|
model: engine,
|
|
}
|
|
return azureModelMapping[model]
|
|
|
|
}
|
|
|
|
if proxyEndpoint != "" {
|
|
proxyUrl, err := url.Parse(proxyEndpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
transport := &http.Transport{
|
|
Proxy: http.ProxyURL(proxyUrl),
|
|
}
|
|
|
|
defaultConfig.HTTPClient = &http.Client{
|
|
Transport: transport,
|
|
}
|
|
}
|
|
if orgId != "" {
|
|
defaultConfig.OrgID = orgId
|
|
}
|
|
|
|
client := openai.NewClientWithConfig(defaultConfig)
|
|
if client == nil {
|
|
return errors.New("error creating Azure OpenAI client")
|
|
}
|
|
c.client = client
|
|
c.model = config.GetModel()
|
|
c.temperature = config.GetTemperature()
|
|
return nil
|
|
}
|
|
|
|
func (c *AzureAIClient) GetCompletion(ctx context.Context, prompt string) (string, error) {
|
|
// Create a completion request
|
|
resp, err := c.client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{
|
|
Model: c.model,
|
|
Messages: []openai.ChatCompletionMessage{
|
|
{
|
|
Role: openai.ChatMessageRoleUser,
|
|
Content: prompt,
|
|
},
|
|
},
|
|
Temperature: c.temperature,
|
|
})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return resp.Choices[0].Message.Content, nil
|
|
}
|
|
|
|
func (c *AzureAIClient) GetName() string {
|
|
return azureAIClientName
|
|
}
|