找回密码
 立即注册
首页 业界区 业界 【译】 数据摄取构建模块简介(预览版)(二) ...

【译】 数据摄取构建模块简介(预览版)(二)

账暴 昨天 23:05
原文 | Luis, Adam
翻译 | 郑子铭
丰富和处理您的数据

下一步是处理数据IngestionChunk块。数据块处理器在数据块级别进行操作,可以丰富内容或执行其他转换。与文档处理器类似,选择使用哪些处理器取决于您的具体应用场景。
此示例使用内置功能SummaryEnricher,通过 AI 服务为每个数据块添加摘要:
  1. IngestionChunkProcessor<string> summaryEnricher = new SummaryEnricher(enricherOptions);
复制代码
存储您的数据

存储已处理的数据块是数据摄取管道的最后一步。s是一个用于将数据块存储在任何存储中的IngestionChunkWriter抽象概念,但它是一个使用向量存储的实现。它构建于  Microsoft.Extensions.VectorData.Abstractions抽象之上,因此您可以使用任何受支持的向量存储。IngestionChunkVectorStoreWriter
在这个例子中,我们将使用 SQLiteSqliteVectorStore将数据块存储在本地 SQLite 数据库中:
  1. using SqliteVectorStore vectorStore = new(
  2.     "Data Source=vectors.db;Pooling=false",
  3.     new()
  4.     {
  5.         EmbeddingGenerator = embeddingGenerator
  6.     });
  7. // The writer requires the embedding dimension count to be specified.
  8. // For OpenAI's `text-embedding-3-small`, the dimension count is 1536.
  9. using VectorStoreWriter<string> writer = new(vectorStore, dimensionCount: 1536);
复制代码
作者将自动:

  • 使用默认架构创建向量存储集合。
  • 使用提供的嵌入生成器为每个数据块生成嵌入。
  • 完成后,删除之前为具有相同 ID 的文档存储的所有数据块(以支持对同一文档的不同版本进行增量分块)。
编写并运行您的管道

使用IngestionPipeline先前配置的读取器、分块器、增强器和写入器来处理当前目录中的所有文件。
  1. using IngestionPipeline<string> pipeline = new(reader, chunker, writer, loggerFactory: loggerFactory)
  2. {
  3.     DocumentProcessors = { imageAlternativeTextEnricher },
  4.     ChunkProcessors = { summaryEnricher }
  5. };
  6. await foreach (var result in pipeline.ProcessAsync(new DirectoryInfo("."), searchPattern: "*.md"))
  7. {
  8.     Console.WriteLine($"Completed processing '{result.DocumentId}'. Succeeded: '{result.Succeeded}'.");
  9. }
复制代码
重要的
单个文档导入失败不应导致整个流程失败。该机制IngestionPipeline.ProcessAsync通过返回部分成功来实现IAsyncEnumerable。调用者负责处理任何失败情况(例如,重试导入失败的文档或在遇到第一个错误时停止)。
检索数据

VectorStoreWriter公开了可用于对存储的数据块执行向量搜索的底层机制VectorStoreCollection。此示例提示用户输入查询,并从向量存储中返回最相似的 3 个数据块:
  1. var collection = writer.VectorStoreCollection;
  2. while (true)  
  3. {
  4.     Console.Write("Enter your question (or 'exit' to quit): ");
  5.     string? searchValue = Console.ReadLine();
  6.     if (string.IsNullOrEmpty(searchValue) || searchValue == "exit")
  7.     {
  8.         break;
  9.     }
  10.     Console.WriteLine("Searching...\n");
  11.     await foreach (var result in collection.SearchAsync(searchValue, top: 3))
  12.     {
  13.         Console.WriteLine($"Score: {result.Score}\n\tContent: {result.Record["content"]}");
  14.     }
  15. }
复制代码
端到端场景

想看看实际效果吗?试试全新的 .NET AI Web 聊天模板,体验完整的端到端流程。您可以使用MarkItDown MCP 服务器解析文档,使用语义感知分块器将其分块,并将分块存储在您选择的矢量数据库中。示例应用程序包含一个 Web 聊天功能,该功能使用已接收的数据进行 RAG(红绿灯)分析。
  1. dotnet new install Microsoft.Extensions.AI.Templates
复制代码
1.webp

2.webp

构建您的分布式应用程序

以下来自模板的代码片段展示了如何在 Aspire 中配置应用程序的不同组件,包括用于托管模型的 Ollama、用于矢量存储的 Qdrant、用于文档解析的 MarkItDown 以及 Web 应用程序本身。
  1. var builder = DistributedApplication.CreateBuilder(args);
  2. var ollama = builder.AddOllama("ollama")
  3.     .WithDataVolume();
  4. var chat = ollama.AddModel("chat", "llama3.2");
  5. var embeddings = ollama.AddModel("embeddings", "all-minilm");
  6. var vectorDB = builder.AddQdrant("vectordb")
  7.     .WithDataVolume()
  8.     .WithLifetime(ContainerLifetime.Persistent);
  9. var markitdown = builder.AddContainer("markitdown", "mcp/markitdown")
  10.     .WithArgs("--http", "--host", "0.0.0.0", "--port", "3001")
  11.     .WithHttpEndpoint(targetPort: 3001, name: "http");
  12. var webApp = builder.AddProject<Projects.RagSample_Web>("aichatweb-app");
  13. webApp
  14.     .WithReference(chat)
  15.     .WithReference(embeddings)
  16.     .WaitFor(chat)
  17.     .WaitFor(embeddings);
  18. webApp
  19.     .WithReference(vectorDB)
  20.     .WaitFor(vectorDB);
  21. webApp
  22.     .WithEnvironment("MARKITDOWN_MCP_URL", markitdown.GetEndpoint("http"));
  23. builder.Build().Run();
复制代码

可观测性

使用Aspire,您可以获得丰富的数据摄取管道可观测性体验。
该模板已经包含了对数据摄取过程和 Web 应用程序的OpenTelemetry 跟踪。
  1. public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
  2. {
  3.     // The rest is omitted for brevity.
  4.     builder.Services.AddOpenTelemetry()
  5.         .WithTracing(tracing =>
  6.         {
  7.             tracing
  8.                 .AddSource("Experimental.Microsoft.Extensions.AI")
  9.                 .AddSource("Experimental.Microsoft.Extensions.DataIngestion");
  10.         });
  11.     return builder;
  12. }
复制代码
4.webp

准备好开始了吗?

最简单的入门方法是安装 AI 网络聊天模板。
安装 AI 网络聊天模板
熟悉模板后,尝试将其用于您自己的文件。
如果您是库作者或生态系统开发者,您可以扩展抽象,使您的用户能够无缝地互操作和组合来自各种提供商的读取器、分块器、处理器和写入器。
请提交您的问题、疑虑和建议,以帮助我们塑造这些构建模块的未来。
原文链接

Introducing Data Ingestion Building Blocks (Preview)
5.png

本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
欢迎转载、使用、重新发布,但务必保留文章署名 郑子铭 (包含链接: http://www.cnblogs.com/MingsonZheng/ ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。
如有任何疑问,请与我联系 (MingsonZheng@outlook.com)

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册