把简洁当作核心特性:Vechord 优先追求极简的 API 表面积以及特定任务下的易用性,旨在让你以最少的样板代码和心智负担在 Postgres 上实现混合搜索。
快速上手 Vechord
定义表结构(Schema)
fromtypingimportAnnotated,Optionalfromvechord.specimportTable,Vector,PrimaryKeyAutoIncrease,ForeignKey,Keyword# use 768 dimension vectorDenseVector=Vector[768]classDocument(Table,kw_only=True):uid:Optional[PrimaryKeyAutoIncrease]=None# auto-increase id, no need to setlink:str=""text:strclassChunk(Table,kw_only=True)uid:Optional[PrimaryKeyAutoIncrease]=Nonedoc_id:Annotated[int,ForeignKey[Document.uid]]# reference to `Document.uid` on DELETE CASCADEvec:DenseVector# this comes with a default vector indexkeyword:Keyword# this comes with a default tokenizer and text indextext:str
使用 Python 装饰器注入数据
importhttpxfromvechord.registryimportVechordRegistryfromvechord.extractimportSimpleExtractorfromvechord.embeddingimportGeminiDenseEmbeddingvr=VechordRegistry(namespace="test",url="postgresql://postgres:[email protected]:5432/")# ensure the table and index are created if not existsvr.register([Document,Chunk])extractor=SimpleExtractor()emb=GeminiDenseEmbedding()@vr.inject(output=Document)# dump to the `Document` table# function parameters are free to define since `inject(input=...)` is not setdefadd_document(url:str)->Document:# the return type is `Document`withhttpx.Client()asclient:resp=client.get(url)text=extractor.extract_html(resp.text)returnDocument(link=url,text=text)@vr.inject(input=Document,output=Chunk)# load from the `Document` table and dump to the `Chunk` table# function parameters are the attributes of the `Document` table, only defined attributes# will be loaded from the `Document` tabledefadd_chunk(uid:int,text:str)->list[Chunk]:# the return type is `list[Chunk]`chunks=text.split("\n")return[Chunk(doc_id=uid,vec=emb.vectorize_chunk(t),keyword=Keyword(t),text=t)fortinchunks]if__name__=="__main__":add_document("https://paulgraham.com/best.html")# add arguments as usualadd_chunk()# omit the arguments since the `input` is will be loaded from the `Document` tablevr.insert(Document(text="hello world"))# insert manuallyprint(vr.select_by(Document.partial_init()))# select all the columns from table `Document`
在单个事务中运行多个步骤以保证数据一致性
pipeline=vr.create_pipeline([add_document,add_chunk])pipeline.run("https://paulgraham.com/best.html")# only accept the arguments for the first function