lplalbert commented on code in PR #412:
URL: https://github.com/apache/hugegraph-ai/pull/412#discussion_r4579784248
##########
hugegraph-llm/src/hugegraph_llm/operators/index_op/vector_index_query.py:
##########
@@ -25,17 +25,45 @@
class VectorIndexQuery:
- def __init__(self, vector_index: type[VectorStoreBase], embedding:
BaseEmbedding, topk: int = 3):
+ """Retrieve matching chunks from the chunk vector index.
+
+ ``vector_dis_threshold`` is forwarded to ``VectorStoreBase.search`` and
+ defaults to the ``CHUNK_DIS_THRESHOLD`` setting (2.0, the historical
+ hard-coded value), which only prunes clearly-off chunk candidates;
+ keyword-to-vertex matching keeps its own ``VECTOR_DIS_THRESHOLD`` knob.
+ Ranking stays top-k and relevance is refined by the downstream
+ merge/rerank step.
+ """
+
+ def __init__(
+ self,
+ vector_index: type[VectorStoreBase],
+ embedding: BaseEmbedding,
+ topk: int = 3,
+ vector_dis_threshold: Optional[float] = None,
+ ):
self.embedding = embedding
self.topk = topk
+ self.vector_dis_threshold = (
+ huge_settings.chunk_dis_threshold if vector_dis_threshold is None
else vector_dis_threshold
+ )
self.vector_index =
vector_index.from_name(embedding.get_embedding_dim(), huge_settings.graph_name,
"chunks")
def run(self, context: Dict[str, Any]) -> Dict[str, Any]:
query = context.get("query")
query_embedding = self.embedding.get_texts_embeddings([query])[0]
- # TODO: why set dis_threshold=2?
- results = self.vector_index.search(query_embedding, self.topk,
dis_threshold=2)
- # TODO: check format results
- context["vector_result"] = results
- log.debug("KNOWLEDGE FROM VECTOR:\n%s", "\n".join(rel for rel in
context["vector_result"]))
+ results = self.vector_index.search(query_embedding, self.topk,
dis_threshold=self.vector_dis_threshold)
+ context["vector_result"] = self._check_results_format(results)
+ log.debug("KNOWLEDGE FROM VECTOR:\n%s",
"\n".join(context["vector_result"]))
return context
+
+ @staticmethod
+ def _check_results_format(results: List[Any]) -> List[str]:
+ """Drop non-string entries so downstream joins and reranks can rely on
the type."""
+ checked = []
+ for result in results:
+ if isinstance(result, str):
+ checked.append(result)
+ else:
+ log.warning("Filter out vector result with unexpected format:
%s", type(result).__name__)
Review Comment:
Fixed in `129cc48`.
Root cause: the Milvus store decoded every retrieved `property` VARCHAR with
a `json.loads` heuristic on the way out, so chunk text that happens to parse as
JSON (`123`, `true`, `{"answer": 42}`) came back as a number/bool/dict, and the
format check at this call site dropped the non-strings — the silent "missing
context" you describe.
Structural fix: at the producing boundary — `search()` and
`get_all_properties()` now return the stored text verbatim (the encode side
keeps one rule: strings stored as-is, other values as JSON text), and the
decode step is deleted, so already-indexed rows read back as their exact stored
text. `get_all_properties()` also stops filtering `property != ""` server-side;
an empty string is a valid property value.
Validation: red-green retrieval tests drive the real `VectorIndexQuery` over
a Milvus collection double —
`test_milvus_retrieval_keeps_json_shaped_chunks_for_chunk_query` (JSON-shaped
chunks reach `context["vector_result"]` unchanged; red before the fix keeps
only `["plain chunk"]`), plus
`test_milvus_search_returns_json_shaped_chunk_text_unchanged` and
`test_milvus_get_all_properties_round_trips_stored_text_including_empty`. Full
suite 1310 passed, 12 skipped, 2 failed (the known pre-existing Windows
mode-bit pair in `test_external_config`); ty unchanged at 2 pre-existing
diagnostics. The format check here remains as a pure type guard — every backend
now hands it strings.
Scope note: non-string `add()` values keep their existing storage form (JSON
text) and come back as that text; an envelope that restored original types
could not distinguish already-indexed JSON-shaped strings. The per-backend
contract is documented on `VectorStoreBase` in #414.
##########
hugegraph-llm/src/hugegraph_llm/nodes/index_node/vector_query_node.py:
##########
@@ -39,11 +40,17 @@ def node_init(self):
from hugegraph_llm.utils.vector_index_utils import
get_vector_index_class
# Read user configuration parameters from wk_input.
+ wk_input = cast(WkFlowInput, self.wk_input)
vector_index =
get_vector_index_class(index_settings.cur_vector_index)
embedding = Embeddings().get_embedding()
- max_items = self.wk_input.max_items if self.wk_input.max_items is
not None else 3
+ max_items = wk_input.max_items if wk_input.max_items is not None
else 3
- self.operator = VectorIndexQuery(vector_index=vector_index,
embedding=embedding, topk=max_items)
+ self.operator = VectorIndexQuery(
+ vector_index=vector_index,
+ embedding=embedding,
+ topk=max_items,
+ vector_dis_threshold=wk_input.vector_dis_threshold,
Review Comment:
Fixed in `f1b9aab` — taking the `CHUNK_DIS_THRESHOLD` option you gave, no
separate request field.
Root cause: `VectorQueryNode.node_init` forwarded the request's
`vector_dis_threshold` into `VectorIndexQuery`, where an explicit value
overrode `CHUNK_DIS_THRESHOLD`, so an explicit `0.9` pruned chunks as well as
keyword matching.
Structural fix: the node no longer passes a threshold — chunk retrieval
always prunes at `CHUNK_DIS_THRESHOLD` — and the operator parameter is renamed
`chunk_dis_threshold` so the coupling cannot return through an ambiguous name.
`SemanticIdQueryNode` still resolves the request field (falling back to
`VECTOR_DIS_THRESHOLD`) unchanged.
Validation: red-green
`test_node_init_keeps_request_threshold_out_of_chunk_retrieval` (request `1.2`
no longer reaches the operator; red before the fix pins the forward), plus
API-to-store chain tests in `test_rag_request_defaults.py` that drive request
-> flow `prepare` -> node -> real `VectorIndexQuery` -> chunk store search: an
omitted and an explicit request threshold both search chunks at
`CHUNK_DIS_THRESHOLD`, while the explicit value still reaches
`SemanticIdQuery`. Full suite 1310 passed / 2 known pre-existing failures; ty
unchanged (2 pre-existing).
##########
hugegraph-llm/src/hugegraph_llm/nodes/index_node/vector_query_node.py:
##########
@@ -43,7 +43,12 @@ def node_init(self):
embedding = Embeddings().get_embedding()
max_items = self.wk_input.max_items if self.wk_input.max_items is
not None else 3
- self.operator = VectorIndexQuery(vector_index=vector_index,
embedding=embedding, topk=max_items)
+ self.operator = VectorIndexQuery(
+ vector_index=vector_index,
+ embedding=embedding,
+ topk=max_items,
+ vector_dis_threshold=self.wk_input.vector_dis_threshold,
Review Comment:
Fixed — the omitted case resolved to the configured value in `5d587ef`, and
`f1b9aab` adds the API-to-store regression test you asked for.
Root cause of the original defect: `RAGRequest` defaulted the field to `0.9`
and the flow forwarded it verbatim, so `VECTOR_DIS_THRESHOLD` could not take
effect for existing clients. The field is now `Optional` with `None` meaning
"resolve per consumer".
Validation:
`test_omitted_request_threshold_keeps_chunk_retrieval_at_configured_default`
and `test_explicit_request_threshold_does_not_change_chunk_retrieval` in
`test_rag_request_defaults.py` drive the real hop — `RAGRequest` ->
`RAGVectorOnlyFlow.prepare` -> `VectorQueryNode` -> `VectorIndexQuery` -> chunk
store search — and assert the store is searched at `CHUNK_DIS_THRESHOLD` for
both an omitted and an explicit request threshold;
`test_explicit_request_threshold_still_scopes_keyword_matching` pins the
request value reaching `SemanticIdQuery`. The model-level tests above still pin
`None` by default and acceptance of explicit `0`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]