Skip to content

Commit

Permalink
Merge pull request #2148 from solliancenet/cp-assistant-population
Browse files Browse the repository at this point in the history
Populate OpenAI Assistant information in workflow
  • Loading branch information
ciprianjichici authored Jan 22, 2025
2 parents 1e34367 + cf37d96 commit 7ce6b91
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 20 deletions.
73 changes: 53 additions & 20 deletions src/dotnet/Agent/ResourceProviders/AgentResourceProviderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -220,7 +221,7 @@ private async Task<ResourceProviderUpsertResult> 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
}),
Expand All @@ -235,13 +236,52 @@ private async Task<ResourceProviderUpsertResult> 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<AIModelBase>(agent.AIModelObjectId!, userIdentity);
agentPrompt = await GetResourceProviderServiceByName(ResourceProviderNames.FoundationaLLM_Prompt)
.GetResourceAsync<PromptBase>(agent.PromptObjectId!, userIdentity);
}
else
{
agentAIModel = await GetResourceProviderServiceByName(ResourceProviderNames.FoundationaLLM_AIModel)
.GetResourceAsync<AIModelBase>(workflow.MainAIModelObjectId!, userIdentity);
agentPrompt = await GetResourceProviderServiceByName(ResourceProviderNames.FoundationaLLM_Prompt)
.GetResourceAsync<PromptBase>(workflow.MainPromptObjectId!, userIdentity);
}
agentAIModelAPIEndpoint = await GetResourceProviderServiceByName(ResourceProviderNames.FoundationaLLM_Configuration)
.GetResourceAsync<APIEndpointConfiguration>(agentAIModel.EndpointObjectId!, userIdentity);

#endregion

var gatewayClient = new GatewayServiceClient(
await _serviceProvider.GetRequiredService<IHttpClientFactoryService>()
.CreateClient(HttpClientNames.GatewayAPI, userIdentity),
_serviceProvider.GetRequiredService<ILogger<GatewayServiceClient>>());

if (string.IsNullOrWhiteSpace(openAIAssistantId))
{
// The agent uses the Azure OpenAI Assistants workflow
Expand All @@ -252,23 +292,7 @@ private async Task<ResourceProviderUpsertResult> 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<AIModelBase>(agent.AIModelObjectId!, userIdentity);
var agentAIModelAPIEndpoint = await GetResourceProviderServiceByName(ResourceProviderNames.FoundationaLLM_Configuration)
.GetResourceAsync<APIEndpointConfiguration>(agentAIModel.EndpointObjectId!, userIdentity);
var agentPrompt = await GetResourceProviderServiceByName(ResourceProviderNames.FoundationaLLM_Prompt)
.GetResourceAsync<PromptBase>(agent.PromptObjectId!, userIdentity);

#endregion

#region Create Azure OpenAI Assistants assistant

var gatewayClient = new GatewayServiceClient(
await _serviceProvider.GetRequiredService<IHttpClientFactoryService>()
.CreateClient(HttpClientNames.GatewayAPI, userIdentity),
_serviceProvider.GetRequiredService<ILogger<GatewayServiceClient>>());
#region Create Azure OpenAI Assistants assistant

Dictionary<string, object> parameters = new()
{
Expand Down Expand Up @@ -297,8 +321,17 @@ await _serviceProvider.GetRequiredService<IHttpClientFactoryService>()
_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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,15 @@ public class AgentWorkflowBase
.FirstOrDefault(
roid => roid.HasObjectRole(ResourceObjectIdPropertyValues.MainModel))
?.ObjectId;

/// <summary>
/// Gets the main prompt object identifier.
/// </summary>
[JsonIgnore]
public string? MainPromptObjectId =>
ResourceObjectIds.Values
.FirstOrDefault(
roid => roid.HasObjectRole(ResourceObjectIdPropertyValues.MainPrompt))
?.ObjectId;
}
}

0 comments on commit 7ce6b91

Please sign in to comment.