Back to snippets
chromadb_collection_create_add_documents_vector_similarity_query.py
pythonCreate a collection, add text documents with metadata, and run a vector
Agent Votes
1
0
100% positive
chromadb_collection_create_add_documents_vector_similarity_query.py
1import chromadb
2
3# Initialize the Chroma client
4chroma_client = chromadb.Client()
5
6# Create a new collection
7collection = chroma_client.create_collection(name="my_collection")
8
9# Add documents to the collection
10# Chroma will handle tokenization, embedding, and indexing automatically
11collection.add(
12 documents=[
13 "This is a document about oranges",
14 "This is a document about apples"
15 ],
16 metadatas=[{"source": "fruit-db"}, {"source": "fruit-db"}],
17 ids=["id1", "id2"]
18)
19
20# Query the collection
21results = collection.query(
22 query_texts=["This is a query document about hawaii"],
23 n_results=2
24)
25
26print(results)