sifts
v1.0.0
? SIFTS是一個簡單但功能強大的Python軟件包,用於管理和查詢文檔集合,並支持SQLite和PostgreSQL數據庫。
它旨在有效地處理全文搜索和矢量搜索,使其非常適合涉及大規模文本數據檢索的應用程序。
篩選的主要思想是利用SQLite和PostgreSQL中內置的全文搜索功能,並通過統一的Pythonic API提供它們。您可以將SQLITE用於小型項目或開發,並瑣碎地切換到PostgreSQL來擴展您的應用程序。
對於矢量搜索,餘弦相似性是通過PGVECTOR擴展在PostgreSQL中計算的,而SQLite相似性是在內存中計算的。
Sifts不帶有服務器模式,因為它是其他應用程序導入的庫。其開發的最初動機是取代Whoosh作為基於燒瓶的Gramps Web中的搜索後端。
您可以通過PIP安裝篩選:
pip install sifts import sifts
# by default, creates a new SQLite database in the working directory
collection = sifts . Collection ( name = "my_collection" )
# Add docs to the index. Can also update and delete.
collection . add (
documents = [ "Lorem ipsum dolor" , "sit amet" ],
metadatas = [{ "foo" : "bar" }, { "foo" : "baz" }], # otpional, can filter on these
ids = [ "doc1" , "doc2" ], # unique for each doc. Uses UUIDs if omitted
)
results = collection . query (
"Lorem" ,
# limit=2, # optionally limit the number of results
# where={"foo": "bar"}, # optional filter
# order_by="foo", # sort by metadata key (rather than rank)
)API靈感來自色度。
篩選支持以下搜索語法:
and操作員or操作員*通配符(在sqlite中,在搜索詞中的任何地方,僅在搜索詞結束時在postgresql中支持)無論後端如何,搜索語法都是相同的。
Sifts也可以用作矢量存儲,用於語義搜索引擎或帶有大語言模型(LLMS)的檢索型發電(RAG)。
只需將embedding_function傳遞到Collection Factory即可啟用向量存儲並在查詢方法中設置vector_search=True 。例如,使用句子變形金剛庫,
from sentence_transformers import SentenceTransformer
model = SentenceTransformer ( "intfloat/multilingual-e5-small" )
def embedding_function ( queries : list [ str ]):
return model . encode ( queries )
collection = sifts . Collection (
db_url = "sqlite:///vector_store.db" ,
name = "my_vector_store" ,
embedding_function = embedding_function
)
# Adding vector data to the collection
collection . add ([ "This is a test sentence." , "Another example query." ])
# Querying the collection with semantic search
results = collection . query ( "Find similar sentences." , vector_search = True ) PostgreSQL收集需要安裝和啟用pgvector擴展。
可以使用其ID更新或刪除文檔。
# Update a document
collection . update ( ids = [ "document_id" ], contents = [ "Updated content" ])
# Delete a document
collection . delete ( ids = [ "document_id" ])歡迎捐款!如果您遇到問題或有改進的建議,請隨時創建一個問題,甚至更好地提交PR!
SIFT已獲得MIT許可證的許可。有關詳細信息,請參見許可證文件。
快樂的篩子!