Skip to content

Commit

Permalink
Merge pull request #2445 from CrowleyRajapakse/ai
Browse files Browse the repository at this point in the history
Adding AI Provider CRD implementation
  • Loading branch information
CrowleyRajapakse authored Sep 11, 2024
2 parents dc729f7 + bf70c12 commit b84cd52
Show file tree
Hide file tree
Showing 63 changed files with 4,944 additions and 310 deletions.
45 changes: 45 additions & 0 deletions adapter/api/proto/wso2/discovery/api/ai_provider.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 LLC. licenses this file to you 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.

syntax = "proto3";

package wso2.discovery.api;


option go_package = "github.com/envoyproxy/go-control-plane/wso2/discovery/api;api";
option java_package = "org.wso2.apk.enforcer.discovery.api";
option java_outer_classname = "AIProviderProto";
option java_multiple_files = true;

// [#protodoc-title: AIProvider]

// Holds AIProvider configs
message AIProvider {
string providerName = 1;
string providerAPIVersion = 2;
string organization = 3;
ValueDetails model = 4;
ValueDetails promptTokens = 5;
ValueDetails completionToken = 6;
ValueDetails totalToken = 7;
bool enabled = 8;
}

// Holds ValueDetails configs
message ValueDetails {
string in = 1;
string value = 2;
}
2 changes: 2 additions & 0 deletions adapter/api/proto/wso2/discovery/api/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import "wso2/discovery/api/BackendJWTTokenInfo.proto";
import "wso2/discovery/api/endpoint_cluster.proto";
import "wso2/discovery/api/security_info.proto";
import "wso2/discovery/api/graphql.proto";
import "wso2/discovery/api/ai_provider.proto";

option go_package = "github.com/envoyproxy/go-control-plane/wso2/discovery/api;api";
option java_package = "org.wso2.apk.enforcer.discovery.api";
Expand Down Expand Up @@ -61,4 +62,5 @@ message Api {
bool subscriptionValidation = 28;
EndpointCluster endpoints = 29;
repeated SecurityInfo endpointSecurity = 30;
AIProvider aiprovider = 31;
}
38 changes: 38 additions & 0 deletions adapter/internal/oasparser/config_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,38 @@ func GetEnforcerAPI(adapterInternalAPI *model.AdapterInternalAPI, vhost string)
}
}

var aiProvider *api.AIProvider

aiProviderFromInternalAPI := adapterInternalAPI.GetAIProvider()
logger.LoggerOasparser.Debugf("Before Internal AI Provider: %+v", aiProviderFromInternalAPI)

if aiProviderFromInternalAPI.Enabled {
aiProvider = &api.AIProvider{
Enabled: aiProviderFromInternalAPI.Enabled,
ProviderName: aiProviderFromInternalAPI.ProviderName,
ProviderAPIVersion: aiProviderFromInternalAPI.ProviderAPIVersion,
Organization: aiProviderFromInternalAPI.Organization,
Model: &api.ValueDetails{
In: aiProviderFromInternalAPI.Model.In,
Value: aiProviderFromInternalAPI.Model.Value,
},
PromptTokens: &api.ValueDetails{
In: aiProviderFromInternalAPI.PromptTokens.In,
Value: aiProviderFromInternalAPI.PromptTokens.Value,
},
CompletionToken: &api.ValueDetails{
In: aiProviderFromInternalAPI.CompletionToken.In,
Value: aiProviderFromInternalAPI.CompletionToken.Value,
},
TotalToken: &api.ValueDetails{
In: aiProviderFromInternalAPI.TotalToken.In,
Value: aiProviderFromInternalAPI.TotalToken.Value,
},
}
}

logger.LoggerOasparser.Debugf("After Conversion AI Provider: %+v", aiProvider)

