Bitzsoft.Integrations.RAG 1.0.0-alpha.2

This is a prerelease version of Bitzsoft.Integrations.RAG.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Bitzsoft.Integrations.RAG --version 1.0.0-alpha.2
                    
NuGet\Install-Package Bitzsoft.Integrations.RAG -Version 1.0.0-alpha.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Bitzsoft.Integrations.RAG" Version="1.0.0-alpha.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bitzsoft.Integrations.RAG" Version="1.0.0-alpha.2" />
                    
Directory.Packages.props
<PackageReference Include="Bitzsoft.Integrations.RAG" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Bitzsoft.Integrations.RAG --version 1.0.0-alpha.2
                    
#r "nuget: Bitzsoft.Integrations.RAG, 1.0.0-alpha.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Bitzsoft.Integrations.RAG@1.0.0-alpha.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Bitzsoft.Integrations.RAG&version=1.0.0-alpha.2&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Bitzsoft.Integrations.RAG&version=1.0.0-alpha.2&prerelease
                    
Install as a Cake Tool

Bitzsoft.Integrations.RAG

检索增强生成 -- Qdrant 向量数据库 + Ollama 嵌入模型集成,开箱即用的 RAG 解决方案。

功能特性

  • 基于 Qdrant 向量数据库的高性能相似度检索
  • 集成 Ollama 嵌入模型,支持单条与批量文本向量化
  • 提供完整的文档处理管线:清理、分块(按段落 / 按固定大小)、嵌入、索引
  • 自动生成基于检索结果的系统提示词(SystemPrompt),无缝对接 AI 对话流程
  • 支持相似度阈值过滤与 Top-K 检索
  • 所有服务均通过 DI 注册,可独立替换各层实现

安装

dotnet add package Bitzsoft.Integrations.RAG

或通过 csproj 引用:

<PackageReference Include="Bitzsoft.Integrations.RAG" Version="*" />

配置

appsettings.json 中添加以下配置节:

{
  "Ollama": {
    "Url": "http://localhost:11434",
    "ApiKey": "",
    "DefaultModel": "nomic-embed-text",
    "VectorSize": 1536
  },
  "Qdrant": {
    "Url": "http://localhost:6334",
    "ApiKey": "",
    "CertificateThumbprint": ""
  }
}

Ollama 配置

配置项 类型 默认值 说明
Url string "http://localhost:11434" Ollama API 地址
ApiKey string? null API 密钥(可选)
DefaultModel string "nomic-embed-text" 默认嵌入模型名称
VectorSize int 1536 向量维度

Qdrant 配置

配置项 类型 默认值 说明
Url string "http://localhost:6334" Qdrant gRPC 服务地址
ApiKey string - API 密钥
CertificateThumbprint string - TLS 证书指纹

注册服务

using Bitzsoft.Integrations.RAG;
using Bitzsoft.Integrations.RAG.Ollama;
using Bitzsoft.Integrations.RAG.Qdrant;

// Program.cs / Startup.cs
services.AddRAG(
    configureOllama: options =>
    {
        options.Url = "http://localhost:11434";
        options.DefaultModel = "nomic-embed-text";
        options.VectorSize = 1536;
    },
    configureQdrant: options =>
    {
        options.Url = "http://localhost:6334";
        options.ApiKey = "your-qdrant-api-key";
    });

AddRAG 会同时注册 IOllamaServiceIQdrantServiceIRAGService 三个服务,均可在 DI 容器中独立注入使用。

使用示例

文档索引与检索问答

using Bitzsoft.Integrations.RAG;
using Bitzsoft.Integrations.RAG.Dto;
using Bitzsoft.Integrations.RAG.Ollama;

public class KnowledgeBaseService
{
    private const string CollectionName = "company-docs";

    private readonly IRAGService _ragService;
    private readonly IOllamaService _ollamaService;

    public KnowledgeBaseService(IRAGService ragService, IOllamaService ollamaService)
    {
        _ragService = ragService;
        _ollamaService = ollamaService;
    }

    /// <summary>
    /// 索引一篇文档到知识库
    /// </summary>
    public async Task<bool> IndexDocumentAsync(
        string content,
        string fileName,
        string title,
        string category = "",
        CancellationToken ct = default)
    {
        // 清理文档内容
        var cleanedContent = DocumentProcessor.CleanDocument(content);

        // 索引到 Qdrant(自动按段落分块并生成向量)
        return await _ragService.IndexDocumentAsync(
            collectionName: CollectionName,
            content: cleanedContent,
            sourceFile: fileName,
            title: title,
            category: category);
    }

    /// <summary>
    /// 基于知识库回答用户问题
    /// </summary>
    public async Task<(string Answer, List<DocumentChunkDto> Sources)> AskAsync(
        string question,
        CancellationToken ct = default)
    {
        // 1. 将用户问题向量化
        var queryVector = await _ollamaService.GetEmbeddingAsync(question, ct);

        // 2. 检索相关文档并生成 SystemPrompt
        var (success, systemPrompt, documents) = await _ragService.GetSystemPromptAsync(
            collectionName: CollectionName,
            queryVector: queryVector,
            topK: 3,
            scoreThreshold: 0.7);

        // 3. 将检索结果作为上下文传给 AI(此处以 IAIClient 为例)
        // var aiResult = await _aiClient.SendAsync(question, systemPrompt, cancellationToken: ct);
        // return (aiResult.GetContent() ?? "无法回答", documents);

        return (systemPrompt, documents);
    }
}

按固定大小分块索引

using Bitzsoft.Integrations.RAG;
using Bitzsoft.Integrations.RAG.Dto;

public class ChunkedIndexingService
{
    private readonly IRAGService _ragService;

    public ChunkedIndexingService(IRAGService ragService)
    {
        _ragService = ragService;
    }

    /// <summary>
    /// 使用固定大小分块策略索引长文档
    /// </summary>
    public async Task<bool> IndexWithFixedSizeChunksAsync(
        string content,
        string fileName,
        string title,
        CancellationToken ct = default)
    {
        // 按固定大小分块,每块 800 字符,重叠 100 字符
        var textChunks = DocumentProcessor.ChunkBySize(
            content,
            chunkSize: 800,
            overlap: 100);

        // 构建文档块 DTO 列表(向量将由 IndexChunksAsync 自动生成)
        var chunks = textChunks.Select((text, index) => new DocumentChunkDto
        {
            Id = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() + index,
            Content = text,
            SourceFile = fileName,
            Title = title,
            Category = "manual-chunk",
            ChunkIndex = index
        }).ToList();

        return await _ragService.IndexChunksAsync("large-docs", chunks);
    }
}

直接使用底层向量数据库服务

using Bitzsoft.Integrations.RAG.Dto;
using Bitzsoft.Integrations.RAG.Ollama;
using Bitzsoft.Integrations.RAG.Qdrant;

public class VectorSearchService
{
    private readonly IQdrantService _qdrantService;
    private readonly IOllamaService _ollamaService;

    public VectorSearchService(IQdrantService qdrantService, IOllamaService ollamaService)
    {
        _qdrantService = qdrantService;
        _ollamaService = ollamaService;
    }

    /// <summary>
    /// 创建集合并初始化
    /// </summary>
    public async Task<bool> InitializeCollectionAsync(string name, CancellationToken ct = default)
    {
        return await _qdrantService.CreateCollectionAsync(name, vectorSize: 1536);
    }

    /// <summary>
    /// 自定义相似度搜索
    /// </summary>
    public async Task<List<DocumentChunkDto>> SearchAsync(
        string query,
        string collectionName,
        int topK = 5,
        double minScore = 0.75,
        CancellationToken ct = default)
    {
        var queryVector = await _ollamaService.GetEmbeddingAsync(query, ct);

        return await _qdrantService.SearchAsync(
            collectionName,
            queryVector,
            (ulong)topK,
            scoreThreshold: minScore);
    }

    /// <summary>
    /// 批量嵌入
    /// </summary>
    public async Task<List<float[]>> BatchEmbedAsync(
        IEnumerable<string> texts,
        CancellationToken ct = default)
    {
        return await _ollamaService.GetEmbeddingsAsync(texts, ct);
    }
}

相关包

包名 说明
Bitzsoft.Integrations.AI 轻量级 AI 客户端,兼容 OpenAI API,可与 RAG 检索结果组合使用
Bitzsoft.Integrations.SemanticKernel 基于 Microsoft Semantic Kernel 的 AI 编排服务
Bitzsoft.Integrations.AgentFramework AI Agent 框架
Bitzsoft.Integrations.Ocr OCR 文字识别服务,可用于文档入库前的文字提取
Bitzsoft.Integrations.FileStorage 文件存储抽象层,支持阿里云 / AWS / Azure
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Bitzsoft.Integrations.RAG:

Package Downloads
Bitzsoft.Integrations.All

Bitzsoft 第三方集成聚合包 — 包含全部 Integration 模块

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-alpha.8 35 7/1/2026
1.0.0-alpha.7 68 6/16/2026
1.0.0-alpha.6 62 6/16/2026
1.0.0-alpha.5 60 6/14/2026
1.0.0-alpha.3 68 6/7/2026
1.0.0-alpha.2 61 5/29/2026
1.0.0-alpha.1 56 5/28/2026