Introduction to Embeddings with the OpenAI API
Emmanuel Pire
Senior Software Engineer, DataCamp
from chromadb.utils.embedding_functions import OpenAIEmbeddingFunction
collection = client.get_collection(
name="netflix_titles",
embedding_function=OpenAIEmbeddingFunction(api_key="...")
)
result = collection.query(
query_texts=["movies where people sing a lot"],
n_results=3
)
print(result)
{'ids': [['s4068', 's293', 's2213']],
'embeddings': None,
'documents': [['Title: Quién te cantará (Movie)\nDescription: When a near-...',
'Title: Quartet (Movie)\nDescription: To save their posh retirement home, ...',
'Title: Sing On! Spain (TV Show)\nDescription: In this fast-paced, high-...']],
'metadatas': [[None, None, None]],
'distances': [[0.350419282913208, 0.36049118638038635, 0.37080681324005127]]
query()
returns a dict with multiple keys:
ids
: The ids of the returned itemsembeddings
: The embeddings of the returned itemsdocuments
: The source texts of the returned itemsmetadatas
: The metadatas of the returned itemsdistances
: The distances of the returned items from the query text{'ids': [...],
'embeddings': None,
'documents': [...],
'metadatas': [...],
'distances': [...]}
collection.update(
ids=["id-1", "id-2"],
documents=["New document 1", "New document 2"]
)
collection.upsert(
ids=["id-1", "id-2"],
documents=["New document 1", "New document 2"]
)
Delete items from a collection
collection.delete(ids=["id-1", "id-2"])
Delete all collections and items
client.reset()
Introduction to Embeddings with the OpenAI API