Class MiddlewareAgent
An agent that allows you to add middleware and modify the behavior of an existing agent.
public class MiddlewareAgent : IMiddlewareAgent, IAgent, IAgentMetaInformation
- Inheritance
-
MiddlewareAgent
- Implements
- Derived
- Inherited Members
- Extension Methods
Constructors
MiddlewareAgent(IAgent, string?, IEnumerable<IMiddleware>?)
Create a new instance of MiddlewareAgent
public MiddlewareAgent(IAgent innerAgent, string? name = null, IEnumerable<IMiddleware>? middlewares = null)
Parameters
innerAgent
IAgentthe inner agent where middleware will be added.
name
stringthe name of the agent if provided. Otherwise, the name of
innerAgent
will be used.middlewares
IEnumerable<IMiddleware>
MiddlewareAgent(MiddlewareAgent)
Create a new instance of MiddlewareAgent by copying the middlewares from another MiddlewareAgent.
public MiddlewareAgent(MiddlewareAgent other)
Parameters
other
MiddlewareAgent
Properties
Agent
Get the inner agent.
public IAgent Agent { get; }
Property Value
Middlewares
Get the middlewares.
public IEnumerable<IMiddleware> Middlewares { get; }
Property Value
Name
public string Name { get; }
Property Value
Methods
GenerateReplyAsync(IEnumerable<IMessage>, GenerateReplyOptions?, CancellationToken)
Generate reply
public Task<IMessage> GenerateReplyAsync(IEnumerable<IMessage> messages, GenerateReplyOptions? options = null, CancellationToken cancellationToken = default)
Parameters
messages
IEnumerable<IMessage>conversation history
options
GenerateReplyOptionscompletion option. If provided, it should override existing option if there's any
cancellationToken
CancellationToken
Returns
ToString()
public override string ToString()
Returns
Use(IMiddleware)
Use middleware.
public void Use(IMiddleware middleware)
Parameters
middleware
IMiddleware
Use(Func<IEnumerable<IMessage>, GenerateReplyOptions?, IAgent, CancellationToken, Task<IMessage>>, string?)
Add a middleware to the agent. If multiple middlewares are added, they will be executed in the LIFO order. Call into the next function to continue the execution of the next middleware. Short cut middleware execution by not calling into the next function.
public void Use(Func<IEnumerable<IMessage>, GenerateReplyOptions?, IAgent, CancellationToken, Task<IMessage>> func, string? middlewareName = null)
Parameters
func
Func<IEnumerable<IMessage>, GenerateReplyOptions, IAgent, CancellationToken, Task<IMessage>>middlewareName
string