Merge pull request #1018 from hchen2020/master

Remove unnecessary agent load
This commit is contained in:
Haiping 2025-04-18 20:48:04 -05:00 committed by GitHub
commit 3e33be356e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 27 additions and 26 deletions

View file

@ -4,6 +4,7 @@ public class RealtimeModelSettings
{
public string Provider { get; set; } = "openai";
public string Model { get; set; } = "gpt-4o-mini-realtime-preview";
public string[] Modalities { get; set; } = ["text", "audio"];
public bool InterruptResponse { get; set; } = true;
public string InputAudioFormat { get; set; } = "g711_ulaw";
public string OutputAudioFormat { get; set; } = "g711_ulaw";

View file

@ -5,9 +5,7 @@ namespace BotSharp.Core.Agents.Services;
public partial class AgentService
{
#if !DEBUG
[SharpCache(10)]
#endif
public async Task<PagedItems<Agent>> GetAgents(AgentFilter filter)
{
var agents = _db.GetAgents(filter);
@ -27,9 +25,7 @@ public partial class AgentService
};
}
#if !DEBUG
[SharpCache(10)]
#endif
public async Task<List<IdName>> GetAgentOptions(List<string>? agentIds)
{
var agents = _db.GetAgents(new AgentFilter
@ -39,9 +35,7 @@ public partial class AgentService
return agents?.Select(x => new IdName(x.Id, x.Name))?.OrderBy(x => x.Name)?.ToList() ?? [];
}
#if !DEBUG
[SharpCache(10)]
#endif
public async Task<Agent> GetAgent(string id)
{
var profile = _db.GetAgent(id);

View file

@ -86,7 +86,7 @@ public class ConversationStateService : IConversationStateService
preValue = prevLeafNode?.Data ?? string.Empty;
}
_logger.LogInformation($"[STATE] {name} = {value}");
_logger.LogDebug($"[STATE] {name} = {value}");
var routingCtx = _services.GetRequiredService<IRoutingContext>();
var isNoChange = ContainsState(name)
@ -221,7 +221,7 @@ public class ConversationStateService : IConversationStateService
var data = leafNode.Data ?? string.Empty;
endNodes[state.Key] = data;
_logger.LogInformation($"[STATE] {key} : {data}");
_logger.LogDebug($"[STATE] {key} : {data}");
}
_logger.LogInformation($"Loaded conversation states: {conversationId}");

View file

@ -129,7 +129,7 @@ public class RoutingContext : IRoutingContext
// Run the routing rule
var agency = _services.GetRequiredService<IAgentService>();
var agent = agency.LoadAgent(currentAgentId).Result;
var agent = agency.GetAgent(currentAgentId).Result;
var message = new RoleDialogModel(AgentRole.User, $"Try to route to agent {agent.Name}")
{

View file

@ -146,7 +146,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
if (message.FunctionName == "route_to_agent") return;
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
var agent = await _agentService.GetAgent(message.CurrentAgentId);
message.FunctionArgs = message.FunctionArgs ?? "{}";
var args = message.FunctionArgs.FormatJson();
var log = $"*{message.Indication.Replace("\r", string.Empty).Replace("\n", string.Empty)}* \r\n\r\n **{message.FunctionName}**()";
@ -169,7 +169,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
if (message.FunctionName == "route_to_agent") return;
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
var agent = await _agentService.GetAgent(message.CurrentAgentId);
message.FunctionArgs = message.FunctionArgs ?? "{}";
var log = $"{message.FunctionName} =>\r\n*{message.Content?.Trim()}*";
@ -196,7 +196,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
var conversationId = _state.GetConversationId();
if (string.IsNullOrEmpty(conversationId)) return;
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
var agent = await _agentService.GetAgent(message.CurrentAgentId);
var log = tokenStats.Prompt;
@ -226,7 +226,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
if (message.Role == AgentRole.Assistant)
{
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
var agent = await _agentService.GetAgent(message.CurrentAgentId);
var log = $"{GetMessageContent(message)}";
if (message.RichContent != null || message.SecondaryRichContent != null)
{
@ -251,7 +251,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
if (string.IsNullOrEmpty(conversationId)) return;
var log = $"{GetMessageContent(message)}";
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
var agent = await _agentService.GetAgent(message.CurrentAgentId);
var input = new ContentLogInputModel(conversationId, message)
{
@ -268,7 +268,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
if (string.IsNullOrEmpty(conversationId)) return;
var log = $"Conversation ended";
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
var agent = await _agentService.GetAgent(message.CurrentAgentId);
var input = new ContentLogInputModel(conversationId, message)
{
@ -290,7 +290,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
}
var routing = _services.GetRequiredService<IRoutingService>();
var agentId = routing.Context.GetCurrentAgentId();
var agent = await _agentService.LoadAgent(agentId);
var agent = await _agentService.GetAgent(agentId);
var input = new ContentLogInputModel()
{
@ -324,7 +324,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
var conversationId = _state.GetConversationId();
if (string.IsNullOrEmpty(conversationId)) return;
var agent = await _agentService.LoadAgent(agentId);
var agent = await _agentService.GetAgent(agentId);
// Agent queue log
var log = $"{agent.Name} is enqueued";
@ -351,8 +351,8 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
var conversationId = _state.GetConversationId();
if (string.IsNullOrEmpty(conversationId)) return;
var agent = await _agentService.LoadAgent(agentId);
var currentAgent = await _agentService.LoadAgent(currentAgentId);
var agent = await _agentService.GetAgent(agentId);
var currentAgent = await _agentService.GetAgent(currentAgentId);
// Agent queue log
var log = $"{agent.Name} is dequeued";
@ -379,8 +379,8 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
var conversationId = _state.GetConversationId();
if (string.IsNullOrEmpty(conversationId)) return;
var fromAgent = await _agentService.LoadAgent(fromAgentId);
var toAgent = await _agentService.LoadAgent(toAgentId);
var fromAgent = await _agentService.GetAgent(fromAgentId);
var toAgent = await _agentService.GetAgent(toAgentId);
// Agent queue log
var log = $"Agent queue is replaced from {fromAgent.Name} to {toAgent.Name}";
@ -432,7 +432,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
var conversationId = _state.GetConversationId();
if (string.IsNullOrEmpty(conversationId)) return;
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
var agent = await _agentService.GetAgent(message.CurrentAgentId);
var log = JsonSerializer.Serialize(instruct, _options.JsonSerializerOptions);
log = $"```json\r\n{log}\r\n```";
@ -451,7 +451,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
var conversationId = _state.GetConversationId();
if (string.IsNullOrEmpty(conversationId)) return;
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
var agent = await _agentService.GetAgent(message.CurrentAgentId);
var log = $"Revised user goal agent to {instruct.OriginalAgent}";
var input = new ContentLogInputModel(conversationId, message)

View file

@ -258,7 +258,7 @@ public class RealTimeCompletionProvider : IRealTimeCompletion
Instructions = instruction,
ToolChoice = "auto",
Tools = functions,
Modalities = [ "text", "audio" ],
Modalities = realtimeModelSettings.Modalities,
Temperature = Math.Max(options.Temperature ?? realtimeModelSettings.Temperature, 0.6f),
MaxResponseOutputTokens = realtimeModelSettings.MaxResponseOutputTokens,
TurnDetection = new RealtimeSessionTurnDetection

View file

@ -44,7 +44,7 @@ public class HangupPhoneCallFn : IFunctionCallback
var processUrl = $"{_twilioSetting.CallbackHost}/twilio/voice/hang-up?agent-id={message.CurrentAgentId}&conversation-id={conversationId}";
// Generate initial assistant audio
string initAudioFile = null;
/*string initAudioFile = null;
if (!string.IsNullOrEmpty(args.ResponseContent))
{
var completion = CompletionProvider.GetAudioSynthesizer(_services);
@ -53,7 +53,7 @@ public class HangupPhoneCallFn : IFunctionCallback
fileStorage.SaveSpeechFile(conversationId, initAudioFile, data);
processUrl += $"&init-audio-file={initAudioFile}";
}
}*/
var call = CallResource.Update(
url: new Uri(processUrl),

View file

@ -139,6 +139,12 @@ public class TwilioService
response.Play(new Uri(uri));
}
}
else
{
response.Pause(5);
response.Say("Goodbye.");
}
response.Hangup();
return response;
}