This guide walks you through integrating ChromaDB with Cycls to create an asynchronous application that can store and retrieve data efficiently using ChromaDB.

1

Set Up Your Environment

First, install the necessary dependencies. Make sure you have the dotenv package to load environment variables and the groq package for interacting with the Groq API.

pip install chromadb python-dotenv 
2

Import Required Modules

Import the necessary modules, including AsyncApp from Cycls, load_dotenv from dotenv, and the chromadb package. Also, import os to access environment variables.

from cycls import AsyncApp
from dotenv import load_dotenv
import os
import chromadb
3

Initialize ChromaDB's Client

Create an instance of the Groq client using the API key from your environment variables.

chroma_client = chromadb.Client()
4

Create a collection

Collections are where you’ll store your embeddings, documents, and any additional metadata. You can create a collection with a name:

collection = chroma_client.create_collection(name="my_collection")
5

Add some text documents to the collection

collection.add(
    documents=[
        "This is a document about pineapple",
        "This is a document about oranges"
    ],
    ids=["id1", "id2"]
)
6

Load Environment Variables

Use load_dotenv to load your environment variables from a .env file. Ensure you have CYCLS_SECRET_KEY defined in your .env file.

load_dotenv()

secret = os.getenv("CYCLS_SECRET_KEY")
7

Initialize the AsyncApp

Create an instance of AsyncApp with the necessary parameters.

app = AsyncApp(
    secret=secret,
    handler="@your-handler",
)
8

Define the Entry Point

Define the entry point function for your application. This function will process incoming messages and use the query ChromaDB to find a response.

@app
async def entry_point(context):
    received_message = context.message.content.text

    results = collection.query(
        query_texts=[received_message],
        n_results=2
    )

    await context.send.text(str(results))
9

Publish Your App

Finally, publish your app by calling the publish method.

app.publish()
10

Run Your App

Run your app in the directory containing main.py:

python main.py

Complete Example

Here is the complete code for integrating ChromaDB with Cycls:

from cycls import AsyncApp
from dotenv import load_dotenv
import chromadb
import os

load_dotenv()

chroma_client = chromadb.Client()

collection = chroma_client.create_collection(name="my_collection")

collection.add(
    documents=[
        "This is a document about pineapple",
        "This is a document about oranges",
    ],
    ids=["id1", "id2"],
)

secret = os.getenv("CYCLS_SECRET_KEY")

app = AsyncApp(
    secret=secret,
    handler="@your-handler",
)


@app
async def entry_point(context):
    received_message = context.message.content.text

    results = collection.query(query_texts=[received_message], n_results=2)

    await context.send.text(str(results))


app.publish()