Vytváření vektorových databází s ChromaDB

Introduction to Embeddings with the OpenAI API

Emmanuel Pire

Senior Software Engineer, DataCamp

Instalace ChromaDB

  • ChromaDB je jednoduchá, ale výkonná vektorová databáze
  • Dva režimy:
    • Lokální: vhodný pro vývoj a prototypování
    • Klient/Server: určen pro produkci

ChromaDB v lokálním režimu a v režimu klient/server. V režimu klient/server jsou klient a server zobrazeny jako samostatné instance.

Introduction to Embeddings with the OpenAI API

Připojení k databázi

import chromadb

client = chromadb.PersistentClient(path="/path/to/save/to")
  • Data budou uložena na disk
Introduction to Embeddings with the OpenAI API

Vytvoření kolekce

  • Kolekce jsou analogií tabulek
from chromadb.utils.embedding_functions import OpenAIEmbeddingFunction
collection = client.create_collection(
    name="my_collection",

embedding_function=OpenAIEmbeddingFunction( model_name="text-embedding-3-small", api_key="..." )
)
  • Kolekce dokáží vytvářet vkládání automaticky
Introduction to Embeddings with the OpenAI API

Prohlížení kolekcí

client.list_collections()
[Collection(name=my_collection)]
Introduction to Embeddings with the OpenAI API

Vkládání embeddingů

Jeden dokument

collection.add(ids=["my-doc"], documents=["This is the source text"])
  • ID musí být zadána
  • Vkládání vytvoří kolekce automaticky!

Více dokumentů

collection.add(
  ids=["my-doc-1", "my-doc-2"], 
  documents=["This is document 1", "This is document 2"]
)
Introduction to Embeddings with the OpenAI API

Prozkoumání kolekce

Počítání dokumentů v kolekci

collection.count()
3
Introduction to Embeddings with the OpenAI API

Prozkoumání kolekce

Náhled prvních 10 položek

collection.peek()
{'ids': ['my-doc', 'my-doc-1', 'my-doc-2'],
 'embeddings': [[...], [...], [...]],
 'documents': ['This is the source text',
  'This is document 1',
  'This is document 2'],
 'metadatas': [None, None, None]}
Introduction to Embeddings with the OpenAI API

Načítání položek

collection.get(ids=["s59"])
{'ids': ['s59'],
 'embeddings': None,
 'metadatas': [None],
 'documents': ['Title: Naruto Shippûden the Movie: The Will of Fire (Movie)\nDescription: When ...'],
 'uris': None,
 'data': None}
Introduction to Embeddings with the OpenAI API

Datová sada Netflix

 

Title: Kota Factory (TV Show)
Description: In a city of coaching centers known to train India's finest...
Categories: International TV Shows, Romantic TV Shows, TV Comedies
Title: The Last Letter From Your Lover (Movie)
Description: After finding a trove of love letters from 1965, a reporter sets...
Categories: Dramas, Romantic Movies
Introduction to Embeddings with the OpenAI API

Odhad nákladů na vkládání

  • Model pro vkládání (text-embedding-3-small) stojí $0,00002 / 1 tis. tokenů
cost = 0.00002 * len(tokens)/1000
  • Tokeny spočítáte pomocí knihovny tiktoken
    • pip install tiktoken
1 https://openai.com/pricing
Introduction to Embeddings with the OpenAI API

Odhad nákladů na vkládání

import tiktoken

enc = tiktoken.encoding_for_model("text-embedding-3-small")

total_tokens = sum(len(enc.encode(text)) for text in documents)
cost_per_1k_tokens = 0.00002 print('Total tokens:', total_tokens) print('Cost:', cost_per_1k_tokens * total_tokens/1000)
Total tokens: 444463
Cost: 0.00888926
Introduction to Embeddings with the OpenAI API

Pojďme si procvičit!

Introduction to Embeddings with the OpenAI API

Preparing Video For Download...