Skip to content

Commit 4e9f46d

Browse files
CyrusNuevoDiaclaude
andcommitted
refactor: flexible LLM provider configuration with JSON configs
- Replace hardcoded AI Gateway URLs with configurable JSON-based provider configs - Add support for azure_openai, groq, and ollama providers alongside existing ones - Update .env.example to show new configuration format supporting both direct API and gateway usage - Move agent instructions to centralized location in lib/ai.py - Update documentation to reflect new flexible configuration approach 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 7e2c361 commit 4e9f46d

File tree

4 files changed

+54
-42
lines changed

4 files changed

+54
-42
lines changed

.env.example

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
# AI Gateway Configuration (required)
2-
# Get these from your Cloudflare AI Gateway dashboard
3-
AI_GATEWAY_URL="https://gateway.ai.cloudflare.com/v1/{account_id}/ai-gateway"
4-
AI_GATEWAY_TOKEN="your-gateway-token" # for authorization
1+
# LLM Provider Configuration (required)
2+
# Configure each provider you plan to use with JSON configuration
3+
# Replace {account_id} and your-gateway-token with actual values
4+
# These are passed directly to BrowserUse's ChatX classes as kwargs
5+
ANTHROPIC_CONFIG='{"base_url": "optional", "default_headers": {...}}'
6+
OPENAI_CONFIG='{"base_url": "optional", "default_headers": {...}}'
7+
GEMINI_CONFIG='{"http_options": {"base_url": "optional", "headers": {...}}}'
8+
AZURE_OPENAI_CONFIG='{"azure_endpoint": "https://your-resource.openai.azure.com/", "api_version": "2024-02-01"}'
9+
GROQ_CONFIG='{"base_url": "https://api.groq.com/openai/v1"}'
10+
OLLAMA_CONFIG='{"base_url": "http://localhost:11434/v1"}'
511

612
# Kernel Platform (required)
713
# Get your API key from the Kernel platform dashboard
814
KERNEL_API_KEY="sk_xxxxx"
915

10-
# Cloudflare R2 Storage (or other S3-compatible) for file uploads (required)
11-
# Configure these in your Cloudflare R2 settings
16+
# S3-compatible for storing downloaded files (required)
1217
S3_BUCKET="browser-agent"
1318
S3_ACCESS_KEY_ID="your-access-key"
1419
S3_ENDPOINT_URL="https://{account_id}.r2.cloudflarestorage.com"

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The browser-agent microservice provides AI-powered browser automation capabiliti
1515
- **CAPTCHA solving**: Built-in capability to handle CAPTCHAs and similar challenges
1616
- **Session management**: Creates isolated browser sessions with proper cleanup
1717
- **Trajectory tracking**: Records and stores complete execution history for analysis
18-
- **Cloudflare AI Gateway integration**: Unified LLM provider routing w/ caching
18+
- **AI Gateway integration**: Compatible with any AI gateway (Cloudflare, Azure, etc.) or direct provider APIs
1919

2020
## Getting Started
2121

@@ -37,14 +37,21 @@ cp .env.example .env
3737
Edit your `.env` file with the required values:
3838

