Skip to content
Merged
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
6 changes: 4 additions & 2 deletions examples/fastapi-vite/backend/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import ai

MODEL = ai.model("ai-gateway", "anthropic/claude-sonnet-4")
MODEL = ai.ai_gateway("anthropic/claude-sonnet-4")


@ai.tool
Expand Down Expand Up @@ -42,7 +42,9 @@ async def graph(context: ai.Context) -> AsyncGenerator[ai.Message]:
if not tool_calls:
return

results = await asyncio.gather(*(_execute_with_approval(tc) for tc in tool_calls))
results = await asyncio.gather(
*(_execute_with_approval(tc) for tc in tool_calls)
)
yield ai.tool_message(*results)


Expand Down
2 changes: 1 addition & 1 deletion examples/multiagent-textual/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Approval(pydantic.BaseModel):
# Model
# ---------------------------------------------------------------------------

MODEL = ai.model("ai-gateway", "anthropic/claude-sonnet-4")
MODEL = ai.ai_gateway("anthropic/claude-sonnet-4")


# ---------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion examples/samples/agent_custom_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async def get_population(city: str) -> int:


async def main() -> None:
model = ai.model("ai-gateway", "anthropic/claude-sonnet-4")
model = ai.ai_gateway("anthropic/claude-sonnet-4")

tools = [get_weather, get_population]
my_agent = ai.agent(tools=tools)
Expand Down
2 changes: 1 addition & 1 deletion examples/samples/agent_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async def contact_mothership(query: str) -> str:


async def main() -> None:
model = ai.model("ai-gateway", "anthropic/claude-sonnet-4")
model = ai.ai_gateway("anthropic/claude-sonnet-4")

my_agent = ai.agent(tools=[contact_mothership])

Expand Down
2 changes: 1 addition & 1 deletion examples/samples/agent_hooks_serverless.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async def delete_file(path: str) -> str:


async def main() -> None:
model = ai.model("ai-gateway", "anthropic/claude-sonnet-4")
model = ai.ai_gateway("anthropic/claude-sonnet-4")

my_agent = ai.agent(tools=[delete_file])

Expand Down
28 changes: 24 additions & 4 deletions examples/samples/check_connection.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
"""Check connection — verify credentials and model availability for all providers."""
"""Check connection and list models — verify credentials and model availability."""

import asyncio

import ai

MODELS = [
ai.model("ai-gateway", "anthropic/claude-sonnet-4"),
ai.model("anthropic", "claude-sonnet-4-20250514"),
ai.model("openai", "gpt-5.4-mini"),
ai.ai_gateway("anthropic/claude-sonnet-4"),
ai.anthropic("claude-sonnet-4-20250514"),
ai.openai("gpt-5.4-mini"),
]

PROVIDERS = [
("ai_gateway", ai.ai_gateway),
("anthropic", ai.anthropic),
("openai", ai.openai),
]


Expand All @@ -20,9 +26,23 @@ async def _check(model: ai.Model) -> None:
print(f" {status} {model.provider}/{model.id}")


async def _list_models(name: str, provider: object) -> None:
try:
ids: list[str] = await provider.list() # type: ignore[union-attr]
print(f" {name}: {len(ids)} models")
for mid in ids:
print(f" - {mid}")
except Exception as exc:
print(f" {name}: [ERR] {exc}")


async def main() -> None:
print("Checking connections...\n")
await asyncio.gather(*[_check(m) for m in MODELS])

print("\nListing models...\n")
await asyncio.gather(*[_list_models(n, p) for n, p in PROVIDERS])

print()


Expand Down
6 changes: 3 additions & 3 deletions examples/samples/explicit_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@

import ai

model = ai.model("ai-gateway", "anthropic/claude-sonnet-4")

# Explicit client — useful for custom auth, proxies, or self-hosted gateways.
client = ai.Client(
base_url="https://ai-gateway.vercel.sh/v3/ai",
api_key=os.environ["AI_GATEWAY_API_KEY"],
headers={"X-Custom-Header": "example"},
)

model = ai.ai_gateway("anthropic/claude-sonnet-4", client=client)

messages = [ai.user_message("Hello!")]


async def main() -> None:
try:
async for msg in await ai.models.stream(model, messages, client=client):
async for msg in await ai.models.stream(model, messages):
if msg.text_delta:
print(msg.text_delta, end="", flush=True)
print()
Expand Down
2 changes: 1 addition & 1 deletion examples/samples/image_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import ai

model = ai.model("ai-gateway", "google/imagen-4.0-generate-001")
model = ai.ai_gateway("google/imagen-4.0-generate-001")

