Tạo cơ sở dữ liệu vector với ChromaDB

Nhập môn Embeddings với OpenAI API

Emmanuel Pire

Senior Software Engineer, DataCamp

Cài đặt ChromaDB

  • ChromaDB là cơ sở dữ liệu vector đơn giản nhưng mạnh mẽ
  • Hai chế độ:
    • Cục bộ: tốt cho phát triển và thử nghiệm
    • Client/Server: phù hợp sản xuất

ChromaDB ở chế độ cục bộ và ở chế độ client/server. Ở chế độ client/server, client và server ở hai tiến trình riêng.

Nhập môn Embeddings với OpenAI API

Kết nối cơ sở dữ liệu

import chromadb

client = chromadb.PersistentClient(path="/path/to/save/to")
  • Dữ liệu sẽ được lưu trên đĩa
Nhập môn Embeddings với OpenAI API

Tạo collection

  • Collection tương tự bảng
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="..." )
)
  • Collection có thể tự tạo embedding
Nhập môn Embeddings với OpenAI API

Xem các collection

client.list_collections()
[Collection(name=my_collection)]
Nhập môn Embeddings với OpenAI API

Chèn embedding

Một tài liệu

collection.add(ids=["my-doc"], documents=["This is the source text"])
  • Phải cung cấp ID
  • Collection sẽ tự tạo embedding!

Nhiều tài liệu

collection.add(
  ids=["my-doc-1", "my-doc-2"], 
  documents=["This is document 1", "This is document 2"]
)
Nhập môn Embeddings với OpenAI API

Kiểm tra bộ sưu tập

Đếm tài liệu trong bộ sưu tập

collection.count()
3
Nhập môn Embeddings với OpenAI API

Kiểm tra bộ sưu tập

Xem nhanh 10 mục đầu

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]}
Nhập môn Embeddings với OpenAI API

Truy xuất mục

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}
Nhập môn Embeddings với OpenAI API

Bộ dữ liệu 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
Nhập môn Embeddings với OpenAI API

Ước tính chi phí embedding

  • Mô hình embedding (text-embedding-3-small) có giá $0.00002/1k token
cost = 0.00002 * len(tokens)/1000
  • Đếm token bằng thư viện tiktoken
    • pip install tiktoken
1 https://openai.com/pricing
Nhập môn Embeddings với OpenAI API

Ước tính chi phí embedding

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
Nhập môn Embeddings với OpenAI API

Ayo berlatih!

Nhập môn Embeddings với OpenAI API

Preparing Video For Download...