mirror of
https://github.com/marcominerva/SqlDatabaseVectorSearch.git
synced 2026-08-04 09:48:57 +00:00
c7138d571c
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.
187 lines
10 KiB
Markdown
187 lines
10 KiB
Markdown
# SQL Database Vector Search Sample
|
|
|
|
[](https://dotnet.microsoft.com/en-us/download/dotnet/10.0)
|
|
[](https://dotnet.microsoft.com/apps/aspnet/apis)
|
|
[](https://dotnet.microsoft.com/apps/aspnet/web-apps/blazor)
|
|
|
|
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](https://github.com/microsoft/agent-framework).
|
|
|
|
## Table of Contents
|
|
- [Overview](#overview)
|
|
- [Screenshots](#screenshots)
|
|
- [Prerequisites](#prerequisites)
|
|
- [Project Structure](#project-structure)
|
|
- [Setup](#setup)
|
|
- [Supported Features](#supported-features)
|
|
- [How to Use](#how-to-use)
|
|
- [Limitations & FAQ](#limitations-faq)
|
|
- [Contributing](#contributing)
|
|
- [License](#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](https://github.com/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](https://dotnet.microsoft.com/en-us/download/dotnet/10.0)
|
|
- [Azure SQL Database](https://learn.microsoft.com/en-us/azure/azure-sql/database/single-database-create-quickstart)
|
|
- Azure OpenAI resource and API keys
|
|
|
|
## Project Structure
|
|
- `SqlDatabaseVectorSearch/` - Main Blazor Web App and API
|
|
- `Components/` - Blazor UI components
|
|
- `Data/` - EF Core context, migrations, and entities
|
|
- `Endpoints/` - Minimal API endpoints
|
|
- `Services/` - Business logic and integration services
|
|
- `TextChunkers/` - Text splitting utilities
|
|
- `Workflows/` - Microsoft Agent Framework workflow executors for document import and embedding generation
|
|
- `Settings/` - Configuration classes
|
|
|
|
## Setup
|
|
|
|
1. Clone the repository
|
|
|
|
```bash
|
|
git clone https://github.com/marcominerva/SqlDatabaseVectorSearch.git
|
|
```
|
|
|
|
2. Configure the database and OpenAI settings
|
|
- Edit `SqlDatabaseVectorSearch/appsettings.json` and set your Azure SQL connection string and OpenAI settings.
|
|
- **Important**: The `ModelId` values for both `ChatCompletion` and `Embedding` are used for token counting via `Microsoft.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`). The `ModelId` may differ from the actual deployment name you're using in Azure OpenAI. For example, for gpt-4.1 and gpt-5 models set the `ModelId` to `gpt-4o` for proper token counting.
|
|
- If using embedding models with shortening (e.g., `text-embedding-3-small` or `text-embedding-3-large`), set the `Dimensions` property accordingly. For `text-embedding-3-large`, you must specify a value <= 1998.
|
|
- If you change the VECTOR size, update both the [ApplicationDbContext](SqlDatabaseVectorSearch/Data/ApplicationDbContext.cs) and the [Initial Migration](SqlDatabaseVectorSearch/Data/Migrations/00000000000000_Initial.cs).
|
|
|
|
3. Run the application
|
|
|
|
```bash
|
|
dotnet run --project SqlDatabaseVectorSearch/SqlDatabaseVectorSearch.csproj
|
|
```
|
|
|
|
5. Access the Web App
|
|
- Navigate to `https://localhost:5001` (or the port shown in the console)
|
|
|
|
## 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 `TextSearchProvider` backed 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/documents` and ask questions via `POST /api/ask` or `POST /api/ask-streaming`.
|
|
|
|
### How it works
|
|
|
|
1. Documents are uploaded through the API and processed by the `EmbeddingWorkflow`.
|
|
2. The workflow converts the uploaded file into text, chunks it, generates embeddings, and stores documents, chunks, and VECTOR embeddings in Azure SQL Database.
|
|
3. When a question is asked, the `ReformulationAgent` can rewrite it using the current conversation context.
|
|
4. The `RagAgent` receives relevant SQL vector-search results through a `TextSearchProvider` and answers using only the provided context.
|
|
5. 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
|
|
|
|
```json
|
|
{
|
|
"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](https://developer.mozilla.org/docs/Web/API/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:
|
|
|
|
```text
|
|
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.
|
|
- Then, there are as many `Delta` events 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 `End` event 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`, set `Dimensions` <= 1998.
|
|
- **Supported file types**: PDF, DOCX, TXT, MD.
|
|
- **Known Issues**: See [Issues](https://github.com/marcominerva/SqlDatabaseVectorSearch/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](LICENSE) file for details.
|
|
|
|
---
|
|
|
|
> [!NOTE]
|
|
> If you prefer to use straight SQL, check out the [sql branch](https://github.com/marcominerva/SqlDatabaseVectorSearch/tree/sql).
|
|
|