Skip to content

Commit

Permalink
Merge pull request #472 from solliancenet/cj-agent-factory-logging
Browse files Browse the repository at this point in the history
Add logging to Agent Factory
  • Loading branch information
joelhulen authored Jan 15, 2024
2 parents 2920eb9 + e3a7ec2 commit cbafd48
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 25 deletions.
14 changes: 1 addition & 13 deletions src/dotnet/AgentFactory/Agents/AgentBase.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -63,10 +53,8 @@ public AgentBase(
/// This will setup the agent based on its metadata.
/// </summary>
/// <returns></returns>
public virtual async Task Configure(string userPrompt, string sessionId)
{
public virtual async Task Configure(string userPrompt, string sessionId) =>
await Task.CompletedTask;
}

/// <summary>
/// The call to execute a completion after the agent is configured.
Expand Down
19 changes: 16 additions & 3 deletions src/dotnet/AgentFactory/Agents/AgentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -23,6 +24,7 @@ public class AgentBuilder
/// <param name="orchestrationServices"></param>
/// <param name="promptHubAPIService"></param>
/// <param name="dataSourceHubAPIService"></param>
/// <param name="loggerFactory">The logger factory used to create new loggers.</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static async Task<AgentBase> Build(
Expand All @@ -33,8 +35,16 @@ public static async Task<AgentBase> Build(
IAgentHubAPIService agentHubAPIService,
IEnumerable<ILLMOrchestrationService> orchestrationServices,
IPromptHubAPIService promptHubAPIService,
IDataSourceHubAPIService dataSourceHubAPIService)
IDataSourceHubAPIService dataSourceHubAPIService,
ILoggerFactory loggerFactory)
{
var logger = loggerFactory.CreateLogger<AgentBuilder>();
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<AgentHubResponse>(
new CacheKey(callContext.AgentHint.Name!, "agent"),
Expand All @@ -45,6 +55,9 @@ public static async Task<AgentBase> 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"
Expand All @@ -54,13 +67,13 @@ public static async Task<AgentBase> 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<DefaultAgent>());

await agent.Configure(userPrompt, sessionId);

Expand Down
13 changes: 12 additions & 1 deletion src/dotnet/AgentFactory/Agents/DefaultAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -20,6 +21,7 @@ public class DefaultAgent : AgentBase
private LLMOrchestrationCompletionRequest _completionRequestTemplate = null!;
private readonly ICacheService _cacheService;
private readonly ICallContext _callContext;
private readonly ILogger<DefaultAgent> _logger;

/// <summary>
/// Constructor for default agent.
Expand All @@ -30,17 +32,20 @@ public class DefaultAgent : AgentBase
/// <param name="orchestrationService"></param>
/// <param name="promptHubService"></param>
/// <param name="dataSourceHubService"></param>
/// <param name="logger">The logger used for logging.</param>
public DefaultAgent(
AgentMetadata agentMetadata,
ICacheService cacheService,
ICallContext callContext,
ILLMOrchestrationService orchestrationService,
IPromptHubAPIService promptHubService,
IDataSourceHubAPIService dataSourceHubService)
IDataSourceHubAPIService dataSourceHubService,
ILogger<DefaultAgent> logger)
: base(agentMetadata, orchestrationService, promptHubService, dataSourceHubService)
{
_cacheService = cacheService;
_callContext = callContext;
_logger = logger;
}

/// <summary>
Expand Down Expand Up @@ -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<DataSourceHubResponse>(
Expand All @@ -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<MetadataBase> dataSourceMetadata = new List<MetadataBase>();

var dataSources = dataSourceResponse!.DataSources!;
Expand Down
15 changes: 15 additions & 0 deletions src/dotnet/AgentFactory/Interfaces/IWarmupService.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Defines
/// </summary>
public interface IWarmupService
{
}
}
14 changes: 9 additions & 5 deletions src/dotnet/AgentFactory/Services/AgentFactoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class AgentFactoryService : IAgentFactoryService
private readonly IDataSourceHubAPIService _dataSourceHubAPIService;

private readonly ILogger<AgentFactoryService> _logger;
private readonly ILoggerFactory _loggerFactory;

/// <summary>
/// Constructor for the Agent Factory Service.
Expand All @@ -38,15 +39,15 @@ public class AgentFactoryService : IAgentFactoryService
/// <param name="agentHubService"></param>
/// <param name="promptHubService"></param>
/// <param name="dataSourceHubService"></param>
/// <param name="logger"></param>
/// <param name="loggerFactory">The logger factory used to create loggers.</param>
public AgentFactoryService(
IEnumerable<ILLMOrchestrationService> orchestrationServices,
ICacheService cacheService,
ICallContext callContext,
IAgentHubAPIService agentHubService,
IPromptHubAPIService promptHubService,
IDataSourceHubAPIService dataSourceHubService,
ILogger<AgentFactoryService> logger)
ILoggerFactory loggerFactory)
{
_orchestrationServices = orchestrationServices;
_cacheService = cacheService;
Expand All @@ -55,7 +56,8 @@ public AgentFactoryService(
_promptHubAPIService = promptHubService;
_dataSourceHubAPIService = dataSourceHubService;

_logger = logger;
_loggerFactory = loggerFactory;
_logger = _loggerFactory.CreateLogger<AgentFactoryService>();
}

/// <summary>
Expand Down Expand Up @@ -90,7 +92,8 @@ public async Task<CompletionResponse> GetCompletion(CompletionRequest completion
_agentHubAPIService,
_orchestrationServices,
_promptHubAPIService,
_dataSourceHubAPIService);
_dataSourceHubAPIService,
_loggerFactory);

return await agent.GetCompletion(completionRequest);
}
Expand Down Expand Up @@ -123,7 +126,8 @@ public async Task<SummaryResponse> GetSummary(SummaryRequest summaryRequest)
_agentHubAPIService,
_orchestrationServices,
_promptHubAPIService,
_dataSourceHubAPIService);
_dataSourceHubAPIService,
_loggerFactory);

return await agent.GetSummary(summaryRequest);
}
Expand Down
19 changes: 19 additions & 0 deletions src/dotnet/AgentFactory/Services/WarmupService.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Implements warmup capabilities
/// </summary>
public class WarmupService(
IAgentHubAPIService agentHubService,
IPromptHubAPIService promptHubService,
IDataSourceHubAPIService dataSourceHubService) : IWarmupService
{
}
}
4 changes: 1 addition & 3 deletions src/dotnet/AgentFactoryAPI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Net;
using Asp.Versioning;
using Azure.Identity;
using FoundationaLLM.AgentFactory.Core.Interfaces;
Expand All @@ -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;
Expand Down Expand Up @@ -92,6 +90,7 @@ public static void Main(string[] args)
builder.Services.AddScoped<ILLMOrchestrationService, LangChainService>();

builder.Services.AddSingleton<ICacheService, MemoryCacheService>();
builder.Services.ActivateSingleton<ICacheService>();
builder.Services.AddScoped<IAgentFactoryService, AgentFactoryService>();
builder.Services.AddScoped<IAgentHubAPIService, AgentHubAPIService>();
builder.Services.AddScoped<IDataSourceHubAPIService, DataSourceHubAPIService>();
Expand All @@ -105,7 +104,6 @@ public static void Main(string[] args)
// Register the downstream services and HTTP clients.
RegisterDownstreamServices(builder);


builder.Services
.AddApiVersioning(options =>
{
Expand Down

0 comments on commit cbafd48

Please sign in to comment.