-
Notifications
You must be signed in to change notification settings - Fork 3.4k
prewarm LLM to skip connection establishment #6484
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,6 +102,7 @@ class LLM( | |
| def __init__(self) -> None: | ||
| super().__init__() | ||
| self._label = f"{type(self).__module__}.{type(self).__name__}" | ||
| self._prewarm_task: asyncio.Task[None] | None = None | ||
|
|
||
| @property | ||
| def label(self) -> str: | ||
|
|
@@ -143,11 +144,50 @@ def chat( | |
| extra_kwargs: NotGivenOr[dict[str, Any]] = NOT_GIVEN, | ||
| ) -> LLMStream: ... | ||
|
|
||
| def prewarm(self) -> None: | ||
| """Pre-warm connection to the LLM service""" | ||
| pass | ||
| def prewarm(self, *, loop: asyncio.AbstractEventLoop | None = None) -> None: | ||
| """Pre-warm connection to the LLM service. | ||
|
|
||
| async def aclose(self) -> None: ... | ||
| Establishes DNS resolution and the TLS connection to the provider before the | ||
| first inference request, reducing time-to-first-token on the initial reply. | ||
| It is called automatically when an ``AgentSession`` is constructed and when an | ||
| agent activity starts, but can also be called directly. | ||
|
|
||
| Non-blocking (fire-and-forget) and idempotent. Providers enable it by | ||
| overriding ``_prewarm_impl``. | ||
|
|
||
| Args: | ||
| loop: Event loop to schedule the prewarm request on. Defaults to the | ||
| running event loop. | ||
| """ | ||
| if type(self)._prewarm_impl is LLM._prewarm_impl: | ||
| return # no provider-specific prewarm implemented | ||
|
|
||
| if self._prewarm_task is not None: | ||
| return | ||
|
|
||
| if loop is None: | ||
| loop = asyncio.get_event_loop() | ||
|
|
||
| async def _prewarm() -> None: | ||
| try: | ||
| await self._prewarm_impl() | ||
| except Exception: | ||
| pass | ||
|
|
||
| self._prewarm_task = loop.create_task(_prewarm()) | ||
|
|
||
| async def _prewarm_impl(self) -> None: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: should we add support for FallbackAdapter? |
||
| """Provider-specific prewarm request, overriding it enables ``prewarm()``. | ||
|
|
||
| Implementations should perform a cheap, token-free request (e.g. listing | ||
| models) using the same client that serves chat requests, so DNS + TLS are | ||
| established and a keep-alive connection is left in the pool. Exceptions | ||
| are swallowed by ``prewarm()``; subclasses overriding ``aclose`` must call | ||
| ``await super().aclose()`` to cancel an in-flight prewarm.""" | ||
|
|
||
| async def aclose(self) -> None: | ||
| if self._prewarm_task is not None: | ||
| await aio.cancel_and_wait(self._prewarm_task) | ||
|
|
||
| async def __aenter__(self) -> LLM: | ||
| return self | ||
|
|
||
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we clear the
_prewarm_taskhere?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we would? if this has failed, we'd just wait for normal cleanup?