GithubHelp home page GithubHelp logo

sdozono / chromadbsharp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ksanman/chromadbsharp

0.0 0.0 0.0 39 KB

Library to interface with an instance of ChromaDB

License: MIT License

C# 100.00%

chromadbsharp's Introduction

ChromaDB Sharp

ChromaDBSharp is a wrapper around the Chroma API that exposes all functionality of that API to .NET. The project follows the ChromaDB Python and JavaScript client patterns.

Library is consumed as a .net standard 2.1 library.

Nuget

ChromaDBSharp

How to Use

ChromaDB is designed to be used against a deployed version of ChromaDB. See HERE for official documentation on how to deploy ChromaDB.

Each Chroma call features a syncronous and and asyncronous version.

// Create your HttpClient and set the base address to the chroma instance
using HttpClient client = new();
client.BaseAddress = new Uri("http://localhost:8000/"); // 
Using local docker version for example.

ChromaDBClient client = new(httpClient);

// Additional options
ChromaDBClient client = new(httpClient, tenantName, databaseName);

string version = client.Version();
long heartbeat = await client.HeartbeatAsync();
  • Creating a client using Dependency Injection.
... //Create app builder
builder.Services.RegisterChromaDBSharp("http://localhost:8000/");

builder.Services.RegisterChromaDBSharp(client => {
// Configure HTTP client here. For example, add authentication.
client.BaseAddress = new Uri("http://localhost:8000/");
byte[] byteArray = Encoding.ASCII.GetBytes("username:password");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
});
  • Creating a collection

Collections rquire an embedding function otherwise an error will through. Define an embedding class as follows:

public sealed class CustomEmbedder : IEmbeddable
{
    public Task<IEnumerable<IEnumerable<float>>> Generate(IEnumerable<string> texts)
    {
        // Embedding logic here
        // For example, call an API, create custom c\# embedding logic, or use library. this is for demonstration only.
        ...
        return embeddings.
    }
}

For example, using AllMiniLML6v2Sharp

public sealed class AllMiniEmbedder : IEmbeddable
{
    private readonly IEmbedder _embedder;
    public AllMiniEmbedding()
    {
        _embedder = new AllMiniLmL6V2Embedder(modelPath: "path/to/model", tokenizer: new AllMiniLmL6V2Sharp.Tokenizer.BertTokenizer("path/to/vocab"));
    }
    public async Task<IEnumerable<IEnumerable<float>>> Generate(IEnumerable<string> texts)
    {
        IEnumerable<IEnumerable<float>> result = _embedder.GenerateEmbeddings(texts);
        return await Task.FromResult(result);
    }
}

Pass into collection when fetching.

IEmbeddable customEmbeddingFunction = new CustomEmbedder();
ICollectionClient collection = client.CreateCollection("Collection Name", metadata: new Dictionary<string, object> { {"prop1", "value 1"},{"prop2",2}}, embeddingFunction: customEmbeddingFunction);
  • Add documents
collection.Add(documents: new[] { "This is document 1", "This is document 2" }, metadatas: new[] { new Dictionary<string, object> { { "source", "notion" } }, new Dictionary<string, object> { { "source", "google-docs" } } }, ids: new[] { "doc 1", "doc 2" });
  • Query Documents
QueryResult result = collection.Query(queryTexts: new[] { "This is a query document" }, numberOfResults: 5);
            
  • Query Documents with a where clause.
QueryResult result = collection.Query(queryTexts: new[] { "This is a query document" }, where: new Dictionary<string, object> {{"source", "notion"}},  numberOfResults: 5);
            

Example Applications

chromadbsharp's People

Contributors

ksanman avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.