Introduction
Enterprise data lives everywhere — in databases, documents, emails, and reports. Traditional keyword-based search often fails to understand meaning or context, leading to irrelevant results.
That’s where AI-powered search using OpenAI embeddings and SQL Server comes in. By combining vector similarity search with semantic understanding, you can build a search experience that understands intent, not just words.
In this educational article, we’ll learn — step by step — how to implement an intelligent semantic search engine using OpenAI embeddings, .NET 8 (ASP.NET Core), and SQL Server.
1. What Is AI-Powered Search?
Traditional search = keyword matching.
AI-powered search = semantic similarity.
Instead of matching exact words, embeddings represent text as vectors in multi-dimensional space — sentences with similar meanings have closer vector values.
For example
“Order shipment delayed” ≈ “Package not yet delivered”
Even though words differ, both phrases are semantically similar.
2. How Embeddings Work
Embeddings are numerical vector representations of text.
They allow mathematical comparison of meaning.
Example (simplified)
| Text | Embedding (vector) |
|---|---|
| "Order delayed" | [0.12, 0.56, 0.33, ...] |
| "Shipment late" | [0.11, 0.57, 0.30, ...] |
To find similarity, we calculate cosine similarity between vectors — a value close to 1 means similar meaning.
3. System Architecture Overview
Here’s the high-level architecture of our AI-Powered Search System:
+-----------------------------------+
| Angular Frontend |
| - Search Box |
| - Result Listing (Semantic) |
+-----------------+-----------------+
|
| HTTP Query
v
+-----------------+-----------------+
| ASP.NET Core API |
| - OpenAI Embedding Generator |
| - SQL Server Query Handler |
+-----------------+-----------------+
|
| Store / Query
v
+-----------------+-----------------+
| SQL Server Database |
| - Text Data Table |
| - Embedding Vector Columns |
+-----------------------------------+4. Database Design for Semantic Search
We’ll store both text content and its vector embeddings.
CREATE TABLE Documents (
Id INT IDENTITY PRIMARY KEY,
Title NVARCHAR(200),
Content NVARCHAR(MAX),
Embedding VARBINARY(MAX)
);
Note: SQL Server 2022+ supports vector data type and vector indexes for AI workloads.
For older versions, you can store embeddings as VARBINARY(MAX) and process similarity in .NET.
5. Generating Embeddings with OpenAI
We’ll use the OpenAI Embeddings API (e.g., text-embedding-3-small) to generate vector representations.
Step 1: Add NuGet Packages
dotnet add package OpenAI
Step 2: Create a Service
using OpenAI.Embeddings;
using OpenAI;
using System.Text.Json;
public class EmbeddingService
{
private readonly OpenAIClient _client;
public EmbeddingService(IConfiguration config)
{
_client = new OpenAIClient(config["OpenAI:ApiKey"]);
}
public async Task<float[]> GenerateEmbeddingAsync(string text)
{
var response = await _client.EmbeddingsEndpoint.CreateEmbeddingAsync(
model: "text-embedding-3-small",
input: text
);
return response.Data[0].Embedding.ToArray();
}
}
6. Storing Embeddings in SQL Server
Convert the float array to binary for storage:
public async Task SaveDocumentAsync(string title, string content)
{
var embedding = await _embeddingService.GenerateEmbeddingAsync(content);
var bytes = MemoryMarshal.AsBytes(embedding.AsSpan()).ToArray();
using var connection = new SqlConnection(_connectionString);
using var cmd = new SqlCommand("INSERT INTO Documents (Title, Content, Embedding) VALUES (@t, @c, @e)", connection);
cmd.Parameters.AddWithValue("@t", title);
cmd.Parameters.AddWithValue("@c", content);
cmd.Parameters.AddWithValue("@e", bytes);
connection.Open();
await cmd.ExecuteNonQueryAsync();
}

Join the conversation! Your thoughts help the community grow.