Skip to content

Commit 749ebb1

Browse files
authored
Merge pull request #17 from philippgille/copy-metadata-in-constructors
Copy metadata in constructors
2 parents 7237b9d + 4b6b4ea commit 749ebb1

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

‎collection.go‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@ type Collection struct {
2323
// We don't export this yet to keep the API surface to the bare minimum.
2424
// Users create collections via [Client.CreateCollection].
2525
func newCollection(name string, metadata map[string]string, embed EmbeddingFunc) *Collection {
26+
// We copy the metadata to avoid data races in case the caller modifies the
27+
// map after creating the collection while we range over it.
28+
m := make(map[string]string, len(metadata))
29+
for k, v := range metadata {
30+
m[k] = v
31+
}
2632
return &Collection{
2733
Name: name,
28-
metadata: metadata,
34+
metadata: m,
2935

3036
documents: make(map[string]*document),
3137

@@ -36,8 +42,10 @@ func newCollection(name string, metadata map[string]string, embed EmbeddingFunc)
3642
// Add embeddings to the datastore.
3743
//
3844
// - ids: The ids of the embeddings you wish to add
39-
// - embeddings: The embeddings to add. If nil, embeddings will be computed based on the documents using the embeddingFunc set for the Collection. Optional.
40-
// - metadatas: The metadata to associate with the embeddings. When querying, you can filter on this metadata. Optional.
45+
// - embeddings: The embeddings to add. If nil, embeddings will be computed based
46+
// on the documents using the embeddingFunc set for the Collection. Optional.
47+
// - metadatas: The metadata to associate with the embeddings. When querying,
48+
// you can filter on this metadata. Optional.
4149
// - documents: The documents to associate with the embeddings.
4250
//
4351
// A row-based API will be added when Chroma adds it (they already plan to).

‎document.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ func newDocument(ctx context.Context, id string, embeddings []float32, metadata
2222
}
2323
embeddings = vectors
2424
}
25+
// We copy the metadata to avoid data races in case the caller modifies the
26+
// map after creating the document while we range over it.
27+
m := make(map[string]string, len(metadata))
28+
for k, v := range metadata {
29+
m[k] = v
30+
}
2531

2632
return &document{
2733
ID: id,

0 commit comments

Comments
 (0)