-
Notifications
You must be signed in to change notification settings - Fork 0
Make LiteLLM imports lazy #49
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,11 +6,9 @@ | |||||||||||||||||
| from queue import Queue | ||||||||||||||||||
| from typing import TYPE_CHECKING, Any, AsyncGenerator, Awaitable, Callable, Generator | ||||||||||||||||||
|
|
||||||||||||||||||
| import litellm | ||||||||||||||||||
| import orjson | ||||||||||||||||||
| from anyio import create_memory_object_stream, create_task_group | ||||||||||||||||||
| from anyio.streams.memory import MemoryObjectSendStream | ||||||||||||||||||
| from litellm import ModelResponseStream | ||||||||||||||||||
|
|
||||||||||||||||||
| from dspy.dsp.utils.settings import settings | ||||||||||||||||||
| from dspy.primitives.prediction import Prediction | ||||||||||||||||||
|
|
@@ -20,6 +18,12 @@ | |||||||||||||||||
|
|
||||||||||||||||||
| logger = logging.getLogger(__name__) | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def _is_litellm_model_response_stream(value: Any) -> bool: | ||||||||||||||||||
| cls = type(value) | ||||||||||||||||||
| return cls.__name__ == "ModelResponseStream" and cls.__module__.startswith("litellm") | ||||||||||||||||||
|
Comment on lines
+22
to
+24
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.
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| if TYPE_CHECKING: | ||||||||||||||||||
| from dspy.primitives.module import Module | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -178,7 +182,7 @@ async def async_streamer(*args, **kwargs): | |||||||||||||||||
| tg.start_soon(generator, args, kwargs, send_stream) | ||||||||||||||||||
|
|
||||||||||||||||||
| async for value in receive_stream: | ||||||||||||||||||
| if isinstance(value, ModelResponseStream): | ||||||||||||||||||
| if _is_litellm_model_response_stream(value): | ||||||||||||||||||
| if len(predict_id_to_listener) == 0: | ||||||||||||||||||
| # No listeners are configured, yield the chunk directly for backwards compatibility. | ||||||||||||||||||
| yield value | ||||||||||||||||||
|
|
@@ -271,7 +275,7 @@ async def streaming_response(streamer: AsyncGenerator) -> AsyncGenerator: | |||||||||||||||||
| if isinstance(value, Prediction): | ||||||||||||||||||
| data = {"prediction": dict(value.items(include_dspy=False))} | ||||||||||||||||||
| yield f"data: {orjson.dumps(data).decode()}\n\n" | ||||||||||||||||||
| elif isinstance(value, litellm.ModelResponseStream): | ||||||||||||||||||
| elif _is_litellm_model_response_stream(value): | ||||||||||||||||||
| data = {"chunk": value.json()} | ||||||||||||||||||
| yield f"data: {orjson.dumps(data).decode()}\n\n" | ||||||||||||||||||
| elif isinstance(value, str) and value.startswith("data:"): | ||||||||||||||||||
|
|
||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.