Add a grounding lecture to the RAG module. Grounding is when the vector DB is always used as the context for the prompt. if the vector DB does not contain an answer to the question then the AI returns a message that it does not know.
See d:\udemy branch AzureChatDataSource (this is OpenAi Only)
for NON-OpenAI source using TextSearchProvider
Semantic Kernel C# Always-On RAG Setup
Great question, Randy. In Semantic Kernel (SK) you don’t have to rely on a “plugin that might be called.” You can wire always‑on data grounding (RAG) so every user turn first retrieves from your vector store and injects the results into the model’s context before the LLM is called.
Below are two production‑friendly patterns in C#:
- Recommended (Agent + TextSearchProvider): attach SK’s AI Context Provider for RAG to the agent’s thread. It always performs a similarity search and injects citations/snippets into the prompt context before generation—no plugin/tool call routing required.
- “Plain” SK (no agents): do the retrieval yourself (vector search → compose system/user prompt with the hits → call the chat completion). This gives you full control and still guarantees the vector DB is consulted every turn.
1) Always‑on grounding with Agents + TextSearchProvider (C#)
This uses SK’s experimental Agent RAG components (ITextSearch, TextSearchStore/VectorStoreTextSearch, TextSearchProvider) to always fetch and inject grounding context.
using Azure;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Data;
using Microsoft.SemanticKernel.Data.TextSearch;
using Microsoft.SemanticKernel.Data.VectorStore;
var aoai = new AzureOpenAIClient(new Uri("<your-aoai-endpoint>"), new AzureCliCredential());
var embeddingGen = aoai.GetEmbeddingClient("<your-embedding-deployment>")
.AsIEmbeddingGenerator(vectorDimensions: 1536);
var vectorStore = new InMemoryVectorStore(new() { EmbeddingGenerator = embeddingGen });
await using var textSearchStore = new TextSearchStore<string>(
vectorStore,
collectionName: "KnowledgeBase",
vectorDimensions: 1536);
await textSearchStore.UpsertTextAsync(new[]
{
"WidgetX warranty lasts 24 months from purchase if registered within 30 days.",
"WidgetX supports Azure AD SSO and SCIM provisioning.",
});
var kernel = new Kernel();
var agent = new ChatCompletionAgent
{
Name = "GroundedAssistant",
Instructions = """
You must
Add a grounding lecture to the RAG module. Grounding is when the vector DB is always used as the context for the prompt. if the vector DB does not contain an answer to the question then the AI returns a message that it does not know.
See d:\udemy branch AzureChatDataSource (this is OpenAi Only)
for NON-OpenAI source using TextSearchProvider
Semantic Kernel C# Always-On RAG Setup
Great question, Randy. In Semantic Kernel (SK) you don’t have to rely on a “plugin that might be called.” You can wire always‑on data grounding (RAG) so every user turn first retrieves from your vector store and injects the results into the model’s context before the LLM is called.
Below are two production‑friendly patterns in C#:
1) Always‑on grounding with Agents +
TextSearchProvider(C#)