Updated README.md and Home.razor to document migration from Semantic Kernel to Microsoft Agent Framework, detailing new orchestration, document import, question reformulation, and RAG agent workflows. Revised API and streaming response examples to use Server-Sent Events (SSE) with event names Start, Delta, and End. Refactored AskEndpoints.cs to use .NET 8 ServerSentEvents API and yield SseItem<Response> with correct event names. Updated Ask.razor, Documents.razor, and VectorSearchService.cs to use StreamState.Delta instead of Append, and replaced StateHasChanged() with await InvokeAsync(StateHasChanged) for async UI updates. Removed MaxInputTokens and MaxOutputTokens from AppSettings.cs and appsettings.json.
SQL Database Vector Search Sample
A Blazor Web App and Minimal API for performing RAG (Retrieval Augmented Generation) and vector search using the native VECTOR type in Azure SQL Database, Azure OpenAI, and Microsoft Agent Framework.
Table of Contents
- Overview
- Screenshots
- Prerequisites
- Project Structure
- Setup
- Supported Features
- How to Use
- Limitations & FAQ
- Contributing
- License
Overview
This application allows you to:
- Load documents (PDF, DOCX, TXT, MD)
- Generate embeddings and save them as vectors in Azure SQL Database
- Perform semantic search and RAG using Azure OpenAI and Microsoft Agent Framework agents
- Interact via a Blazor Web App or programmatically via Minimal API
Embeddings and chat completion are orchestrated with Microsoft Agent Framework. The application uses an embedding workflow to import documents, a reformulation agent to rewrite follow-up questions with conversation context, and a RAG agent connected to a SQL vector-search context provider.
Screenshots
Web App
Web API
Prerequisites
- .NET 10 SDK
- Azure SQL Database
- Azure OpenAI resource and API keys
Project Structure
SqlDatabaseVectorSearch/- Main Blazor Web App and APIComponents/- Blazor UI componentsData/- EF Core context, migrations, and entitiesEndpoints/- Minimal API endpointsServices/- Business logic and integration servicesTextChunkers/- Text splitting utilitiesWorkflows/- Microsoft Agent Framework workflow executors for document import and embedding generationSettings/- Configuration classes
Setup
-
Clone the repository
git clone https://github.com/marcominerva/SqlDatabaseVectorSearch.git -
Configure the database and OpenAI settings
- Edit
SqlDatabaseVectorSearch/appsettings.jsonand set your Azure SQL connection string and OpenAI settings. - Important: The
ModelIdvalues for bothChatCompletionandEmbeddingare used for token counting viaMicrosoft.ML.Tokenizers. These values must be valid model identifiers supported by the tokenizer library (e.g.,gpt-4o,gpt-4,gpt-3.5-turbo,text-embedding-3-small,text-embedding-3-large,text-embedding-ada-002). TheModelIdmay differ from the actual deployment name you're using in Azure OpenAI. For example, for gpt-4.1 and gpt-5 models set theModelIdtogpt-4ofor proper token counting. - If using embedding models with shortening (e.g.,
text-embedding-3-smallortext-embedding-3-large), set theDimensionsproperty accordingly. Fortext-embedding-3-large, you must specify a value <= 1998. - If you change the VECTOR size, update both the ApplicationDbContext and the Initial Migration.
- Edit
-
Run the application
dotnet run --project SqlDatabaseVectorSearch/SqlDatabaseVectorSearch.csproj -
Access the Web App
- Navigate to
https://localhost:5001(or the port shown in the console)
- Navigate to
Supported features
- Microsoft Agent Framework orchestration: Document import is implemented as a workflow, while question reformulation and RAG are implemented as agents.
- Conversation history with question reformulation: The reformulation agent rewrites each question using the conversation context before vector search is performed.
- SQL vector-search context provider: The RAG agent receives relevant chunks from Azure SQL Database through a
TextSearchProviderbacked by native VECTOR search. - Information about token usage: The Blazor chat page and API responses expose token usage for reformulation and final answer generation.
- Response streaming: The Blazor chat page uses streaming responses, appending answer tokens as they arrive.
- Markdown source citations: Citations are included directly in the generated Markdown answer as a localized sources section with source name, page number when available, and a short supporting excerpt.
How to Use
- Web App: Use the Blazor interface to manage documents and chat with your indexed content. The chat page streams answers, shows token usage, supports conversation reset, and renders source citations as part of the Markdown answer.
- API: Import documents via
POST /api/documentsand ask questions viaPOST /api/askorPOST /api/ask-streaming.
How it works
- Documents are uploaded through the API and processed by the
EmbeddingWorkflow. - The workflow converts the uploaded file into text, chunks it, generates embeddings, and stores documents, chunks, and VECTOR embeddings in Azure SQL Database.
- When a question is asked, the
ReformulationAgentcan rewrite it using the current conversation context. - The
RagAgentreceives relevant SQL vector-search results through aTextSearchProviderand answers using only the provided context. - Sources are not returned as a separate JSON collection. They are formatted directly in the Markdown answer.
Example API Request
POST /api/ask
Content-Type: application/json
{
"conversationId": "3d0bd178-499d-433a-b2bc-c35e488d9e2c"
"text": "Why is Mars called the red planet?"
}
Example API Response
{
"conversationId": "3d0bd178-499d-433a-b2bc-c35e488d9e2c",
"originalQuestion": "why is mars called the red planet?",
"reformulatedQuestion": "Why is the planet Mars called the red planet?",
"answer": "Mars is called the Red Planet because its surface has an orange-red color caused by iron oxide dust.\n\n*Sources*\n1. **Mars.pdf**, page 1: *surface of Mars is orange-red because it is covered in iron oxide dust*",
"streamState": null,
"tokenUsage": {
"reformulation": {
"inputTokenCount": 812,
"outputTokenCount": 11,
"totalTokenCount": 823
},
"question": {
"inputTokenCount": 31708,
"outputTokenCount": 227,
"totalTokenCount": 31935
}
}
}
How response streaming works
When using the /api/ask-streaming endpoint, answers are streamed as Server-Sent Events. Each event has a name matching the streamState value and a JSON Response payload in the data field. The format is as follows:
event: Start
data: {"conversationId":"3d0bd178-499d-433a-b2bc-c35e488d9e2c","originalQuestion":"why is mars called the red planet?","reformulatedQuestion":"Why is the planet Mars known as the red planet?","answer":null,"streamState":"Start","tokenUsage":{"reformulation":{"inputTokenCount":541,"outputTokenCount":12,"totalTokenCount":553,"cachedInputTokenCount":0,"reasoningTokenCount":0,"inputAudioTokenCount":null,"inputTextTokenCount":null,"outputAudioTokenCount":null,"outputTextTokenCount":null,"additionalCounts":null},"question":null}}
event: Delta
data: {"conversationId":"3d0bd178-499d-433a-b2bc-c35e488d9e2c","originalQuestion":null,"reformulatedQuestion":null,"answer":"Mars","streamState":"Delta","tokenUsage":null}
event: Delta
data: {"conversationId":"3d0bd178-499d-433a-b2bc-c35e488d9e2c","originalQuestion":null,"reformulatedQuestion":null,"answer":" is known as the red planet because its surface is rich in iron oxide dust.\n\n","streamState":"Delta","tokenUsage":null}
event: Delta
data: {"conversationId":"3d0bd178-499d-433a-b2bc-c35e488d9e2c","originalQuestion":null,"reformulatedQuestion":null,"answer":"Sources\n1. **Mars.pdf**, page 1: *surface of Mars is orange-red because it is covered in iron oxide dust*","streamState":"Delta","tokenUsage":null}
event: End
data: {"conversationId":"3d0bd178-499d-433a-b2bc-c35e488d9e2c","originalQuestion":null,"reformulatedQuestion":null,"answer":null,"streamState":"End","tokenUsage":{"reformulation":null,"question":{"inputTokenCount":30949,"outputTokenCount":221,"totalTokenCount":31170,"cachedInputTokenCount":3840,"reasoningTokenCount":0,"inputAudioTokenCount":null,"inputTextTokenCount":null,"outputAudioTokenCount":null,"outputTextTokenCount":null,"additionalCounts":null}}}
- The first event has the following characteristics:
- The SSE event name is
Start. - The streamState property is set to
Start. - It contains the question and its reformulation (if not requested, reformulatedQuestion will be equal to originalQuestion).
- The tokenUsage section holds information about tokens used for reformulation, if done.
- The SSE event name is
- Then, there are as many
Deltaevents as necessary for the actual answer:- Each event contains a token or chunk of generated text in the answer property.
- The streamState property is set to
Delta. - originalQuestion, reformulatedQuestion and tokenUsage are always
null.
- The stream ends when an
Endevent is received. This event contains token usage information for the final answer. - Sources are included in the Markdown answer text.
Limitations & FAQ
- VECTOR column size: Maximum allowed is 1998. For
text-embedding-3-large, setDimensions<= 1998. - Supported file types: PDF, DOCX, TXT, MD.
- Known Issues: See Issues
Contributing
Contributions are welcome! Please open issues or pull requests. For major changes, discuss them first via an issue.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Note
If you prefer to use straight SQL, check out the sql branch.

