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许可证的许可。有关详细信息,请参见许可证文件。
快乐的筛子!