Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing ai configuration and ai provider related implementation #1224

Merged
merged 2 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 5 additions & 6 deletions apim-apk-agent/internal/synchronizer/aiproviders_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ func FetchAIProvidersOnEvent(aiProviderName string, aiProviderVersion string, or
logger.LoggerSynchronizer.Errorf("Error occurred while unmarshelling AI Provider event data %v", err)
return
}
logger.LoggerSynchronizer.Debugf("AI Providers received: %v", aiProviderList.APIs)
var aiProviders []eventhubTypes.AIProvider = aiProviderList.APIs
logger.LoggerSynchronizer.Debugf("AI Providers received: %v", aiProviderList.AIProviders)
var aiProviders []eventhubTypes.AIProvider = aiProviderList.AIProviders

if cleanupDeletedProviders {
aiProvidersFromK8, _, errK8 := k8sclient.RetrieveAllAIProvidersFromK8s(c, "")
Expand All @@ -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
3 changes: 2 additions & 1 deletion apim-apk-agent/pkg/eventhub/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,12 @@ type SubscriptionPolicyList struct {

// AIProviderList for struct list of AIProvider
type AIProviderList struct {
APIs []AIProvider `json:"apis"`
AIProviders []AIProvider `json:"llmProviders"`
}

// 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
24 changes: 12 additions & 12 deletions apim-apk-agent/pkg/managementserver/rest_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,18 @@ func createAPIYaml(apiCPEvent *APICPEvent) (string, string) {
if apiCPEvent.API.APIType == "GraphQL" {
apiType = "GRAPHQL"
}
apiSubType := "DEFAULT"
if apiCPEvent.API.APISubType != "" {
apiSubType = apiCPEvent.API.APISubType
}
var aiConfiguration = make(map[string]interface{})
if apiCPEvent.API.AIConfiguration.LLMProviderName != "" || apiCPEvent.API.AIConfiguration.LLMProviderAPIVersion != "" {

var subTypeConfiguration = make(map[string]interface{})
if apiCPEvent.API.APISubType != "" || apiCPEvent.API.AIConfiguration.LLMProviderID != "" ||
apiCPEvent.API.AIConfiguration.LLMProviderName != "" ||
apiCPEvent.API.AIConfiguration.LLMProviderAPIVersion != "" {
logger.LoggerMgtServer.Debugf("AI Configuration: %+v", apiCPEvent.API.AIConfiguration)
aiConfiguration["llmProviderName"] = apiCPEvent.API.AIConfiguration.LLMProviderName
aiConfiguration["llmProviderApiVersion"] = apiCPEvent.API.AIConfiguration.LLMProviderAPIVersion
subTypeConfiguration["subtype"] = apiCPEvent.API.APISubType
subTypeConfiguration["_configuration"] = "{\"llmProviderId\":\"" +
apiCPEvent.API.AIConfiguration.LLMProviderID + "\"}"
}
logger.LoggerMgtServer.Debugf("Recieved AI Configuration: %+v", aiConfiguration)
logger.LoggerMgtServer.Debugf("Subtype Configuration: %+v", subTypeConfiguration)

data := map[string]interface{}{
"type": "api",
"version": "v4.4.0",
Expand All @@ -189,7 +190,6 @@ func createAPIYaml(apiCPEvent *APICPEvent) (string, string) {
"enableSchemaValidation": false,
"enableSubscriberVerification": false,
"type": apiType,
"subType": apiSubType,
"transport": []string{"http", "https"},
"endpointConfig": map[string]interface{}{
"endpoint_type": apiCPEvent.API.EndpointProtocol,
Expand All @@ -211,8 +211,8 @@ func createAPIYaml(apiCPEvent *APICPEvent) (string, string) {
"scopes": scopes,
},
}
if len(aiConfiguration) > 0 {
data["data"].(map[string]interface{})["aiConfiguration"] = aiConfiguration
if len(subTypeConfiguration) > 0 {
data["data"].(map[string]interface{})["subtypeConfiguration"] = subTypeConfiguration
}
// TODO when we start to process sandbox we need to have this if condition. For now we remove sandbox endpoint always.
// if apiCPEvent.API.SandEndpoint == "" {
Expand Down
1 change: 1 addition & 0 deletions apim-apk-agent/pkg/managementserver/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ type AIRL struct {

// AIConfiguration holds the AI configuration
type AIConfiguration struct {
LLMProviderID string `json:"llmProviderID"`
LLMProviderName string `json:"llmProviderName"`
LLMProviderAPIVersion string `json:"llmProviderApiVersion"`
}
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
Loading