mirror of
https://github.com/browser-use/browser-use
synced 2026-05-06 17:52:15 +02:00
48 lines
1.2 KiB
Python
48 lines
1.2 KiB
Python
"""
|
|
Demonstrate output validator.
|
|
|
|
@dev You need to add OPENAI_API_KEY to your environment variables.
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import asyncio
|
|
|
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
|
|
|
from dotenv import load_dotenv
|
|
load_dotenv()
|
|
|
|
from langchain_openai import ChatOpenAI
|
|
from pydantic import BaseModel
|
|
from browser_use import ActionResult, Agent, Controller
|
|
|
|
controller = Controller()
|
|
|
|
|
|
class DoneResult(BaseModel):
|
|
title: str
|
|
comments: str
|
|
hours_since_start: int
|
|
|
|
|
|
# we overwrite done() in this example to demonstrate the validator
|
|
@controller.registry.action('Done with task', param_model=DoneResult)
|
|
async def done(params: DoneResult):
|
|
result = ActionResult(is_done=True, extracted_content=params.model_dump_json())
|
|
print(result)
|
|
# NOTE: this is clearly wrong - to demonstrate the validator
|
|
return 'blablabla'
|
|
|
|
|
|
async def main():
|
|
task = 'Go to hackernews hn and give me the top 1 post'
|
|
model = ChatOpenAI(model='gpt-4o')
|
|
agent = Agent(task=task, llm=model, controller=controller, validate_output=True)
|
|
# NOTE: this should fail to demonstrate the validator
|
|
await agent.run(max_steps=5)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
asyncio.run(main())
|