forked from browser-use/browser-use
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_output.py
56 lines (38 loc) · 1.29 KB
/
custom_output.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
Show how to use custom outputs.
@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):
post_title: str
post_url: str
num_comments: int
hours_since_post: int
@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())
return result
async def main():
task = 'Go to hackernews show hn and give me the number 1 post in the list'
model = ChatOpenAI(model='gpt-4o')
agent = Agent(task=task, llm=model, controller=controller)
history = await agent.run()
result = history.final_result()
if result:
parsed = DoneResult.model_validate_json(result)
print('--------------------------------')
print(f'Title: {parsed.post_title}')
print(f'URL: {parsed.post_url}')
print(f'Comments: {parsed.num_comments}')
print(f'Hours since post: {parsed.hours_since_post}')
if __name__ == '__main__':
asyncio.run(main())