spanking
0.1.5
使用? VectorDB类并通过美丽的UI访问其功能,请按照以下步骤:
首先,将存储库克隆到您的本地计算机:
git clone https://github.com/rishiraj/spanking.git
cd spanking要通过直观的Web界面管理矢量数据库,您可以运行提供的app.py脚本:
python app.py这将启动本地Web服务器。然后,您可以通过在Web浏览器中导航到http://127.0.0.1:5000访问UI。
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 )这将将文本编码到嵌入式中,然后将其存储在数据库中。
搜索类似的文本或图像:
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 } " )这将根据余弦相似性检索到2个最相似的文本或图像中的查询。 search方法返回一个元组列表,其中每个元组包含文本及其相似性分数。您可以使用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' )这将从名为“ vector_db.pkl”的文件中加载VectorDB实例并返回。
转换为数据框:
df = vector_db . to_df ()这将将VectorDB实例的当前状态转换为PANDAS DataFrame。
迭代存储的文本:
for text in vector_db :
print ( text )通过索引访问单个文本:
index = 2
text = vector_db [ index ]
print ( text )获取文本数:
num_texts = len ( vector_db )
print ( num_texts )这是一个示例,以说明如何使用? VectorDB类:
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 ())此示例演示了如何创建? VectorDB实例,添加文本,搜索相似的文本,更新和删除文本,然后在存储的文本上迭代。