Skip to content

Commit 32cc5c5

Browse files
committed
merge
1 parent 372db8b commit 32cc5c5

File tree

4 files changed

+132
-127
lines changed

4 files changed

+132
-127
lines changed

clai/README.md

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ Launch a web-based chat interface for your agent:
5959
clai web --agent module:agent_variable
6060
```
6161

62+
![Web Chat UI](https://github.com/user-attachments/assets/8a1c90dc-f62b-4e35-9d66-59459b45790d)
63+
6264
For example, if you have an agent defined in `my_agent.py`:
6365

6466
```python
@@ -78,9 +80,10 @@ This will start a web server (default: http://127.0.0.1:7932) with a chat interf
7880
### Web Command Options
7981

8082
- `--agent`, `-a`: Agent to serve in `module:variable` format
81-
- `--models`, `-m`: Comma-separated models to make available (e.g., `gpt-5,sonnet-4-5`)
82-
- `--tools`, `-t`: Comma-separated builtin tool IDs to enable (e.g., `web_search,code_execution`)
83-
- `--instructions`, `-i`: System instructions for generic agent (when `--agent` not specified)
83+
- `--model`, `-m`: Model to make available (repeatable, agent's model is default if present)
84+
- `--tool`, `-t`: [Builtin tool](https://ai.pydantic.dev/builtin-tools/) to enable (repeatable). See [available tools](https://ai.pydantic.dev/ui/web/#builtin-tool-support).
85+
- `--mcp`: Path to MCP server config JSON file
86+
- `--instructions`, `-i`: System instructions. In generic mode (no `--agent`), these are the agent instructions. With `--agent`, these are passed as extra instructions to each run.
8487
- `--host`: Host to bind the server to (default: 127.0.0.1)
8588
- `--port`: Port to bind the server to (default: 7932)
8689

@@ -90,29 +93,21 @@ You can specify which models and builtin tools are available in the UI via CLI f
9093

9194
```bash
9295
# Generic agent with specific models and tools
93-
clai web -m gpt-5,sonnet-4-5 -t web_search,code_execution
96+
clai web -m openai:gpt-5 -m anthropic:claude-sonnet-4-5 -t web_search -t code_execution
9497

9598
# Custom agent with additional models
96-
clai web --agent my_agent:my_agent -m gpt-5,gemini-2.5-pro
99+
clai web --agent my_agent:my_agent -m openai:gpt-5 -m google:gemini-2.5-pro
97100

98101
# Generic agent with system instructions
99-
clai web -m gpt-5 -i 'You are a helpful coding assistant'
100-
```
101-
102-
You can also launch the web UI directly from an `Agent` instance using `Agent.to_web()`:
103-
104-
```python
105-
from pydantic_ai import Agent
106-
from pydantic_ai.builtin_tools import WebSearchTool
102+
clai web -m openai:gpt-5 -i 'You are a helpful coding assistant'
107103

108-
agent = Agent('openai:gpt-5')
104+
# Custom agent with extra instructions for each run
105+
clai web --agent my_agent:my_agent -i 'Always respond in Spanish'
106+
```
109107

110-
# Use defaults
111-
app = agent.to_web()
108+
When using `--agent`, the agent's configured model becomes the default. CLI models (`-m`) are additional options. Without `--agent`, the first `-m` model is the default.
112109

113-
# Or customize models and tools
114-
app = agent.to_web(builtin_tools=[WebSearchTool()])
115-
```
110+
For full documentation, see [Web Chat UI](https://ai.pydantic.dev/ui/web/).
116111

117112
## Help
118113

docs/cli.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,49 @@ clai web --agent my_agent:my_agent
7272

7373
This will start a web server (default: http://127.0.0.1:7932) with a chat interface for your agent.
7474

75-
For more details on web UI options, MCP server configuration, and programmatic usage with `Agent.to_web()`, see the [Web Chat UI documentation](ui/web.md).
75+
#### CLI Options
76+
77+
```bash
78+
# With a custom agent
79+
clai web --agent my_module:my_agent
80+
81+
# With specific models (first is default when no --agent)
82+
clai web -m openai:gpt-5 -m anthropic:claude-sonnet-4-5
83+
84+
# With builtin tools
85+
clai web -m openai:gpt-5 -t web_search -t code_execution
86+
87+
# Generic agent with system instructions
88+
clai web -m openai:gpt-5 -i 'You are a helpful coding assistant'
89+
90+
# Custom agent with extra instructions for each run
91+
clai web --agent my_module:my_agent -i 'Always respond in Spanish'
92+
```
93+
94+
| Option | Description |
95+
|--------|-------------|
96+
| `--agent`, `-a` | Agent to serve in `module:variable` format |
97+
| `--model`, `-m` | Model to make available (repeatable, agent's model is default if present) |
98+
| `--tool`, `-t` | [Builtin tool](builtin-tools.md) to enable (repeatable). See [available tools](ui/web.md#builtin-tool-support). |
99+
| `--mcp` | Path to MCP server config JSON file |
100+
| `--instructions`, `-i` | System instructions. In generic mode (no `--agent`), these are the agent instructions. With `--agent`, these are passed as extra instructions to each run. |
101+
| `--host` | Host to bind server (default: 127.0.0.1) |
102+
| `--port` | Port to bind server (default: 7932) |
103+
104+
!!! note "Memory Tool"
105+
The `memory` tool requires the agent to have memory configured and cannot be enabled via `-t memory` alone. An agent with memory must be provided via `--agent`.
106+
107+
#### MCP Server Configuration
108+
109+
You can enable [MCP (Model Context Protocol)](mcp/overview.md) servers using a JSON configuration file:
110+
111+
```bash
112+
clai web --agent my_agent:my_agent --mcp mcp-servers.json
113+
```
114+
115+
See [Loading MCP Servers from Configuration](mcp/client.md#loading-mcp-servers-from-configuration) for the full configuration format and environment variable syntax.
116+
117+
For programmatic usage with `Agent.to_web()`, see the [Web UI documentation](ui/web.md).
76118

77119
### Help
78120

docs/ui/web.md

Lines changed: 72 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -2,149 +2,115 @@
22

33
Pydantic AI includes a built-in web chat interface that you can use to interact with your agents through a browser.
44

5-
https://github.com/user-attachments/assets/8a1c90dc-f62b-4e35-9d66-59459b45790d
5+
<video src="https://github.com/user-attachments/assets/8a1c90dc-f62b-4e35-9d66-59459b45790d" autoplay loop muted playsinline></video>
66

77
## Installation
88

9-
Install the `web` extra to get the required dependencies:
9+
Install the `web` extra (installs Starlette):
1010

1111
```bash
1212
pip/uv-add 'pydantic-ai-slim[web]'
1313
```
1414

15-
## Usage
16-
17-
There are two ways to launch the web chat UI:
15+
For CLI usage with `clai web`, see the [CLI documentation](../cli.md#web-chat-ui).
1816

19-
### 1. Using the CLI (`clai web`)
17+
## Usage
2018

21-
The simplest way to start the web UI is using the `clai web` command:
19+
Create a web app from an agent instance using [`Agent.to_web()`][pydantic_ai.agent.Agent.to_web]:
2220

23-
```bash
24-
# With a custom agent
25-
clai web --agent my_module:my_agent
21+
=== "Using Model Names"
2622

27-
# With specific models
28-
clai web -m openai:gpt-5 -m anthropic:claude-sonnet-4-5
23+
```python
24+
from pydantic_ai import Agent
25+
from pydantic_ai.builtin_tools import WebSearchTool
2926

30-
# With builtin tools
31-
clai web -m openai:gpt-5 -t web_search -t code_execution
27+
model = 'openai:gpt-5'
28+
agent = Agent(model)
3229

33-
# Generic agent with system instructions
34-
clai web -m openai:gpt-5 -i 'You are a helpful coding assistant'
35-
```
30+
@agent.tool_plain
31+
def get_weather(city: str) -> str:
32+
return f'The weather in {city} is sunny'
3633

37-
#### CLI Options
34+
# Create app with model names (their display names are auto-generated)
35+
app = agent.to_web(
36+
models=['openai:gpt-5', 'anthropic:claude-sonnet-4-5'],
37+
builtin_tools=[WebSearchTool()],
38+
)
3839

39-
| Option | Description |
40-
|--------|-------------|
41-
| `--agent`, `-a` | Agent to serve in `module:variable` format |
42-
| `--model`, `-m` | Model to make available (repeatable) |
43-
| `--tool`, `-t` | Builtin tool to enable (repeatable) |
44-
| `--instructions`, `-i` | System instructions (when `--agent` not specified) |
45-
| `--host` | Host to bind server (default: 127.0.0.1) |
46-
| `--port` | Port to bind server (default: 7932) |
47-
| `--mcp` | Path to MCP server config JSON file |
40+
# Or with custom display labels
41+
app = agent.to_web(
42+
models={'GPT 5': 'openai:gpt-5', 'Claude': 'anthropic:claude-sonnet-4-5'},
43+
builtin_tools=[WebSearchTool()],
44+
)
45+
```
4846

49-
Model names without a provider prefix are automatically inferred:
47+
=== "Using Model Instances"
5048

51-
- `gpt-*`, `o1`, `o3` → OpenAI
52-
- `claude-*`, `sonnet`, `opus`, `haiku` → Anthropic
53-
- `gemini-*` → Google
49+
```python
50+
from pydantic_ai import Agent
51+
from pydantic_ai.builtin_tools import WebSearchTool
52+
from pydantic_ai.models.anthropic import AnthropicModel
53+
from pydantic_ai.models.openai import OpenAIModel
5454

55-
### 2. Using `Agent.to_web()` Programmatically
55+
# Create separate models with their own custom configuration
56+
anthropic_model = AnthropicModel('claude-sonnet-4-5')
57+
openai_model = OpenAIModel('gpt-5', api_key='custom-key')
5658

57-
For more control, you can create a web app from an agent instance:
59+
agent = Agent(openai_model)
5860

59-
```python
60-
from pydantic_ai import Agent
61-
from pydantic_ai.builtin_tools import WebSearchTool
61+
@agent.tool_plain
62+
def get_weather(city: str) -> str:
63+
return f'The weather in {city} is sunny'
6264

63-
agent = Agent('openai:gpt-5')
65+
# Use the instances directly
66+
app = agent.to_web(
67+
models=[openai_model, anthropic_model],
68+
builtin_tools=[WebSearchTool()],
69+
)
6470

65-
@agent.tool_plain
66-
def get_weather(city: str) -> str:
67-
return f'The weather in {city} is sunny'
71+
# Or mix instances and strings with custom labels
72+
app = agent.to_web(
73+
models={'Custom GPT': openai_model, 'Claude': 'anthropic:claude-sonnet-4-5'},
74+
builtin_tools=[WebSearchTool()],
75+
)
6876

69-
# Create app with model names (their display names are auto-generated)
70-
app = agent.to_web(
71-
models=['openai:gpt-5', 'anthropic:claude-sonnet-4-5'],
72-
builtin_tools=[WebSearchTool()],
73-
)
74-
75-
# Or with custom display labels
76-
app = agent.to_web(
77-
models={'GPT 5': 'openai:gpt-5', 'Claude': 'anthropic:claude-sonnet-4-5'},
78-
builtin_tools=[WebSearchTool()],
79-
)
80-
```
77+
# With extra instructions passed to each run
78+
app = agent.to_web(
79+
models=[openai_model],
80+
instructions='Always respond in a friendly tone.',
81+
)
82+
```
8183

8284
The returned Starlette app can be run with any ASGI server:
8385

8486
```bash
8587
uvicorn my_module:app --host 0.0.0.0 --port 8080
8688
```
8789

88-
!!! note "Reserved Routes"
89-
The web UI app uses the following routes which should not be overwritten:
90-
91-
- `/` and `/{id}` - Serves the chat UI
92-
- `/api/chat` - Chat endpoint (POST, OPTIONS)
93-
- `/api/configure` - Frontend configuration (GET)
94-
- `/api/health` - Health check (GET)
95-
96-
The app cannot currently be mounted at a subpath (e.g., `/chat`) because the UI expects these routes at the root. You can add additional routes to the app, but avoid conflicts with these reserved paths.
97-
98-
## MCP Server Configuration
99-
100-
You can enable [MCP (Model Context Protocol)](https://modelcontextprotocol.io/) servers using a JSON configuration file:
101-
102-
```bash
103-
clai web --agent my_agent:my_agent --mcp mcp-servers.json
104-
```
90+
## Builtin Tool Support
10591

106-
Example JSON configuration:
107-
108-
```json
109-
{
110-
"mcpServers": {
111-
"deepwiki": {
112-
"url": "https://mcp.deepwiki.com/mcp"
113-
},
114-
"github": {
115-
"url": "https://api.githubcopilot.com/mcp",
116-
"authorizationToken": "${GITHUB_TOKEN}"
117-
}
118-
}
119-
}
120-
```
92+
Builtin tool support is automatically determined from each model's profile. The UI will only show tools that the selected model supports.
12193

122-
Environment variables can be referenced using `${VAR_NAME}` syntax, with optional defaults: `${VAR_NAME:-default_value}`.
94+
Available [builtin tools](../builtin-tools.md):
12395

124-
Each server entry supports:
96+
- `web_search` - Web search capability ([`WebSearchTool`][pydantic_ai.builtin_tools.WebSearchTool])
97+
- `code_execution` - Code execution in a sandbox ([`CodeExecutionTool`][pydantic_ai.builtin_tools.CodeExecutionTool])
98+
- `image_generation` - Image generation ([`ImageGenerationTool`][pydantic_ai.builtin_tools.ImageGenerationTool])
99+
- `web_fetch` - Fetch content from URLs ([`WebFetchTool`][pydantic_ai.builtin_tools.WebFetchTool])
100+
- `memory` - Persistent memory across conversations ([`MemoryTool`][pydantic_ai.builtin_tools.MemoryTool])
125101

126-
| Field | Description |
127-
|-------|-------------|
128-
| `url` (required) | The MCP server URL |
129-
| `authorizationToken` | Authorization token for the server |
130-
| `description` | Description shown in the UI |
131-
| `allowedTools` | List of allowed tool names |
132-
| `headers` | Additional HTTP headers |
102+
!!! note "Memory Tool Requirements"
103+
The `memory` tool requires the agent to have memory configured via the
104+
`memory` parameter when creating the agent.
133105

134-
## Builtin Tool Support
135106

136-
When using the new models API, builtin tool support is automatically determined from each model's profile. The UI will only show tools that the selected model supports.
107+
## Reserved Routes
137108

138-
Available builtin tools:
109+
The web UI app uses the following routes which should not be overwritten:
139110

140-
- `web_search` - Web search capability
141-
- `code_execution` - Code execution in a sandbox
142-
- `image_generation` - Image generation
143-
- `web_fetch` - Fetch content from URLs
144-
- `memory` - Persistent memory across conversations
111+
- `/` and `/{id}` - Serves the chat UI
112+
- `/api/chat` - Chat endpoint (POST, OPTIONS)
113+
- `/api/configure` - Frontend configuration (GET)
114+
- `/api/health` - Health check (GET)
145115

146-
!!! note "Memory Tool Requirements"
147-
The `memory` tool requires the agent to have memory configured via the
148-
`memory` parameter when creating the agent. It cannot be enabled via
149-
the CLI `-t memory` flag alone - an agent with memory must be provided
150-
via `--agent`.
116+
The app cannot currently be mounted at a subpath (e.g., `/chat`) because the UI expects these routes at the root. You can add additional routes to the app, but avoid conflicts with these reserved paths.

mkdocs.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,12 @@ nav:
9797
- Overview: ui/overview.md
9898
- AG-UI: ui/ag-ui.md
9999
- Vercel AI: ui/vercel-ai.md
100+
- Web Chat UI: ui/web.md
100101
- Agent2Agent (A2A): a2a.md
101102

102103
- Related Packages:
103-
- Clai: cli.md
104+
- Clai:
105+
- CLI: cli.md
104106

105107
- Examples:
106108
- Setup: examples/setup.md

0 commit comments

Comments
 (0)