การสร้าง vector database ด้วย ChromaDB

การสร้าง Embeddings ด้วย OpenAI API

Emmanuel Pire

Senior Software Engineer, DataCamp

การติดตั้ง ChromaDB

  • ChromaDB คือ vector database ที่เรียบง่ายแต่ทรงพลัง
  • มี 2 รูปแบบ:
    • Local: เหมาะสำหรับการพัฒนาและสร้างต้นแบบ
    • Client/Server: ออกแบบมาสำหรับ production

ChromaDB ในโหมด local เปรียบเทียบกับโหมด client/server โดยในโหมด client/server จะแสดง client และ server แยกกัน

การสร้าง Embeddings ด้วย OpenAI API

การเชื่อมต่อฐานข้อมูล

import chromadb

client = chromadb.PersistentClient(path="/path/to/save/to")
  • ข้อมูลจะถูกบันทึกลงดิสก์
การสร้าง Embeddings ด้วย OpenAI API

การสร้าง collection

  • Collection มีลักษณะคล้ายกับตาราง
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 สามารถสร้าง embedding ได้อัตโนมัติ
การสร้าง Embeddings ด้วย OpenAI API

การตรวจสอบ collection

client.list_collections()
[Collection(name=my_collection)]
การสร้าง Embeddings ด้วย OpenAI API

การแทรก embedding

เอกสารเดียว

collection.add(ids=["my-doc"], documents=["This is the source text"])
  • ต้องระบุ ID
  • Collection จะสร้าง embedding ให้อัตโนมัติ!

หลายเอกสาร

collection.add(
  ids=["my-doc-1", "my-doc-2"], 
  documents=["This is document 1", "This is document 2"]
)
การสร้าง Embeddings ด้วย OpenAI API

การตรวจสอบ collection

นับเอกสารใน collection

collection.count()
3
การสร้าง Embeddings ด้วย OpenAI API

การตรวจสอบ collection

ดู 10 รายการแรก

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]}
การสร้าง Embeddings ด้วย OpenAI API

การดึงข้อมูลรายการ

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}
การสร้าง Embeddings ด้วย OpenAI API

ชุดข้อมูล 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
การสร้าง Embeddings ด้วย OpenAI API

การประมาณต้นทุน embedding

  • โมเดล embedding (text-embedding-3-small) ราคา $0.00002/1k token
cost = 0.00002 * len(tokens)/1000
  • นับ token ด้วยไลบรารี tiktoken
    • pip install tiktoken
1 https://openai.com/pricing
การสร้าง Embeddings ด้วย OpenAI API

การประมาณต้นทุน 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
การสร้าง Embeddings ด้วย OpenAI API

มาฝึกกันเถอะ!

การสร้าง Embeddings ด้วย OpenAI API

Preparing Video For Download...