How to Start with AI in NET: Semantic Search Implementation

Listen to this Post

Featured Image
The easiest way to start using AI in .NET is by leveraging the Microsoft.Extensions.AI library alongside Large Language Models (LLMs). In this guide, we’ll explore how to implement semantic search in .NET using Ollama and the .NET AI library in fewer than 50 lines of code.

🔗 Reference Video: How to Create Semantic Search with .NET & Ollama

You Should Know: Key Steps & Code

1. Setting Up the Environment

Install the required NuGet packages:

dotnet add package Microsoft.Extensions.AI 
dotnet add package OllamaSharp 

2. Configure Ollama for Local LLM

Run Ollama locally (requires Docker):

docker run -d -p 11434:11434 ollama/ollama 
ollama pull llama2  Download a model 

3. Implementing Semantic Search in .NET

using Microsoft.Extensions.AI; 
using OllamaSharp;

var builder = WebApplication.CreateBuilder(args);

// Add AI services 
builder.Services.AddAIServices(ai => 
{ 
ai.UseOllama("http://localhost:11434", "llama2"); 
});

var app = builder.Build();

app.MapGet("/search", async (IAIService aiService, string query) => 
{ 
var embeddings = await aiService.GenerateEmbeddingsAsync(query); 
// Compare with stored vectors (e.g., from a DB) 
return Results.Ok($"Search results for: {query}"); 
});

app.Run(); 

4. Testing the Semantic Search

curl "http://localhost:5000/search?query=What%20is%20.NET%20AI?" 

5. Advanced: Storing & Retrieving Embeddings

Use Azure AI Search or PostgreSQL with pgvector:

CREATE TABLE documents ( 
id SERIAL PRIMARY KEY, 
content TEXT, 
embedding VECTOR(1536) 
); 

What Undercode Say

Semantic search revolutionizes traditional keyword-based search by understanding context. Here are some related commands and tools to deepen your knowledge:

Linux & IT Commands

 Monitor Ollama logs 
docker logs -f ollama_container

Check GPU usage (if using CUDA) 
nvidia-smi

Install .NET on Linux 
sudo apt-get install -y dotnet-sdk-8.0 

Windows & .NET

 List installed .NET SDKs 
dotnet --list-sdks

Benchmark your AI service 
dotnet counters monitor Microsoft.AspNetCore.Hosting 

AI & Cloud Integrations

Expected Output:

A functional semantic search API in .NET, scalable with vector databases, and optimized for low-latency AI inferences.

🔗 Further Reading:

References:

Reported By: Djokic Stefan – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 Telegram