Skip to content

Commit

Permalink
Merge pull request wso2#12382 from ashera96/ai-features
Browse files Browse the repository at this point in the history
AI feature related bug fixes
  • Loading branch information
ashera96 authored Mar 27, 2024
2 parents 91d6f31 + 48ebcfc commit e668ed8
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ public final class APIConstants {
// Constants related to AI features: API chat and Marketplace Assistant
public static class AI {

public static final String API_CHAT = "APIChat";
public static final String API_CHAT = "APIChat";
public static final String API_CHAT_ENABLED = "Enabled";
public static final String API_CHAT_AUTH_TOKEN = "AuthToken";
public static final String API_CHAT_ENDPOINT = "Endpoint";
Expand All @@ -514,15 +514,16 @@ public static class AI {
public static final String API_CHAT_EXECUTE_RESOURCE = "ExecuteResource";
public static final String API_CHAT_ACTION_PREPARE = "PREPARE";
public static final String API_CHAT_ACTION_EXECUTE = "EXECUTE";
public static final String API_CHAT_REQUEST_ID = "apiChatRequestId";

public static final String MARKETPLACE_ASSISTANT = "MarketplaceAssistant";
public static final String MARKETPLACE_ASSISTANT = "MarketplaceAssistant";
public static final String MARKETPLACE_ASSISTANT_ENABLED = "Enabled";
public static final String MARKETPLACE_ASSISTANT_AUTH_TOKEN = "AuthToken";
public static final String MARKETPLACE_ASSISTANT_ENDPOINT = "Endpoint";
public static final String MARKETPLACE_ASSISTANT_CHAT_RESOURCE = "ChatResource";
public static final String MARKETPLACE_ASSISTANT_PUBLISH_API_RESOURCE = "ApiPublishResource";
public static final String MARKETPLACE_ASSISTANT_DELETE_API_RESOURCE = "ApiDeleteResource";
public static final String MARKETPLACE_ASSISTANT_API_COUNT_RESOURCE = "ApiCountResource";
public static final String MARKETPLACE_ASSISTANT_API_COUNT_RESOURCE = "ApiCountResource";

private AI() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
import org.wso2.carbon.apimgt.api.model.policy.PolicyConstants;
import org.wso2.carbon.apimgt.api.model.webhooks.Subscription;
import org.wso2.carbon.apimgt.api.model.webhooks.Topic;
import org.wso2.carbon.apimgt.impl.ai.ApiChatConfigurationDto;
import org.wso2.carbon.apimgt.impl.dto.ai.ApiChatConfigurationDTO;
import org.wso2.carbon.apimgt.impl.caching.CacheProvider;
import org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO;
import org.wso2.carbon.apimgt.impl.definitions.OASParserUtil;
Expand Down Expand Up @@ -3322,7 +3322,7 @@ public String getOpenAPIDefinition(String apiId, String organization) throws API

@Override
public String invokeApiChatExecute(String apiChatRequestId, String requestPayload) throws APIManagementException {
ApiChatConfigurationDto configDto = ServiceReferenceHolder.
ApiChatConfigurationDTO configDto = ServiceReferenceHolder.
getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration().getApiChatConfigurationDto();
return APIUtil.invokeAIService(configDto.getEndpoint(),
configDto.getAccessToken(), configDto.getExecuteResource(), requestPayload,
Expand All @@ -3339,7 +3339,7 @@ public String invokeApiChatPrepare(String apiId, String apiChatRequestId, String
ObjectNode payload = objectMapper.createObjectNode();
payload.set("openapi", openAPIDefinitionJsonNode);

ApiChatConfigurationDto configDto = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService()
ApiChatConfigurationDTO configDto = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService()
.getAPIManagerConfiguration().getApiChatConfigurationDto();
return APIUtil.invokeAIService(configDto.getEndpoint(), configDto.getAccessToken(),
configDto.getPrepareResource(), payload.toString(), apiChatRequestId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
import org.wso2.carbon.apimgt.api.model.Environment;
import org.wso2.carbon.apimgt.api.model.VHost;
import org.wso2.carbon.apimgt.common.gateway.configdto.HttpClientConfigurationDTO;
import org.wso2.carbon.apimgt.impl.ai.ApiChatConfigurationDto;
import org.wso2.carbon.apimgt.impl.ai.MarketplaceAssistantConfigurationDto;
import org.wso2.carbon.apimgt.impl.dto.ai.ApiChatConfigurationDTO;
import org.wso2.carbon.apimgt.impl.dto.ai.MarketplaceAssistantConfigurationDTO;
import org.wso2.carbon.apimgt.common.gateway.dto.ClaimMappingDto;
import org.wso2.carbon.apimgt.common.gateway.dto.JWKSConfigurationDTO;
import org.wso2.carbon.apimgt.common.gateway.dto.TokenIssuerDto;
Expand All @@ -50,7 +50,6 @@
import org.wso2.carbon.apimgt.impl.monetization.MonetizationConfigurationDto;
import org.wso2.carbon.apimgt.impl.recommendationmgt.RecommendationEnvironment;
import org.wso2.carbon.apimgt.impl.utils.APIUtil;
import org.wso2.carbon.utils.CarbonUtils;
import org.wso2.securevault.SecretResolver;
import org.wso2.securevault.SecretResolverFactory;
import org.wso2.securevault.commons.MiscellaneousUtil;
Expand Down Expand Up @@ -118,8 +117,8 @@ public class APIManagerConfiguration {
private boolean initialized;
private ThrottleProperties throttleProperties = new ThrottleProperties();
private ExtendedJWTConfigurationDto jwtConfigurationDto = new ExtendedJWTConfigurationDto();
private static MarketplaceAssistantConfigurationDto marketplaceAssistantConfigurationDto = new MarketplaceAssistantConfigurationDto();
private static ApiChatConfigurationDto apiChatConfigurationDto = new ApiChatConfigurationDto();
private static MarketplaceAssistantConfigurationDTO marketplaceAssistantConfigurationDto = new MarketplaceAssistantConfigurationDTO();
private static ApiChatConfigurationDTO apiChatConfigurationDto = new ApiChatConfigurationDTO();

private WorkflowProperties workflowProperties = new WorkflowProperties();
private Map<String, Environment> apiGatewayEnvironments = new LinkedHashMap<String, Environment>();
Expand Down Expand Up @@ -165,12 +164,12 @@ public static boolean isTokenRevocationEnabled() {
return !tokenRevocationClassName.isEmpty();
}

public MarketplaceAssistantConfigurationDto getMarketplaceAssistantConfigurationDto() {
public MarketplaceAssistantConfigurationDTO getMarketplaceAssistantConfigurationDto() {

return marketplaceAssistantConfigurationDto;
}

public ApiChatConfigurationDto getApiChatConfigurationDto() {
public ApiChatConfigurationDTO getApiChatConfigurationDto() {

return apiChatConfigurationDto;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
package org.wso2.carbon.apimgt.impl.ai;
/*
* 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.
*/

public class ApiChatConfigurationDto {
package org.wso2.carbon.apimgt.impl.dto.ai;

/**
* This class represent the API chat configuration DTO.
*/
public class ApiChatConfigurationDTO {

private String accessToken;
private String endpoint;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
package org.wso2.carbon.apimgt.impl.ai;

public class MarketplaceAssistantConfigurationDto {
/*
* 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.
*/

package org.wso2.carbon.apimgt.impl.dto.ai;

/**
* This class represent the Marketplace Assistant configuration DTO.
*/
public class MarketplaceAssistantConfigurationDTO {

private String accessToken;
private String endpoint;
Expand All @@ -11,8 +32,6 @@ public class MarketplaceAssistantConfigurationDto {
private boolean isEnabled;
private boolean isAuthTokenProvided;



public String getAccessToken() {
return accessToken;
}
Expand Down Expand Up @@ -76,4 +95,4 @@ public boolean isAuthTokenProvided() {
public void setAuthTokenProvided(boolean authTokenProvided) {
this.isAuthTokenProvided = authTokenProvided;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.wso2.carbon.apimgt.impl.APIConstants;
import org.wso2.carbon.apimgt.impl.APIManagerConfiguration;
import org.wso2.carbon.apimgt.impl.APIManagerFactory;
import org.wso2.carbon.apimgt.impl.ai.MarketplaceAssistantConfigurationDto;
import org.wso2.carbon.apimgt.impl.dto.ai.MarketplaceAssistantConfigurationDTO;
import org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO;
import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder;
import org.wso2.carbon.apimgt.impl.notifier.events.APIEvent;
Expand All @@ -41,7 +41,7 @@
public class MarketplaceAssistantApiPublisherNotifier extends ApisNotifier{
protected ApiMgtDAO apiMgtDAO;
private static final Log log = LogFactory.getLog(MarketplaceAssistantApiPublisherNotifier.class);
private static MarketplaceAssistantConfigurationDto marketplaceAssistantConfigurationDto = new MarketplaceAssistantConfigurationDto();
private static MarketplaceAssistantConfigurationDTO marketplaceAssistantConfigurationDto = new MarketplaceAssistantConfigurationDTO();

@Override
public boolean publishEvent(Event event) throws NotifierException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.DeprecatedRuntimeConstants;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.apache.xerces.util.SecurityManager;
import org.everit.json.schema.Schema;
import org.everit.json.schema.ValidationException;
Expand Down Expand Up @@ -153,8 +152,6 @@
import org.wso2.carbon.apimgt.impl.IDPConfiguration;
import org.wso2.carbon.apimgt.impl.PasswordResolverFactory;
import org.wso2.carbon.apimgt.impl.RESTAPICacheConfiguration;
import org.wso2.carbon.apimgt.impl.ai.ApiChatConfigurationDto;
import org.wso2.carbon.apimgt.impl.ai.MarketplaceAssistantConfigurationDto;
import org.wso2.carbon.apimgt.impl.caching.CacheProvider;
import org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO;
import org.wso2.carbon.apimgt.impl.dao.CorrelationConfigDAO;
Expand Down Expand Up @@ -10409,7 +10406,7 @@ public static String invokeAIService(String endpoint, String authToken, String r
HttpPost preparePost = new HttpPost(endpoint + resource);
preparePost.setHeader(APIConstants.API_KEY_AUTH, authToken);
preparePost.setHeader(HttpHeaders.CONTENT_TYPE, APIConstants.APPLICATION_JSON_MEDIA_TYPE);
preparePost.setHeader("apiChatRequestId", requestId);
preparePost.setHeader(APIConstants.AI.API_CHAT_REQUEST_ID, requestId);
StringEntity requestEntity = new StringEntity(payload, ContentType.APPLICATION_JSON);
preparePost.setEntity(requestEntity);

Expand Down Expand Up @@ -10449,16 +10446,16 @@ public static String invokeAIService(String endpoint, String authToken, String r
/**
* This method is used to get the no of apis in the vector db for an organization
*
* @param endpoint Config name to retrieve the Service URL
* @param resource Resource that we should forward the request to
* @param endpoint Endpoint to be invoked
* @param authToken OnPremKey for the organization
* @param resource Resource that we should forward the request to
* @return CloseableHttpResponse of the GET call
* @throws APIManagementException
* @throws APIManagementException if an error occurs while retrieving API count
*/
public static CloseableHttpResponse getMarketplaceChatApiCount(String endpoint, String authToken, String resource)
throws APIManagementException {

try{
try {
HttpGet apiCountGet = new HttpGet(endpoint + resource);
apiCountGet.setHeader(APIConstants.API_KEY_AUTH, authToken);
URL url = new URL(endpoint);
Expand All @@ -10473,8 +10470,18 @@ public static CloseableHttpResponse getMarketplaceChatApiCount(String endpoint,
}
}

public static String marketplaceAssistantPostService(String endpoint, String authToken,
String resource, String payload) throws APIManagementException {
/**
* This method is used to invoke the Choreo deployed AI service to accommodate the Marketplace Assistant chats.
*
* @param endpoint Endpoint to be invoked
* @param authToken OnPremKey for the organization
* @param resource Resource that we should forward the request to
* @param payload Request payload that needs to be attached to the request
* @return returns the response if invocation is successful
* @throws APIManagementException if an error occurs while invoking the AI service
*/
public static String marketplaceAssistantPostService(String endpoint, String authToken, String resource,
String payload) throws APIManagementException {

try {
HttpPost preparePost = new HttpPost(endpoint + resource);
Expand Down Expand Up @@ -10505,9 +10512,17 @@ public static String marketplaceAssistantPostService(String endpoint, String aut
}
}


public static void deleteApi(String endpoint, String authToken,
String resource, String uuid) throws APIManagementException {
/**
* This method is used to delete an API from the vector database service to accommodate the Marketplace assistant
*
* @param endpoint Endpoint to be invoked
* @param authToken OnPremKey for the organization
* @param resource Resource that we should forward the request to
* @param uuid UUID of the API to be deleted
* @throws APIManagementException if an error occurs while deleting the API
*/
public static void deleteApi(String endpoint, String authToken, String resource, String uuid)
throws APIManagementException {

try {
String resourceWithPathParam = endpoint + resource + "/{uuid}";
Expand All @@ -10525,11 +10540,12 @@ public static void deleteApi(String endpoint, String authToken,
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
if (log.isDebugEnabled()) {
log.debug("Successfully completed the Marketplace Chat API publisher delete call with status code: " + statusCode);
log.debug("Successfully completed the Marketplace Assistant API publisher delete call with " +
"status code: " + statusCode);
}
} else {
String errorMessage = "Error encountered while Deleting the API from the vector database service to accommodate the " +
"Marketplace assistant";
String errorMessage = "Error encountered while deleting the API from the vector database service " +
"to accommodate the Marketplace assistant";
log.error(errorMessage);
}
} catch (MalformedURLException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.wso2.carbon.apimgt.impl.APIManagerConfiguration;
import org.wso2.carbon.apimgt.impl.ai.MarketplaceAssistantConfigurationDto;
import org.wso2.carbon.apimgt.impl.dto.ai.MarketplaceAssistantConfigurationDTO;
import org.wso2.carbon.apimgt.impl.utils.APIMgtDBUtil;
import org.wso2.carbon.apimgt.impl.utils.APIUtil;
import org.wso2.carbon.base.MultitenantConstants;
Expand Down Expand Up @@ -69,8 +69,8 @@ public void setup() throws Exception {
Registry registry = Mockito.mock(Registry.class);
CarbonContext carbonContext = Mockito.mock(CarbonContext.class);
APIManagerConfiguration configuration = Mockito.mock(APIManagerConfiguration.class);
MarketplaceAssistantConfigurationDto marketplaceAssistantConfigurationDto = Mockito.mock(
MarketplaceAssistantConfigurationDto.class);
MarketplaceAssistantConfigurationDTO marketplaceAssistantConfigurationDto = Mockito.mock(
MarketplaceAssistantConfigurationDTO.class);

Mockito.when(componentContext.getBundleContext()).thenReturn(bundleContext);
Mockito.when(realmService.getTenantUserRealm(MultitenantConstants.SUPER_TENANT_ID)).thenReturn(userRealm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.wso2.carbon.apimgt.impl.APIConstants;
import org.wso2.carbon.apimgt.impl.APIManagerConfiguration;
import org.wso2.carbon.apimgt.impl.APIManagerConfigurationService;
import org.wso2.carbon.apimgt.impl.ai.MarketplaceAssistantConfigurationDto;
import org.wso2.carbon.apimgt.impl.dto.ai.MarketplaceAssistantConfigurationDTO;
import org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO;
import org.wso2.carbon.apimgt.impl.dto.EventHubConfigurationDto;
import org.wso2.carbon.apimgt.impl.dto.GatewayArtifactSynchronizerProperties;
Expand Down Expand Up @@ -80,8 +80,8 @@ public void testShouldActivateWhenAllPrerequisitesMet() throws Exception {
UserRealm userRealm = Mockito.mock(UserRealm.class);
OutputEventAdapterService adapterService = Mockito.mock(OutputEventAdapterService.class);
ThrottleProperties throttleProperties = new ThrottleProperties();
MarketplaceAssistantConfigurationDto marketplaceAssistantConfigurationDto = Mockito.mock(
MarketplaceAssistantConfigurationDto.class);
MarketplaceAssistantConfigurationDTO marketplaceAssistantConfigurationDto = Mockito.mock(
MarketplaceAssistantConfigurationDTO.class);

Mockito.doNothing().when(configuration).load(Mockito.anyString());
Mockito.doNothing().when(authManager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import org.wso2.carbon.apimgt.impl.APIClientGenerationException;
import org.wso2.carbon.apimgt.impl.APIClientGenerationManager;
import org.wso2.carbon.apimgt.impl.APIConstants;
import org.wso2.carbon.apimgt.impl.ai.ApiChatConfigurationDto;
import org.wso2.carbon.apimgt.impl.dto.ai.ApiChatConfigurationDTO;
import org.wso2.carbon.apimgt.impl.definitions.GraphQLSchemaDefinition;
import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder;
import org.wso2.carbon.apimgt.impl.utils.APIUtil;
Expand Down Expand Up @@ -276,7 +276,7 @@ public Response addCommentToAPI(String apiId, PostRequestBodyDTO postRequestBody
@Override
public Response apiChatPost(String apiId, String apiChatAction, ApiChatRequestDTO apiChatRequestDTO,
MessageContext messageContext) throws APIManagementException {
ApiChatConfigurationDto configDto = ServiceReferenceHolder.getInstance().
ApiChatConfigurationDTO configDto = ServiceReferenceHolder.getInstance().
getAPIManagerConfigurationService().getAPIManagerConfiguration().getApiChatConfigurationDto();
if (configDto.isAuthTokenProvided()) {
// Check the action
Expand Down
Loading

0 comments on commit e668ed8

Please sign in to comment.