chroma-core / chroma

the AI-native open-source embedding database
https://www.trychroma.com/
Apache License 2.0
15.46k stars 1.3k forks source link

[Bug]: TypeError: Type is not JSON serializable: numpy.float64 chromadb/api/fastapi.py #1800

Open P1et1e opened 8 months ago

P1et1e commented 8 months ago

What happened?

Using chromadb.HttpClient with LllamaIndex chat_engine.stream_chat(). stream_chat execution leads to TypeError: Type is not JSON serializable: numpy.float64 triggered within chromadb/api/fastapi.py Running ChromaDB Docker Container with chromadb/chroma:0.4.23 image

Versions

Chroma v.0.4.23, Python 3.10.12, Ubuntu 22.04, chromadb/chroma:0.4.23 docker image

Relevant log output

.../site-packages/chromadb/api/fastapi.py", line 578, in _query
    data=json.dumps(
TypeError: Type is not JSON serializable: numpy.float64
P1et1e commented 8 months ago

Can be resolved by adding query_embeddings = [[float(elem) for elem in query_embeddings[0]]] to _query().

def _query( self, collection_id: UUID, query_embeddings: Embeddings, n_results: int = 10, where: Optional[Where] = {}, where_document: Optional[WhereDocument] = {}, include: Include = ["metadatas", "documents", "distances"], ) -> QueryResult: """Gets the nearest neighbors of a single embedding""" query_embeddings = [[float(elem) for elem in query_embeddings[0]]] resp = self._session.post( self._api_url + "/collections/" + str(collection_id) + "/query", data=json.dumps(

tazarov commented 8 months ago

@P1et1e, Chroma does not support f64 embeddings. The underlying hnsw lib and by extension everything else in Chroma is f32. If I recall correctly we do have NPArrays as input to the local and http APIs.

P1et1e commented 8 months ago

@tazarov Actually i am using LlamaIndex and AzureOpenAIEmbedding to generate the Embeddings. With the Persistent Client everything works fine. With the HttpClient and the ChromaDB Docker Container I received the Error. Actually I don't know exactly where the numpy.float64 is coming from.

tazarov commented 8 months ago

@P1et1e, thanks for explaining. I'll have a look

kamushekp commented 7 months ago

this fast solution might help https://github.com/chroma-core/chroma/pull/1930/commits/fc161984600ca119d28511faaf3a2b291492f096