-
Notifications
You must be signed in to change notification settings - Fork 203
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
feat: add get_task_result_stream to new client #351
Merged
Merged
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 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 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 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 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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import asyncio | ||
from typing import Any, Generic, TypeVar | ||
import inspect | ||
from typing import Any, AsyncGenerator, Callable, Generic, TypeVar | ||
|
||
from asgiref.sync import async_to_sync | ||
from pydantic import BaseModel, ConfigDict, Field, PrivateAttr | ||
from typing_extensions import ParamSpec | ||
|
||
from llama_deploy.client.base import _BaseClient | ||
|
||
|
@@ -42,15 +44,37 @@ def list(self) -> list[T]: | |
return [self.get(id) for id in self.items.keys()] | ||
|
||
|
||
# Generic type for what's returned by the async generator | ||
_G = TypeVar("_G") | ||
# Generic parameter for the wrapped generator method | ||
_P = ParamSpec("_P") | ||
# Generic parameter for the wrapped generator method return value | ||
_R = TypeVar("_R") | ||
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. Possibly dumb question as perhaps I just missed this, but where are we using this generic param? 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. Not dumb, leftover! I'll remove it, good catch! |
||
|
||
|
||
async def _async_gen_to_list(async_gen: AsyncGenerator[_G, None]) -> list[_G]: | ||
return [item async for item in async_gen] | ||
|
||
|
||
def make_sync(_class: type[T]) -> Any: | ||
"""Wraps the methods of the given model class so that they can be called without `await`.""" | ||
|
||
class Wrapper(_class): # type: ignore | ||
class ModelWrapper(_class): # type: ignore | ||
_instance_is_sync: bool = True | ||
|
||
def generator_wrapper( | ||
func: Callable[_P, AsyncGenerator[_G, None]], /, *args: Any, **kwargs: Any | ||
) -> Callable[_P, list[_G]]: | ||
def new_func(*fargs: Any, **fkwargs: Any) -> list[_G]: | ||
return asyncio.run(_async_gen_to_list(func(*fargs, **fkwargs))) | ||
logan-markewich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return new_func | ||
|
||
for name, method in _class.__dict__.items(): | ||
# Only wrap async public methods | ||
if asyncio.iscoroutinefunction(method) and not name.startswith("_"): | ||
setattr(Wrapper, name, async_to_sync(method)) | ||
if inspect.isasyncgenfunction(method): | ||
setattr(ModelWrapper, name, generator_wrapper(method)) | ||
elif asyncio.iscoroutinefunction(method) and not name.startswith("_"): | ||
setattr(ModelWrapper, name, async_to_sync(method)) | ||
|
||
return Wrapper | ||
return ModelWrapper |
This file contains 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 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 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
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.
Oh, was this something we were running into before. Anyways, nice catch!
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.
This problem always existed but didn't run into it until @logan-markewich made me notice!