-
Notifications
You must be signed in to change notification settings - Fork 571
/
aimodels.go
97 lines (87 loc) · 2.59 KB
/
aimodels.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package bedrock
import (
"encoding/json"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/bedrock"
"github.com/aws/aws-sdk-go/service/bedrockruntime"
"github.com/deepfence/ThreatMapper/deepfence_server/model"
"github.com/deepfence/ThreatMapper/deepfence_utils/log"
)
func CheckBedrockModelAvailability() (bool, error) {
foundModel := false
for _, region := range BedrockRegions {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(region),
})
if err != nil {
return false, err
}
svc := bedrock.New(sess)
models, err := svc.ListFoundationModels(&bedrock.ListFoundationModelsInput{
ByOutputModality: &textModality,
})
if err != nil {
return false, err
}
for _, modelSummary := range models.ModelSummaries {
if *modelSummary.ModelLifecycle.Status == modelLifecycleActive {
if _, ok := BedrockModelBody[*modelSummary.ModelId]; ok {
foundModel = true
break
}
}
}
if foundModel {
break
}
}
return foundModel, nil
}
// ListBedrockModels Fetch enabled Bedrock models using IAM roles
func ListBedrockModels(region string) ([]model.AddGenerativeAiIntegrationRequest, error) {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(region),
})
if err != nil {
return nil, err
}
svc := bedrock.New(sess)
bedrockRuntimeSvc := bedrockruntime.New(sess)
models, err := svc.ListFoundationModels(&bedrock.ListFoundationModelsInput{
ByOutputModality: &textModality,
})
if err != nil {
return nil, err
}
var bedrockModels []model.AddGenerativeAiIntegrationRequest
message := "hello"
for _, modelSummary := range models.ModelSummaries {
if *modelSummary.ModelLifecycle.Status == modelLifecycleActive {
if body, ok := BedrockModelBody[*modelSummary.ModelId]; ok {
body[BedrockModelBodyInputKey[*modelSummary.ModelId]] = body[BedrockModelBodyInputKey[*modelSummary.ModelId]].(string) + message + BedrockModelBodyInputSuffix[*modelSummary.ModelId]
bodyBytes, err := json.Marshal(body)
if err != nil {
log.Warn().Msg(err.Error())
continue
}
_, err = bedrockRuntimeSvc.InvokeModel(&bedrockruntime.InvokeModelInput{
Accept: &acceptHeader,
Body: bodyBytes,
ContentType: &contentTypeHeader,
ModelId: modelSummary.ModelId,
})
if err != nil {
log.Warn().Msg(err.Error())
continue
}
bedrockModels = append(bedrockModels, model.AddGenerativeAiBedrockIntegration{
AWSRegion: region,
UseIAMRole: true,
ModelID: *modelSummary.ModelId,
})
}
}
}
return bedrockModels, nil
}