-
Notifications
You must be signed in to change notification settings - Fork 618
UN-3215 [FIX] Add LLMCompat bridge class to fix retriever LLM compatibility with llama-index #1788
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
pk-zipstack
wants to merge
31
commits into
main
Choose a base branch
from
fix/retriever-llm-bridge-class
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
Show all changes
31 commits
Select commit
Hold shift + click to select a range
0acab2b
Added bridge class for llms to fix retrieval issue
pk-zipstack 5fc3ef8
Fixed import error with sub-question retrieval
pk-zipstack dc57c53
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0055521
[FEAT] Rewrite LLMCompat to emulate llama-index interface without dep…
hari-kuriakose 0be9fb1
Merge branch 'main' into fix/retriever-llm-bridge-class
pk-zipstack 922a8a1
Remove investigation notes file from branch
pk-zipstack 4a6c392
Update prompt-service/src/unstract/prompt_service/core/retrievers/bas…
pk-zipstack 92dba83
Address PR review comments: simplify LLM conversion in retrievers
pk-zipstack a1040c1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e3e91fe
Merge branch 'main' into fix/retriever-llm-bridge-class
pk-zipstack 2b3c3ff
Merge branch 'main' into fix/retriever-llm-bridge-class
pk-zipstack 306e3e7
Add LLMCompat.from_llm() factory and unit tests for retriever LLM
pk-zipstack 97f5d2c
Commit uv.lock changes
pk-zipstack c6fe158
Move SDK1 tests to sdk1/tests and keep only RetrieverLLM tests in pro…
pk-zipstack cbec777
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 211baf6
Address code review feedback from greptile
pk-zipstack 3273408
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 12f78e1
Fix test mock for system_prompt and move litellm.drop_params to modul…
pk-zipstack 2ac1613
Make RetrieverLLM construction lazy to avoid redundant init
pk-zipstack 0b9c5e5
Merge branch 'main' into fix/retriever-llm-bridge-class
pk-zipstack d55f8e4
Reuse existing LLM instance in LLMCompat.from_llm() instead of re-cre…
pk-zipstack ebeb1a3
Delegate LLMCompat calls to LLM instead of calling litellm directly
pk-zipstack f8f88c0
Add tests for LLMCompat delegation and BaseRetriever.llm lazy property
pk-zipstack 787eb0d
Use drop litellm params at the module level
pk-zipstack 4d68900
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3b5b6f0
Update LLMCompat and _messages_to_prompt docstrings
pk-zipstack 17aabaf
Preserve system messages in _messages_to_prompt and add pytest-asynci…
pk-zipstack b77c707
Commit uv.lock changes
pk-zipstack aa2e5ee
Merge branch 'main' into fix/retriever-llm-bridge-class
pk-zipstack 08564f1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b4f63d5
Add require_llm() guard for retrievers that need an LLM
pk-zipstack 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
126 changes: 126 additions & 0 deletions
126
prompt-service/src/unstract/prompt_service/core/retrievers/retriever_llm.py
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,126 @@ | ||
| from collections.abc import Sequence | ||
| from typing import Any | ||
|
|
||
| from llama_index.core.base.llms.types import ( | ||
| ChatMessage, | ||
| ChatResponse, | ||
| ChatResponseAsyncGen, | ||
| ChatResponseGen, | ||
| CompletionResponse, | ||
| CompletionResponseAsyncGen, | ||
| CompletionResponseGen, | ||
| LLMMetadata, | ||
| MessageRole, | ||
| ) | ||
| from llama_index.core.llms.llm import LLM as LlamaIndexBaseLLM # noqa: N811 | ||
| from pydantic import PrivateAttr | ||
|
|
||
| from unstract.sdk1.llm import LLM, LLMCompat | ||
|
|
||
|
|
||
| class RetrieverLLM(LlamaIndexBaseLLM): | ||
| """Bridges SDK1's LLMCompat with llama-index's LLM for retriever use. | ||
|
|
||
| Llama-index's ``resolve_llm()`` asserts ``isinstance(llm, LLM)`` | ||
| where ``LLM`` is ``llama_index.core.llms.llm.LLM``. Since SDK1's | ||
| ``LLMCompat`` is a plain class without llama-index inheritance, | ||
| it fails this check. | ||
|
|
||
| ``RetrieverLLM`` inherits from llama-index's ``LLM`` base class | ||
| (passing the isinstance check) and delegates all LLM calls to an | ||
| internal ``LLMCompat`` instance. | ||
| """ | ||
|
|
||
| _compat: LLMCompat = PrivateAttr() | ||
|
|
||
| def __init__(self, llm: LLM, **kwargs: Any) -> None: # noqa: ANN401 | ||
| """Initialize with an SDK1 LLM instance.""" | ||
| super().__init__(**kwargs) | ||
| self._compat = LLMCompat.from_llm(llm) | ||
|
|
||
| @property | ||
| def metadata(self) -> LLMMetadata: | ||
| return LLMMetadata( | ||
| is_chat_model=True, | ||
| model_name=self._compat.get_model_name(), | ||
| ) | ||
|
|
||
| # ── Sync ───────────────────────────────────────────────────────────────── | ||
|
|
||
| def chat( | ||
| self, | ||
| messages: Sequence[ChatMessage], | ||
| **kwargs: Any, # noqa: ANN401 | ||
| ) -> ChatResponse: | ||
| result = self._compat.chat(messages, **kwargs) | ||
| return ChatResponse( | ||
| message=ChatMessage( | ||
| role=MessageRole.ASSISTANT, | ||
| content=result.message.content, | ||
| ), | ||
| raw=result.raw, | ||
| ) | ||
pk-zipstack marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def complete( | ||
| self, | ||
| prompt: str, | ||
| formatted: bool = False, | ||
| **kwargs: Any, # noqa: ANN401 | ||
| ) -> CompletionResponse: | ||
| result = self._compat.complete(prompt, formatted=formatted, **kwargs) | ||
| return CompletionResponse(text=result.text, raw=result.raw) | ||
|
|
||
| def stream_chat( | ||
| self, | ||
| messages: Sequence[ChatMessage], | ||
| **kwargs: Any, # noqa: ANN401 | ||
| ) -> ChatResponseGen: | ||
| raise NotImplementedError("stream_chat is not supported.") | ||
|
|
||
| def stream_complete( | ||
| self, | ||
| prompt: str, | ||
| formatted: bool = False, | ||
| **kwargs: Any, # noqa: ANN401 | ||
| ) -> CompletionResponseGen: | ||
| raise NotImplementedError("stream_complete is not supported.") | ||
|
|
||
| # ── Async ──────────────────────────────────────────────────────────────── | ||
|
|
||
| async def achat( | ||
| self, | ||
| messages: Sequence[ChatMessage], | ||
| **kwargs: Any, # noqa: ANN401 | ||
| ) -> ChatResponse: | ||
| result = await self._compat.achat(messages, **kwargs) | ||
| return ChatResponse( | ||
| message=ChatMessage( | ||
| role=MessageRole.ASSISTANT, | ||
| content=result.message.content, | ||
| ), | ||
| raw=result.raw, | ||
| ) | ||
|
|
||
| async def acomplete( | ||
| self, | ||
| prompt: str, | ||
| formatted: bool = False, | ||
| **kwargs: Any, # noqa: ANN401 | ||
| ) -> CompletionResponse: | ||
| result = await self._compat.acomplete(prompt, formatted=formatted, **kwargs) | ||
| return CompletionResponse(text=result.text, raw=result.raw) | ||
|
|
||
| async def astream_chat( | ||
| self, | ||
| messages: Sequence[ChatMessage], | ||
| **kwargs: Any, # noqa: ANN401 | ||
| ) -> ChatResponseAsyncGen: | ||
| raise NotImplementedError("astream_chat is not supported.") | ||
|
|
||
| async def astream_complete( | ||
| self, | ||
| prompt: str, | ||
| formatted: bool = False, | ||
| **kwargs: Any, # noqa: ANN401 | ||
| ) -> CompletionResponseAsyncGen: | ||
| raise NotImplementedError("astream_complete is not supported.") | ||
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
Empty file.
12 changes: 12 additions & 0 deletions
12
prompt-service/src/unstract/prompt_service/tests/unit/conftest.py
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,12 @@ | ||
| """Pytest configuration for unit tests. | ||
|
|
||
| Unit tests should not require external dependencies or the full app. | ||
| This conftest intentionally does NOT import Flask app components. | ||
|
|
||
| WARNING: This file is NOT auto-loaded when running via tox because | ||
| --noconftest is used to skip the parent tests/conftest.py (which | ||
| imports Flask blueprints and triggers the full adapter import chain). | ||
| If you add shared fixtures here, either remove --noconftest from | ||
| tox.ini and fix the parent conftest's eager imports, or define | ||
| fixtures directly in test files. | ||
| """ |
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.