Whether you’re building AI solutions or enhancing existing projects with advanced search capabilities, you now have the option of using Chroma as a database provider in your .NET applications.
What is Chroma?
Chroma is an open-source database for your AI applications.
With support for storing embeddings, metadata filtering, vector search, full-text search, document storage, and multi-modal retrieval, you can use Chroma to power semantic search and Retrieval Augmented Generation (RAG) features in your app.
For more details, check out the Chroma website.
Get started with Chroma in your C# application
In this scenario, we’ll be using the ChromaDB.Client package to connect to a Chroma database and search for movies using vector search.
The easiest way to start is locally using the Chroma Docker image. You can also deploy an instance in Azure.
Note: the ChromaDB.Client is an open-source community-supported library.
Connect to the database
- Create a C# console application.
- Install the ChromaDB.Client NuGet package.
- Create a
ChromaClient
with configuration options.using ChromaDB.Client; var configOptions = new ChromaConfigurationOptions(uri: "http://localhost:8000/api/v1/"); using var httpClient = new HttpClient(); var client = new ChromaClient(configOptions, httpClient);
When using a hosted version of Chroma, replace the uri
with your hosted endpoint.
Create a collection
Now that you have a client, create a collection to store movie data.
var collection = await client.GetOrCreateCollection("movies");
To perform operations on that collection, you’ll then need to create a collection client.
var collectionClient = new ChromaCollectionClient(collection, configOptions, httpClient);
Add data to your collection
Once your collection is created, it’s time to add data to it. The data we’re storing will consist of:
- Movie IDs
- Embeddings to represent the movie description.
- Metadata containing the movie title
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);
Search for movies (using vector search)
Now that your data is in the database, you can query it. In this case, we’re using vector search.
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)}");
}
}
The result should look similar to the following output.
Title: Toy Story 0.028396977
Title: Shrek 0.032012463
Watch it live
Join Jiří Činčura on the .NET Data Community Standup on February 26 to learn more about how to use Chroma and the new C# SDK.
Conclusion
This latest addition enhances the growing AI ecosystem in .NET. It paves the way for a simpler implementation of the existing Semantic Kernel connector and seamless integration into your .NET apps using foundational components like Microsoft.Extensions.VectorData and Microsoft.Extensions.AI.
We’d like to thank @ssone95 for his work and contributions to the project. We’re excited to continue building partnerships and working with the community to enable .NET developers to build AI applications.
To learn how you can start building AI apps using databases like Chroma, check out the .NET AI documentation.
Try out the Chroma C# SDK today and provide feedback.
0 comments
Be the first to start the discussion.