From 4b6b4eace4d4395cad204b74181d5c2a07a78c31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Gill=C3=A9?= Date: Sun, 18 Feb 2024 13:56:29 +0100 Subject: [PATCH] Copy metadata in constructors --- collection.go | 14 +++++++++++--- document.go | 6 ++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/collection.go b/collection.go index d367a31..01c0351 100644 --- a/collection.go +++ b/collection.go @@ -23,9 +23,15 @@ type Collection struct { // We don't export this yet to keep the API surface to the bare minimum. // Users create collections via [Client.CreateCollection]. func newCollection(name string, metadata map[string]string, embed EmbeddingFunc) *Collection { + // We copy the metadata to avoid data races in case the caller modifies the + // map after creating the collection while we range over it. + m := make(map[string]string, len(metadata)) + for k, v := range metadata { + m[k] = v + } return &Collection{ Name: name, - metadata: metadata, + metadata: m, documents: make(map[string]*document), @@ -36,8 +42,10 @@ func newCollection(name string, metadata map[string]string, embed EmbeddingFunc) // Add embeddings to the datastore. // // - ids: The ids of the embeddings you wish to add -// - embeddings: The embeddings to add. If nil, embeddings will be computed based on the documents using the embeddingFunc set for the Collection. Optional. -// - metadatas: The metadata to associate with the embeddings. When querying, you can filter on this metadata. Optional. +// - embeddings: The embeddings to add. If nil, embeddings will be computed based +// on the documents using the embeddingFunc set for the Collection. Optional. +// - metadatas: The metadata to associate with the embeddings. When querying, +// you can filter on this metadata. Optional. // - documents: The documents to associate with the embeddings. // // A row-based API will be added when Chroma adds it (they already plan to). diff --git a/document.go b/document.go index 20730f4..49b8a38 100644 --- a/document.go +++ b/document.go @@ -22,6 +22,12 @@ func newDocument(ctx context.Context, id string, embeddings []float32, metadata } embeddings = vectors } + // We copy the metadata to avoid data races in case the caller modifies the + // map after creating the document while we range over it. + m := make(map[string]string, len(metadata)) + for k, v := range metadata { + m[k] = v + } return &document{ ID: id,