ai-agent-book 精选快照(<2MB 代码与文档,来自 github.com/bojieli/ai-agent-book)
Build latest book artifacts / build (push) Canceled after 0s
dependency resolution / resolve (3.11) (push) Canceled after 0s
dependency resolution / resolve (3.13) (push) Canceled after 0s
deploy-pages / build (push) Canceled after 0s
deploy-pages / deploy (push) Canceled after 0s
i18n consistency check / check (push) Canceled after 0s
provider adoption tests / test (chapter2/context-compression) (push) Canceled after 0s
provider adoption tests / test (chapter2/prompt-injection) (push) Canceled after 0s
provider adoption tests / test (chapter2/system-hint) (push) Canceled after 0s
provider adoption tests / test (chapter3/log-sanitization) (push) Canceled after 0s
web-search-agent tests / test (push) Canceled after 0s
web-search-agent tests / agentbook (push) Canceled after 0s
Build latest book artifacts / build (push) Canceled after 0s
dependency resolution / resolve (3.11) (push) Canceled after 0s
dependency resolution / resolve (3.13) (push) Canceled after 0s
deploy-pages / build (push) Canceled after 0s
deploy-pages / deploy (push) Canceled after 0s
i18n consistency check / check (push) Canceled after 0s
provider adoption tests / test (chapter2/context-compression) (push) Canceled after 0s
provider adoption tests / test (chapter2/prompt-injection) (push) Canceled after 0s
provider adoption tests / test (chapter2/system-hint) (push) Canceled after 0s
provider adoption tests / test (chapter3/log-sanitization) (push) Canceled after 0s
web-search-agent tests / test (push) Canceled after 0s
web-search-agent tests / agentbook (push) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
"""
|
||||
AWS Bedrock Examples
|
||||
|
||||
This file demonstrates how to use AWS Bedrock models with browser-use.
|
||||
We provide two classes:
|
||||
1. ChatAnthropicBedrock - Convenience class for Anthropic Claude models
|
||||
2. ChatAWSBedrock - General AWS Bedrock client supporting all providers
|
||||
|
||||
Requirements:
|
||||
- AWS credentials configured via environment variables
|
||||
- boto3 installed: pip install boto3
|
||||
- Access to AWS Bedrock models in your region
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
||||
from browser_use import Agent
|
||||
from browser_use.llm import ChatAnthropicBedrock, ChatAWSBedrock
|
||||
|
||||
|
||||
async def example_anthropic_bedrock():
|
||||
"""Example using ChatAnthropicBedrock - convenience class for Claude models."""
|
||||
print('🔹 ChatAnthropicBedrock Example')
|
||||
|
||||
# Initialize with Anthropic Claude via AWS Bedrock
|
||||
llm = ChatAnthropicBedrock(
|
||||
model='us.anthropic.claude-sonnet-4-20250514-v1:0',
|
||||
aws_region='us-east-1',
|
||||
temperature=0.7,
|
||||
)
|
||||
|
||||
print(f'Model: {llm.name}')
|
||||
print(f'Provider: {llm.provider}')
|
||||
|
||||
# Create agent
|
||||
agent = Agent(
|
||||
task="Navigate to google.com and search for 'AWS Bedrock pricing'",
|
||||
llm=llm,
|
||||
)
|
||||
|
||||
print("Task: Navigate to google.com and search for 'AWS Bedrock pricing'")
|
||||
|
||||
# Run the agent
|
||||
result = await agent.run(max_steps=2)
|
||||
print(f'Result: {result}')
|
||||
|
||||
|
||||
async def example_aws_bedrock():
|
||||
"""Example using ChatAWSBedrock - general client for any Bedrock model."""
|
||||
print('\n🔹 ChatAWSBedrock Example')
|
||||
|
||||
# Initialize with any AWS Bedrock model (using Meta Llama as example)
|
||||
llm = ChatAWSBedrock(
|
||||
model='us.meta.llama4-maverick-17b-instruct-v1:0',
|
||||
aws_region='us-east-1',
|
||||
temperature=0.5,
|
||||
)
|
||||
|
||||
print(f'Model: {llm.name}')
|
||||
print(f'Provider: {llm.provider}')
|
||||
|
||||
# Create agent
|
||||
agent = Agent(
|
||||
task='Go to github.com and find the most popular Python repository',
|
||||
llm=llm,
|
||||
)
|
||||
|
||||
print('Task: Go to github.com and find the most popular Python repository')
|
||||
|
||||
# Run the agent
|
||||
result = await agent.run(max_steps=2)
|
||||
print(f'Result: {result}')
|
||||
|
||||
|
||||
async def main():
|
||||
"""Run AWS Bedrock examples."""
|
||||
print('🚀 AWS Bedrock Examples')
|
||||
print('=' * 40)
|
||||
|
||||
print('Make sure you have AWS credentials configured:')
|
||||
print('export AWS_ACCESS_KEY_ID=your_key')
|
||||
print('export AWS_SECRET_ACCESS_KEY=your_secret')
|
||||
print('export AWS_DEFAULT_REGION=us-east-1')
|
||||
print('=' * 40)
|
||||
|
||||
try:
|
||||
# Run both examples
|
||||
await example_aws_bedrock()
|
||||
await example_anthropic_bedrock()
|
||||
|
||||
except Exception as e:
|
||||
print(f'❌ Error: {e}')
|
||||
print('Make sure you have:')
|
||||
print('- Valid AWS credentials configured')
|
||||
print('- Access to AWS Bedrock in your region')
|
||||
print('- boto3 installed: pip install boto3')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,44 @@
|
||||
"""
|
||||
Simple try of the agent.
|
||||
|
||||
@dev You need to add AZURE_OPENAI_KEY and AZURE_OPENAI_ENDPOINT to your environment variables.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import sys
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
|
||||
from browser_use import Agent
|
||||
from browser_use.llm import ChatAzureOpenAI
|
||||
|
||||
# Make sure your deployment exists, double check the region and model name
|
||||
api_key = os.getenv('AZURE_OPENAI_KEY')
|
||||
azure_endpoint = os.getenv('AZURE_OPENAI_ENDPOINT')
|
||||
llm = ChatAzureOpenAI(
|
||||
model='gpt-4.1-mini',
|
||||
api_key=api_key,
|
||||
azure_endpoint=azure_endpoint,
|
||||
)
|
||||
|
||||
TASK = """
|
||||
Go to google.com/travel/flights and find the cheapest flight from New York to Paris on 2025-10-15
|
||||
"""
|
||||
|
||||
agent = Agent(
|
||||
task=TASK,
|
||||
llm=llm,
|
||||
)
|
||||
|
||||
|
||||
async def main():
|
||||
await agent.run(max_steps=10)
|
||||
|
||||
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,31 @@
|
||||
"""
|
||||
Simple script that runs the task of opening amazon and searching.
|
||||
@dev Ensure we have a `ANTHROPIC_API_KEY` variable in our `.env` file.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import sys
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
from browser_use import Agent
|
||||
from browser_use.llm import ChatAnthropic
|
||||
|
||||
llm = ChatAnthropic(model='claude-sonnet-4-0', temperature=0.0)
|
||||
|
||||
agent = Agent(
|
||||
task='Go to amazon.com, search for laptop, sort by best rating, and give me the price of the first result',
|
||||
llm=llm,
|
||||
)
|
||||
|
||||
|
||||
async def main():
|
||||
await agent.run(max_steps=10)
|
||||
|
||||
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,36 @@
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
from browser_use import Agent
|
||||
from browser_use.llm import ChatDeepSeek
|
||||
|
||||
# Add your custom instructions
|
||||
extend_system_message = """
|
||||
Remember the most important rules:
|
||||
1. When performing a search task, open https://www.google.com/ first for search.
|
||||
2. Final output.
|
||||
"""
|
||||
deepseek_api_key = os.getenv('DEEPSEEK_API_KEY')
|
||||
if deepseek_api_key is None:
|
||||
print('Make sure you have DEEPSEEK_API_KEY:')
|
||||
print('export DEEPSEEK_API_KEY=your_key')
|
||||
exit(0)
|
||||
|
||||
|
||||
async def main():
|
||||
llm = ChatDeepSeek(
|
||||
base_url='https://api.deepseek.com/v1',
|
||||
model='deepseek-chat',
|
||||
api_key=deepseek_api_key,
|
||||
)
|
||||
|
||||
agent = Agent(
|
||||
task='What should we pay attention to in the recent new rules on tariffs in China-US trade?',
|
||||
llm=llm,
|
||||
use_vision=False,
|
||||
extend_system_message=extend_system_message,
|
||||
)
|
||||
await agent.run()
|
||||
|
||||
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,30 @@
|
||||
import asyncio
|
||||
import os
|
||||
import sys
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from browser_use import Agent, ChatGoogle
|
||||
|
||||
load_dotenv()
|
||||
|
||||
api_key = os.getenv('GOOGLE_API_KEY')
|
||||
if not api_key:
|
||||
raise ValueError('GOOGLE_API_KEY is not set')
|
||||
|
||||
llm = ChatGoogle(model='gemini-2.5-flash', api_key=api_key, thinking_budget=-1)
|
||||
|
||||
|
||||
async def run_search():
|
||||
agent = Agent(
|
||||
task='How many stars does the browser-use repo have?',
|
||||
llm=llm,
|
||||
)
|
||||
|
||||
await agent.run()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(run_search())
|
||||
@@ -0,0 +1,28 @@
|
||||
"""
|
||||
Simple try of the agent.
|
||||
|
||||
@dev You need to add OPENAI_API_KEY to your environment variables.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from browser_use import Agent, ChatOpenAI
|
||||
|
||||
load_dotenv()
|
||||
|
||||
# All the models are type safe from OpenAI in case you need a list of supported models
|
||||
llm = ChatOpenAI(model='gpt-4.1-mini')
|
||||
agent = Agent(
|
||||
task='Go to amazon.com, click on the first link, and give me the title of the page',
|
||||
llm=llm,
|
||||
)
|
||||
|
||||
|
||||
async def main():
|
||||
await agent.run(max_steps=10)
|
||||
input('Press Enter to continue...')
|
||||
|
||||
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,28 @@
|
||||
"""
|
||||
Simple try of the agent.
|
||||
|
||||
@dev You need to add OPENAI_API_KEY to your environment variables.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from browser_use import Agent, ChatOpenAI
|
||||
|
||||
load_dotenv()
|
||||
|
||||
# All the models are type safe from OpenAI in case you need a list of supported models
|
||||
llm = ChatOpenAI(model='gpt-5-mini')
|
||||
agent = Agent(
|
||||
llm=llm,
|
||||
task='Find out which one is cooler: the monkey park or a dolphin tour in Tenerife?',
|
||||
)
|
||||
|
||||
|
||||
async def main():
|
||||
await agent.run(max_steps=20)
|
||||
input('Press Enter to continue...')
|
||||
|
||||
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,33 @@
|
||||
# Langchain Models (legacy)
|
||||
|
||||
This directory contains example of how to still use Langchain models with the new Browser Use chat models.
|
||||
|
||||
## How to use
|
||||
|
||||
```python
|
||||
from langchain_openai import ChatOpenAI
|
||||
|
||||
from browser_use import Agent
|
||||
from .chat import ChatLangchain
|
||||
|
||||
async def main():
|
||||
"""Basic example using ChatLangchain with OpenAI through LangChain."""
|
||||
|
||||
# Create a LangChain model (OpenAI)
|
||||
langchain_model = ChatOpenAI(
|
||||
model='gpt-4.1-mini',
|
||||
temperature=0.1,
|
||||
)
|
||||
|
||||
# Wrap it with ChatLangchain to make it compatible with browser-use
|
||||
llm = ChatLangchain(chat=langchain_model)
|
||||
|
||||
agent = Agent(
|
||||
task="Go to google.com and search for 'browser automation with Python'",
|
||||
llm=llm,
|
||||
)
|
||||
|
||||
history = await agent.run()
|
||||
|
||||
print(history.history)
|
||||
```
|
||||
@@ -0,0 +1,195 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import TYPE_CHECKING, TypeVar, overload
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from browser_use.llm.base import BaseChatModel
|
||||
from browser_use.llm.exceptions import ModelProviderError
|
||||
from browser_use.llm.messages import BaseMessage
|
||||
from browser_use.llm.views import ChatInvokeCompletion, ChatInvokeUsage
|
||||
from examples.models.langchain.serializer import LangChainMessageSerializer
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from langchain_core.language_models.chat_models import BaseChatModel as LangChainBaseChatModel # type: ignore
|
||||
from langchain_core.messages import AIMessage as LangChainAIMessage # type: ignore
|
||||
|
||||
T = TypeVar('T', bound=BaseModel)
|
||||
|
||||
|
||||
@dataclass
|
||||
class ChatLangchain(BaseChatModel):
|
||||
"""
|
||||
A wrapper around LangChain BaseChatModel that implements the browser-use BaseChatModel protocol.
|
||||
|
||||
This class allows you to use any LangChain-compatible model with browser-use.
|
||||
"""
|
||||
|
||||
# The LangChain model to wrap
|
||||
chat: 'LangChainBaseChatModel'
|
||||
|
||||
@property
|
||||
def model(self) -> str:
|
||||
return self.name
|
||||
|
||||
@property
|
||||
def provider(self) -> str:
|
||||
"""Return the provider name based on the LangChain model class."""
|
||||
model_class_name = self.chat.__class__.__name__.lower()
|
||||
if 'openai' in model_class_name:
|
||||
return 'openai'
|
||||
elif 'anthropic' in model_class_name or 'claude' in model_class_name:
|
||||
return 'anthropic'
|
||||
elif 'google' in model_class_name or 'gemini' in model_class_name:
|
||||
return 'google'
|
||||
elif 'groq' in model_class_name:
|
||||
return 'groq'
|
||||
elif 'ollama' in model_class_name:
|
||||
return 'ollama'
|
||||
elif 'deepseek' in model_class_name:
|
||||
return 'deepseek'
|
||||
else:
|
||||
return 'langchain'
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return the model name."""
|
||||
# Try to get model name from the LangChain model using getattr to avoid type errors
|
||||
model_name = getattr(self.chat, 'model_name', None)
|
||||
if model_name:
|
||||
return str(model_name)
|
||||
|
||||
model_attr = getattr(self.chat, 'model', None)
|
||||
if model_attr:
|
||||
return str(model_attr)
|
||||
|
||||
return self.chat.__class__.__name__
|
||||
|
||||
def _get_usage(self, response: 'LangChainAIMessage') -> ChatInvokeUsage | None:
|
||||
usage = response.usage_metadata
|
||||
if usage is None:
|
||||
return None
|
||||
|
||||
prompt_tokens = usage['input_tokens'] or 0
|
||||
completion_tokens = usage['output_tokens'] or 0
|
||||
total_tokens = usage['total_tokens'] or 0
|
||||
|
||||
input_token_details = usage.get('input_token_details', None)
|
||||
|
||||
if input_token_details is not None:
|
||||
prompt_cached_tokens = input_token_details.get('cache_read', None)
|
||||
prompt_cache_creation_tokens = input_token_details.get('cache_creation', None)
|
||||
else:
|
||||
prompt_cached_tokens = None
|
||||
prompt_cache_creation_tokens = None
|
||||
|
||||
return ChatInvokeUsage(
|
||||
prompt_tokens=prompt_tokens,
|
||||
prompt_cached_tokens=prompt_cached_tokens,
|
||||
prompt_cache_creation_tokens=prompt_cache_creation_tokens,
|
||||
prompt_image_tokens=None,
|
||||
completion_tokens=completion_tokens,
|
||||
total_tokens=total_tokens,
|
||||
)
|
||||
|
||||
@overload
|
||||
async def ainvoke(self, messages: list[BaseMessage], output_format: None = None) -> ChatInvokeCompletion[str]: ...
|
||||
|
||||
@overload
|
||||
async def ainvoke(self, messages: list[BaseMessage], output_format: type[T]) -> ChatInvokeCompletion[T]: ...
|
||||
|
||||
async def ainvoke(
|
||||
self, messages: list[BaseMessage], output_format: type[T] | None = None
|
||||
) -> ChatInvokeCompletion[T] | ChatInvokeCompletion[str]:
|
||||
"""
|
||||
Invoke the LangChain model with the given messages.
|
||||
|
||||
Args:
|
||||
messages: List of browser-use chat messages
|
||||
output_format: Optional Pydantic model class for structured output (not supported in basic LangChain integration)
|
||||
|
||||
Returns:
|
||||
Either a string response or an instance of output_format
|
||||
"""
|
||||
|
||||
# Convert browser-use messages to LangChain messages
|
||||
langchain_messages = LangChainMessageSerializer.serialize_messages(messages)
|
||||
|
||||
try:
|
||||
if output_format is None:
|
||||
# Return string response
|
||||
response = await self.chat.ainvoke(langchain_messages) # type: ignore
|
||||
|
||||
# Import at runtime for isinstance check
|
||||
from langchain_core.messages import AIMessage as LangChainAIMessage # type: ignore
|
||||
|
||||
if not isinstance(response, LangChainAIMessage):
|
||||
raise ModelProviderError(
|
||||
message=f'Response is not an AIMessage: {type(response)}',
|
||||
model=self.name,
|
||||
)
|
||||
|
||||
# Extract content from LangChain response
|
||||
content = response.content if hasattr(response, 'content') else str(response)
|
||||
|
||||
usage = self._get_usage(response)
|
||||
return ChatInvokeCompletion(
|
||||
completion=str(content),
|
||||
usage=usage,
|
||||
)
|
||||
|
||||
else:
|
||||
# Use LangChain's structured output capability
|
||||
try:
|
||||
structured_chat = self.chat.with_structured_output(output_format)
|
||||
parsed_object = await structured_chat.ainvoke(langchain_messages)
|
||||
|
||||
# For structured output, usage metadata is typically not available
|
||||
# in the parsed object since it's a Pydantic model, not an AIMessage
|
||||
usage = None
|
||||
|
||||
# Type cast since LangChain's with_structured_output returns the correct type
|
||||
return ChatInvokeCompletion(
|
||||
completion=parsed_object, # type: ignore
|
||||
usage=usage,
|
||||
)
|
||||
except AttributeError:
|
||||
# Fall back to manual parsing if with_structured_output is not available
|
||||
response = await self.chat.ainvoke(langchain_messages) # type: ignore
|
||||
|
||||
if not isinstance(response, 'LangChainAIMessage'):
|
||||
raise ModelProviderError(
|
||||
message=f'Response is not an AIMessage: {type(response)}',
|
||||
model=self.name,
|
||||
)
|
||||
|
||||
content = response.content if hasattr(response, 'content') else str(response)
|
||||
|
||||
try:
|
||||
if isinstance(content, str):
|
||||
import json
|
||||
|
||||
parsed_data = json.loads(content)
|
||||
if isinstance(parsed_data, dict):
|
||||
parsed_object = output_format(**parsed_data)
|
||||
else:
|
||||
raise ValueError('Parsed JSON is not a dictionary')
|
||||
else:
|
||||
raise ValueError('Content is not a string and structured output not supported')
|
||||
except Exception as e:
|
||||
raise ModelProviderError(
|
||||
message=f'Failed to parse response as {output_format.__name__}: {e}',
|
||||
model=self.name,
|
||||
) from e
|
||||
|
||||
usage = self._get_usage(response)
|
||||
return ChatInvokeCompletion(
|
||||
completion=parsed_object,
|
||||
usage=usage,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
# Convert any LangChain errors to browser-use ModelProviderError
|
||||
raise ModelProviderError(
|
||||
message=f'LangChain model error: {str(e)}',
|
||||
model=self.name,
|
||||
) from e
|
||||
@@ -0,0 +1,60 @@
|
||||
"""
|
||||
Example of using LangChain models with browser-use.
|
||||
|
||||
This example demonstrates how to:
|
||||
1. Wrap a LangChain model with ChatLangchain
|
||||
2. Use it with a browser-use Agent
|
||||
3. Run a simple web automation task
|
||||
|
||||
@file purpose: Example usage of LangChain integration with browser-use
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
||||
from langchain_openai import ChatOpenAI # pyright: ignore
|
||||
|
||||
from browser_use import Agent
|
||||
from examples.models.langchain.chat import ChatLangchain
|
||||
|
||||
|
||||
async def main():
|
||||
"""Basic example using ChatLangchain with OpenAI through LangChain."""
|
||||
|
||||
# Create a LangChain model (OpenAI)
|
||||
langchain_model = ChatOpenAI(
|
||||
model='gpt-4.1-mini',
|
||||
temperature=0.1,
|
||||
)
|
||||
|
||||
# Wrap it with ChatLangchain to make it compatible with browser-use
|
||||
llm = ChatLangchain(chat=langchain_model)
|
||||
|
||||
# Create a simple task
|
||||
task = "Go to google.com and search for 'browser automation with Python'"
|
||||
|
||||
# Create and run the agent
|
||||
agent = Agent(
|
||||
task=task,
|
||||
llm=llm,
|
||||
)
|
||||
|
||||
print(f'🚀 Starting task: {task}')
|
||||
print(f'🤖 Using model: {llm.name} (provider: {llm.provider})')
|
||||
|
||||
# Run the agent
|
||||
history = await agent.run()
|
||||
|
||||
print(f'✅ Task completed! Steps taken: {len(history.history)}')
|
||||
|
||||
# Print the final result if available
|
||||
if history.final_result():
|
||||
print(f'📋 Final result: {history.final_result()}')
|
||||
|
||||
return history
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print('🌐 Browser-use LangChain Integration Example')
|
||||
print('=' * 45)
|
||||
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,149 @@
|
||||
import json
|
||||
from typing import overload
|
||||
|
||||
from langchain_core.messages import ( # pyright: ignore
|
||||
AIMessage,
|
||||
HumanMessage,
|
||||
SystemMessage,
|
||||
)
|
||||
from langchain_core.messages import ( # pyright: ignore
|
||||
ToolCall as LangChainToolCall,
|
||||
)
|
||||
from langchain_core.messages.base import BaseMessage as LangChainBaseMessage # pyright: ignore
|
||||
|
||||
from browser_use.llm.messages import (
|
||||
AssistantMessage,
|
||||
BaseMessage,
|
||||
ContentPartImageParam,
|
||||
ContentPartRefusalParam,
|
||||
ContentPartTextParam,
|
||||
ToolCall,
|
||||
UserMessage,
|
||||
)
|
||||
from browser_use.llm.messages import (
|
||||
SystemMessage as BrowserUseSystemMessage,
|
||||
)
|
||||
|
||||
|
||||
class LangChainMessageSerializer:
|
||||
"""Serializer for converting between browser-use message types and LangChain message types."""
|
||||
|
||||
@staticmethod
|
||||
def _serialize_user_content(
|
||||
content: str | list[ContentPartTextParam | ContentPartImageParam],
|
||||
) -> str | list[str | dict]:
|
||||
"""Convert user message content for LangChain compatibility."""
|
||||
if isinstance(content, str):
|
||||
return content
|
||||
|
||||
serialized_parts = []
|
||||
for part in content:
|
||||
if part.type == 'text':
|
||||
serialized_parts.append(
|
||||
{
|
||||
'type': 'text',
|
||||
'text': part.text,
|
||||
}
|
||||
)
|
||||
elif part.type == 'image_url':
|
||||
# LangChain format for images
|
||||
serialized_parts.append(
|
||||
{'type': 'image_url', 'image_url': {'url': part.image_url.url, 'detail': part.image_url.detail}}
|
||||
)
|
||||
|
||||
return serialized_parts
|
||||
|
||||
@staticmethod
|
||||
def _serialize_system_content(
|
||||
content: str | list[ContentPartTextParam],
|
||||
) -> str:
|
||||
"""Convert system message content to text string for LangChain compatibility."""
|
||||
if isinstance(content, str):
|
||||
return content
|
||||
|
||||
text_parts = []
|
||||
for part in content:
|
||||
if part.type == 'text':
|
||||
text_parts.append(part.text)
|
||||
|
||||
return '\n'.join(text_parts)
|
||||
|
||||
@staticmethod
|
||||
def _serialize_assistant_content(
|
||||
content: str | list[ContentPartTextParam | ContentPartRefusalParam] | None,
|
||||
) -> str:
|
||||
"""Convert assistant message content to text string for LangChain compatibility."""
|
||||
if content is None:
|
||||
return ''
|
||||
if isinstance(content, str):
|
||||
return content
|
||||
|
||||
text_parts = []
|
||||
for part in content:
|
||||
if part.type == 'text':
|
||||
text_parts.append(part.text)
|
||||
# elif part.type == 'refusal':
|
||||
# # Include refusal content as text
|
||||
# text_parts.append(f'[Refusal: {part.refusal}]')
|
||||
|
||||
return '\n'.join(text_parts)
|
||||
|
||||
@staticmethod
|
||||
def _serialize_tool_call(tool_call: ToolCall) -> LangChainToolCall:
|
||||
"""Convert browser-use ToolCall to LangChain ToolCall."""
|
||||
# Parse the arguments string to a dict for LangChain
|
||||
try:
|
||||
args_dict = json.loads(tool_call.function.arguments)
|
||||
except json.JSONDecodeError:
|
||||
# If parsing fails, wrap in a dict
|
||||
args_dict = {'arguments': tool_call.function.arguments}
|
||||
|
||||
return LangChainToolCall(
|
||||
name=tool_call.function.name,
|
||||
args=args_dict,
|
||||
id=tool_call.id,
|
||||
)
|
||||
|
||||
# region - Serialize overloads
|
||||
@overload
|
||||
@staticmethod
|
||||
def serialize(message: UserMessage) -> HumanMessage: ...
|
||||
|
||||
@overload
|
||||
@staticmethod
|
||||
def serialize(message: BrowserUseSystemMessage) -> SystemMessage: ...
|
||||
|
||||
@overload
|
||||
@staticmethod
|
||||
def serialize(message: AssistantMessage) -> AIMessage: ...
|
||||
|
||||
@staticmethod
|
||||
def serialize(message: BaseMessage) -> LangChainBaseMessage:
|
||||
"""Serialize a browser-use message to a LangChain message."""
|
||||
|
||||
if isinstance(message, UserMessage):
|
||||
content = LangChainMessageSerializer._serialize_user_content(message.content)
|
||||
return HumanMessage(content=content, name=message.name)
|
||||
|
||||
elif isinstance(message, BrowserUseSystemMessage):
|
||||
content = LangChainMessageSerializer._serialize_system_content(message.content)
|
||||
return SystemMessage(content=content, name=message.name)
|
||||
|
||||
elif isinstance(message, AssistantMessage):
|
||||
# Handle content
|
||||
content = LangChainMessageSerializer._serialize_assistant_content(message.content)
|
||||
|
||||
# For simplicity, we'll ignore tool calls in LangChain integration
|
||||
# as requested by the user
|
||||
return AIMessage(
|
||||
content=content,
|
||||
name=message.name,
|
||||
)
|
||||
|
||||
else:
|
||||
raise ValueError(f'Unknown message type: {type(message)}')
|
||||
|
||||
@staticmethod
|
||||
def serialize_messages(messages: list[BaseMessage]) -> list[LangChainBaseMessage]:
|
||||
"""Serialize a list of browser-use messages to LangChain messages."""
|
||||
return [LangChainMessageSerializer.serialize(m) for m in messages]
|
||||
@@ -0,0 +1,6 @@
|
||||
from browser_use import Agent, models
|
||||
|
||||
# available providers for this import style: openai, azure, google
|
||||
agent = Agent(task='Find founders of browser-use', llm=models.azure_gpt_4_1_mini)
|
||||
|
||||
agent.run_sync()
|
||||
@@ -0,0 +1,39 @@
|
||||
import asyncio
|
||||
import os
|
||||
import sys
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
|
||||
from browser_use import Agent
|
||||
from browser_use.llm import ChatGroq
|
||||
|
||||
groq_api_key = os.environ.get('GROQ_API_KEY')
|
||||
llm = ChatGroq(
|
||||
model='meta-llama/llama-4-maverick-17b-128e-instruct',
|
||||
# temperature=0.1,
|
||||
)
|
||||
|
||||
# llm = ChatGroq(
|
||||
# model='meta-llama/llama-4-maverick-17b-128e-instruct',
|
||||
# api_key=os.environ.get('GROQ_API_KEY'),
|
||||
# temperature=0.0,
|
||||
# )
|
||||
|
||||
task = 'Go to amazon.com, search for laptop, sort by best rating, and give me the price of the first result'
|
||||
|
||||
|
||||
async def main():
|
||||
agent = Agent(
|
||||
task=task,
|
||||
llm=llm,
|
||||
)
|
||||
await agent.run()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,45 @@
|
||||
"""
|
||||
Simple try of the agent.
|
||||
|
||||
@dev You need to add NOVITA_API_KEY to your environment variables.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import sys
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
|
||||
from browser_use import Agent, ChatOpenAI
|
||||
|
||||
api_key = os.getenv('NOVITA_API_KEY', '')
|
||||
if not api_key:
|
||||
raise ValueError('NOVITA_API_KEY is not set')
|
||||
|
||||
|
||||
async def run_search():
|
||||
agent = Agent(
|
||||
task=(
|
||||
'1. Go to https://www.reddit.com/r/LocalLLaMA '
|
||||
"2. Search for 'browser use' in the search bar"
|
||||
'3. Click on first result'
|
||||
'4. Return the first comment'
|
||||
),
|
||||
llm=ChatOpenAI(
|
||||
base_url='https://api.novita.ai/v3/openai',
|
||||
model='deepseek/deepseek-v3-0324',
|
||||
api_key=api_key,
|
||||
),
|
||||
use_vision=False,
|
||||
)
|
||||
|
||||
await agent.run()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(run_search())
|
||||
@@ -0,0 +1,10 @@
|
||||
# 1. Install Ollama: https://github.com/ollama/ollama
|
||||
# 2. Run `ollama serve` to start the server
|
||||
# 3. In a new terminal, install the model you want to use: `ollama pull llama3.1:8b` (this has 4.9GB)
|
||||
|
||||
|
||||
from browser_use import Agent, ChatOllama
|
||||
|
||||
llm = ChatOllama(model='llama3.1:8b')
|
||||
|
||||
Agent('find the founders of browser-use', llm=llm).run_sync()
|
||||
@@ -0,0 +1,33 @@
|
||||
"""
|
||||
Simple try of the agent.
|
||||
|
||||
@dev You need to add OPENAI_API_KEY to your environment variables.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from browser_use import Agent, ChatOpenAI
|
||||
|
||||
load_dotenv()
|
||||
|
||||
# All the models are type safe from OpenAI in case you need a list of supported models
|
||||
llm = ChatOpenAI(
|
||||
model='x-ai/grok-4',
|
||||
base_url='https://openrouter.ai/api/v1',
|
||||
api_key=os.getenv('OPENROUTER_API_KEY'),
|
||||
)
|
||||
agent = Agent(
|
||||
task='Go to example.com, click on the first link, and give me the title of the page',
|
||||
llm=llm,
|
||||
)
|
||||
|
||||
|
||||
async def main():
|
||||
await agent.run(max_steps=10)
|
||||
input('Press Enter to continue...')
|
||||
|
||||
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,27 @@
|
||||
import os
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from browser_use import Agent, ChatOpenAI
|
||||
|
||||
load_dotenv()
|
||||
import asyncio
|
||||
|
||||
# get an api key from https://modelstudio.console.alibabacloud.com/?tab=playground#/api-key
|
||||
api_key = os.getenv('ALIBABA_CLOUD')
|
||||
base_url = 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1'
|
||||
|
||||
# so far we only had success with qwen-vl-max
|
||||
# other models, even qwen-max, do not return the right output format. They confuse the action schema.
|
||||
# E.g. they return actions: [{"go_to_url": "google.com"}] instead of [{"go_to_url": {"url": "google.com"}}]
|
||||
# If you want to use smaller models and you see they mix up the action schema, add concrete examples to your prompt of the right format.
|
||||
llm = ChatOpenAI(model='qwen-vl-max', api_key=api_key, base_url=base_url)
|
||||
|
||||
|
||||
async def main():
|
||||
agent = Agent(task='go find the founders of browser-use', llm=llm, use_vision=True, max_actions_per_step=1)
|
||||
await agent.run()
|
||||
|
||||
|
||||
if '__main__' == __name__:
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user