forked from k8sgpt-ai/k8sgpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: amazonsagemaker AI provider (k8sgpt-ai#731)
* feat(amazonsagemaker): Add AmazonSageMaker AI provider Co-authored-by: NAME 18630245+zaremb@users.noreply.github.com Signed-off-by: Damian Kuroczko <7778327+dkuroczk@users.noreply.github.com> * feat(amazonsagemaker): Add AmazonSageMaker AI provider Co-authored-by: Mateusz Zaremba <18630245+zaremb@users.noreply.github.com> Signed-off-by: Damian Kuroczko <7778327+dkuroczk@users.noreply.github.com> * feat(auth): add top p and max tokens to auth and use them in sagemaker backend Signed-off-by: Mateusz Zaremba <18630245+zaremb@users.noreply.github.com> * feat: Updates SageMaker docs, validate topP, ident Signed-off-by: Damian Kuroczko <7778327+dkuroczk@users.noreply.github.com> * feat: list of passwordlessProviders Signed-off-by: Damian Kuroczko <7778327+dkuroczk@users.noreply.github.com> * feat: returns err Signed-off-by: Damian Kuroczko <7778327+dkuroczk@users.noreply.github.com> * fix: remove log.Fatal(err) Signed-off-by: Damian Kuroczko <7778327+dkuroczk@users.noreply.github.com> --------- Signed-off-by: Damian Kuroczko <7778327+dkuroczk@users.noreply.github.com> Signed-off-by: Mateusz Zaremba <18630245+zaremb@users.noreply.github.com> Co-authored-by: Mateusz Zaremba <18630245+zaremb@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
278 additions
and
4 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
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,170 @@ | ||
/* | ||
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" | ||
"encoding/base64" | ||
"fmt" | ||
"strings" | ||
|
||
"encoding/json" | ||
|
||
"github.com/fatih/color" | ||
"github.com/k8sgpt-ai/k8sgpt/pkg/cache" | ||
"github.com/k8sgpt-ai/k8sgpt/pkg/util" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/sagemakerruntime" | ||
) | ||
|
||
type SageMakerAIClient struct { | ||
client *sagemakerruntime.SageMakerRuntime | ||
language string | ||
model string | ||
temperature float32 | ||
endpoint string | ||
topP float32 | ||
maxTokens int | ||
} | ||
|
||
type Generations []struct { | ||
Generation struct { | ||
Role string `json:"role"` | ||
Content string `json:"content"` | ||
} `json:"generation"` | ||
} | ||
|
||
type Request struct { | ||
Inputs [][]Message `json:"inputs"` | ||
Parameters Parameters `json:"parameters"` | ||
} | ||
|
||
type Message struct { | ||
Role string `json:"role"` | ||
Content string `json:"content"` | ||
} | ||
|
||
type Parameters struct { | ||
MaxNewTokens int `json:"max_new_tokens"` | ||
TopP float64 `json:"top_p"` | ||
Temperature float64 `json:"temperature"` | ||
} | ||
|
||
func (c *SageMakerAIClient) Configure(config IAIConfig, language string) error { | ||
|
||
// Create a new AWS session | ||
sess := session.Must(session.NewSessionWithOptions(session.Options{ | ||
Config: aws.Config{Region: aws.String(config.GetProviderRegion())}, | ||
SharedConfigState: session.SharedConfigEnable, | ||
})) | ||
|
||
c.language = language | ||
// Create a new SageMaker runtime client | ||
c.client = sagemakerruntime.New(sess) | ||
c.model = config.GetModel() | ||
c.endpoint = config.GetEndpointName() | ||
c.temperature = config.GetTemperature() | ||
c.maxTokens = config.GetMaxTokens() | ||
c.topP = config.GetTopP() | ||
return nil | ||
} | ||
|
||
func (c *SageMakerAIClient) GetCompletion(ctx context.Context, prompt string, promptTmpl string) (string, error) { | ||
// Create a completion request | ||
|
||
if len(promptTmpl) == 0 { | ||
promptTmpl = PromptMap["default"] | ||
} | ||
|
||
request := Request{ | ||
Inputs: [][]Message{ | ||
{ | ||
{Role: "system", Content: "DEFAULT_PROMPT"}, | ||
{Role: "user", Content: fmt.Sprintf(promptTmpl, c.language, prompt)}, | ||
}, | ||
}, | ||
|
||
Parameters: Parameters{ | ||
MaxNewTokens: int(c.maxTokens), | ||
TopP: float64(c.topP), | ||
Temperature: float64(c.temperature), | ||
}, | ||
} | ||
|
||
// Convert request to []byte | ||
bytesData, err := json.Marshal(request) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// Create an input object | ||
input := &sagemakerruntime.InvokeEndpointInput{ | ||
Body: bytesData, | ||
EndpointName: aws.String(c.endpoint), | ||
ContentType: aws.String("application/json"), // Set the content type as per your model's requirements | ||
Accept: aws.String("application/json"), // Set the accept type as per your model's requirements | ||
CustomAttributes: aws.String("accept_eula=true"), | ||
} | ||
|
||
// Call the InvokeEndpoint function | ||
result, err := c.client.InvokeEndpoint(input) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// // Define a slice of Generations | ||
var generations Generations | ||
|
||
err = json.Unmarshal([]byte(string(result.Body)), &generations) | ||
if err != nil { | ||
return "", err | ||
} | ||
// Check for length of generations | ||
if len(generations) != 1 { | ||
return "", fmt.Errorf("Expected exactly one generation, but got %d", len(generations)) | ||
} | ||
|
||
// Access the content | ||
content := generations[0].Generation.Content | ||
return content, nil | ||
} | ||
|
||
func (a *SageMakerAIClient) Parse(ctx context.Context, prompt []string, cache cache.ICache, promptTmpl string) (string, error) { | ||
// parse the text with the AI backend | ||
inputKey := strings.Join(prompt, " ") | ||
// Check for cached data | ||
sEnc := base64.StdEncoding.EncodeToString([]byte(inputKey)) | ||
cacheKey := util.GetCacheKey(a.GetName(), a.language, sEnc) | ||
|
||
response, err := a.GetCompletion(ctx, inputKey, promptTmpl) | ||
if err != nil { | ||
color.Red("error getting completion: %v", err) | ||
return "", err | ||
} | ||
|
||
err = cache.Store(cacheKey, base64.StdEncoding.EncodeToString([]byte(response))) | ||
|
||
if err != nil { | ||
color.Red("error storing value to cache: %v", err) | ||
return "", err | ||
} | ||
|
||
return response, nil | ||
} | ||
|
||
func (a *SageMakerAIClient) GetName() string { | ||
return "amazonsagemaker" | ||
} |
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