Skip to content

Commit

Permalink
fixing ai configuration and ai provider related implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
CrowleyRajapakse committed Oct 17, 2024
1 parent 540a18c commit d005635
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 40 deletions.
2 changes: 1 addition & 1 deletion apim-apk-agent/internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const (
// APIM Mediation constants
const (
InterceptorService = "apkCallInterceptorService"
BackendJWT = "BackEndJWT"
BackendJWT = "backEndJWT"
AddHeader = "apkAddHeader"
RemoveHeader = "apkRemoveHeader"
MirrorRequest = "apkMirrorRequest"
Expand Down
17 changes: 8 additions & 9 deletions apim-apk-agent/internal/messaging/notification_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,21 +428,20 @@ func handleAIProviderEvents(data []byte, eventType string, c client.Client) {
}

if strings.EqualFold(aiProviderCreate, eventType) {
logger.LoggerMessaging.Infof("Create for AI Provider: %s for tenant: %s", aiProviderEvent.Name, aiProviderEvent.TenantDomain)
synchronizer.FetchAIProvidersOnEvent(aiProviderEvent.Name, aiProviderEvent.APIVersion, aiProviderEvent.TenantDomain, c, false)
logger.LoggerMessaging.Infof("Create for AI Provider: %s for tenant: %s", aiProviderEvent.Name, aiProviderEvent.Event.TenantDomain)
synchronizer.FetchAIProvidersOnEvent(aiProviderEvent.Name, aiProviderEvent.APIVersion, aiProviderEvent.Event.TenantDomain, c, false)
aiProviders := managementserver.GetAllAIProviders()
logger.LoggerMessaging.Debugf("AI Providers Internal Map: %v", aiProviders)
} else if strings.EqualFold(aiProviderUpdate, eventType) {
logger.LoggerMessaging.Infof("Update for AI Provider: %s for tenant: %s", aiProviderEvent.Name, aiProviderEvent.TenantDomain)
synchronizer.FetchAIProvidersOnEvent(aiProviderEvent.Name, aiProviderEvent.APIVersion, aiProviderEvent.TenantDomain, c, false)
logger.LoggerMessaging.Infof("Update for AI Provider: %s for tenant: %s", aiProviderEvent.Name, aiProviderEvent.Event.TenantDomain)
synchronizer.FetchAIProvidersOnEvent(aiProviderEvent.Name, aiProviderEvent.APIVersion, aiProviderEvent.Event.TenantDomain, c, false)
aiProviders := managementserver.GetAllAIProviders()
logger.LoggerMessaging.Debugf("AI Providers Internal Map: %v", aiProviders)
} else if strings.EqualFold(aiProviderDelete, eventType) {
logger.LoggerMessaging.Infof("Deletion for AI Provider: %s for tenant: %s", aiProviderEvent.Name, aiProviderEvent.TenantDomain)
aiProvider := managementserver.GetAIProvider(aiProviderEvent.Name, aiProviderEvent.APIVersion, aiProviderEvent.TenantDomain)
sha1ValueforCRName := synchronizer.GetSha1Value(aiProvider.Name + "-" + aiProvider.APIVersion + "-" + aiProvider.Organization)
k8sclient.DeleteAIProviderCR(sha1ValueforCRName, c)
managementserver.DeleteAIProvider(aiProviderEvent.Name, aiProviderEvent.APIVersion, aiProviderEvent.TenantDomain)
logger.LoggerMessaging.Infof("Deletion for AI Provider: %s for tenant: %s", aiProviderEvent.Name, aiProviderEvent.Event.TenantDomain)
aiProvider := managementserver.GetAIProvider(aiProviderEvent.ID)
k8sclient.DeleteAIProviderCR(aiProvider.ID, c)
managementserver.DeleteAIProvider(aiProviderEvent.ID)
aiProviders := managementserver.GetAllAIProviders()
logger.LoggerMessaging.Debugf("AI Providers Internal Map: %v", aiProviders)
}
Expand Down
7 changes: 3 additions & 4 deletions apim-apk-agent/internal/synchronizer/aiproviders_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func FetchAIProvidersOnEvent(aiProviderName string, aiProviderVersion string, or
if cpName, exists := aiP.ObjectMeta.Labels["CPName"]; exists {
found := false
for _, aiProviderFromCP := range aiProviders {
if (aiProviderFromCP.Name == cpName) {
if aiProviderFromCP.Name == cpName {
found = true
break
}
Expand Down Expand Up @@ -186,11 +186,10 @@ func createAIProvider(aiProvider *eventhubTypes.AIProvider) dpv1alpha3.AIProvide
conf, _ := config.ReadConfigs()
sha1ValueofAIProviderName := GetSha1Value(aiProvider.Name)
sha1ValueOfOrganization := GetSha1Value(aiProvider.Organization)
sha1ValueforCRName := GetSha1Value(aiProvider.Name + "-" + aiProvider.APIVersion + "-" + aiProvider.Organization)
labelMap := map[string]string{"name": sha1ValueofAIProviderName,
"organization": sha1ValueOfOrganization,
"InitiateFrom": "CP",
"CPName" : aiProvider.Name,
"CPName": aiProvider.Name,
}
var modelInputSource string
var modelAttributeIdentifier string
Expand Down Expand Up @@ -225,7 +224,7 @@ func createAIProvider(aiProvider *eventhubTypes.AIProvider) dpv1alpha3.AIProvide

crAIProvider := dpv1alpha3.AIProvider{
ObjectMeta: metav1.ObjectMeta{
Name: sha1ValueforCRName,
Name: aiProvider.ID,
Namespace: conf.DataPlane.Namespace,
Labels: labelMap,
},
Expand Down
6 changes: 3 additions & 3 deletions apim-apk-agent/internal/utils/apis_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
package synchronizer

import (
"fmt"
"crypto/sha1"
"encoding/hex"
"fmt"

"archive/zip"
"bytes"
Expand All @@ -39,8 +39,8 @@ import (
transformer "github.com/wso2/product-apim-tooling/apim-apk-agent/pkg/transformer"
"sigs.k8s.io/controller-runtime/pkg/client"

mapperUtil "github.com/wso2/product-apim-tooling/apim-apk-agent/internal/mapper"
k8sclientUtil "github.com/wso2/product-apim-tooling/apim-apk-agent/internal/k8sClient"
mapperUtil "github.com/wso2/product-apim-tooling/apim-apk-agent/internal/mapper"
)

func init() {
Expand Down Expand Up @@ -111,7 +111,7 @@ func FetchAPIsOnEvent(conf *config.Config, apiUUID *string, k8sClient client.Cli
return nil, err
}

apkConf, apiUUID, revisionID, configuredRateLimitPoliciesMap, endpointSecurityData, api, prodAIRL, sandAIRL, apkErr := transformer.GenerateAPKConf(artifact.APIJson, artifact.CertArtifact, apiDeployment.OrganizationID)
apkConf, apiUUID, revisionID, configuredRateLimitPoliciesMap, endpointSecurityData, api, prodAIRL, sandAIRL, apkErr := transformer.GenerateAPKConf(artifact.APIJson, artifact.CertArtifact, apiDeployment.OrganizationID)
if prodAIRL == nil {
// Try to delete production AI ratelimit for this api
k8sclientUtil.DeleteAIRatelimitPolicy(generateSHA1HexHash(api.Name, api.Version, "production"), k8sClient)
Expand Down
1 change: 1 addition & 0 deletions apim-apk-agent/pkg/eventhub/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type AIProviderList struct {

// AIProvider for struct AIProvider
type AIProvider struct {
ID string `json:"id"`
Name string `json:"name"`
APIVersion string `json:"apiVersion"`
Organization string `json:"organization"`
Expand Down
12 changes: 6 additions & 6 deletions apim-apk-agent/pkg/managementserver/event_holder.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ func init() {

// AddAIProvider adds an AI provider to the aiProviderMap
func AddAIProvider(aiProvider eventHub.AIProvider) {
aiProviderMap[aiProvider.Name+aiProvider.APIVersion+aiProvider.Organization] = aiProvider
aiProviderMap[aiProvider.ID] = aiProvider
}

// GetAIProvider returns an AI provider from the aiProviderMap
func GetAIProvider(name string, apiVersion string, organization string) eventHub.AIProvider {
return aiProviderMap[name+apiVersion+organization]
func GetAIProvider(id string) eventHub.AIProvider {
return aiProviderMap[id]
}

// DeleteAIProvider deletes an AI provider from the aiProviderMap
func DeleteAIProvider(name string, apiVersion string, organization string) {
delete(aiProviderMap, name+apiVersion+organization)
func DeleteAIProvider(id string) {
delete(aiProviderMap, id)
}

// GetAllAIProviders returns all the AI providers in the aiProviderMap
Expand Down Expand Up @@ -101,7 +101,7 @@ func DeleteRateLimitPolicy(name string, tenantDomain string) {
}

// DeleteSubscriptionPolicy deletes a subscription policy from the subscriptionPolicyMap
func DeleteSubscriptionPolicy(name string, tenantDomain string){
func DeleteSubscriptionPolicy(name string, tenantDomain string) {
delete(subscriptionPolicyMap, name+tenantDomain)
}

Expand Down
1 change: 1 addition & 0 deletions apim-apk-agent/pkg/messaging/event_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ type PolicyInfo struct {
type AIProviderEvent struct {
Name string `json:"name"`
APIVersion string `json:"apiVersion"`
ID string `json:"id"`
Event
}

Expand Down
33 changes: 19 additions & 14 deletions apim-apk-agent/pkg/transformer/api_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,27 @@ type APIMApi struct {
SecuritySchemes []string `json:"securityScheme"`
AdditionalProperties []AdditionalProperties `yaml:"additionalProperties"`
// AdditionalPropertiesMap []AdditionalPropertiesMap `yaml:"additionalPropertiesMap"`
CORSConfiguration CORSConfiguration `yaml:"corsConfiguration"`
EndpointConfig EndpointConfig `yaml:"endpointConfig"`
Operations []APIMOperation `yaml:"operations"`
OrganizationID string `yaml:"organizationId"`
RevisionID uint32 `yaml:"revisionId"`
RevisionedAPIID string `yaml:"revisionedApiId"`
APIThrottlingPolicy string `yaml:"apiThrottlingPolicy"`
APIPolicies APIMOperationPolicies `yaml:"apiPolicies"`
AIConfiguration APIMAIConfiguration `yaml:"aiConfiguration"`
MaxTps *MaxTps `yaml:"maxTps"`
CORSConfiguration CORSConfiguration `yaml:"corsConfiguration"`
EndpointConfig EndpointConfig `yaml:"endpointConfig"`
Operations []APIMOperation `yaml:"operations"`
OrganizationID string `yaml:"organizationId"`
RevisionID uint32 `yaml:"revisionId"`
RevisionedAPIID string `yaml:"revisionedApiId"`
APIThrottlingPolicy string `yaml:"apiThrottlingPolicy"`
APIPolicies APIMOperationPolicies `yaml:"apiPolicies"`
SubtypeConfiguration SubtypeConfiguration `yaml:"subtypeConfiguration"`
MaxTps *MaxTps `yaml:"maxTps"`
}

// APIMAIConfiguration holds the configuration details for AI providers
type APIMAIConfiguration struct {
LLMProviderName string `yaml:"llmProviderName"`
LLMProviderAPIVersion string `yaml:"llmProviderApiVersion"`
// SubtypeConfiguration holds the details for Subtypes
type SubtypeConfiguration struct {
Subtype string `json:"subtype"`
Configuration string `json:"_configuration"`
}

// Configuration holds the configuration details for the subtype
type Configuration struct {
LLMProviderID string `json:"llmProviderId"`
}

// APIYaml is a wrapper struct for YAML representation of an API.
Expand Down
14 changes: 11 additions & 3 deletions apim-apk-agent/pkg/transformer/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func GenerateAPKConf(APIJson string, certArtifact CertificateArtifact, organizat
}

apiYamlData := apiYaml.Data
logger.LoggerTransformer.Debugf("apiYamlData: %v", apiYamlData)

apk.Name = apiYamlData.Name
apk.Context = apiYamlData.Context
Expand All @@ -84,11 +85,18 @@ func GenerateAPKConf(APIJson string, certArtifact CertificateArtifact, organizat
apk.DefinitionPath = "/definition"
apk.SubscriptionValidation = true

if apiYamlData.AIConfiguration.LLMProviderName != "" && apiYamlData.AIConfiguration.LLMProviderAPIVersion != "" {
sha1ValueforCRName := GetSha1Value(apiYamlData.AIConfiguration.LLMProviderName + "-" + apiYamlData.AIConfiguration.LLMProviderAPIVersion + "-" + organizationID)
if apiYamlData.SubtypeConfiguration.Subtype == "AIAPI" && apiYamlData.SubtypeConfiguration.Configuration != "" {
// Unmarshal the _configuration field into the Configuration struct
var config Configuration
err := json.Unmarshal([]byte(apiYamlData.SubtypeConfiguration.Configuration), &config)
if err != nil {
fmt.Println("Error unmarshalling _configuration:", err)
return "", "null", 0, nil, EndpointSecurityConfig{}, nil, nil, nil, err
}
sha1ValueforCRName := config.LLMProviderID
apk.AIProvider = &AIProvider{
Name: sha1ValueforCRName,
APIVersion: apiYamlData.AIConfiguration.LLMProviderAPIVersion,
APIVersion: "1",
}
}

Expand Down

0 comments on commit d005635

Please sign in to comment.