feat: add kanon-2-reranker integration#1
Conversation
There was a problem hiding this comment.
Pull request overview
Adds support for Isaacus “kanon-2-reranker” as a LangChain BaseDocumentCompressor, alongside docs and tests, and adjusts embeddings client construction to handle SecretStr API keys correctly.
Changes:
- Introduces
IsaacusRerank(sync + async) document reranking compressor. - Adds unit + integration tests for reranking behavior and initialization.
- Updates docs/README examples and fixes embeddings API key handling when passed as
SecretStr.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 10 comments.
Show a summary per file
| File | Description |
|---|---|
langchain_isaacus/rerank.py |
New reranker integration implementing compress_documents / acompress_documents. |
langchain_isaacus/embeddings.py |
Adjusts client creation to unwrap SecretStr API keys. |
langchain_isaacus/__init__.py |
Exports IsaacusRerank from the package root. |
tests/unit_tests/test_rerank.py |
Adds unit tests for reranker initialization. |
tests/integration_tests/test_rerank.py |
Adds integration tests that call the live reranking API. |
docs/text_embedding.ipynb |
Updates embeddings notebook links/text formatting. |
docs/reranking.ipynb |
Adds reranking notebook with an end-to-end example. |
README.md |
Adds reranking usage snippet and updates embeddings snippet. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| documents = [Document("foo bar"), Document("bar foo"), Document("foo")] | ||
| rerank= IsaacusRerank(model=MODEL) | ||
| output = rerank.compress_documents(documents, query) |
| @@ -0,0 +1,28 @@ | |||
| """Test embedding model integration.""" | |||
| def test_initialization_kanon_2_reranker() -> None: | ||
| """Test embedding model initialization.""" | ||
| rr = IsaacusRerank(api_key=SecretStr("NOT_A_VALID_KEY"), model=MODEL) |
| "source": [ | ||
| "## Instantiation\n", | ||
| "\n", | ||
| "Now we can instantiate our model object and generate chat completions:" |
| __all__ = [ | ||
| "ChatIsaacus", | ||
| "IsaacusVectorStore", | ||
| "IsaacusEmbeddings", | ||
| "IsaacusLoader", | ||
| "IsaacusRetriever", | ||
| "IsaacusRerank", | ||
| "IsaacusToolkit", | ||
| "IsaacusTool", | ||
| "__version__", |
| from langchain_core.documents import Document | ||
|
|
||
| rerankings = IsaacusRerank(model="kanon-2-reranker") | ||
| embeddings.compress_documents( |
| "# Show the ranked results\n", | ||
| "print(query)\n", | ||
| "for idx, doc in enumerate(reranked):\n", | ||
| " print(f\"Rank {idx+1} ({doc.metadata[\"score\"]:.4f}): {doc.page_content}\")" |
| def rerank( | ||
| self, documents: Sequence[Union[Document, str]], query: str | ||
| ) -> list[dict[str, Any]]: | ||
| """Returns an ordered list of documents ordered by their relevance to | ||
| the query. | ||
| """ | ||
| docs = [self._document_to_str(doc) for doc in documents] |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
@copilot apply changes based on the comments in this thread |
There was a problem hiding this comment.
Pull request overview
Adds an Isaacus “kanon-2-reranker” integration to the langchain_isaacus package, alongside docs/examples and a small fix to the embeddings client initialization.
Changes:
- Introduces
IsaacusRerank(BaseDocumentCompressor) with sync/async reranking andtop_nsupport. - Fixes embeddings client construction to unwrap
SecretStrAPI keys before passing them to the Isaacus client. - Adds unit/integration tests and documentation (new reranking notebook + README updates).
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
langchain_isaacus/rerank.py |
New reranker implementation (sync/async) that compresses documents by Isaacus relevance score. |
langchain_isaacus/embeddings.py |
Changes how API keys are passed to Isaacus clients (unwrap SecretStr). |
langchain_isaacus/__init__.py |
Exports IsaacusRerank at package level. |
tests/unit_tests/test_rerank.py |
Adds unit tests covering reranker initialization behavior. |
tests/integration_tests/test_rerank.py |
Adds integration tests for reranking behavior (sync/async, top_n). |
docs/text_embedding.ipynb |
Updates embedding notebook links/text and minor formatting. |
docs/reranking.ipynb |
Adds a new reranking “getting started” notebook. |
README.md |
Adds a reranking usage example and adjusts embeddings example. |
Comments suppressed due to low confidence (1)
langchain_isaacus/init.py:23
__all__lists several symbols (ChatIsaacus,IsaacusVectorStore,IsaacusLoader, etc.) that aren’t imported or defined anywhere in this package (the only modules present areembeddings.pyandrerank.py). As a result,from langchain_isaacus import ChatIsaacuswill raiseAttributeError. Either import/re-export these symbols here (possibly via lazy imports) or remove them from__all__to match what the package actually exposes.
__all__ = [
"ChatIsaacus",
"IsaacusVectorStore",
"IsaacusEmbeddings",
"IsaacusLoader",
"IsaacusRetriever",
"IsaacusRerank",
"IsaacusToolkit",
"IsaacusTool",
"__version__",
]
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| rerankings = IsaacusRerank(model="kanon-2-reranker") | ||
| embeddings.compress_documents( | ||
| documents=[Document("The meaning of life is ...")], |
| "# Show the ranked results\n", | ||
| "print(query)\n", | ||
| "for idx, doc in enumerate(reranked):\n", | ||
| " print(f\"Rank {idx+1} ({doc.metadata[\"score\"]:.4f}): {doc.page_content}\")" |
| def _get_api_key(self) -> str: | ||
| if isinstance(self.api_key, SecretStr): | ||
| api_key = self.api_key.get_secret_value() | ||
| else: | ||
| api_key = self.api_key | ||
|
|
||
| return api_key | ||
|
|
||
| def _get_client(self) -> Isaacus: | ||
| if not self._client: | ||
| self._client = Isaacus( | ||
| api_key=self.api_key, | ||
| api_key=self._get_api_key(), | ||
| base_url=self.base_url, |
| query = "foo" | ||
| documents = [Document("foo bar")] | ||
| rerank = IsaacusRerank(model=MODEL) | ||
| output = rerank.compress_documents(documents, query) |
| query = "foo" | ||
| documents = [Document("foo bar"), Document("bar foo"), Document("foo")] | ||
| rerank = IsaacusRerank(model=MODEL) | ||
| output = await rerank.acompress_documents(documents, query) |
| """Test embedding model integration.""" | ||
|
|
||
| from langchain_core.documents import BaseDocumentCompressor | ||
| from pydantic import SecretStr | ||
|
|
||
| from langchain_isaacus import IsaacusRerank | ||
|
|
||
| MODEL = "kanon-2-reranker" | ||
|
|
||
|
|
||
| def test_initialization_kanon_2_reranker() -> None: | ||
| """Test embedding model initialization.""" | ||
| rr = IsaacusRerank(api_key=SecretStr("NOT_A_VALID_KEY"), model=MODEL) | ||
| assert isinstance(rr, BaseDocumentCompressor) |
| "source": [ | ||
| "## Instantiation\n", | ||
| "\n", | ||
| "Now we can instantiate our model object and generate chat completions:" |
| query = "foo" | ||
| documents = [Document("foo bar"), Document("bar foo"), Document("foo")] | ||
| rerank = IsaacusRerank(model=MODEL) | ||
| output = rerank.compress_documents(documents, query) |
| query = "foo" | ||
| documents = [Document("foo bar"), Document("bar foo"), Document("foo")] | ||
| rerank = IsaacusRerank(model=MODEL, top_n=2) | ||
| output = rerank.compress_documents(documents, query) |
|
@copilot apply changes based on the comments in this thread |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Kanon 2 Reranker Integration
Features:
Documentation:
Fixes: