Skip to content

Add to_cli() method to Agent #1642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 38 commits into from
May 13, 2025
Merged

Conversation

AndrewHannigan
Copy link
Contributor

@AndrewHannigan AndrewHannigan commented May 4, 2025

Overview

Exposes existing CLI functionality as a method on Agent. This allows users to use CLI with custom Agents.

Related issue: #1595

Demo for Agent.run_cli()

Script

pydantic-ai add-agent-cli % cat /Users/andrewhannigan/Downloads/agent_cli.py
from pydantic_ai import Agent

agent = Agent("openai:gpt-4o")


@agent.system_prompt
def system_prompt() -> str:
    return "Your name is Alice."


agent.run_cli()

Result

pydantic-ai add-agent-cli % uv run /Users/andrewhannigan/Downloads/agent_cli.py
pai - PydanticAI CLI v0.1.10.dev1+1e56101 using openai:gpt-4o
pai ➤ what is your name
My name is Alice. How can I assist you today?

Demo for pai --agent

pydantic-ai add-agent-cli % echo "from pydantic_ai import Agent\nagent = Agent(system_prompt='Your name is Bob.')" > pydantic_ai_slim/pydantic_ai/my_agent.py
pydantic-ai add-agent-cli % uv run pai -a pydantic_ai.my_agent:agent "what is your name"
pai - PydanticAI CLI v0.1.10.dev1+1e56101 using openai:gpt-4o
Using custom agent: pydantic_ai.my_agent:agent
My name is Bob. How can I assist you today?

@AndrewHannigan AndrewHannigan changed the title Add agent CLI functionality Add Agent.run_cli() Method May 4, 2025
@AndrewHannigan AndrewHannigan changed the title Add Agent.run_cli() Method Expose CLI as Agent method May 4, 2025
@AndrewHannigan AndrewHannigan changed the title Expose CLI as Agent method Expose run_cli() method on Agent May 4, 2025
Copy link
Contributor

hyperlint-ai bot commented May 5, 2025

PR Change Summary

Exposed the existing CLI functionality as a method on the Agent, allowing users to utilize CLI with custom Agents.

  • Introduced the run_cli() method on the Agent class for direct CLI access.
  • Updated documentation to include usage examples for custom agents.
  • Enhanced CLI documentation with installation and usage instructions.

Modified Files

  • docs/cli.md

How can I customize these reviews?

Check out the Hyperlint AI Reviewer docs for more information on how to customize the review.

If you just want to ignore it on this PR, you can add the hyperlint-ignore label to the PR. Future changes won't trigger a Hyperlint review.

Note specifically for link checks, we only check the first 30 links in a file and we cache the results for several hours (for instance, if you just added a page, you might experience this). Our recommendation is to add hyperlint-ignore to the PR to ignore the link check for this PR.

@AndrewHannigan
Copy link
Contributor Author

AndrewHannigan commented May 7, 2025

@samuelcolvin @Kludex any concerns merging this before #1504? This PR is a minor refactor, mainly just allows cli() to accept an Agent object. The only references to pai in this PR are in a few lines of documentation so I think the conflict risk is low.

@@ -83,7 +84,7 @@ def cli_system_prompt() -> str:
The user is running {sys.platform}."""


def cli(args_list: Sequence[str] | None = None) -> int:
def cli(args_list: Sequence[str] | None = None, agent: Agent[None, str] = cli_agent) -> int:
Copy link
Member

Choose a reason for hiding this comment

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

agent doesn't need to be a parameter here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Kludex It's necessary to support the run_cli() method here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could also remove run_cli() and just align on --agent mymodule.submodule:my_agent as the interface if that's preferable, let me know your thoughts.

@Kludex Kludex changed the title Expose run_cli() method on Agent Add to_cli() method to Agent May 13, 2025
@Kludex Kludex merged commit 3bc5219 into pydantic:main May 13, 2025
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants