Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions examples/coding_agent_minimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import asyncio
import json
import sys
import typing

import ai

Expand All @@ -16,9 +15,7 @@
with sed, make sure to double check the result.
"""

STREAM_PARAMS: dict[str, typing.Any] = {
"providerOptions": {"gateway": {"caching": "auto"}},
}
STREAM_PARAMS = ai.InferenceRequestParams(cache=ai.CacheParams(mode="auto"))


@ai.tool
Expand Down
12 changes: 6 additions & 6 deletions examples/model_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@


async def main() -> None:
params = {
"providerOptions": {
"gateway": {"sort": "cost"},
"anthropic": {"speed": "fast"},
}
}
params = ai.InferenceRequestParams(
routing=ai.RoutingParams(
provider_ranking=ai.ProviderRankingStrategy.COST
),
extra_body={"providerOptions": {"anthropic": {"speed": "fast"}}},
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra_body feels clunky... but maybe only because it has like two more levels of ai gateway wrapping on it?

)
async with ai.stream(model, messages, params=params) as stream:
async for event in stream:
if isinstance(event, ai.events.TextDelta):
Expand Down
2 changes: 1 addition & 1 deletion examples/prompt_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ async def _run(user_text: str) -> ai.types.usage.Usage | None:
ai.system_message(SYSTEM_PROMPT),
ai.user_message(user_text),
]
params = {"providerOptions": {"gateway": {"caching": "auto"}}}
params = ai.InferenceRequestParams(cache=ai.CacheParams(mode="auto"))
async with agent.run(model, messages, params=params) as stream:
async for _event in stream:
pass
Expand Down
67 changes: 60 additions & 7 deletions src/ai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,41 @@
UnsupportedProviderError,
)
from .models import (
DEFAULT,
GLOBAL,
RANDOM,
UNSET,
CacheParams,
CloudRegion,
ContextManagementParams,
GeoRegion,
ImageParams,
InferenceRequestParams,
MinPSamplerParams,
Model,
ModelProviderDefault,
OutputParams,
Provider,
ProviderProtocol,
ProviderRankingStrategy,
ProviderServiceParams,
RandomSeed,
ReasoningParams,
RepetitionPenaltyParams,
RoutingParams,
RoutingTarget,
RoutingTargetChain,
SeedSamplerParams,
Stream,
TemperatureSamplerParams,
TokenThreshold,
ToolCallingParams,
ToolChoiceMode,
ToolRef,
ToolSelection,
TopKSamplerParams,
TopPSamplerParams,
Unset,
VideoParams,
generate,
get_model,
Expand All @@ -72,18 +102,27 @@
)

__all__ = [
# Models (from models/)
"DEFAULT",
"GLOBAL",
"RANDOM",
"UNSET",
"AIError",
# Agents — primary API
"Agent",
# Agents — tools
"AgentTool",
"CacheParams",
"CloudRegion",
"ConfigurationError",
"Context",
"ContextManagementParams",
"GeoRegion",
"HTTPErrorContext",
"ImageParams",
"InferenceRequestParams",
"InstallationError",
"MinPSamplerParams",
"Model",
"ModelProviderDefault",
"OutputParams",
"Provider",
"ProviderAPIError",
"ProviderAuthenticationError",
Expand All @@ -99,34 +138,50 @@
"ProviderOverloadedError",
"ProviderPermissionDeniedError",
"ProviderProtocol",
"ProviderRankingStrategy",
"ProviderRateLimitError",
"ProviderRequestTooLargeError",
"ProviderResponseError",
"ProviderServiceParams",
"ProviderServiceUnavailableError",
"ProviderStatusError",
"ProviderTimeoutError",
"ProviderUnprocessableEntityError",
"RandomSeed",
"ReasoningParams",
"RepetitionPenaltyParams",
"RoutingParams",
"RoutingTarget",
"RoutingTargetChain",
"SeedSamplerParams",
"Stream",
"StreamingStatusTool",
"StreamingTextTool",
"SubAgentTool",
"TemperatureSamplerParams",
"TokenThreshold",
"Tool",
"ToolCall",
"ToolCallingParams",
"ToolChoiceMode",
"ToolRef",
"ToolRunner",
"ToolSelection",
"TopKSamplerParams",
"TopPSamplerParams",
"Unset",
"UnsupportedProviderError",
"VideoParams",
"abort_pending_hook",
"agent",
"assistant_message",
"cancel_hook",
"errors",
# Submodules
"events",
"file_part",
"generate",
"get_model",
"get_provider",
# Agents — hooks
"hook",
"mcp",
"messages",
Expand All @@ -143,9 +198,7 @@
"tool_result",
"tool_result_part",
"tools",
# Builders (from types/builders)
"user_message",
"util",
# Agents — composition
"yield_from",
]
10 changes: 10 additions & 0 deletions src/ai/_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from collections.abc import Iterator
from typing import Protocol, TypeVar

_T_co = TypeVar("_T_co", covariant=True)


class Collection(Protocol[_T_co]):
def __contains__(self, value: object, /) -> bool: ...
def __iter__(self) -> Iterator[_T_co]: ...
def __len__(self) -> int: ...
3 changes: 2 additions & 1 deletion src/ai/agents/_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import pydantic

from ..models.core.model import Model
from ..models.core.params import GenerateParams
from ..types import events as events_
from ..types.tools import Tool
from .agent import Context
Expand Down Expand Up @@ -71,7 +72,7 @@ class GenerateContext:

model: Model
messages: list[messages_.Message]
params: Any
params: GenerateParams

def __post_init__(self) -> None:
object.__setattr__(self, "messages", list(self.messages))
Expand Down
12 changes: 7 additions & 5 deletions src/ai/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,9 @@ class Context(pydantic.BaseModel):
output_type: type[pydantic.BaseModel] | None = pydantic.Field(
default=None, exclude=True, repr=False
)
params: Any = pydantic.Field(default=None, exclude=True, repr=False)
params: models.InferenceRequestParams | None = pydantic.Field(
default=None, exclude=True, repr=False
)

_agent_tools_by_name: dict[str, AgentTool] = pydantic.PrivateAttr(
default_factory=dict
Expand Down Expand Up @@ -1178,7 +1180,7 @@ def run(
model: models.Model,
messages: list[types.messages.Message],
*,
params: Any = None,
params: models.InferenceRequestParams | None = None,
_middleware: list[middleware_._Middleware] | None = None,
) -> AbstractAsyncContextManager[AgentStream[str]]: ...
@overload
Expand All @@ -1188,7 +1190,7 @@ def run[T: pydantic.BaseModel](
messages: list[types.messages.Message],
*,
output_type: type[T],
params: Any = None,
params: models.InferenceRequestParams | None = None,
_middleware: list[middleware_._Middleware] | None = None,
) -> AbstractAsyncContextManager[AgentStream[T]]: ...
def run(
Expand All @@ -1197,7 +1199,7 @@ def run(
messages: list[types.messages.Message],
*,
output_type: type[pydantic.BaseModel] | None = None,
params: Any = None,
params: models.InferenceRequestParams | None = None,
_middleware: list[middleware_._Middleware] | None = None,
) -> AbstractAsyncContextManager[AgentStream[Any]]:
"""Run the agent loop, yielding events to the consumer.
Expand Down Expand Up @@ -1237,7 +1239,7 @@ async def _run(
messages: list[types.messages.Message],
*,
output_type: type[pydantic.BaseModel] | None,
params: Any,
params: models.InferenceRequestParams | None,
_middleware: list[middleware_._Middleware] | None,
) -> AsyncIterator[AgentStream[Any]]:
context = Context(
Expand Down
68 changes: 65 additions & 3 deletions src/ai/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,85 @@
stream,
)
from .core.model import Model, get_model
from .core.params import GenerateParams, ImageParams, VideoParams
from .core.params import (
DEFAULT,
GLOBAL,
RANDOM,
UNSET,
CacheParams,
CloudRegion,
ContextManagementParams,
GenerateParams,
GeoRegion,
ImageParams,
InferenceRequestParams,
MinPSamplerParams,
ModelProviderDefault,
OutputParams,
ProviderRankingStrategy,
ProviderServiceParams,
RandomSeed,
ReasoningParams,
RepetitionPenaltyParams,
RoutingParams,
RoutingTarget,
RoutingTargetChain,
SeedSamplerParams,
TemperatureSamplerParams,
TokenThreshold,
ToolCallingParams,
ToolChoiceMode,
ToolRef,
ToolSelection,
TopKSamplerParams,
TopPSamplerParams,
Unset,
VideoParams,
)

__all__ = [
# Core types
"DEFAULT",
"GLOBAL",
"RANDOM",
"UNSET",
"CacheParams",
"CloudRegion",
"ContextManagementParams",
"Executor",
"GenerateExecutor",
"GenerateParams",
"GenerateRequest",
"GeoRegion",
"ImageParams",
"InferenceRequestParams",
"MinPSamplerParams",
"Model",
"ModelProviderDefault",
"OutputParams",
"Provider",
"ProviderProtocol",
"ProviderRankingStrategy",
"ProviderServiceParams",
"RandomSeed",
"ReasoningParams",
"RepetitionPenaltyParams",
"RoutingParams",
"RoutingTarget",
"RoutingTargetChain",
"SeedSamplerParams",
"Stream",
"StreamExecutor",
"StreamRequest",
"TemperatureSamplerParams",
"TokenThreshold",
"ToolCallingParams",
"ToolChoiceMode",
"ToolRef",
"ToolSelection",
"TopKSamplerParams",
"TopPSamplerParams",
"Unset",
"VideoParams",
# Public API
"generate",
"get_model",
"probe",
Expand Down
Loading
Loading