Basic Function Registration
Functions can be either sync
or async
. Keep them focused and single-purpose.
from openoperator import Controller, ActionResult
# Initialize the controller
controller = Controller()
@controller.action('Ask user for information')
def ask_human(question: str) -> str:
answer = input(f'\n{question}\nInput: ')
return ActionResult(extracted_content=answer)
Basic Controller
has all basic functionality you might need to interact with
the browser already implemented.
# ... then pass controller to the agent
agent = Agent(
llm=llm,
controller=controller
)
Keep the function name and description short and concise. The Agent use the
function solely based on the name and description. The stringified output of
the action is passed to the Agent.
Browser-Aware Functions
For actions that need browser access, use the requires_browser=True
parameter:
from openoperator import Browser, Controller, ActionResult
controller = Controller()
@controller.action('Open website', requires_browser=True)
async def open_website(url: str, browser: Browser):
page = browser.get_current_page()
await page.goto(url)
return ActionResult(extracted_content='Website opened')
Structured Parameters with Pydantic
For complex actions, you can define parameter schemas using Pydantic models:
from pydantic import BaseModel
from typing import Optional
from openoperator import Controller, ActionResult, Browser
controller = Controller()
class JobDetails(BaseModel):
title: str
company: str
job_link: str
salary: Optional[str] = None
@controller.action(
'Save job details which you found on page',
param_model=JobDetails,
requires_browser=True
)
async def save_job(params: JobDetails, browser: Browser):
print(f"Saving job: {params.title} at {params.company}")
# Access browser if needed
page = browser.get_current_page()
await page.goto(params.job_link)
Using Custom Actions with multiple agents
You can use the same controller for multiple agents.
controller = Controller()
# ... register actions to the controller
agent = Agent(
llm=llm,
controller=controller
)
agent.add_task("Go to website X and find the latest news")
# Run the agent
await agent.run()
agent2 = Agent(
llm=llm,
controller=controller
)
agent2.add_task("Go to website Y and find the latest news")
await agent2.run()
The controller is stateless and can be used to register multiple actions and
multiple agents.
Exclude functions
If you want less actions to be used by the agent, you can exclude them from the controller.
controller = Controller(exclude_actions=['open_tab', 'google_search'])
For more examples like file upload or notifications, visit examples/custom-functions.