-
Notifications
You must be signed in to change notification settings - Fork 324
Add typed root query workflow options #2222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jioffe502
wants to merge
1
commit into
NVIDIA:main
Choose a base branch
from
jioffe502:codex/query-core-2218
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2024-26, NVIDIA CORPORATION & AFFILIATES. | ||
| # All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Core query planning and execution package.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2024-26, NVIDIA CORPORATION & AFFILIATES. | ||
| # All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass, field | ||
| from typing import Sequence | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class QueryRetrievalOptions: | ||
| top_k: int = 10 | ||
| candidate_k: int | None = None | ||
| page_dedup: bool = False | ||
| content_types: str | Sequence[str] | None = None | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class QueryEmbedOptions: | ||
| embed_invoke_url: str | None = None | ||
| embed_model_name: str | None = None | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class QueryRerankOptions: | ||
| enabled: bool = False | ||
| reranker_invoke_url: str | None = None | ||
| reranker_model_name: str | None = None | ||
| reranker_backend: str | None = None | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class QueryStorageOptions: | ||
| lancedb_uri: str = "lancedb" | ||
| table_name: str = "nemo-retriever" | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class QueryRequest: | ||
| query: str | ||
| retrieval: QueryRetrievalOptions = field(default_factory=QueryRetrievalOptions) | ||
| embed: QueryEmbedOptions = field(default_factory=QueryEmbedOptions) | ||
| rerank: QueryRerankOptions = field(default_factory=QueryRerankOptions) | ||
| storage: QueryStorageOptions = field(default_factory=QueryStorageOptions) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2024-26, NVIDIA CORPORATION & AFFILIATES. | ||
| # All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
| from nemo_retriever.params import build_embed_option_kwargs | ||
| from nemo_retriever.query.options import QueryRequest, QueryRerankOptions | ||
| from nemo_retriever.retriever import Retriever | ||
| from nemo_retriever.utils.remote_auth import resolve_remote_api_key | ||
| from nemo_retriever.vdb.records import RetrievalHit | ||
|
|
||
| _LOCAL_VL_RERANK_MODEL = "nvidia/llama-nemotron-rerank-vl-1b-v2" | ||
|
|
||
|
|
||
| def _build_rerank_kwargs(options: QueryRerankOptions) -> dict[str, str]: | ||
| """Build kwargs for the rerank stage using the existing root query behavior.""" | ||
| reranker_url = (options.reranker_invoke_url or "").strip() | ||
| if reranker_url: | ||
| rerank_kwargs: dict[str, str] = {"rerank_invoke_url": reranker_url} | ||
| if options.reranker_model_name: | ||
| rerank_kwargs["model_name"] = options.reranker_model_name | ||
| api_key = resolve_remote_api_key() | ||
| if api_key is not None: | ||
| rerank_kwargs["api_key"] = api_key | ||
| return rerank_kwargs | ||
|
|
||
| local: dict[str, str] = {"model_name": options.reranker_model_name or _LOCAL_VL_RERANK_MODEL} | ||
| if options.reranker_backend: | ||
| local["local_reranker_backend"] = options.reranker_backend | ||
| return local | ||
|
|
||
|
|
||
| def _build_retriever_kwargs(request: QueryRequest) -> dict[str, Any]: | ||
| embed_kwargs = build_embed_option_kwargs(request.embed.embed_invoke_url, request.embed.embed_model_name) | ||
| retriever_kwargs: dict[str, Any] = { | ||
| "top_k": request.retrieval.top_k, | ||
| "vdb_kwargs": { | ||
| "uri": request.storage.lancedb_uri, | ||
| "table_name": request.storage.table_name, | ||
| }, | ||
| } | ||
| if embed_kwargs: | ||
| retriever_kwargs["embed_kwargs"] = embed_kwargs | ||
| if request.rerank.enabled: | ||
| rerank_kwargs = _build_rerank_kwargs(request.rerank) | ||
| retriever_kwargs["rerank"] = True | ||
| if rerank_kwargs: | ||
| retriever_kwargs["rerank_kwargs"] = rerank_kwargs | ||
| return retriever_kwargs | ||
|
|
||
|
|
||
| def query_documents(request: QueryRequest) -> list[RetrievalHit]: | ||
| """Run the SDK query path used by the root CLI.""" | ||
| retriever = Retriever(**_build_retriever_kwargs(request)) | ||
| return retriever.query( | ||
| request.query, | ||
| candidate_k=request.retrieval.candidate_k, | ||
| page_dedup=request.retrieval.page_dedup, | ||
| content_types=request.retrieval.content_types, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2024-26, NVIDIA CORPORATION & AFFILIATES. | ||
| # All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
| import nemo_retriever.query.workflow as query_workflow | ||
| from nemo_retriever.query.options import ( | ||
| QueryEmbedOptions, | ||
| QueryRerankOptions, | ||
| QueryRequest, | ||
| QueryRetrievalOptions, | ||
| QueryStorageOptions, | ||
| ) | ||
|
|
||
|
|
||
| def test_query_request_builds_retriever_kwargs_without_rerank(monkeypatch) -> None: | ||
| retriever_calls: list[dict[str, Any]] = [] | ||
|
|
||
| class FakeRetriever: | ||
| def __init__(self, **kwargs: Any) -> None: | ||
| retriever_calls.append(kwargs) | ||
|
|
||
| def query(self, query: str, **_kwargs: Any) -> list[dict[str, Any]]: | ||
| return [] | ||
|
|
||
| monkeypatch.setattr(query_workflow, "Retriever", FakeRetriever) | ||
| request = QueryRequest( | ||
| query="deployment?", | ||
| retrieval=QueryRetrievalOptions(top_k=3), | ||
| storage=QueryStorageOptions(lancedb_uri="/tmp/lancedb", table_name="docs"), | ||
| ) | ||
|
|
||
| assert query_workflow.query_documents(request) == [] | ||
| assert retriever_calls == [ | ||
| { | ||
| "top_k": 3, | ||
| "vdb_kwargs": {"uri": "/tmp/lancedb", "table_name": "docs"}, | ||
| } | ||
| ] | ||
|
|
||
|
|
||
| def test_query_request_builds_retriever_kwargs_with_embed_and_remote_rerank(monkeypatch) -> None: | ||
| retriever_calls: list[dict[str, Any]] = [] | ||
| monkeypatch.setenv("NVIDIA_API_KEY", "nvapi-test") | ||
|
|
||
| class FakeRetriever: | ||
| def __init__(self, **kwargs: Any) -> None: | ||
| retriever_calls.append(kwargs) | ||
|
|
||
| def query(self, query: str, **_kwargs: Any) -> list[dict[str, Any]]: | ||
| return [] | ||
|
|
||
| monkeypatch.setattr(query_workflow, "Retriever", FakeRetriever) | ||
| request = QueryRequest( | ||
| query="deployment?", | ||
| embed=QueryEmbedOptions( | ||
| embed_invoke_url="http://embed:8000/v1/embeddings", | ||
| embed_model_name="nvidia/llama-nemotron-embed-1b-v2", | ||
| ), | ||
| rerank=QueryRerankOptions( | ||
| enabled=True, | ||
| reranker_invoke_url="http://rerank:8000/v1/ranking", | ||
| ), | ||
| ) | ||
|
|
||
| assert query_workflow.query_documents(request) == [] | ||
| assert retriever_calls == [ | ||
| { | ||
| "top_k": 10, | ||
| "vdb_kwargs": {"uri": "lancedb", "table_name": "nemo-retriever"}, | ||
| "embed_kwargs": { | ||
| "embed_invoke_url": "http://embed:8000/v1/embeddings", | ||
| "embedding_endpoint": "http://embed:8000/v1/embeddings", | ||
| "model_name": "nvidia/llama-nemotron-embed-1b-v2", | ||
| "embed_model_name": "nvidia/llama-nemotron-embed-1b-v2", | ||
| }, | ||
| "rerank": True, | ||
| "rerank_kwargs": { | ||
| "rerank_invoke_url": "http://rerank:8000/v1/ranking", | ||
| "api_key": "nvapi-test", | ||
| }, | ||
| } | ||
| ] | ||
|
|
||
|
|
||
| def test_query_documents_uses_typed_request(monkeypatch) -> None: | ||
| retriever_calls: list[dict[str, Any]] = [] | ||
| query_calls: list[tuple[str, dict[str, Any]]] = [] | ||
|
|
||
| class FakeRetriever: | ||
| def __init__(self, **kwargs: Any) -> None: | ||
| retriever_calls.append(kwargs) | ||
|
|
||
| def query(self, query: str, **kwargs: Any) -> list[dict[str, Any]]: | ||
| query_calls.append((query, kwargs)) | ||
| return [{"text": "passage", "source": "doc.pdf", "page_number": 1}] | ||
|
|
||
| monkeypatch.setattr(query_workflow, "Retriever", FakeRetriever) | ||
|
|
||
| request = QueryRequest( | ||
| query="deployment?", | ||
| retrieval=QueryRetrievalOptions( | ||
| top_k=1, | ||
| candidate_k=3, | ||
| page_dedup=True, | ||
| content_types="text,table", | ||
| ), | ||
| ) | ||
|
|
||
| assert query_workflow.query_documents(request) == [{"text": "passage", "source": "doc.pdf", "page_number": 1}] | ||
| assert retriever_calls == [{"top_k": 1, "vdb_kwargs": {"uri": "lancedb", "table_name": "nemo-retriever"}}] | ||
| assert query_calls == [ | ||
| ( | ||
| "deployment?", | ||
| { | ||
| "candidate_k": 3, | ||
| "page_dedup": True, | ||
| "content_types": "text,table", | ||
| }, | ||
| ) | ||
| ] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.