[Add] browser-use and main.py
This commit is contained in:
parent
08e64bdf45
commit
96914d44ac
221 changed files with 30952 additions and 1 deletions
49
browser-use/examples/features/validate_output.py
Normal file
49
browser-use/examples/features/validate_output.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
"""
|
||||
Demonstrate output validator.
|
||||
|
||||
@dev You need to add OPENAI_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 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())
|
||||
Loading…
Add table
Add a link
Reference in a new issue