messages = [
ai.user_message(
Expand Down
2 changes: 1 addition & 1 deletion examples/samples/middleware_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async def get_population(city: str) -> int:


async def main() -> None:
model = ai.model("ai-gateway", "anthropic/claude-sonnet-4")
model = ai.ai_gateway("anthropic/claude-sonnet-4")

my_agent = ai.agent(tools=[get_weather, get_population])

Expand Down
2 changes: 1 addition & 1 deletion examples/samples/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import ai

model = ai.model("ai-gateway", "anthropic/claude-sonnet-4")
model = ai.ai_gateway("anthropic/claude-sonnet-4")

messages = [
ai.system_message("Be concise."),
Expand Down
2 changes: 1 addition & 1 deletion examples/samples/video_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import ai

model = ai.model("ai-gateway", "google/veo-3.0-generate-001")
model = ai.ai_gateway("google/veo-3.0-generate-001")

messages = [
ai.user_message(
Expand Down
4 changes: 2 additions & 2 deletions examples/temporal-direct/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class LLMResult:
@temporalio.activity.defn
async def llm_call_activity(params: LLMParams) -> LLMResult:
"""Call the LLM, drain the stream, return the final message."""
model = ai.model("ai-gateway", "anthropic/claude-sonnet-4")
model = ai.ai_gateway("anthropic/claude-sonnet-4")
messages = [ai.Message.model_validate(m) for m in params.messages]
tools = [ai.ToolSchema(return_type=None, **t) for t in params.tool_schemas]

Expand Down Expand Up @@ -166,7 +166,7 @@ async def run_tool(tc: ai.ToolCallPart) -> ai.ToolResultPart:
class WeatherWorkflow:
@temporalio.workflow.run
async def run(self, user_query: str) -> str:
model = ai.model("ai-gateway", "anthropic/claude-sonnet-4")
model = ai.ai_gateway("anthropic/claude-sonnet-4")
messages: list[ai.Message] = [
ai.system_message(
"Answer questions using the weather and population tools."
Expand Down
4 changes: 2 additions & 2 deletions examples/temporal-middleware/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class LLMResult:
@temporalio.activity.defn
async def llm_call_activity(params: LLMParams) -> LLMResult:
"""Call the LLM, drain the stream, return the final message."""
model = ai.model("ai-gateway", "anthropic/claude-sonnet-4")
model = ai.ai_gateway("anthropic/claude-sonnet-4")
messages = [ai.Message.model_validate(m) for m in params.messages]
tools = [ai.ToolSchema(return_type=None, **t) for t in params.tool_schemas]

Expand Down Expand Up @@ -194,7 +194,7 @@ async def wrap_tool(
class WeatherWorkflow:
@temporalio.workflow.run
async def run(self, user_query: str) -> str:
model = ai.model("ai-gateway", "anthropic/claude-sonnet-4")
model = ai.ai_gateway("anthropic/claude-sonnet-4")
messages: list[ai.Message] = [
ai.system_message(
"Answer questions using the weather and population tools."
Expand Down
15 changes: 10 additions & 5 deletions src/ai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
Client,
ImageParams,
Model,
ModelCost,
Provider,
StreamResult,
VideoParams,
ai_gateway,
anthropic,
check_connection,
generate,
model,
openai,
stream,
)

Expand Down Expand Up @@ -83,17 +85,20 @@
"thinking",
# Models (from models/)
"Model",
"ModelCost",
"Provider",
"ImageParams",
"VideoParams",
"Client",
"StreamResult",
"StreamResultLike",
"check_connection",
"model",
"models",
"stream",
"generate",
"models",
# Provider factories
"openai",
"anthropic",
"ai_gateway",
# Agents — primary API
"Agent",
"agent",
Expand Down
3 changes: 0 additions & 3 deletions src/ai/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

if TYPE_CHECKING:
from .agents.agent import Tool
from .models.core.client import Client
from .models.core.model import Model


Expand All @@ -50,7 +49,6 @@ class ModelContext:
messages: list[messages_.Message]
tools: Sequence[tools_.ToolLike] | None
output_type: type[pydantic.BaseModel] | None
client: Client | None
kwargs: dict[str, Any]

def __post_init__(self) -> None:
Expand All @@ -67,7 +65,6 @@ class GenerateContext:
model: Model
messages: list[messages_.Message]
params: Any
client: Client | None = None

def __post_init__(self) -> None:
object.__setattr__(self, "messages", list(self.messages))
Expand Down
Loading
Loading