Перейти до основного вмісту

How to Work with Chroma DB

Unlock the power of Chroma, a versatile database management system, for efficient data storage and retrieval in your UBOS projects!

примітка

Chroma has become a cornerstone in modern data management. This guide will walk you through connecting and using Chroma in Node-Red.

1. Initial Connection to Chroma Client 🔌

Prerequisites

In the "function" node, install these npm libraries:

modules

Initializing the Chroma Client

Use this code snippet to set up your Chroma client:

const { ChromaClient, OpenAIEmbeddingFunction } = chromadb;

const embedder = new OpenAIEmbeddingFunction({
openai_api_key: "your_API_Key"
});

const client = new ChromaClient({
path: "path_to_your_chroma_client"
});
інформація

The OpenAIEmbeddingFunction is a convenient wrapper for OpenAI's embedding API. It requires an API key, which you can obtain by signing up at OpenAI.

2. Working with Collections 📚

Create a Collection

const collection = await client.createCollection({ 
name: 'Your_collection_name',
embeddingFunction: embedder
});

return msg;

Get a Collection

After creating a collection, use getCollection to work with it:

const collection = await client.getCollection({ 
name: 'Your_collection_name'
});

3. Managing Data in Collections 🔄

Insert Data (collection.add)

Before inserting, create embeddings and store them in msg.emb.

const create = await collection.add({
ids: [`your_vector_id`],
embeddings: msg.emb,
metadatas: ['your_custom_metadata'],
documents: 'your_document_name'
});

Query Data (collection.query)

Create embeddings from the user request and store in msg.emb before querying.

const result = await collection.query({    
queryEmbeddings: msg.emb,
query_text: 'user_request',
nResults: 2 // Number of results to return
});

Usage Example 🌟

example

Here's a practical example of using Chroma DB in a flow:

  • User asks: "Who was your mother?"
  • Create a vector (embedding) from this question embeding
  • Query the shevchenko collection in ChromaDB
  • Output the result to msg.result result

This example demonstrates creating an embedding, querying a collection, and processing the result.

інформація

Remember to handle errors and edge cases in your production code!

🚀 Ready to supercharge your data management with Chroma DB in UBOS? Start implementing these techniques today! 💡