Chroma-2.py
· 984 B · Python
原始文件
import chromadb
# setup Chroma in-memory, for easy prototyping. Can add persistence easily!
client = chromadb.Client()
# Create collection. get_collection, get_or_create_collection, delete_collection also available!
collection = client.create_collection("all-my-documents")
# Add docs to the collection. Can also update and delete. Row-based API coming soon!
collection.add(
documents=["This is document1", "This is document2"], # we handle tokenization, embedding, and indexing automatically. You can skip that and add your own embeddings as well
metadatas=[{"source": "notion"}, {"source": "google-docs"}], # filter on these!
ids=["doc1", "doc2"], # unique for each doc
)
# Query/search 2 most similar results. You can also .get by id
results = collection.query(
query_texts=["This is a query document"],
n_results=2,
# where={"metadata_field": "is_equal_to_this"}, # optional filter
# where_document={"$contains":"search_string"} # optional filter
)
1 | import chromadb |
2 | # setup Chroma in-memory, for easy prototyping. Can add persistence easily! |
3 | client = chromadb.Client() |
4 | |
5 | # Create collection. get_collection, get_or_create_collection, delete_collection also available! |
6 | collection = client.create_collection("all-my-documents") |
7 | |
8 | # Add docs to the collection. Can also update and delete. Row-based API coming soon! |
9 | collection.add( |
10 | documents=["This is document1", "This is document2"], # we handle tokenization, embedding, and indexing automatically. You can skip that and add your own embeddings as well |
11 | metadatas=[{"source": "notion"}, {"source": "google-docs"}], # filter on these! |
12 | ids=["doc1", "doc2"], # unique for each doc |
13 | ) |
14 | |
15 | # Query/search 2 most similar results. You can also .get by id |
16 | results = collection.query( |
17 | query_texts=["This is a query document"], |
18 | n_results=2, |
19 | # where={"metadata_field": "is_equal_to_this"}, # optional filter |
20 | # where_document={"$contains":"search_string"} # optional filter |
21 | ) |