Skip to content

Commit 8d28862

Browse files
committed
feat(ag-ui): example command line options
Add command line options to the Pydantic AI AG UI examples, allowing users to specify: - Listening port - Adapter log level - Reload
1 parent 622cdb5 commit 8d28862

File tree

5 files changed

+111
-25
lines changed

5 files changed

+111
-25
lines changed

docs/ag-ui.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,27 @@ installed with either of the following:
289289
pip/uv-add pydantic-ai-examples
290290
```
291291

292-
Direct:
292+
Direct, which supports command line flags:
293293

294294
```shell
295-
python -m pydantic_ai_ag_ui_examples.dojo_server
295+
python -m pydantic_ai_ag_ui_examples.dojo_server --help
296+
usage: dojo_server.py [-h] [--port PORT] [--reload] [--no-reload] [--log-level {critical,error,warning,info,debug,trace}]
297+
298+
PydanticAI AG-UI Dojo server
299+
300+
options:
301+
-h, --help show this help message and exit
302+
--port PORT, -p PORT Port to run the server on (default: 9000)
303+
--reload Enable auto-reload (default: True)
304+
--no-reload Disable auto-reload
305+
--log-level {critical,error,warning,info,debug,trace}
306+
Agent log level (default: info)
307+
```
308+
309+
Run with adapter debug logging:
310+
311+
```shell
312+
python -m pydantic_ai_ag_ui_examples.dojo_server --log-level debug
296313
```
297314

298315
Using uvicorn:

examples/pydantic_ai_ag_ui_examples/basic.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,16 @@ async def handler(
4343

4444

4545
if __name__ == '__main__':
46-
import logging
47-
4846
import uvicorn
4947

50-
logging.basicConfig(
51-
format='%(levelname)s: %(message)s',
52-
level=logging.INFO,
53-
force=True,
54-
)
48+
from .cli import Args, parse_args
49+
50+
args: Args = parse_args()
5551

5652
uvicorn.run(
5753
'pydantic_ai_ag_ui_examples.dojo_server:app',
58-
host='127.0.0.1',
59-
port=9000,
60-
reload=True,
61-
log_level='info',
54+
port=args.port,
55+
reload=args.reload,
56+
log_level=args.log_level,
57+
log_config=args.log_config(),
6258
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""Command line interface for the PydanticAI AG-UI servers."""
2+
3+
from .args import Args, parse_args
4+
5+
__all__ = [
6+
'Args',
7+
'parse_args',
8+
]
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""CLI argument parser for the PydanticAI AG-UI servers."""
2+
3+
import argparse
4+
from typing import Any
5+
6+
from uvicorn.config import LOGGING_CONFIG
7+
8+
from pydantic_ai.models import dataclass
9+
10+
11+
@dataclass
12+
class Args:
13+
"""Custom namespace for command line arguments."""
14+
15+
port: int
16+
reload: bool
17+
log_level: str
18+
loggers: tuple[str, ...]
19+
20+
def log_config(self) -> dict[str, Any]:
21+
"""Return the logging configuration based on the log level."""
22+
log_config: dict[str, Any] = LOGGING_CONFIG.copy()
23+
log_config['loggers']['pydantic_ai_ag_ui.adapter'] = {
24+
'handlers': ['default'],
25+
'level': self.log_level.upper(),
26+
'propagate': False,
27+
}
28+
return log_config
29+
30+
31+
def parse_args(
32+
loggers: tuple[str, ...] = tuple(
33+
'pydantic_ai_ag_ui.adapter',
34+
),
35+
) -> Args:
36+
"""Parse command line arguments for the PydanticAI AG-UI servers.
37+
38+
Args:
39+
loggers: A tuple of logger names to configure. Defaults to the adapter logger.
40+
41+
Returns:
42+
Args: A dataclass containing the parsed command line arguments.
43+
"""
44+
parser: argparse.ArgumentParser = argparse.ArgumentParser(
45+
description='PydanticAI AG-UI Dojo server'
46+
)
47+
parser.add_argument(
48+
'--port',
49+
'-p',
50+
type=int,
51+
default=9000,
52+
help='Port to run the server on (default: 9000)',
53+
)
54+
parser.add_argument(
55+
'--reload',
56+
action='store_true',
57+
default=True,
58+
help='Enable auto-reload (default: True)',
59+
)
60+
parser.add_argument(
61+
'--no-reload', dest='reload', action='store_false', help='Disable auto-reload'
62+
)
63+
parser.add_argument(
64+
'--log-level',
65+
choices=['critical', 'error', 'warning', 'info', 'debug', 'trace'],
66+
default='info',
67+
help='Adapter log level (default: info)',
68+
)
69+
70+
args: argparse.Namespace = parser.parse_args()
71+
return Args(loggers=loggers, **vars(args))

examples/pydantic_ai_ag_ui_examples/dojo_server.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
from __future__ import annotations
1515

16-
import logging
17-
1816
from fastapi import FastAPI
1917

2018
from .api import (
@@ -36,19 +34,15 @@
3634

3735

3836
if __name__ == '__main__':
39-
import logging
40-
4137
import uvicorn
4238

43-
logging.basicConfig(
44-
format='%(levelname)s: %(message)s',
45-
level=logging.INFO,
46-
force=True,
47-
)
39+
from .cli import Args, parse_args
40+
41+
args: Args = parse_args()
4842

4943
uvicorn.run(
5044
'pydantic_ai_ag_ui_examples.dojo_server:app',
51-
port=9000,
52-
reload=True,
53-
log_level='info',
45+
port=args.port,
46+
reload=args.reload,
47+
log_config=args.log_config(),
5448
)

0 commit comments

Comments
 (0)