Skip to content

Commit

Permalink
Merge pull request #12687 from PasanT9/llm-name
Browse files Browse the repository at this point in the history
Return LLM provider name and api version
  • Loading branch information
ashera96 authored Oct 22, 2024
2 parents 3fcf88a + ff42649 commit 239c237
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@
import java.util.stream.Collectors;

import static org.wso2.carbon.apimgt.impl.APIConstants.API_SUBTYPE_AI_API;
import static org.wso2.carbon.apimgt.impl.APIConstants.API_SUBTYPE_DEFAULT;
import static org.wso2.carbon.apimgt.impl.APIConstants.COMMERCIAL_TIER_PLAN;

/**
Expand Down Expand Up @@ -5355,7 +5354,7 @@ private void populateSubtypeConfiguration(API api) throws APIManagementException
apiUuid = apiRevision.getApiUUID();
revisionUuid = apiRevision.getRevisionUUID();
}
AIConfiguration configurations = apiMgtDAO.getAIConfiguration(apiUuid, revisionUuid, api.getOrganization());
AIConfiguration configurations = apiMgtDAO.getAIConfiguration(apiUuid, revisionUuid);
if (configurations != null) {
api.setAiConfiguration(configurations);
}
Expand Down Expand Up @@ -6001,8 +6000,7 @@ public String addAPIRevision(APIRevision apiRevision, String organization) throw

try {
apiMgtDAO.addAPIRevision(apiRevision);
AIConfiguration aiConfiguration = apiMgtDAO.getAIConfiguration(apiRevision.getApiUUID(), null,
organization);
AIConfiguration aiConfiguration = apiMgtDAO.getAIConfiguration(apiRevision.getApiUUID(), null);
if (aiConfiguration != null) {
addAIConfiguration(apiRevision.getApiUUID(), apiRevision.getRevisionUUID(), aiConfiguration,
organization);
Expand Down Expand Up @@ -6056,17 +6054,15 @@ public String addAPIRevision(APIRevision apiRevision, String organization) throw
private void addAIConfiguration(String uuid, String revisionUuid, AIConfiguration aiConfiguration,
String organization) throws APIManagementException {

String aiConfigurationId = UUID.randomUUID().toString();
String llmProviderId = aiConfiguration.getLlmProviderId();
String llmProviderId = resolveLlmProviderByNameAndApiVersion(aiConfiguration.getLlmProviderName(),
aiConfiguration.getLlmProviderApiVersion(), organization);

if (llmProviderId == null) {
llmProviderId = resolveLlmProviderId(aiConfiguration, organization);
} else {
validateLlmProviderById(llmProviderId, organization);
llmProviderId = aiConfiguration.getLlmProviderId();
}

if (llmProviderId != null) {
apiMgtDAO.addAIConfiguration(uuid, revisionUuid, llmProviderId, aiConfigurationId);
apiMgtDAO.addAIConfiguration(uuid, revisionUuid, llmProviderId, UUID.randomUUID().toString());
} else {
throw new APIManagementException("Invalid AI configuration: LLM provider details missing.");
}
Expand All @@ -6075,17 +6071,19 @@ private void addAIConfiguration(String uuid, String revisionUuid, AIConfiguratio
/**
* Resolves the LLM provider ID based on the provider's name and API version.
*
* @param aiConfiguration AI configuration object containing LLM provider details
* @param organization Organization to which the API belongs
* @param llmProviderName Name of the LLM Provider
* @param llmProviderApiVersion API version of the LLM Provider
* @param organization Organization to which the API belongs
* @return Resolved LLM provider ID or null if provider not found
* @throws APIManagementException if LLM provider is not found
*/
private String resolveLlmProviderId(AIConfiguration aiConfiguration, String organization)
private String resolveLlmProviderByNameAndApiVersion(String llmProviderName, String llmProviderApiVersion,
String organization)
throws APIManagementException {

if (aiConfiguration.getLlmProviderName() != null && aiConfiguration.getLlmProviderApiVersion() != null) {
if (llmProviderName != null && llmProviderApiVersion != null) {
LLMProvider provider = apiMgtDAO.getLLMProvider(organization,
aiConfiguration.getLlmProviderName(), aiConfiguration.getLlmProviderApiVersion());
llmProviderName, llmProviderApiVersion);
if (provider != null) {
return provider.getId();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19928,7 +19928,7 @@ public void deleteAIConfigurationRevision(String revisionUUID)
* @return The AIConfiguration object if found, or null if no configuration exists.
* @throws APIManagementException If an error occurs while retrieving the AI configuration.
*/
public AIConfiguration getAIConfiguration(String uuid, String revisionUUID, String organization)
public AIConfiguration getAIConfiguration(String uuid, String revisionUUID)
throws APIManagementException {


Expand All @@ -19947,6 +19947,8 @@ public AIConfiguration getAIConfiguration(String uuid, String revisionUUID, Stri
if (rs.next()) {
aiConfiguration = new AIConfiguration();
aiConfiguration.setLlmProviderId(rs.getString("LLM_PROVIDER_UUID"));
aiConfiguration.setLlmProviderName(rs.getString("NAME"));
aiConfiguration.setLlmProviderApiVersion(rs.getString("API_VERSION"));
}
}
connection.commit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2789,12 +2789,16 @@ public class SQLConstants {
"LLM_PROVIDER_UUID) VALUES (?, ?, ?, ?)";

public static final String GET_AI_CONFIGURATION =
"SELECT AI_CONFIGURATION_UUID, LLM_PROVIDER_UUID " +
"FROM AM_API_AI_CONFIGURATION WHERE API_UUID = ? AND API_REVISION_UUID IS NULL";
"SELECT A.AI_CONFIGURATION_UUID, A.LLM_PROVIDER_UUID, P.NAME, P.API_VERSION " +
"FROM AM_API_AI_CONFIGURATION A " +
"JOIN AM_LLM_PROVIDER P ON A.LLM_PROVIDER_UUID = P.UUID " +
"WHERE A.API_UUID = ? AND A.API_REVISION_UUID IS NULL";

public static final String GET_AI_CONFIGURATION_REVISION =
"SELECT AI_CONFIGURATION_UUID, LLM_PROVIDER_UUID " +
"FROM AM_API_AI_CONFIGURATION WHERE API_UUID = ? AND API_REVISION_UUID = ?";
"SELECT A.AI_CONFIGURATION_UUID, A.LLM_PROVIDER_UUID, P.NAME, P.API_VERSION " +
"FROM AM_API_AI_CONFIGURATION A " +
"JOIN AM_LLM_PROVIDER P ON A.LLM_PROVIDER_UUID = P.UUID " +
"WHERE A.API_UUID = ? AND A.API_REVISION_UUID = ?";

public static final String DELETE_AI_CONFIGURATION_REVISION =
"DELETE FROM AM_API_AI_CONFIGURATION WHERE API_REVISION_UUID = ?";
Expand Down

0 comments on commit 239c237

Please sign in to comment.