VectorDB
is a simple and efficient Python library for managing and querying vector-based databases using FAISS and Transformer models. It is designed to help you create, manage, update, and search through embeddings with ease.
Install VectorDB
from PyPI using pip:
pip install free-search
Start by importing the necessary components:
from free_search.db import VectorDB
To initialize a vector database, create an instance of VectorDB
:
free_db = VectorDB()
You can create a vector database with a list of texts. These texts will be embedded using a Transformer model, and the embeddings will be stored in the FAISS index.
free_db.create_db(["天氣好", "心情好", "食慾好"])
To add new data to the existing database:
free_db.add_data(["今天心情很棒", "早餐很好吃"])
To update a specific item in the database, provide the index of the item and the new text:
free_db.update_data(1, "心情非常好")
You can delete data by specifying the indices of the items you want to remove:
free_db.delete_data([0])
To search the database with a query, specify the query text and the number of top results (k) to return:
results = free_db.search(["心情好"], k=2)
for result in results:
print(result)
To save the current state of the database (index and texts) to files:
free_db.save_index("index.faiss", "texts.json")
To load a previously saved database:
free_db.load_index("index.faiss", "texts.json")
You can print all the current data stored in the database:
free_db.print_all_data()
To export all the data from the database as a list:
exported_data = free_db.export_data()
print(exported_data)
VectorDB
also supports batch operations for adding, updating, and deleting data.
-
Batch Add:
free_db.batch_add(["新資料1", "新資料2"])
-
Batch Update:
free_db.batch_update([0, 1], ["更新後資料1", "更新後資料2"])
-
Batch Delete:
free_db.batch_delete([0, 1])
...