A sample application demonstrating Retrieval-Augmented Generation (RAG) for legal document Q&A using local vector search with QDrant, Ollama-hosted AI models, and Microsoft KernelMemory. Intended for legal, compliance, and technical professionals seeking to explore next-gen semantic search in the legal domain using C# and .NET 9.0.
- .NET 9.0 SDK or later
- QDrant installed and running locally (via Docker or binary)
- Ollama installed and running locally
- Required AI models downloaded in Ollama:
nomic-embed-text:latest(for embeddings)gemma3:1b(for legal Q&A/chat)
-
Clone this repository:
git clone https://github.com/your-username/nl-rag-qdrant-legal.git cd nl-rag-qdrant-legal -
Restore NuGet packages:
dotnet restore
-
Start QDrant (Vector Database):
- The easiest way is via Docker:
docker run -p 6333:6333 -p 6334:6334 qdrant/qdrant
- QDrant will run at http://localhost:6333
- The easiest way is via Docker:
-
Install and Start Ollama:
- Download Ollama from ollama.ai, install and launch it.
- Download and prepare required models:
ollama pull nomic-embed-text:latest ollama pull gemma3:1b
- Ollama will run at http://localhost:11434
-
Verify Model & Service Configuration:
- Ensure both services are running on their default endpoints:
- QDrant:
http://localhost:6333 - Ollama:
http://localhost:11434
- QDrant:
- Check the config in
LegalDocConfig.csfor model versions and endpoints if you change ports/models.
- Ensure both services are running on their default endpoints:
Run the console application to start the legal RAG Q&A example:
cd RAGQdrantLegal
# Restore and build
dotnet restore
# (optional, if not yet run)
dotnet build
# Run
dotnet runYou will see interactive output and will be prompted to ask legal questions against the database. Example prompts:
- "What are the key confidentiality clauses in the NDA?"
- "How can I terminate the employment contract as per the document?"
- "What is the defined uptime in the SLA?"
- "Who is responsible for breach notification according to the DPA?"
- Legal Document Import:
- The app loads a set of sample legal documents, processes them into semantic embeddings using
nomic-embed-text, and stores them in a QDrant vector collection.
- The app loads a set of sample legal documents, processes them into semantic embeddings using
- Semantic Search & Q&A:
- You enter your legal query; the app uses KernelMemory to search for the most relevant text chunks using vector similarity and the QDrant engine.
- The app uses the
gemma3:1bmodel (locally via Ollama) to generate a context-aware answer, optionally showing relevant sources from the vector DB.
- Non-Disclosure Agreement (NDA)
- Employment Contract
- Data Processing Addendum (DPA)
- Service Level Agreement (SLA)
- (Plus other sample agreements for demonstration - see
LegalDocumentData.cs)
- .NET 9.0 (C# Console App)
- QDrant (Vector similarity search)
- Ollama (Local AI model runtime)
- Microsoft KernelMemory (
Microsoft.KernelMemory.Core) - nomic-embed-text:latest (Nomic AI, for embeddings)
- gemma3:1b (Google, for chat/QA)
NuGet packages required (see .csproj):
<PackageReference Include="Microsoft.KernelMemory.Core" Version="0.98.250508.3" />
<PackageReference Include="Microsoft.KernelMemory.MemoryDb.Qdrant" Version="0.98.250508.3" />
<PackageReference Include="Microsoft.KernelMemory.AI.Ollama" Version="0.98.250508.3" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.61.0" />
<PackageReference Include="Qdrant.Client" Version="1.15.0" />
<PackageReference Include="Microsoft.Extensions.VectorData.Abstractions" Version="9.7.0" />
<PackageReference Include="Microsoft.Extensions.AI" Version="9.7.1" />LegalDocRagApp.cs: Orchestrates initialization, import, and chatProgram.cs: Entry pointLegalDocumentImporter.cs: Imports legal documents into the semantic memoryLegalDocumentData.cs: Sample legal document databaseChatService.cs: Interactive Q&A loopLegalDocConfig.cs: Contains configuration for endpoints and models
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please submit an issue or open a Pull Request if you find a bug or have an enhancement idea.