3939
```bash
40-
# AI Gateway Configuration (required)
40+
# LLM Provider Configuration
41+
# Option 1: Direct API access (no gateway)
42+
# Nothing required here!
43+
44+
# Option 2: With AI Gateway (Cloudflare example)
4145
AI_GATEWAY_URL="https://gateway.ai.cloudflare.com/v1/{account_id}/ai-gateway"
42-
AI_GATEWAY_TOKEN="your-gateway-token"
46+
AI_GATEWAY_HEADERS='{"cf-aig-authorization": "Bearer your-gateway-token"}'
47+
ANTHROPIC_CONFIG='{"base_url": "${AI_GATEWAY_URL}/anthropic", "default_headers": ${AI_GATEWAY_HEADERS}}'
48+
OPENAI_CONFIG='{"base_url": "${AI_GATEWAY_URL}/openai", "default_headers": ${AI_GATEWAY_HEADERS}}'
49+
GEMINI_CONFIG='{"http_options": {"base_url": "${AI_GATEWAY_URL}/google-ai-studio", "headers": ${AI_GATEWAY_HEADERS}}}'
4350

4451
# Kernel Platform (required)
4552
KERNEL_API_KEY="sk_xxxxx"
4653

47-
# Cloudflare R2 Storage (required)
54+
# S3-compatible storage for file downloads (required)
4855
S3_BUCKET="browser-agent"
4956
S3_ACCESS_KEY_ID="your-access-key"
5057
S3_ENDPOINT_URL="https://{account_id}.r2.cloudflarestorage.com"
@@ -222,7 +229,7 @@ The deployment process:
222229
### Architecture Flow
223230

224231
1. Request received via Kernel platform
225-
2. LLM client created based on provider/model through AI Gateway
232+
2. LLM client created based on provider/model (direct API or through AI Gateway)
226233
3. Remote browser session established with custom configuration
227234
4. browser-use Agent instantiated with reasoning capabilities
228235
5. Task executed with intelligent planning and step-by-step execution

lib/ai.py

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,44 @@
11
from functools import partial
2-
from os import environ as env
2+
from os import getenv
33
import typing as t
44

5-
from browser_use import ChatGoogle, ChatOpenAI, ChatAnthropic
5+
from browser_use import (
6+
ChatGoogle,
7+
ChatOpenAI,
8+
ChatAnthropic,
9+
ChatAzureOpenAI,
10+
ChatGroq,
11+
ChatOllama,
12+
)
613
from browser_use.llm import BaseChatModel
7-
from google.genai.types import HttpOptions
14+
import orjson
815

9-
ModelProvider = t.Literal["anthropic", "gemini", "openai"]
16+
__all__ = ["ModelProvider", "ChatFactory", "AGENT_INSTRUCTIONS"]
17+
18+
19+
ModelProvider = t.Literal[
20+
"anthropic", "gemini", "openai", "azure_openai", "groq", "ollama"
21+
]
1022

11-
AI_GATEWAY_URL = env["AI_GATEWAY_URL"]
12-
DEFAULT_HEADERS = {
13-
"cf-aig-authorization": env["AI_GATEWAY_TOKEN"],
14-
}
23+
24+
def config(provider: ModelProvider) -> dict:
25+
return orjson.loads(getenv(f"{provider.upper()}_CONFIG", "{}"))
1526

1627

1728
ChatFactory: dict[ModelProvider, t.Callable[[str, str], BaseChatModel]] = {
18-
"gemini": partial(
19-
ChatGoogle,
20-
http_options=HttpOptions(
21-
base_url=f"{AI_GATEWAY_URL}/google-ai-studio",
22-
headers=DEFAULT_HEADERS,
23-
),
24-
),
25-
"openai": partial(
26-
ChatOpenAI,
27-
base_url=f"{AI_GATEWAY_URL}/openai",
28-
default_headers=DEFAULT_HEADERS,
29-
),
30-
"anthropic": partial(
31-
ChatAnthropic,
32-
base_url=f"{AI_GATEWAY_URL}/anthropic",
33-
default_headers=DEFAULT_HEADERS,
34-
),
29+
"gemini": partial(ChatGoogle, **config("gemini")),
30+
"openai": partial(ChatOpenAI, **config("openai")),
31+
"anthropic": partial(ChatAnthropic, **config("anthropic")),
32+
"azure-openai": partial(ChatAzureOpenAI, **config("azure_openai")),
33+
"groq": partial(ChatGroq, **config("groq")),
34+
"ollama": partial(ChatOllama, **config("ollama")),
3535
}
3636

37+
3738
AGENT_INSTRUCTIONS = """Remember, you are an agent - please keep going until the user's query is completely resolved, before ending your turn and yielding back to the user. Decompose the user's query into all required sub-requests, and confirm that each is completed. Do not stop after completing only part of the request. Only terminate your turn when you are sure that the problem is solved. You must be prepared to answer multiple queries and only finish the call once the user has confirmed they're done.
3839
39-
You must plan extensively in accordance with the workflow steps before making subsequent function calls, and reflect extensively on the outcomes each function call made, ensuring the user's query, and related sub-requests are completely resolved."""
40+
You must plan extensively in accordance with the workflow steps before making subsequent function calls, and reflect extensively on the outcomes each function call made, ensuring the user's query, and related sub-requests are completely resolved.
41+
42+
Note that your browser will automatically:
43+
1. Download the PDF file upon viewing it. Just wait for it. You do not need to read the PDF.
44+
2. Solve CAPTCHAs or similar tests. Just wait for it."""

main.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ async def perform(ctx: KernelContext, params: dict):
3131
filter(bool, [request.instructions, AGENT_INSTRUCTIONS])
3232
),
3333
"input": request.input,
34-
"notes": """
35-
Your browser will automatically:
36-
1. Download the PDF file upon viewing it. Just wait for it. You do not need to read the PDF.
37-
2. Solve CAPTCHAs or similar tests. Just wait for it.
38-
""",
3934
}
4035

4136
agent = Agent(

0 commit comments

Comments
 (0)