mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-30 01:08:33 +00:00
* refactor(typing): narrow Any sources in proxy, guardrails, bedrock and responses code Turns untyped json.loads, response.json(), header lookups and forwarded kwargs into object or precise types at their source, so downstream code type-checks without reportAny / reportExplicitAny. No runtime behavior changes. * refactor(typing): narrow Any sources in caching, MCP, providers and proxy request code Decoded JSON, cache reads and header lookups now enter as object or precise types, prisma tables used by the shadow-eval logger get typed Protocol views, and a few SDK parameters move from dict[str, Any] to Mapping[str, object]. No runtime behavior changes. * refactor(typing): narrow Any sources in proxy server, router, logging and shared utils * refactor(typing): narrow Any sources in provider transformations, integrations and proxy endpoints * refactor(typing): narrow Any sources across providers, proxy and router Deepens the Any cleanup on the hottest files: typed helpers in core_helpers for attribute and item access, identity TypeAdapter(object) wraps at untyped boundaries, explicit return annotations on shared provider and proxy helpers, and Protocols in place of Any parameters. No intended behavior change.
146 lines
4 KiB
Python
146 lines
4 KiB
Python
"""
|
|
Public entrypoints for running model-generated code in a sandbox.
|
|
|
|
Low-level lifecycle:
|
|
acreate_container -> arun_code -> adelete_container
|
|
|
|
High-level convenience:
|
|
acode_interpreter_tool (ephemeral: create -> run -> delete)
|
|
|
|
Each entrypoint is `@client`-decorated, so every operation is logged the same
|
|
way `litellm.asearch` is.
|
|
"""
|
|
|
|
from typing import Final
|
|
|
|
from litellm._logging import verbose_logger
|
|
from litellm.llms.base_llm.sandbox.transformation import (
|
|
BaseSandboxConfig,
|
|
CodeExecutionResult,
|
|
ContainerHandle,
|
|
)
|
|
from litellm.types.utils import SandboxProviders
|
|
from litellm.utils import ProviderConfigManager, client
|
|
|
|
__all__ = [
|
|
"acode_interpreter_tool",
|
|
"acreate_sandbox",
|
|
"adelete_sandbox",
|
|
"arun_code",
|
|
]
|
|
|
|
_LITELLM_INTERNAL_KWARGS: Final = {
|
|
"litellm_logging_obj",
|
|
"litellm_call_id",
|
|
"litellm_trace_id",
|
|
"litellm_metadata",
|
|
}
|
|
|
|
|
|
def _get_config(provider: str) -> BaseSandboxConfig:
|
|
config: Final = ProviderConfigManager.get_provider_sandbox_config(SandboxProviders(provider))
|
|
if config is None:
|
|
raise ValueError(f"Code execution is not supported for provider: {provider}")
|
|
return config
|
|
|
|
|
|
def _forward_kwargs(kwargs: dict) -> dict:
|
|
return {k: v for k, v in kwargs.items() if k not in _LITELLM_INTERNAL_KWARGS}
|
|
|
|
|
|
def _update_logging(kwargs: dict, provider: str, operation: str) -> None:
|
|
logging_obj: Final = kwargs.get("litellm_logging_obj")
|
|
if logging_obj is None:
|
|
return
|
|
logging_obj.update_from_kwargs(
|
|
kwargs=kwargs,
|
|
model=f"{provider}/{operation}",
|
|
optional_params={},
|
|
litellm_params={"litellm_call_id": kwargs.get("litellm_call_id")},
|
|
custom_llm_provider=provider,
|
|
)
|
|
|
|
|
|
@client
|
|
async def acreate_sandbox(
|
|
provider: str,
|
|
template: str | None = None,
|
|
timeout: int | None = None,
|
|
allow_internet_access: bool | None = None,
|
|
api_key: str | None = None,
|
|
api_base: str | None = None,
|
|
**kwargs,
|
|
) -> ContainerHandle:
|
|
_update_logging(kwargs, provider, "create_sandbox")
|
|
return await _get_config(provider).acreate_sandbox(
|
|
template=template,
|
|
timeout=timeout,
|
|
allow_internet_access=allow_internet_access,
|
|
api_key=api_key,
|
|
api_base=api_base,
|
|
**_forward_kwargs(kwargs),
|
|
)
|
|
|
|
|
|
@client
|
|
async def arun_code(
|
|
provider: str,
|
|
container: ContainerHandle | str,
|
|
code: str,
|
|
api_key: str | None = None,
|
|
**kwargs,
|
|
) -> CodeExecutionResult:
|
|
_update_logging(kwargs, provider, "run_code")
|
|
return await _get_config(provider).arun_code(
|
|
container=container,
|
|
code=code,
|
|
api_key=api_key,
|
|
**_forward_kwargs(kwargs),
|
|
)
|
|
|
|
|
|
@client
|
|
async def adelete_sandbox(
|
|
provider: str,
|
|
container: ContainerHandle | str,
|
|
api_key: str | None = None,
|
|
api_base: str | None = None,
|
|
**kwargs,
|
|
) -> bool:
|
|
_update_logging(kwargs, provider, "delete_sandbox")
|
|
return await _get_config(provider).adelete_sandbox(
|
|
container=container,
|
|
api_key=api_key,
|
|
api_base=api_base,
|
|
**_forward_kwargs(kwargs),
|
|
)
|
|
|
|
|
|
@client
|
|
async def acode_interpreter_tool(
|
|
provider: str,
|
|
code: str,
|
|
template: str | None = None,
|
|
timeout: int | None = None,
|
|
api_key: str | None = None,
|
|
api_base: str | None = None,
|
|
**kwargs,
|
|
) -> CodeExecutionResult:
|
|
_update_logging(kwargs, provider, "code_interpreter_tool")
|
|
config: Final = _get_config(provider)
|
|
forwarded: Final = _forward_kwargs(kwargs)
|
|
|
|
container: Final = await config.acreate_sandbox(
|
|
template=template,
|
|
timeout=timeout,
|
|
api_key=api_key,
|
|
api_base=api_base,
|
|
**forwarded,
|
|
)
|
|
try:
|
|
return await config.arun_code(container=container, code=code, api_key=api_key, **forwarded)
|
|
finally:
|
|
try:
|
|
await config.adelete_sandbox(container=container, api_key=api_key, api_base=api_base, **forwarded)
|
|
except Exception as e:
|
|
verbose_logger.debug("sandbox: failed to delete ephemeral container: %s", e)
|