diff --git a/src/dotnet/Agent/ResourceProviders/AgentResourceProviderService.cs b/src/dotnet/Agent/ResourceProviders/AgentResourceProviderService.cs index 8e252c977..7f2a90a67 100644 --- a/src/dotnet/Agent/ResourceProviders/AgentResourceProviderService.cs +++ b/src/dotnet/Agent/ResourceProviders/AgentResourceProviderService.cs @@ -17,6 +17,7 @@ using FoundationaLLM.Common.Models.ResourceProviders; using FoundationaLLM.Common.Models.ResourceProviders.Agent; using FoundationaLLM.Common.Models.ResourceProviders.Agent.AgentAccessTokens; +using FoundationaLLM.Common.Models.ResourceProviders.Agent.AgentWorkflows; using FoundationaLLM.Common.Models.ResourceProviders.AIModel; using FoundationaLLM.Common.Models.ResourceProviders.Configuration; using FoundationaLLM.Common.Models.ResourceProviders.Prompt; @@ -220,7 +221,7 @@ private async Task UpdateAgent(ResourcePath resour DataSourceObjectId = kmAgent.Vectorization.DataSourceObjectId!, TextPartitioningProfileObjectId = kmAgent.Vectorization.TextPartitioningProfileObjectId!, TextEmbeddingProfileObjectId = kmAgent.Vectorization.TextEmbeddingProfileObjectId!, - IndexingProfileObjectId = kmAgent.Vectorization.IndexingProfileObjectIds[0]!, + IndexingProfileObjectId = kmAgent.Vectorization.IndexingProfileObjectIds![0]!, TriggerType = (VectorizationPipelineTriggerType)kmAgent.Vectorization.TriggerType!, TriggerCronSchedule = kmAgent.Vectorization.TriggerCronSchedule }), @@ -235,13 +236,52 @@ private async Task UpdateAgent(ResourcePath resour StatusCodes.Status500InternalServerError); } - if (agent.HasCapability(AgentCapabilityCategoryNames.OpenAIAssistants)) + if (agent.HasCapability(AgentCapabilityCategoryNames.OpenAIAssistants) + || (agent.Workflow != null && agent.Workflow is AzureOpenAIAssistantsAgentWorkflow)) { agent.Properties ??= []; var openAIAssistantId = agent.Properties.GetValueOrDefault( AgentPropertyNames.AzureOpenAIAssistantId); + var workflow = agent.Workflow as AzureOpenAIAssistantsAgentWorkflow; + + if (workflow != null) + { + openAIAssistantId = workflow.AssistantId; + } + + #region Resolve various agent properties + + AIModelBase agentAIModel = null!; + APIEndpointConfiguration agentAIModelAPIEndpoint = null!; + PromptBase agentPrompt = null!; + + if (workflow == null) + { + // Legacy path + agentAIModel = await GetResourceProviderServiceByName(ResourceProviderNames.FoundationaLLM_AIModel) + .GetResourceAsync(agent.AIModelObjectId!, userIdentity); + agentPrompt = await GetResourceProviderServiceByName(ResourceProviderNames.FoundationaLLM_Prompt) + .GetResourceAsync(agent.PromptObjectId!, userIdentity); + } + else + { + agentAIModel = await GetResourceProviderServiceByName(ResourceProviderNames.FoundationaLLM_AIModel) + .GetResourceAsync(workflow.MainAIModelObjectId!, userIdentity); + agentPrompt = await GetResourceProviderServiceByName(ResourceProviderNames.FoundationaLLM_Prompt) + .GetResourceAsync(workflow.MainPromptObjectId!, userIdentity); + } + agentAIModelAPIEndpoint = await GetResourceProviderServiceByName(ResourceProviderNames.FoundationaLLM_Configuration) + .GetResourceAsync(agentAIModel.EndpointObjectId!, userIdentity); + + #endregion + + var gatewayClient = new GatewayServiceClient( + await _serviceProvider.GetRequiredService() + .CreateClient(HttpClientNames.GatewayAPI, userIdentity), + _serviceProvider.GetRequiredService>()); + if (string.IsNullOrWhiteSpace(openAIAssistantId)) { // The agent uses the Azure OpenAI Assistants workflow @@ -252,23 +292,7 @@ private async Task UpdateAgent(ResourcePath resour "Starting to create the Azure OpenAI assistant for agent {AgentName}", agent.Name); - #region Resolve various agent properties - - var agentAIModel = await GetResourceProviderServiceByName(ResourceProviderNames.FoundationaLLM_AIModel) - .GetResourceAsync(agent.AIModelObjectId!, userIdentity); - var agentAIModelAPIEndpoint = await GetResourceProviderServiceByName(ResourceProviderNames.FoundationaLLM_Configuration) - .GetResourceAsync(agentAIModel.EndpointObjectId!, userIdentity); - var agentPrompt = await GetResourceProviderServiceByName(ResourceProviderNames.FoundationaLLM_Prompt) - .GetResourceAsync(agent.PromptObjectId!, userIdentity); - - #endregion - - #region Create Azure OpenAI Assistants assistant - - var gatewayClient = new GatewayServiceClient( - await _serviceProvider.GetRequiredService() - .CreateClient(HttpClientNames.GatewayAPI, userIdentity), - _serviceProvider.GetRequiredService>()); + #region Create Azure OpenAI Assistants assistant Dictionary parameters = new() { @@ -297,8 +321,17 @@ await _serviceProvider.GetRequiredService() _logger.LogInformation( "The Azure OpenAI assistant {AssistantId} for agent {AgentName} was created successfuly.", newOpenAIAssistantId, agent.Name); - agent.Properties[AgentPropertyNames.AzureOpenAIAssistantId] = newOpenAIAssistantId; + if (workflow == null) + { + // Legacy path + agent.Properties[AgentPropertyNames.AzureOpenAIAssistantId] = newOpenAIAssistantId; + } + else + { + // Workflow path + workflow.AssistantId = newOpenAIAssistantId; + } #endregion } } diff --git a/src/dotnet/Common/Models/ResourceProviders/Agent/AgentWorkflows/AgentWorkflowBase.cs b/src/dotnet/Common/Models/ResourceProviders/Agent/AgentWorkflows/AgentWorkflowBase.cs index 7117db71c..a4c8c9961 100644 --- a/src/dotnet/Common/Models/ResourceProviders/Agent/AgentWorkflows/AgentWorkflowBase.cs +++ b/src/dotnet/Common/Models/ResourceProviders/Agent/AgentWorkflows/AgentWorkflowBase.cs @@ -60,5 +60,15 @@ public class AgentWorkflowBase .FirstOrDefault( roid => roid.HasObjectRole(ResourceObjectIdPropertyValues.MainModel)) ?.ObjectId; + + /// + /// Gets the main prompt object identifier. + /// + [JsonIgnore] + public string? MainPromptObjectId => + ResourceObjectIds.Values + .FirstOrDefault( + roid => roid.HasObjectRole(ResourceObjectIdPropertyValues.MainPrompt)) + ?.ObjectId; } }