Skip to content

Commit

Permalink
Merge branch 'main' into patch-2
Browse files Browse the repository at this point in the history
  • Loading branch information
sonichi authored Jul 13, 2024
2 parents 801033f + 24d509c commit 6d2a077
Showing 1 changed file with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,48 @@ public class MiddlewareAgentCodeSnippet
public async Task CreateMiddlewareAgentAsync()
{
#region create_middleware_agent_with_original_agent
// Create an agent that always replies "Hello World"
IAgent agent = new DefaultReplyAgent(name: "assistant", defaultReply: "Hello World");
// Create an agent that always replies "Hi!"
IAgent agent = new DefaultReplyAgent(name: "assistant", defaultReply: "Hi!");

// Create a middleware agent on top of default reply agent
var middlewareAgent = new MiddlewareAgent(innerAgent: agent);
middlewareAgent.Use(async (messages, options, agent, ct) =>
{
var lastMessage = messages.Last() as TextMessage;
lastMessage.Content = $"[middleware 0] {lastMessage.Content}";
if (lastMessage != null && lastMessage.Content.Contains("Hello World"))
{
lastMessage.Content = $"[middleware 0] {lastMessage.Content}";
return lastMessage;
}

return await agent.GenerateReplyAsync(messages, options, ct);
});

var reply = await middlewareAgent.SendAsync("Hello World");
reply.GetContent().Should().Be("[middleware 0] Hello World");
reply = await middlewareAgent.SendAsync("Hello AI!");
reply.GetContent().Should().Be("Hi!");
#endregion create_middleware_agent_with_original_agent

#region register_middleware_agent
middlewareAgent = agent.RegisterMiddleware(async (messages, options, agent, ct) =>
{
var lastMessage = messages.Last() as TextMessage;
lastMessage.Content = $"[middleware 0] {lastMessage.Content}";
if (lastMessage != null && lastMessage.Content.Contains("Hello World"))
{
lastMessage.Content = $"[middleware 0] {lastMessage.Content}";
return lastMessage;
}

return await agent.GenerateReplyAsync(messages, options, ct);
});
#endregion register_middleware_agent

#region short_circuit_middleware_agent
// This middleware will short circuit the agent and return the last message directly.
// This middleware will short circuit the agent and return a message directly.
middlewareAgent.Use(async (messages, options, agent, ct) =>
{
var lastMessage = messages.Last() as TextMessage;
lastMessage.Content = $"[middleware shortcut]";
return lastMessage;
return new TextMessage(Role.Assistant, $"[middleware shortcut]");
});
#endregion short_circuit_middleware_agent
}
Expand Down

0 comments on commit 6d2a077

Please sign in to comment.