เพื่อใช้? คลาส VectorDB และเข้าถึงฟังก์ชั่นของมันผ่าน UI ที่สวยงามทำตามขั้นตอนเหล่านี้:
ก่อนอื่นให้โคลนที่เก็บไปยังเครื่องในพื้นที่ของคุณ:
git clone https://github.com/rishiraj/spanking.git
cd spanking ในการจัดการฐานข้อมูลเวกเตอร์ของคุณผ่านเว็บอินเตอร์เฟสที่ใช้งานง่ายคุณสามารถเรียกใช้สคริปต์ app.py ที่ให้ไว้:
python app.py สิ่งนี้จะเริ่มต้นเว็บเซิร์ฟเวอร์ท้องถิ่น จากนั้นคุณสามารถเข้าถึง UI ได้โดยการนำทางไปยัง http://127.0.0.1:5000 ในเว็บเบราว์เซอร์ของคุณ
VectorDB โดยทางโปรแกรม หากคุณต้องการทำงานกับรหัสคุณสามารถโต้ตอบกับคลาส VectorDB โดยตรง นี่คือวิธี:
สร้างอินสแตนซ์:
from spanking import VectorDB
vector_db = VectorDB ( model_name = 'BAAI/bge-base-en-v1.5' )คุณสามารถเลือกระบุโมเดลหม้อแปลงประโยคที่ผ่านการฝึกอบรมมาก่อนโดยส่งชื่อไปยังตัวสร้าง
เพิ่มข้อความ:
texts = [ "i eat pizza" , "i play chess" , "i drive bus" ]
vector_db . add_texts ( texts )สิ่งนี้จะเข้ารหัสข้อความลงใน embeddings และเก็บไว้ในฐานข้อมูล
ค้นหาข้อความหรือรูปภาพที่คล้ายกัน:
text_query = "we play football"
text_results = vector_db . search ( text_query , top_k = 2 , type = 'text' )
print ( "Text search results:" )
for text , similarity in text_results :
print ( f"Text: { text } , Similarity: { similarity } " )
image_url = "https://example.com/image.jpg"
image_results = vector_db . search ( image_url , top_k = 2 , type = 'image' )
print ( " n Image search results:" )
for text , similarity in image_results :
print ( f"Text: { text } , Similarity: { similarity } " ) สิ่งนี้จะดึงข้อความหรือรูปภาพที่คล้ายกันมากที่สุดไปยังแบบสอบถามตามความคล้ายคลึงกันของโคไซน์ วิธี search ส่งคืนรายการของ tuples ซึ่งแต่ละ tuple มีข้อความและคะแนนความคล้ายคลึงกัน คุณสามารถระบุประเภทการค้นหาโดยใช้พารามิเตอร์ type ( 'text' สำหรับการค้นหาข้อความและ 'image' สำหรับการค้นหาภาพ)
ลบข้อความ:
index = 1
vector_db . delete_text ( index )สิ่งนี้จะลบข้อความและการฝังที่สอดคล้องกันที่ดัชนีที่ระบุ
อัปเดตข้อความ:
index = 0
new_text = "i enjoy eating pizza"
vector_db . update_text ( index , new_text )สิ่งนี้จะอัปเดตข้อความและการฝังที่สอดคล้องกันที่ดัชนีที่ระบุด้วยข้อความใหม่
บันทึกฐานข้อมูล:
vector_db . save ( 'vector_db.pkl' ) สิ่งนี้จะบันทึกสถานะปัจจุบันของอินสแตนซ์ VectorDB ไปยังไฟล์ชื่อ 'vector_db.pkl'
โหลดฐานข้อมูล:
vector_db = VectorDB . load ( 'vector_db.pkl' ) สิ่งนี้จะโหลดอินสแตนซ์ VectorDB จากไฟล์ชื่อ 'vector_db.pkl' และส่งคืน
แปลงเป็น DataFrame:
df = vector_db . to_df () สิ่งนี้จะแปลงสถานะปัจจุบันของอินสแตนซ์ VectorDB เป็น dataframe pandas
ทำซ้ำข้อความที่เก็บไว้:
for text in vector_db :
print ( text )เข้าถึงข้อความแต่ละข้อความด้วยดัชนี:
index = 2
text = vector_db [ index ]
print ( text )รับจำนวนข้อความ:
num_texts = len ( vector_db )
print ( num_texts ) นี่คือตัวอย่างที่จะแสดงให้เห็นว่าคุณสามารถใช้งานได้อย่างไร? VectorDB Class:
from spanking import VectorDB
vector_db = VectorDB ()
# Add texts to the database
texts = [ "i eat pizza" , "i play chess" , "i drive bus" ]
vector_db . add_texts ( texts )
# Search for similar texts
query = "we play football"
top_results = vector_db . search ( query , top_k = 2 )
print ( "Top results:" )
for text , similarity in top_results :
print ( f"Text: { text } , Similarity: { similarity } " )
# Update a text
vector_db . update_text ( 1 , "i enjoy playing chess" )
# Delete a text
vector_db . delete_text ( 2 )
# Save the database
vector_db . save ( 'vector_db.pkl' )
# Load the database
loaded_vector_db = VectorDB . load ( 'vector_db.pkl' )
# Iterate over the stored texts in the loaded database
print ( " n Stored texts in the loaded database:" )
for text in loaded_vector_db :
print ( text )
# Convert to dataframe
df = loaded_vector_db . to_df ()
print ( df . head ()) ตัวอย่างนี้แสดงให้เห็นถึงวิธีการสร้าง A? อินสแตนซ์ VectorDB เพิ่มข้อความค้นหาข้อความที่คล้ายกันอัปเดตและลบข้อความและทำซ้ำผ่านข้อความที่เก็บไว้