TextSearchProvider Class

Definition

A text search context provider that performs a search over external knowledge and injects the formatted results into the AI invocation context, or exposes a search tool for on-demand use. This provider can be used to enable Retrieval Augmented Generation (RAG) on an agent.

public sealed class TextSearchProvider : Microsoft.Agents.AI.MessageAIContextProvider
type TextSearchProvider = class
    inherit MessageAIContextProvider
Public NotInheritable Class TextSearchProvider
Inherits MessageAIContextProvider
Inheritance

Remarks

The provider supports two behaviors controlled via SearchTime:

  • BeforeAIInvoke – Automatically performs a search prior to every AI invocation and injects results as additional messages.
  • OnDemandFunctionCalling – Exposes a function tool that the model may invoke to retrieve contextual information when needed.

When RecentMessageMemoryLimit is greater than zero the provider will retain the most recent user and assistant messages (up to the configured limit) across invocations and prepend them (in chronological order) to the current request messages when forming the search input. This can improve search relevance by providing multi-turn context to the retrieval layer without permanently altering the conversation history.

Security considerations: Search results retrieved from external sources are injected into the LLM context and may contain adversarial content designed to manipulate LLM behavior via indirect prompt injection. Developers should be aware that:

  • The search query may be constructed from user input or LLM-generated content, both of which are untrusted. Implementers of the search delegate should validate search inputs and apply appropriate access controls to search results.
  • Retrieved documents are formatted and injected as messages in the AI request context. If the external data source is compromised, adversarial content could influence the LLM's responses.
  • When using OnDemandFunctionCalling, the AI model controls when and what to search for β€” the search query text is AI-generated and should be treated as untrusted input by the search implementation.

Constructors

Name Description
TextSearchProvider(Func<String,CancellationToken,Task<IEnumerable<TextSearchProvider.TextSearchResult>>>, TextSearchProviderOptions, ILoggerFactory)

Initializes a new instance of the TextSearchProvider class.

Properties

Name Description
ProvideInputMessageFilter

Gets the filter function to apply to input messages before providing context via ProvideAIContextAsync(AIContextProvider+InvokingContext, CancellationToken).

(Inherited from AIContextProvider)
StateKeys

Gets the set of keys used to store the provider state in the StateBag.

StoreInputRequestMessageFilter

Gets the filter function to apply to request messages before storing context via StoreAIContextAsync(AIContextProvider+InvokedContext, CancellationToken).

(Inherited from AIContextProvider)
StoreInputResponseMessageFilter

Gets the filter function to apply to response messages before storing context via StoreAIContextAsync(AIContextProvider+InvokedContext, CancellationToken).

(Inherited from AIContextProvider)

Methods

Name Description
GetService(Type, Object)

Asks the AIContextProvider for an object of the specified type serviceType.

(Inherited from AIContextProvider)
GetService<TService>(Object)

Asks the AIContextProvider for an object of type TService.

(Inherited from AIContextProvider)
InvokedAsync(AIContextProvider+InvokedContext, CancellationToken)

Called at the end of the agent invocation to process the invocation results.

(Inherited from AIContextProvider)
InvokedCoreAsync(AIContextProvider+InvokedContext, CancellationToken)

Called at the end of the agent invocation to process the invocation results.

(Inherited from AIContextProvider)
InvokingAsync(AIContextProvider+InvokingContext, CancellationToken)

Called at the start of agent invocation to provide additional context.

(Inherited from AIContextProvider)
InvokingAsync(MessageAIContextProvider+InvokingContext, CancellationToken)

Called at the start of agent invocation to provide additional messages.

(Inherited from MessageAIContextProvider)
InvokingCoreAsync(AIContextProvider+InvokingContext, CancellationToken)

Called at the start of agent invocation to provide additional context.

(Inherited from AIContextProvider)
InvokingCoreAsync(MessageAIContextProvider+InvokingContext, CancellationToken)

Called at the start of agent invocation to provide additional messages.

(Inherited from MessageAIContextProvider)
ProvideAIContextAsync(AIContextProvider+InvokingContext, CancellationToken)

When overridden in a derived class, provides additional AI context to be merged with the input context for the current invocation.

(Inherited from MessageAIContextProvider)
ProvideMessagesAsync(MessageAIContextProvider+InvokingContext, CancellationToken)

When overridden in a derived class, provides additional messages to be merged with the input messages for the current invocation.

(Inherited from MessageAIContextProvider)
StoreAIContextAsync(AIContextProvider+InvokedContext, CancellationToken)

When overridden in a derived class, processes invocation results at the end of the agent invocation.

(Inherited from AIContextProvider)

Applies to