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
innerAgentIAgentthe inner agent where middleware will be added.
namestringthe name of the agent if provided. Otherwise, the name of
innerAgentwill be used.middlewaresIEnumerable<IMiddleware>
MiddlewareAgent(MiddlewareAgent)
Create a new instance of MiddlewareAgent by copying the middlewares from another MiddlewareAgent.
public MiddlewareAgent(MiddlewareAgent other)
Parameters
otherMiddlewareAgent
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
messagesIEnumerable<IMessage>conversation history
optionsGenerateReplyOptionscompletion option. If provided, it should override existing option if there's any
cancellationTokenCancellationToken
Returns
ToString()
public override string ToString()
Returns
Use(IMiddleware)
Use middleware.
public void Use(IMiddleware middleware)
Parameters
middlewareIMiddleware
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
funcFunc<IEnumerable<IMessage>, GenerateReplyOptions, IAgent, CancellationToken, Task<IMessage>>middlewareNamestring