return &api.Api{
Id: adapterInternalAPI.UUID,
Title: adapterInternalAPI.GetTitle(),
Expand Down Expand Up @@ -209,6 +241,12 @@ func GetEnforcerAPI(adapterInternalAPI *model.AdapterInternalAPI, vhost string)
ApiDefinitionFile: adapterInternalAPI.GetAPIDefinitionFile(),
Environment: adapterInternalAPI.GetEnvironment(),
SubscriptionValidation: adapterInternalAPI.GetSubscriptionValidation(),
Aiprovider: func() *api.AIProvider {
if aiProvider != nil && aiProvider.Enabled {
return aiProvider
}
return nil
}(),
}
}

Expand Down
55 changes: 53 additions & 2 deletions adapter/internal/oasparser/model/adapter_internal_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type AdapterInternalAPI struct {
environment string
Endpoints *EndpointCluster
EndpointSecurity []*EndpointSecurity
AIProvider InternalAIProvider
}

// BackendJWTTokenInfo represents the object structure holding the information related to the JWT Generator
Expand All @@ -92,6 +93,24 @@ type BackendJWTTokenInfo struct {
CustomClaims []ClaimMapping
}

// InternalAIProvider represents the object structure holding the information related to the AI Provider
type InternalAIProvider struct {
Enabled bool
ProviderName string
ProviderAPIVersion string
Organization string
Model ValueDetails
PromptTokens ValueDetails
CompletionToken ValueDetails
TotalToken ValueDetails
}

// ValueDetails defines the value details
type ValueDetails struct {
In string `json:"in"`
Value string `json:"value"`
}

// ClaimMapping represents the object structure holding the information related to the JWT Generator Claims
type ClaimMapping struct {
Claim string
Expand Down Expand Up @@ -426,6 +445,37 @@ func (adapterInternalAPI *AdapterInternalAPI) GetEnvironment() string {
return adapterInternalAPI.environment
}

// SetAIProvider sets the AIProvider of the API.
func (adapterInternalAPI *AdapterInternalAPI) SetAIProvider(aiProvider dpv1alpha3.AIProvider) {
adapterInternalAPI.AIProvider = InternalAIProvider{
Enabled: true,
ProviderName: aiProvider.Spec.ProviderName,
ProviderAPIVersion: aiProvider.Spec.ProviderAPIVersion,
Organization: aiProvider.Spec.Organization,
Model: ValueDetails{
In: aiProvider.Spec.Model.In,
Value: aiProvider.Spec.Model.Value,
},
PromptTokens: ValueDetails{
In: aiProvider.Spec.RateLimitFields.PromptTokens.In,
Value: aiProvider.Spec.RateLimitFields.PromptTokens.Value,
},
CompletionToken: ValueDetails{
In: aiProvider.Spec.RateLimitFields.CompletionToken.In,
Value: aiProvider.Spec.RateLimitFields.CompletionToken.Value,
},
TotalToken: ValueDetails{
In: aiProvider.Spec.RateLimitFields.TotalToken.In,
Value: aiProvider.Spec.RateLimitFields.TotalToken.Value,
},
}
}

// GetAIProvider returns the AIProvider of the API
func (adapterInternalAPI *AdapterInternalAPI) GetAIProvider() InternalAIProvider {
return adapterInternalAPI.AIProvider
}

// Validate method confirms that the adapterInternalAPI has all required fields in the required format.
// This needs to be checked prior to generate router/enforcer related resources.
func (adapterInternalAPI *AdapterInternalAPI) Validate() error {
Expand Down Expand Up @@ -460,7 +510,7 @@ func (adapterInternalAPI *AdapterInternalAPI) SetInfoHTTPRouteCR(httpRoute *gwap
if outputAuthScheme != nil {
authScheme = *outputAuthScheme
}
var apiPolicy *dpv1alpha2.APIPolicy
var apiPolicy *dpv1alpha3.APIPolicy
if outputAPIPolicy != nil {
apiPolicy = *outputAPIPolicy
}
Expand Down Expand Up @@ -925,6 +975,7 @@ func (adapterInternalAPI *AdapterInternalAPI) SetInfoHTTPRouteCR(httpRoute *gwap
}.String()].Spec
adapterInternalAPI.backendJWTTokenInfo = parseBackendJWTTokenToInternal(backendJWTPolicy)
}

return nil
}

Expand All @@ -943,7 +994,7 @@ func (adapterInternalAPI *AdapterInternalAPI) SetInfoGQLRouteCR(gqlRoute *dpv1al
if outputAuthScheme != nil {
authScheme = *outputAuthScheme
}
var apiPolicy *dpv1alpha2.APIPolicy
var apiPolicy *dpv1alpha3.APIPolicy
if outputAPIPolicy != nil {
apiPolicy = *outputAPIPolicy
}
Expand Down
12 changes: 6 additions & 6 deletions adapter/internal/oasparser/model/http_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import (
type ResourceParams struct {
AuthSchemes map[string]dpv1alpha2.Authentication
ResourceAuthSchemes map[string]dpv1alpha2.Authentication
APIPolicies map[string]dpv1alpha2.APIPolicy
ResourceAPIPolicies map[string]dpv1alpha2.APIPolicy
APIPolicies map[string]dpv1alpha3.APIPolicy
ResourceAPIPolicies map[string]dpv1alpha3.APIPolicy
InterceptorServiceMapping map[string]dpv1alpha1.InterceptorService
BackendJWTMapping map[string]dpv1alpha1.BackendJWT
BackendMapping map[string]*dpv1alpha2.ResolvedBackend
Expand Down Expand Up @@ -69,7 +69,7 @@ func parseBackendJWTTokenToInternal(backendJWTToken dpv1alpha1.BackendJWTSpec) *
return backendJWTTokenInternal
}

func getCorsConfigFromAPIPolicy(apiPolicy *dpv1alpha2.APIPolicy) *CorsConfig {
func getCorsConfigFromAPIPolicy(apiPolicy *dpv1alpha3.APIPolicy) *CorsConfig {
globalCorsConfig := config.ReadConfigs().Enforcer.Cors

var corsConfig = CorsConfig{
Expand Down Expand Up @@ -111,7 +111,7 @@ func parseRateLimitPolicyToInternal(ratelimitPolicy *dpv1alpha3.RateLimitPolicy)
}

// addOperationLevelInterceptors add the operation level interceptor policy to the policies
func addOperationLevelInterceptors(policies *OperationPolicies, apiPolicy *dpv1alpha2.APIPolicy,
func addOperationLevelInterceptors(policies *OperationPolicies, apiPolicy *dpv1alpha3.APIPolicy,
interceptorServicesMapping map[string]dpv1alpha1.InterceptorService,
backendMapping map[string]*dpv1alpha2.ResolvedBackend, namespace string) {
if apiPolicy != nil && apiPolicy.Spec.Override != nil {
Expand Down Expand Up @@ -204,8 +204,8 @@ func concatRateLimitPolicies(schemeUp *dpv1alpha3.RateLimitPolicy, schemeDown *d
return &finalRateLimit
}

func concatAPIPolicies(schemeUp *dpv1alpha2.APIPolicy, schemeDown *dpv1alpha2.APIPolicy) *dpv1alpha2.APIPolicy {
apiPolicy := dpv1alpha2.APIPolicy{}
func concatAPIPolicies(schemeUp *dpv1alpha3.APIPolicy, schemeDown *dpv1alpha3.APIPolicy) *dpv1alpha3.APIPolicy {
apiPolicy := dpv1alpha3.APIPolicy{}
if schemeUp != nil && schemeDown != nil {
apiPolicy.Spec.Override = utils.SelectPolicy(&schemeUp.Spec.Override, &schemeUp.Spec.Default, &schemeDown.Spec.Override, &schemeDown.Spec.Default)
} else if schemeUp != nil {
Expand Down
Loading

0 comments on commit b84cd52

Please sign in to comment.