Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on Chroma.fromTexts() when creating a collection #1311

Closed
kradyy opened this issue May 18, 2023 · 8 comments
Closed

Error on Chroma.fromTexts() when creating a collection #1311

kradyy opened this issue May 18, 2023 · 8 comments

Comments

@kradyy
Copy link

kradyy commented May 18, 2023

I encountered an error while using the Chroma.fromTexts() function in the Langchain library. The error occurs when attempting to create a collection using the fromTexts() method. The function throws an error with the message throw new Error(newCollection.error).

Use the following example code snippet:

// Import necessary libraries and modules
import { Chroma, OpenAIEmbeddings } from 'langchain';

// Define the texts and metadata
const texts = [
  `Tortoise: Labyrinth? Labyrinth? Could it Are we in the notorious Little
  Harmonic Labyrinth of the dreaded Majotaur?`,
  "Achilles: Yiikes! What is that?",
  `Tortoise: They say-although I person never believed it myself-that an I
  Majotaur has created a tiny labyrinth sits in a pit in the middle of
  it, waiting innocent victims to get lost in its fears complexity.
  Then, when they wander and dazed into the center, he laughs and
  laughs at them-so hard, that he laughs them to death!`,
  "Achilles: Oh, no!",
  "Tortoise: But it's only a myth. Courage, Achilles.",
];

const metadatas = [{ id: 2 }, { id: 1 }, { id: 3 }];

// Create OpenAIEmbeddings instance
const embeddings = new OpenAIEmbeddings();

// Create Chroma vector store
const vectorStore = await Chroma.fromTexts(texts, metadatas, embeddings, {
  collectionName: "godel-escher-bach",
});

Expected Behavior:

The Chroma.fromTexts() function should create a new Chroma vector store and perform a similarity search without errors.

Actual Behavior:

An error is thrown during the creation of the collection in Chroma's index.js file. The error message received is newCollection.error. The server is running - i checked docker and i get an endpoint request with a 422 error.

chroma-server-1 | 2023-05-18 09:36:46 INFO uvicorn.access 192.168.48.1:61414 - "POST /api/v1/collections HTTP/1.1" 422

It seems to be due to getOrCreateCollection in production langchain/dist/chroma.js only passing a name but it excepts an object:

  async ensureCollection() {
        if (!this.collection) {
            if (!this.index) {
                const { ChromaClient } = await Chroma.imports();
                this.index = new ChromaClient(this.url);
            }
            this.collection = await this.index.getOrCreateCollection(this.collectionName);
        }
        return this.collection;
    }

And in /chromadb/dist/main/index.js

    async getOrCreateCollection({ name, metadata, embeddingFunction }) {
        const newCollection = await this.api
            .createCollection({
            name,
            metadata,
            'get_or_create': true
        })
            .then(handleSuccess)
            .catch(handleError);
        if (newCollection.error) {
            throw new Error(newCollection.error);
        }
        return new Collection(name, newCollection.id, this.api, newCollection.metadata, embeddingFunction);
    }

Additional Details:

Langchainjs version: 0.0.76
Chromadb version: 1.4.2

Error Stack Trace:

newCollection.error
    at getOrCreateCollection (index.js:37)
    at Chroma.addDocuments (chroma.js:82)
    at Chroma.fromTexts (chroma.js:138)
@thisisommore
Copy link

thisisommore commented May 18, 2023

+1, there seems to be signature miss match between functions like getOrCreateCollection,
Langchain: Passes name directly https://github.com/hwchase17/langchainjs/blob/2943cc382724381d38da4635d92f7525f6656127/langchain/src/vectorstores/chroma.ts#L57-L59
Chroma: Expects object
https://github.com/chroma-core/chroma/blob/main/clients/js/src/index.ts#L864

public async getOrCreateCollection({
    name,
    metadata,
    embeddingFunction
  }: {
    name: string,
    metadata?: CollectionMetadata,
    embeddingFunction?: CallableFunction
  }): Promise<Collection>

@sknightq
Copy link

sknightq commented May 19, 2023

same issue when I'm using the flowise.
What's more, the add method has a similar issue.

langchain chroma

async addVectors(vectors, documents) {
    if (vectors.length === 0) {
        return;
    }
    if (this.numDimensions === undefined) {
        this.numDimensions = vectors[0].length;
    }
    if (vectors.length !== documents.length) {
        throw new Error(`Vectors and metadatas must have the same length`);
    }
    if (vectors[0].length !== this.numDimensions) {
        throw new Error(`Vectors must have the same length as the number of dimensions (${this.numDimensions})`);
    }
    const collection = await this.ensureCollection();
    const docstoreSize = await collection.count();
    // pass the array to add method
    await collection.add(Array.from({ length: vectors.length }, (_, i) => (docstoreSize + i).toString()), vectors, documents.map(({ metadata }) => metadata), documents.map(({ pageContent }) => pageContent));
}

chromadb

async add({ ids, embeddings, metadatas, documents, }) {
    const [idsArray, embeddingsArray, metadatasArray, documentsArray] = await this.validate(true, ids, embeddings, metadatas, documents);
    const response = await this.api.add(this.id, {
        // @ts-ignore
        ids: idsArray,
        embeddings: embeddingsArray,
        // @ts-ignore
        documents: documentsArray,
        metadatas: metadatasArray,
    })
        .then(handleSuccess)
        .catch(handleError);
    return response;
}

@jacoblee93
Copy link
Collaborator

jacoblee93 commented May 25, 2023

Can you try updating your Chroma package to > 1.4.2 and bumping your LangChainJS version? #1348 should have fixed this.

@rodrigo398
Copy link

Can you try updating your Chroma package to > 1.4.2 and bumping your LangChainJS version? #1348 should have fixed this.

Yes, I've just done this and it definitely fix this issue. Thanks @jacoblee93

@jacoblee93
Copy link
Collaborator

Excellent, thank you!

@satpalsr
Copy link

satpalsr commented Jul 11, 2023

I was trying this example

 const chromaInst = await Chroma.fromDocuments(docs, new OpenAIEmbeddings(), {
        collectionName: "a-test-collection",
      });;

I get error

Error: Chroma getOrCreateCollection error: Error: TypeError: fetch failed

"langchain": "^0.0.96",
"chromadb": "^1.5.3",

@sudhamjayanthi
Copy link

I was trying this example

 const chromaInst = await Chroma.fromDocuments(docs, new OpenAIEmbeddings(), {
        collectionName: "a-test-collection",
      });;

I get error

Error: Chroma getOrCreateCollection error: Error: TypeError: fetch failed

"langchain": "^0.0.96", "chromadb": "^1.5.3",

i still get the same error! have you been able to solve it?

@jwhenry28
Copy link

jwhenry28 commented Dec 22, 2023

@satpalsr @sudhamjayanthi Did either of you discover a solution?

EDIT: It seems we all missed step one of the setup instructions:

git clone git@github.com:chroma-core/chroma.git
cd chroma
docker-compose up -d --build

This starts the Chroma server running locally as a Docker container and accessible via port 8000. Once ChromaDB is running, you should be able to get this working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants