Skip to content

Commit 02da423

Browse files
committed
make the module run in pure LOCAL
1 parent ec22cb9 commit 02da423

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+6548
-2039
lines changed

README.md

Lines changed: 169 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ await stagehand.agent.execute("book a reservation for 2 people for a trip to the
8484
```
8585

8686

87-
## Installation:
87+
## Installation
8888

8989
To get started, simply:
9090

@@ -96,10 +96,14 @@ pip install stagehand
9696
9797
```bash
9898
uv venv .venv
99-
source .venv/bin/activate
99+
source .venv/bin/activate # On Windows: .venv\Scripts\activate
100100
uv pip install stagehand
101101
```
102102

103+
### Prerequisites
104+
105+
Stagehand requires a local browser installation. The library uses Playwright to manage browser instances automatically. No additional browser setup is required - Playwright will download the necessary browser binaries on first use.
106+
103107
## Quickstart
104108

105109
```python
@@ -111,7 +115,7 @@ from pydantic import BaseModel, Field
111115
from stagehand import StagehandConfig, Stagehand
112116

113117
# Load environment variables
114-
load_dotenv()
118+
load_dotenv() # Create a .env file or set environment variables in your shell
115119

116120
# Define Pydantic models for structured data extraction
117121
class Company(BaseModel):
@@ -122,25 +126,25 @@ class Companies(BaseModel):
122126
companies: list[Company] = Field(..., description="List of companies")
123127

124128
async def main():
125-
# Create configuration
129+
# Create configuration with Alibaba ModelScope (DashScope)
126130
config = StagehandConfig(
127-
env = "BROWSERBASE", # or LOCAL
128-
api_key=os.getenv("BROWSERBASE_API_KEY"),
129-
project_id=os.getenv("BROWSERBASE_PROJECT_ID"),
130-
model_name="google/gemini-2.5-flash-preview-05-20",
131-
model_api_key=os.getenv("MODEL_API_KEY"),
131+
model_name="dashscope/qwen-turbo",
132+
model_client_options={
133+
"api_base": os.getenv("ALIBABA_ENDPOINT", "https://dashscope.aliyuncs.com/compatible-mode/v1"),
134+
"api_key": os.getenv("ALIBABA_API_KEY")
135+
},
136+
local_browser_launch_options={
137+
"headless": False # Set to True for headless mode
138+
}
132139
)
133140

134141
stagehand = Stagehand(config)
135142

136143
try:
137144
print("\nInitializing 🤘 Stagehand...")
138-
# Initialize Stagehand
145+
# Initialize Stagehand with local browser
139146
await stagehand.init()
140147

141-
if stagehand.env == "BROWSERBASE":
142-
print(f"🌐 View your live browser: https://www.browserbase.com/sessions/{stagehand.session_id}")
143-
144148
page = stagehand.page
145149

146150
await page.goto("https://www.aigrant.com")
@@ -156,9 +160,9 @@ async def main():
156160
for idx, company in enumerate(companies_data.companies, 1):
157161
print(f"{idx}. {company.name}: {company.description}")
158162

159-
observe = await page.observe("the link to the company Browserbase")
163+
observe = await page.observe("the search bar")
160164
print("\nObserve result:", observe)
161-
act = await page.act("click the link to the company Browserbase")
165+
act = await page.act("click on the search bar")
162166
print("\nAct result:", act)
163167

164168
except Exception as e:
@@ -173,6 +177,143 @@ if __name__ == "__main__":
173177
asyncio.run(main())
174178
```
175179

180+
## Configuration Options
181+
182+
### Basic Configuration
183+
184+
```python
185+
# OpenAI (default)
186+
config = StagehandConfig(
187+
model_name="gpt-4o-mini",
188+
model_client_options={
189+
"api_key": os.getenv("OPENAI_API_KEY")
190+
}
191+
)
192+
193+
# Anthropic Claude
194+
config = StagehandConfig(
195+
model_name="claude-3-haiku-20240307",
196+
model_client_options={
197+
"api_base": "https://api.anthropic.com",
198+
"api_key": os.getenv("ANTHROPIC_API_KEY")
199+
}
200+
)
201+
```
202+
203+
### Custom API Endpoints
204+
205+
Stagehand supports various OpenAI/Anthropic compatible providers:
206+
207+
```python
208+
# Together AI
209+
config = StagehandConfig(
210+
model_name="meta-llama/Llama-2-7b-chat-hf",
211+
model_client_options={
212+
"api_base": "https://api.together.xyz/v1",
213+
"api_key": os.getenv("TOGETHER_API_KEY")
214+
}
215+
)
216+
217+
# Groq
218+
config = StagehandConfig(
219+
model_name="llama2-70b-4096",
220+
model_client_options={
221+
"api_base": "https://api.groq.com/openai/v1",
222+
"api_key": os.getenv("GROQ_API_KEY")
223+
}
224+
)
225+
226+
# Local OpenAI-compatible server
227+
config = StagehandConfig(
228+
model_name="local/custom-model",
229+
model_client_options={
230+
"api_base": "http://localhost:8000/v1",
231+
"api_key": "local-key"
232+
}
233+
)
234+
```
235+
236+
### Browser Configuration
237+
238+
```python
239+
config = StagehandConfig(
240+
model_name="gpt-4o-mini",
241+
model_client_options={"api_key": os.getenv("OPENAI_API_KEY")},
242+
local_browser_launch_options={
243+
"headless": True, # Run in headless mode
244+
"viewport": {"width": 1280, "height": 720},
245+
"user_data_dir": "./browser_data", # Persistent browser data
246+
"args": ["--no-sandbox", "--disable-dev-shm-usage"] # Additional Chrome args
247+
}
248+
)
249+
```
250+
251+
## Migration from Browserbase
252+
253+
If you're upgrading from a previous version that used Browserbase, here's how to migrate your configuration:
254+
255+
### Quick Migration Check
256+
257+
Use our migration utility to scan your project:
258+
259+
```bash
260+
# Scan current directory for files needing migration
261+
python docs/migration_utility.py scan .
262+
263+
# Generate configuration examples
264+
python docs/migration_utility.py config openai
265+
```
266+
267+
### Before (Browserbase Configuration)
268+
```python
269+
# Old Browserbase configuration
270+
config = StagehandConfig(
271+
env="BROWSERBASE",
272+
api_key="browserbase-api-key",
273+
project_id="browserbase-project-id",
274+
model_name="gpt-4o",
275+
model_api_key="openai-api-key"
276+
)
277+
```
278+
279+
### After (Local Configuration)
280+
```python
281+
# New local configuration
282+
config = StagehandConfig(
283+
model_name="gpt-4o",
284+
model_client_options={
285+
"api_key": "openai-api-key",
286+
# Optional: specify custom endpoint
287+
"api_base": "https://api.openai.com/v1"
288+
},
289+
local_browser_launch_options={
290+
"headless": False # Configure browser options as needed
291+
}
292+
)
293+
```
294+
295+
### Key Changes
296+
- **Removed**: `env`, `api_key`, `project_id` parameters
297+
- **Replaced**: `model_api_key` with `model_client_options.api_key`
298+
- **Added**: `local_browser_launch_options` for browser configuration
299+
- **Enhanced**: Support for custom API endpoints via `model_client_options.api_base`
300+
301+
### Environment Variables
302+
Update your environment variables:
303+
```bash
304+
# Remove these (no longer needed)
305+
# BROWSERBASE_API_KEY=your-browserbase-key
306+
# BROWSERBASE_PROJECT_ID=your-project-id
307+
308+
# Keep or add these
309+
OPENAI_API_KEY=your-openai-key
310+
# Or for other providers:
311+
# ANTHROPIC_API_KEY=your-anthropic-key
312+
# TOGETHER_API_KEY=your-together-key
313+
```
314+
315+
For a complete migration guide with troubleshooting, see [docs/migration_guide.md](docs/migration_guide.md).
316+
176317
## Documentation
177318

178319
See our full documentation [here](https://docs.stagehand.dev/).
@@ -219,8 +360,21 @@ cd stagehand-python
219360

220361
# Install in editable mode with development dependencies
221362
pip install -r requirements.txt
363+
364+
# On Windows, you may need to install Playwright browsers
365+
playwright install
222366
```
223367

368+
### Dependencies
369+
370+
Stagehand has minimal dependencies and no longer requires external browser services:
371+
372+
- **Core**: `playwright`, `pydantic`, `python-dotenv`
373+
- **LLM Support**: `openai`, `anthropic`, `litellm`
374+
- **Utilities**: `rich`, `nest-asyncio`
375+
376+
All dependencies are automatically installed with `pip install stagehand`.
377+
224378

225379
## License
226380

0 commit comments

Comments
 (0)