Skip to content

Commit 4f1999f

Browse files
authored
fix: some type check and argument alignment (#64)
* fix: some type check and argument alignment Signed-off-by: Keming <kemingyang@tensorchord.ai> * fix lint Signed-off-by: Keming <kemingyang@tensorchord.ai> --------- Signed-off-by: Keming <kemingyang@tensorchord.ai>
1 parent 5e8bb89 commit 4f1999f

11 files changed

Lines changed: 544 additions & 355 deletions

File tree

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ classifiers = [
1515
]
1616
dependencies = [
1717
"defspec>=0.4.0",
18-
"falcon>=4.0.2",
18+
"falcon>=4.1.0",
1919
"httpx>=0.28.1",
2020
"msgspec>=0.19.0",
2121
"numpy>=2.0.2",
@@ -106,6 +106,7 @@ python_version = "3.10"
106106
warn_redundant_casts = true
107107
warn_unreachable = true
108108
pretty = true
109+
exclude = ["docs/build/html/_downloads/.*"]
109110

110111
[[tool.mypy.overrides]]
111112
module = [

uv.lock

Lines changed: 490 additions & 327 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vechord/chunk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ async def segment(self, text: str) -> list[str]:
158158
return chunks
159159

160160
all_chunks = []
161-
for chunk in self.regex_chunker.segment(text):
161+
for chunk in await self.regex_chunker.segment(text):
162162
chunks = await self.structure_query(
163163
self.prompt.format(size=self.size, document=chunk)
164164
)

vechord/graph.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,29 @@ def __init__(self, model: str = "en_core_web_sm"):
9696

9797
def recognize(self, text) -> list[GraphEntity]:
9898
doc = self.nlp(text)
99-
return [GraphEntity(text=ent.text, label=ent.label_) for ent in doc.ents]
99+
return [
100+
GraphEntity(text=ent.text, label=ent.label_, description="")
101+
for ent in doc.ents
102+
]
100103

101104
def recognize_with_relations(
102105
self, text
103106
) -> tuple[list[GraphEntity], list[GraphRelation]]:
104107
doc = self.nlp(text)
105-
ents = [GraphEntity(text=ent.text, label=ent.label_) for ent in doc.ents]
108+
ents = [
109+
GraphEntity(text=ent.text, label=ent.label_, description="")
110+
for ent in doc.ents
111+
]
106112
relations: list[GraphRelation] = []
107113
matches = self.matcher(doc)
108114
for _, start, end in matches:
109115
span = doc[start:end]
110116
ent0 = ent1 = None
111117
for token in span:
112118
if token.ent_type_:
113-
ent = GraphEntity(text=token.text, label=token.ent_type_)
119+
ent = GraphEntity(
120+
text=token.text, label=token.ent_type_, description=""
121+
)
114122
if ent0 is None:
115123
ent0 = ent
116124
else:
@@ -119,9 +127,13 @@ def recognize_with_relations(
119127
relations.append(
120128
GraphRelation(
121129
source=ent0
122-
or GraphEntity(text=span[0].text, label=span[0].ent_type_),
130+
or GraphEntity(
131+
text=span[0].text, label=span[0].ent_type_, description=""
132+
),
123133
target=ent1
124-
or GraphEntity(text=span[-1].text, label=span[-1].ent_type_),
134+
or GraphEntity(
135+
text=span[-1].text, label=span[-1].ent_type_, description=""
136+
),
125137
description=" ".join(token.text for token in span),
126138
)
127139
)

vechord/model/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
from vechord.model.web import (
3333
InputType,
3434
ResourceRequest,
35-
RunAck,
35+
RunIngestAck,
3636
RunRequest,
37-
RunResponse,
37+
RunSearchResponse,
3838
)
3939

4040
__all__ = [
@@ -56,9 +56,9 @@
5656
"LlamaCloudParseResponse",
5757
"ResourceRequest",
5858
"RetrievedChunk",
59-
"RunAck",
59+
"RunIngestAck",
6060
"RunRequest",
61-
"RunResponse",
61+
"RunSearchResponse",
6262
"SparseEmbedding",
6363
"UMBRELAScore",
6464
"VoyageEmbeddingRequest",

vechord/model/jina.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class JinaRerankRequest(msgspec.Struct, kw_only=True):
9090
model: Literal["jina-reranker-v2-base-multilingual", "jina-reranker-m0"]
9191
query: str
9292
top_n: int
93-
documents: list[str | JinaInput]
93+
documents: list[str] | list[JinaInput]
9494
return_documents: bool = False
9595

9696
@classmethod

vechord/model/web.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class RunRequest(msgspec.Struct, kw_only=True, frozen=True):
4141
steps: list[ResourceRequest] = msgspec.field(default_factory=list)
4242

4343

44-
class RunAck(msgspec.Struct, kw_only=True, frozen=True):
44+
class RunIngestAck(msgspec.Struct, kw_only=True, frozen=True, tag="ingest"):
4545
"""Acknowledgment of an index request."""
4646

4747
name: str
@@ -55,7 +55,7 @@ class SearchResponse(msgspec.Struct, kw_only=True, omit_defaults=True):
5555
text: Optional[str] = None
5656

5757

58-
class RunResponse(msgspec.Struct, kw_only=True, omit_defaults=True):
58+
class RunSearchResponse(msgspec.Struct, kw_only=True, omit_defaults=True, tag="search"):
5959
"""Response to a search request.
6060
6161
metrics:
@@ -87,5 +87,13 @@ def cleanup(self):
8787
for chunk in self.chunks:
8888
chunk.text = None
8989

90+
def deduplicate(self):
91+
"""Deduplicate chunks while maintain the order."""
92+
unique_chunks = {}
93+
for chunk in self.chunks:
94+
if chunk.uid not in unique_chunks:
95+
unique_chunks[chunk.uid] = chunk
96+
self.chunks = list(unique_chunks.values())
97+
9098
def reorder(self, indices: list[int]):
9199
self.chunks = [self.chunks[i] for i in indices]

vechord/pipeline.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
GraphRelation,
3434
InputType,
3535
ResourceRequest,
36-
RunAck,
36+
RunIngestAck,
3737
RunRequest,
38-
RunResponse,
38+
RunSearchResponse,
3939
)
4040
from vechord.rerank import BaseReranker, CohereReranker, JinaReranker
4141
from vechord.spec import (
@@ -209,7 +209,7 @@ def from_steps(cls, steps: list[ResourceRequest]) -> Self:
209209

210210
async def run(
211211
self, request: RunRequest, vr: "VechordRegistry"
212-
) -> RunAck | RunResponse:
212+
) -> RunIngestAck | RunSearchResponse:
213213
"""Run the dynamic pipeline with the given request."""
214214
async with set_namespace(request.name):
215215
resp = (
@@ -248,7 +248,9 @@ def _convert_from_extracted_graph(
248248
]
249249
return converted_ents, converted_rels
250250

251-
async def run_index(self, request: RunRequest, vr: "VechordRegistry") -> RunAck: # noqa: PLR0912
251+
async def run_index( # noqa: PLR0912
252+
self, request: RunRequest, vr: "VechordRegistry"
253+
) -> RunIngestAck:
252254
dim = (
253255
self.text_emb.get_dim() if self.text_emb else self.multimodal_emb.get_dim()
254256
)
@@ -335,7 +337,7 @@ async def run_index(self, request: RunRequest, vr: "VechordRegistry") -> RunAck:
335337
await self.graph_insert(
336338
ents=ents, rels=rels, ent_cls=Entity, rel_cls=Relation, vr=vr
337339
)
338-
return RunAck(name=request.name, msg="succeed", uid=doc.uid)
340+
return RunIngestAck(name=request.name, msg="succeed", uid=doc.uid)
339341

340342
async def graph_insert(
341343
self,
@@ -392,7 +394,7 @@ async def graph_insert(
392394

393395
async def run_search(
394396
self, request: RunRequest, vr: "VechordRegistry"
395-
) -> RunResponse:
397+
) -> RunSearchResponse:
396398
query = request.data.decode("utf-8")
397399

398400
# for type hint and compatibility
@@ -405,7 +407,7 @@ class Entity(_Entity):
405407
class Relation(_Relation):
406408
pass
407409

408-
resp = RunResponse()
410+
resp = RunSearchResponse()
409411
if self.search.vector:
410412
vec = (
411413
await self.text_emb.vectorize_query(query)
@@ -423,6 +425,7 @@ class Relation(_Relation):
423425
)
424426
if self.search.graph:
425427
resp.extend(await self.graph_search(query, Chunk, Entity, Relation, vr))
428+
resp.deduplicate()
426429
if self.rerank:
427430
if self.multimodal_emb:
428431
indices = await self.rerank.rerank_multimodal(
@@ -432,7 +435,7 @@ class Relation(_Relation):
432435
)
433436
else:
434437
indices = await self.rerank.rerank(
435-
query=query, chunks=[chunk.text for chunk in resp]
438+
query=query, chunks=[chunk.text for chunk in resp.chunks]
436439
)
437440
resp.reorder(indices)
438441
if self.evaluate:

vechord/provider.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
from os import environ
33
from typing import Literal
4+
from uuid import UUID
45

56
import httpx
67
import msgspec
@@ -236,7 +237,7 @@ async def parse(self, req: LlamaCloudParseRequest) -> LlamaCloudParseResponse:
236237
)
237238
return self.decoder.decode(response.content)
238239

239-
async def get_text(self, job_id: str) -> str:
240+
async def get_text(self, job_id: UUID) -> str:
240241
"""Get the text result from a LlamaCloud job."""
241242
loop = asyncio.get_running_loop()
242243
deadline = loop.time() + EXTRACT_MAX_POLLING_TIME

vechord/rerank.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ def __init__(self, model: str = "jina-reranker-m0"):
7171

7272
async def rerank(self, query: str, chunks: list[str]) -> list[int]:
7373
resp = await self.query(
74-
JinaRerankRequest.from_query_docs(query=query, docs=chunks)
74+
JinaRerankRequest.from_query_docs(
75+
query=query, documents=chunks, model=self.model
76+
)
7577
)
7678
return resp.get_indices()
7779

@@ -84,7 +86,7 @@ async def rerank_multimodal(
8486
"""
8587
resp = await self.query(
8688
JinaRerankRequest.from_query_multimodal(
87-
query=query, documents=chunks, doc_type=doc_type
89+
query=query, documents=chunks, doc_type=doc_type, model=self.model
8890
)
8991
)
9092
return resp.get_indices()

0 commit comments

Comments
 (0)