Skip to content

Commit

Permalink
Merge pull request #12607 from PasanT9/payload-fix-ai-api
Browse files Browse the repository at this point in the history
Add a property to identify empty payloads in  AI APIs
  • Loading branch information
PasanT9 authored Sep 27, 2024
2 parents 2ba0c2a + a8a32a5 commit dd2f6c1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public static class AIAPIConstants {
public static final String CONNECTOR_TYPE = "connectorType";
public static final String LLM_PROVIDER_ID = "id";
public static final String LLM_PROVIDER_NAME = "name";
public static final String LLM_PROVIDER_IS_EMPTY_PAYLOAD = "IS_EMPTY_PAYLOAD";
public static final String LLM_PROVIDER_API_VERSION = "apiVersion";
public static final String LLM_PROVIDER_CONFIGURATIONS = "configurations";
public static final String LLM_CONFIGS_ENDPOINT = "/llm-providers";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,18 @@ private boolean processMessage(MessageContext messageContext, boolean isRequest)
log.error("LLM provider service not found for the provider with ID: " + llmProviderId);
return false;
}
String payload = extractPayloadFromContext(messageContext, providerConfiguration);
String payload = null;
Map<String, String> metadataMap = new HashMap<>();
if (!extractPayloadFromContext(messageContext, providerConfiguration, payload)) {
log.error("Error occurred while reading payload for API with path: " + path + " in tenant domain: "
+ tenantDomain);
metadataMap.put(APIConstants.AIAPIConstants.LLM_PROVIDER_IS_EMPTY_PAYLOAD, Boolean.toString(true));
} else {
metadataMap.put(APIConstants.AIAPIConstants.LLM_PROVIDER_IS_EMPTY_PAYLOAD, Boolean.toString(false));
}
Map<String, String> queryParams = extractQueryParamsFromContext(messageContext);
Map<String, String> headers = extractHeadersFromContext(messageContext);

Map<String, String> metadataMap = new HashMap<>();

metadataMap.put(APIConstants.AIAPIConstants.LLM_PROVIDER_NAME, provider.getName());
metadataMap.put(APIConstants.AIAPIConstants.LLM_PROVIDER_API_VERSION, provider.getApiVersion());
if (isRequest) {
Expand Down Expand Up @@ -231,20 +238,23 @@ private String decryptSecret(String cipherText) throws CryptoException {
*
* @param messageContext the Synapse MessageContext
* @param config LLM provider configuration
* @return the extracted payload
* @param payload Payload
* @return whether payload is extracted successfully
*/
private String extractPayloadFromContext(MessageContext messageContext, LLMProviderConfiguration config)
private boolean extractPayloadFromContext(MessageContext messageContext, LLMProviderConfiguration config,
String payload)
throws XMLStreamException, IOException {

org.apache.axis2.context.MessageContext axis2MessageContext =
((Axis2MessageContext) messageContext).getAxis2MessageContext();

for (LLMProviderMetadata metadata : config.getMetadata()) {
if (APIConstants.AIAPIConstants.INPUT_SOURCE_PAYLOAD.equals(metadata.getInputSource())) {
return getPayload(axis2MessageContext);
payload = getPayload(axis2MessageContext);
break;
}
}
return null;
return payload != null;
}

/**
Expand Down

0 comments on commit dd2f6c1

Please sign in to comment.