Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
55 changes: 16 additions & 39 deletions examples/agent_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,71 +33,48 @@
quiet_dependencies=True, # Reduce noise from dependencies
)

console.print(
Panel.fit(
"[yellow]Logging Levels:[/]\n"
"[white]- Set [bold]verbose=0[/] for errors (ERROR)[/]\n"
"[white]- Set [bold]verbose=1[/] for minimal logs (INFO)[/]\n"
"[white]- Set [bold]verbose=2[/] for medium logs (WARNING)[/]\n"
"[white]- Set [bold]verbose=3[/] for detailed logs (DEBUG)[/]",
title="Verbosity Options",
border_style="blue",
)
)

async def main():
# Build a unified configuration object for Stagehand
config = StagehandConfig(
env="BROWSERBASE",
# env="BROWSERBASE",
env="LOCAL",
api_key=os.getenv("BROWSERBASE_API_KEY"),
project_id=os.getenv("BROWSERBASE_PROJECT_ID"),
headless=False,
dom_settle_timeout_ms=3000,
model_name="gpt-4o",
self_heal=True,
wait_for_captcha_solves=True,
system_prompt="You are a browser automation assistant that helps users navigate websites effectively.",
model_client_options={"apiKey": os.getenv("MODEL_API_KEY")},
verbose=2,
verbose=1,
)

# Create a Stagehand client using the configuration object.
stagehand = Stagehand(
config=config,
api_url=os.getenv("STAGEHAND_API_URL"),
)
stagehand = Stagehand(config)

# Initialize - this creates a new session automatically.
console.print("\n🚀 [info]Initializing Stagehand...[/]")
await stagehand.init()
console.print(f"\n[yellow]Created new session:[/] {stagehand.session_id}")
console.print(
f"🌐 [white]View your live browser:[/] [url]https://www.browserbase.com/sessions/{stagehand.session_id}[/]"
)
if stagehand.env == "BROWSERBASE":
console.print(f"\n[yellow]Created new session:[/] {stagehand.session_id}")
console.print(
f"🌐 [white]View your live browser:[/] [url]https://www.browserbase.com/sessions/{stagehand.session_id}[/]"
)

console.print("\n▶️ [highlight] Navigating[/] to Google")
await stagehand.page.goto("https://google.com/")
console.print("✅ [success]Navigated to Google[/]")

# Configure the agent
agent_config = AgentConfig(
provider=AgentProvider.OPENAI,
console.print("\n▶️ [highlight] Using Agent to perform a task[/]: playing a game of 2048")
agent = stagehand.agent(
model="computer-use-preview",
instructions="You are a helpful web navigation assistant that helps users find information. You are currently on the following page: google.com. Do not ask follow up questions, the user will trust your judgement.",
options={"apiKey": os.getenv("MODEL_API_KEY")}
)

# Define the task for the agent
execute_options = AgentExecuteOptions(
agent_result = await agent.execute(
instruction="Play a game of 2048",
max_steps=20,
auto_screenshot=True,
)

console.print("\n▶️ [highlight] Navigating[/] to Google")
await stagehand.page.goto("https://google.com/")
console.print("✅ [success]Navigated to Google[/]")

console.print("\n▶️ [highlight] Using Agent to perform a task[/]: playing a game of 2048")
agent = stagehand.agent(agent_config)
agent_result = await agent.execute(execute_options)

console.print("📊 [info]Agent execution result:[/]")
console.print(f"✅ Success: [bold]{'Yes' if agent_result.success else 'No'}[/]")
console.print(f"🎯 Completed: [bold]{'Yes' if agent_result.completed else 'No'}[/]")
Expand Down
110 changes: 0 additions & 110 deletions stagehand/agent.py

This file was deleted.

Loading
Loading