February 27th, 2025

使用Chroma构建.NET AI应用

Eddie Chen
Partner Technical Advisor

本文翻译自 Luis 和 JiriBuilding .NET AI apps with Chroma

无论您是构建 AI 解决方案还是想使用高级搜索功能增强现有项目现在都可以使用 Chroma 作为 .NET 应用程序中数据库提供程序

什么是Chroma?

Chroma 是一款适用于人工智能应用程序的开源数据库。

借助对存储嵌入、元数据过滤、向量搜索、全文搜索、文档存储和多模式检索的支持,您可以使用 Chroma 在应用程序中支持语义搜索和检索增强生成(RAG)功能。

有关更多详情,请访问 Chroma 网站

在 C# 应用程序中使用Chroma

在本示例中,我们将使用 ChromaDB.Client 来连接到 Chroma 数据库并使用向量搜索来搜索电影。

最简单的开始方式是在本地使用 Chroma Docker 映像。您也可以在 Azure 中部署实例

注意

ChromaDB.Client 是一个开源的由社区支持的库。

连接数据库

  1. 创建 C# 控制台应用程序
  2. 安装 ChromaDB.Client Nuget 包
  3. 创建带有配置选项的 ChromaClient
using ChromaDB.Client;
var configOptions = new ChromaConfigurationOptions(uri: "http://localhost:8000/api/v1/");
using var httpClient = new HttpClient();
var client = new ChromaClient(configOptions, httpClient);

当使用托管版本的 Chroma 时,请将 uri 替换为您的托管端点。

创建集合

现在您有了客户端,请创建一个集合来存储电影数据。

var collection = await client.GetOrCreateCollection("movies");

要对该集合执行操作,您需要创建一个集合客户端。

var collectionClient = new ChromaCollectionClient(collection, configOptions, httpClient);

向集合中添加数据

创建集合后,就可以向其中添加数据了。我们存储的数据包括:

  1. 电影 ID
  2. 用于表示电影描述的嵌入向量。
  3. 包含电影标题的元数据
ID Title Embedding Movie Description
1 The Lion King [0.10022575, -0.23998135] The Lion King is a classic Disney animated film that tells the story of a young lion named Simba who embarks on a journey to reclaim his throne as the king of the Pride Lands after the tragic death of his father.
2 Inception [0.10327095, 0.2563685] Inception is a mind-bending science fiction film directed by Christopher Nolan. It follows the story of Dom Cobb, a skilled thief who specializes in entering people’s dreams to steal their secrets. However, he is offered a final job that involves planting an idea into someone’s mind.
3 Toy Story [0.095857024, -0.201278] Toy Story is a groundbreaking animated film from Pixar. It follows the secret lives of toys when their owner, Andy, is not around. Woody and Buzz Lightyear are the main characters in this heartwarming tale.
4 Pulp Fiction [0.106827796, 0.21676421] Pulp Fiction is a crime film directed by Quentin Tarantino. It weaves together interconnected stories of mobsters, hitmen, and other colorful characters in a non-linear narrative filled with dark humor and violence.
5 Shrek [0.09568083, -0.21177962] Shrek is an animated comedy film that follows the adventures of Shrek, an ogre who embarks on a quest to rescue Princess Fiona from a dragon-guarded tower in order to get his swamp back.
List<string> movieIds = ["1", "2", "3", "4", "5" ];

List<ReadOnlyMemory<float>> descriptionEmbeddings = [

    new [] { 0.10022575f, -0.23998135f },
    new [] { 0.10327095f, 0.2563685f },
    new [] { 0.095857024f, -0.201278f },
    new [] { 0.106827796f, 0.21676421f },
    new [] { 0.09568083f, -0.21177962f },
];

List<Dictionary<string,object>> metadata = 
[
    new Dictionary<string, object> { ["Title"] = "The Lion King" },
    new Dictionary<string, object> { ["Title"] = "Inception"  },
    new Dictionary<string, object> { ["Title"] = "Toy Story"  },
    new Dictionary<string, object> { ["Title"] = "Pulp Fiction" },
    new Dictionary<string, object> { ["Title"] = "Shrek"  },
];

await collectionClient.Add(movieIds, descriptionEmbeddings, metadata);

搜索电影(使用向量搜索)

现在您的数据已存储在数据库中,您可以查询它。在本例中,我们使用向量搜索。

Text Embedding
A family friendly movie [0.12217915, -0.034832448]
List<ReadOnlyMemory<float>> queryEmbedding = [new([0.12217915f, -0.034832448f])];

var queryResult = await collectionClient.Query(
    queryEmbeddings: queryEmbedding,
    nResults: 2,
    include: ChromaQueryInclude.Metadatas | ChromaQueryInclude.Distances);

foreach (var result in queryResult)
{
    foreach (var item in result)
    {
        Console.WriteLine($"Title: {(string)item.Metadata["Title"] ?? string.Empty} {(item.Distance)}");
    }
}

结果应类似于以下输出。

Title: Toy Story 0.028396977
Title: Shrek 0.032012463

总结

这一最新功能增强了 .NET 中不断发展的 AI 生态系统。它为更简单地实现现有的语义内核连接器以及在您的 .NET 应用程序中无缝集成 Microsoft.Extensions.VectorDataMicrosoft.Extensions.AI 等基础组件铺平了道路。

我们要感谢 @ssone95 为该项目所做的工作和贡献。我们很高兴能继续与社区合作建立合作伙伴关系,使 .NET 开发人员能够构建 AI 应用程序。

要了解如何使用数据库如Chroma构建 AI 应用程序,请查看 .NET AI 文档

现在就试用 Chroma C# SDK 并 提供反馈

Author

Eddie Chen
Partner Technical Advisor

1 comment

  • Stevie White 10 minutes ago

    Mr. Chen,

    Can you please explain what the rationale was behind mixing Chinese blog posts in with an English blog? I have checked in every concievable corner on this website as well as via Chat GPT and your own Microsoft Co-Pilot and I cannot find any official reasion or business rationale for this practice.