From e3a7ec219b88f8feec35ad07ceb229b94272774d Mon Sep 17 00:00:00 2001 From: Ciprian Jichici Date: Mon, 15 Jan 2024 20:13:57 +0200 Subject: [PATCH] Add logging to Agent Factory --- src/dotnet/AgentFactory/Agents/AgentBase.cs | 14 +------------- .../AgentFactory/Agents/AgentBuilder.cs | 19 ++++++++++++++++--- .../AgentFactory/Agents/DefaultAgent.cs | 13 ++++++++++++- .../AgentFactory/Interfaces/IWarmupService.cs | 15 +++++++++++++++ .../Services/AgentFactoryService.cs | 14 +++++++++----- .../AgentFactory/Services/WarmupService.cs | 19 +++++++++++++++++++ src/dotnet/AgentFactoryAPI/Program.cs | 4 +--- 7 files changed, 73 insertions(+), 25 deletions(-) create mode 100644 src/dotnet/AgentFactory/Interfaces/IWarmupService.cs create mode 100644 src/dotnet/AgentFactory/Services/WarmupService.cs diff --git a/src/dotnet/AgentFactory/Agents/AgentBase.cs b/src/dotnet/AgentFactory/Agents/AgentBase.cs index 04070823e5..77e9ed953b 100644 --- a/src/dotnet/AgentFactory/Agents/AgentBase.cs +++ b/src/dotnet/AgentFactory/Agents/AgentBase.cs @@ -1,17 +1,7 @@ using FoundationaLLM.AgentFactory.Core.Interfaces; using FoundationaLLM.AgentFactory.Core.Models.Messages; -using FoundationaLLM.AgentFactory.Core.Models.Orchestration.DataSourceConfigurations; -using FoundationaLLM.AgentFactory.Core.Models.Orchestration.Metadata; -using FoundationaLLM.AgentFactory.Core.Models.Orchestration; using FoundationaLLM.AgentFactory.Interfaces; -using FoundationaLLM.AgentFactory.Models.Orchestration; using FoundationaLLM.Common.Models.Orchestration; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Runtime.CompilerServices; namespace FoundationaLLM.AgentFactory.Core.Agents { @@ -63,10 +53,8 @@ public AgentBase( /// This will setup the agent based on its metadata. /// /// - public virtual async Task Configure(string userPrompt, string sessionId) - { + public virtual async Task Configure(string userPrompt, string sessionId) => await Task.CompletedTask; - } /// /// The call to execute a completion after the agent is configured. diff --git a/src/dotnet/AgentFactory/Agents/AgentBuilder.cs b/src/dotnet/AgentFactory/Agents/AgentBuilder.cs index 8a58280f3b..c9d79874b6 100644 --- a/src/dotnet/AgentFactory/Agents/AgentBuilder.cs +++ b/src/dotnet/AgentFactory/Agents/AgentBuilder.cs @@ -4,6 +4,7 @@ using FoundationaLLM.AgentFactory.Models.Orchestration; using FoundationaLLM.Common.Interfaces; using FoundationaLLM.Common.Models.Cache; +using Microsoft.Extensions.Logging; namespace FoundationaLLM.AgentFactory.Core.Agents { @@ -23,6 +24,7 @@ public class AgentBuilder /// /// /// + /// The logger factory used to create new loggers. /// /// public static async Task Build( @@ -33,8 +35,16 @@ public static async Task Build( IAgentHubAPIService agentHubAPIService, IEnumerable orchestrationServices, IPromptHubAPIService promptHubAPIService, - IDataSourceHubAPIService dataSourceHubAPIService) + IDataSourceHubAPIService dataSourceHubAPIService, + ILoggerFactory loggerFactory) { + var logger = loggerFactory.CreateLogger(); + if (callContext.AgentHint == null) + logger.LogInformation("The AgentBuilder is starting to build an agent without an agent hint."); + else + logger.LogInformation("The AgentBuilder is starting to build an agent with the following agent hint: {AgentName},{IsPrivateAgent}.", + callContext.AgentHint.Name, callContext.AgentHint.Private); + var agentResponse = callContext.AgentHint != null ? await cacheService.Get( new CacheKey(callContext.AgentHint.Name!, "agent"), @@ -45,6 +55,9 @@ public static async Task Build( var agentInfo = agentResponse!.Agent; + logger.LogInformation("The AgentBuilder received the following agent from the AgentHub: {AgentName}.", + agentResponse.Agent!.Name); + // TODO: Extend the Agent Hub API service response to include the orchestrator var orchestrationType = string.IsNullOrWhiteSpace(agentResponse.Agent!.Orchestrator) ? "LangChain" @@ -54,13 +67,13 @@ public static async Task Build( if (!validType) throw new ArgumentException($"The agent factory does not support the {orchestrationType} orchestration type."); var orchestrationService = SelectOrchestrationService(llmOrchestrationType, orchestrationServices); - AgentBase? agent = null; agent = new DefaultAgent( agentInfo!, cacheService, callContext, - orchestrationService, promptHubAPIService, dataSourceHubAPIService); + orchestrationService, promptHubAPIService, dataSourceHubAPIService, + loggerFactory.CreateLogger()); await agent.Configure(userPrompt, sessionId); diff --git a/src/dotnet/AgentFactory/Agents/DefaultAgent.cs b/src/dotnet/AgentFactory/Agents/DefaultAgent.cs index abfecf807b..68f54d3ae0 100644 --- a/src/dotnet/AgentFactory/Agents/DefaultAgent.cs +++ b/src/dotnet/AgentFactory/Agents/DefaultAgent.cs @@ -9,6 +9,7 @@ using FoundationaLLM.AgentFactory.Core.Services; using FoundationaLLM.Common.Models.Cache; using FoundationaLLM.Common.Models.Context; +using Microsoft.Extensions.Logging; namespace FoundationaLLM.AgentFactory.Core.Agents { @@ -20,6 +21,7 @@ public class DefaultAgent : AgentBase private LLMOrchestrationCompletionRequest _completionRequestTemplate = null!; private readonly ICacheService _cacheService; private readonly ICallContext _callContext; + private readonly ILogger _logger; /// /// Constructor for default agent. @@ -30,17 +32,20 @@ public class DefaultAgent : AgentBase /// /// /// + /// The logger used for logging. public DefaultAgent( AgentMetadata agentMetadata, ICacheService cacheService, ICallContext callContext, ILLMOrchestrationService orchestrationService, IPromptHubAPIService promptHubService, - IDataSourceHubAPIService dataSourceHubService) + IDataSourceHubAPIService dataSourceHubService, + ILogger logger) : base(agentMetadata, orchestrationService, promptHubService, dataSourceHubService) { _cacheService = cacheService; _callContext = callContext; + _logger = logger; } /// @@ -69,6 +74,9 @@ public override async Task Configure(string userPrompt, string sessionId) sessionId ); + _logger.LogInformation("The DefaultAgent received the following prompt from the Prompt Hub: {PromptName}.", + promptResponse!.Prompt!.Name); + // Get data sources listed for the agent. var dataSourceResponse = _callContext.AgentHint != null ? await _cacheService.Get( @@ -78,6 +86,9 @@ public override async Task Configure(string userPrompt, string sessionId) TimeSpan.FromHours(1)) : await _dataSourceHubService.ResolveRequest(_agentMetadata.AllowedDataSourceNames!, sessionId); + _logger.LogInformation("The DefaultAgent received the following data sources from the Data Source Hub: {DataSourceList}.", + string.Join(",", dataSourceResponse!.DataSources!.Select(ds => ds.Name))); + List dataSourceMetadata = new List(); var dataSources = dataSourceResponse!.DataSources!; diff --git a/src/dotnet/AgentFactory/Interfaces/IWarmupService.cs b/src/dotnet/AgentFactory/Interfaces/IWarmupService.cs new file mode 100644 index 0000000000..dfb039c2c7 --- /dev/null +++ b/src/dotnet/AgentFactory/Interfaces/IWarmupService.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FoundationaLLM.AgentFactory.Core.Interfaces +{ + /// + /// Defines + /// + public interface IWarmupService + { + } +} diff --git a/src/dotnet/AgentFactory/Services/AgentFactoryService.cs b/src/dotnet/AgentFactory/Services/AgentFactoryService.cs index 9e7d67a15c..4bf94f855f 100644 --- a/src/dotnet/AgentFactory/Services/AgentFactoryService.cs +++ b/src/dotnet/AgentFactory/Services/AgentFactoryService.cs @@ -28,6 +28,7 @@ public class AgentFactoryService : IAgentFactoryService private readonly IDataSourceHubAPIService _dataSourceHubAPIService; private readonly ILogger _logger; + private readonly ILoggerFactory _loggerFactory; /// /// Constructor for the Agent Factory Service. @@ -38,7 +39,7 @@ public class AgentFactoryService : IAgentFactoryService /// /// /// - /// + /// The logger factory used to create loggers. public AgentFactoryService( IEnumerable orchestrationServices, ICacheService cacheService, @@ -46,7 +47,7 @@ public AgentFactoryService( IAgentHubAPIService agentHubService, IPromptHubAPIService promptHubService, IDataSourceHubAPIService dataSourceHubService, - ILogger logger) + ILoggerFactory loggerFactory) { _orchestrationServices = orchestrationServices; _cacheService = cacheService; @@ -55,7 +56,8 @@ public AgentFactoryService( _promptHubAPIService = promptHubService; _dataSourceHubAPIService = dataSourceHubService; - _logger = logger; + _loggerFactory = loggerFactory; + _logger = _loggerFactory.CreateLogger(); } /// @@ -90,7 +92,8 @@ public async Task GetCompletion(CompletionRequest completion _agentHubAPIService, _orchestrationServices, _promptHubAPIService, - _dataSourceHubAPIService); + _dataSourceHubAPIService, + _loggerFactory); return await agent.GetCompletion(completionRequest); } @@ -123,7 +126,8 @@ public async Task GetSummary(SummaryRequest summaryRequest) _agentHubAPIService, _orchestrationServices, _promptHubAPIService, - _dataSourceHubAPIService); + _dataSourceHubAPIService, + _loggerFactory); return await agent.GetSummary(summaryRequest); } diff --git a/src/dotnet/AgentFactory/Services/WarmupService.cs b/src/dotnet/AgentFactory/Services/WarmupService.cs new file mode 100644 index 0000000000..88aefd729e --- /dev/null +++ b/src/dotnet/AgentFactory/Services/WarmupService.cs @@ -0,0 +1,19 @@ +using FoundationaLLM.AgentFactory.Core.Interfaces; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FoundationaLLM.AgentFactory.Core.Services +{ + /// + /// Implements warmup capabilities + /// + public class WarmupService( + IAgentHubAPIService agentHubService, + IPromptHubAPIService promptHubService, + IDataSourceHubAPIService dataSourceHubService) : IWarmupService + { + } +} diff --git a/src/dotnet/AgentFactoryAPI/Program.cs b/src/dotnet/AgentFactoryAPI/Program.cs index fd0cf97ac8..19d4c21bfa 100644 --- a/src/dotnet/AgentFactoryAPI/Program.cs +++ b/src/dotnet/AgentFactoryAPI/Program.cs @@ -1,4 +1,3 @@ -using System.Net; using Asp.Versioning; using Azure.Identity; using FoundationaLLM.AgentFactory.Core.Interfaces; @@ -17,7 +16,6 @@ using FoundationaLLM.Common.Services; using FoundationaLLM.Common.Settings; using Microsoft.ApplicationInsights.AspNetCore.Extensions; -using Microsoft.Extensions.Http.Resilience; using Microsoft.Extensions.Options; using Polly; using Swashbuckle.AspNetCore.SwaggerGen; @@ -92,6 +90,7 @@ public static void Main(string[] args) builder.Services.AddScoped(); builder.Services.AddSingleton(); + builder.Services.ActivateSingleton(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); @@ -105,7 +104,6 @@ public static void Main(string[] args) // Register the downstream services and HTTP clients. RegisterDownstreamServices(builder); - builder.Services .AddApiVersioning(options => {