use / examples /features /validate_output.py
Boobs00's picture
Upload folder using huggingface_hub
db4810d verified
"""
Demostrate output validator.
@dev You need to add OPENAI_API_KEY to your environment variables.
"""
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import asyncio
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from pydantic import BaseModel
from browser_use import ActionResult, Agent, Controller
load_dotenv()
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())