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 or SQL Server 2025, Azure OpenAI, and Microsoft Agent Framework.
- Overview
- Screenshots
- Prerequisites
- Project Structure
- Setup
- Supported Features
- How to Use
- Limitations & FAQ
- Contributing
- License
This application allows you to:
- Load documents (PDF, DOCX, TXT, MD)
- Generate embeddings and save them as vectors in Azure SQL Database or SQL Server 2025
- Perform semantic search and RAG using Azure OpenAI and Microsoft Agent Framework agents
- Interact via a Blazor Web App or programmatically via Minimal API
The native VECTOR type is available in both Azure SQL Database and SQL Server 2025, so no external vector store is required.
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.
- .NET 10 SDK
- One of the following, both of which support the native
VECTORtype:- Azure SQL Database
- SQL Server 2025 or later
- Azure OpenAI resource and API keys
SqlDatabaseVectorSearch/- Main Blazor Web App and APIComponents/- Blazor UI componentsContentDecoders/- Decoders that extract text from PDF, DOCX, TXT and MD filesData/- EF Core context, migrations, and entitiesEndpoints/- Minimal API endpointsExtensions/- Extension methods and helpersModels/- Request and response modelsServices/- Business logic and integration servicesSettings/- Configuration classesTextChunkers/- Text splitting utilitiesValidations/- Request validatorsWorkflows/- Microsoft Agent Framework workflow executors for document import and embedding generation
-
Clone the repository
git clone https://github.com/marcominerva/SqlDatabaseVectorSearch.git
-
Configure the database and OpenAI settings
- Edit
SqlDatabaseVectorSearch/appsettings.jsonand set your connection string (Azure SQL Database or SQL Server 2025) and OpenAI settings. - Important: The
ModelIdvalues for bothChatCompletionandEmbeddingare used only for token counting viaMicrosoft.ML.Tokenizers, while the actual calls to the service use theDeploymentvalues.ModelIdmust therefore be a model name recognized by the tokenizer library (e.g.,gpt-5,gpt-4.1,gpt-4o,gpt-4,gpt-3.5-turbo,text-embedding-3-small,text-embedding-3-large,text-embedding-ada-002), which is typically different from the deployment name you have chosen in Azure OpenAI. If a model is not recognized,TiktokenTokenizer.CreateForModelthrows at startup: in that case, fall back to the closest supported model that shares the same encoding (for examplegpt-4ofor newer GPT models). - 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:7025(or the port shown in the console)
- Navigate to
- 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 or SQL Server 2025 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 and the
/api/ask-streamingendpoint stream answers, appending 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.
- 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.
- 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 or SQL Server 2025.
- 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.
POST /api/ask
Content-Type: application/json
{
"conversationId": "3d0bd178-499d-433a-b2bc-c35e488d9e2c",
"text": "Why is Mars called the red planet?"
}
{
"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
}
}
}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.
- Database: Azure SQL Database or SQL Server 2025 (or later). Both provide the native
VECTORtype used by this sample. - 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
Contributions are welcome! Please open issues or pull requests. For major changes, discuss them first via an issue.
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.

