-
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 all commits
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 | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,42 @@ | ||||||||||||||||||||||||||||||||||||||||||
| from __future__ import annotations | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| import functools | ||||||||||||||||||||||||||||||||||||||||||
| import sys | ||||||||||||||||||||||||||||||||||||||||||
| import types | ||||||||||||||||||||||||||||||||||||||||||
| from typing import Any | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| from dspy.utils.lazy_import import require | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| @functools.cache | ||||||||||||||||||||||||||||||||||||||||||
| def _configure_litellm_defaults(litellm: types.ModuleType) -> None: | ||||||||||||||||||||||||||||||||||||||||||
| """Apply DSPy's global LiteLLM defaults once when LiteLLM is first imported.""" | ||||||||||||||||||||||||||||||||||||||||||
| litellm.telemetry = False | ||||||||||||||||||||||||||||||||||||||||||
| litellm.cache = None # By default we disable LiteLLM cache and use DSPy on-disk cache. | ||||||||||||||||||||||||||||||||||||||||||
| if not getattr(litellm, "_dspy_logging_configured", False): | ||||||||||||||||||||||||||||||||||||||||||
| litellm.suppress_debug_info = True | ||||||||||||||||||||||||||||||||||||||||||
| litellm._dspy_logging_configured = True | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+11
to
+18
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.
Previously these were set at
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def _materialize_litellm(litellm: types.ModuleType) -> None: | ||||||||||||||||||||||||||||||||||||||||||
| """Force LiteLLM's lazy module to execute, or raise the missing dependency error.""" | ||||||||||||||||||||||||||||||||||||||||||
| # `require()` returns either an importlib LazyLoader-backed module or a _MissingModule. | ||||||||||||||||||||||||||||||||||||||||||
| # Accessing a real LiteLLM attribute forces LazyLoader execution; on _MissingModule it raises | ||||||||||||||||||||||||||||||||||||||||||
| # the helpful install-hint ImportError immediately at the DSPy call site. | ||||||||||||||||||||||||||||||||||||||||||
| _completion = litellm.completion | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| @functools.cache | ||||||||||||||||||||||||||||||||||||||||||
| def get_litellm(*, feature: str) -> Any: | ||||||||||||||||||||||||||||||||||||||||||
| """Import LiteLLM, apply DSPy's defaults once, and return the module.""" | ||||||||||||||||||||||||||||||||||||||||||
| litellm = require("litellm", extra="litellm", feature=feature) | ||||||||||||||||||||||||||||||||||||||||||
| _materialize_litellm(litellm) | ||||||||||||||||||||||||||||||||||||||||||
| _configure_litellm_defaults(litellm) | ||||||||||||||||||||||||||||||||||||||||||
| return litellm | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def is_litellm_context_window_error(error: Exception) -> bool: | ||||||||||||||||||||||||||||||||||||||||||
| """Return whether an exception is LiteLLM's context-window error, if LiteLLM is loaded.""" | ||||||||||||||||||||||||||||||||||||||||||
| litellm_module = sys.modules.get("litellm") | ||||||||||||||||||||||||||||||||||||||||||
| context_window_error = getattr(litellm_module, "ContextWindowExceededError", None) | ||||||||||||||||||||||||||||||||||||||||||
| return context_window_error is not None and isinstance(error, context_window_error) | ||||||||||||||||||||||||||||||||||||||||||
| 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.