File size: 1,973 Bytes
db4810d |
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import asyncio
import threading
import time
from langchain_openai import ChatOpenAI
from browser_use import Agent
class AgentController:
def __init__(self):
llm = ChatOpenAI(model='gpt-4o')
self.agent = Agent(
task='open in one action https://www.google.com, https://www.wikipedia.org, https://www.youtube.com, https://www.github.com, https://amazon.com',
llm=llm,
)
self.running = False
async def run_agent(self):
"""Run the agent"""
self.running = True
await self.agent.run()
def start(self):
"""Start the agent in a separate thread"""
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(self.run_agent())
def pause(self):
"""Pause the agent"""
self.agent.pause()
def resume(self):
"""Resume the agent"""
self.agent.resume()
def stop(self):
"""Stop the agent"""
self.agent.stop()
self.running = False
def print_menu():
print('\nAgent Control Menu:')
print('1. Start')
print('2. Pause')
print('3. Resume')
print('4. Stop')
print('5. Exit')
def main():
controller = AgentController()
agent_thread = None
while True:
print_menu()
choice = input('Enter your choice (1-5): ')
if choice == '1' and not agent_thread:
print('Starting agent...')
agent_thread = threading.Thread(target=controller.start)
agent_thread.start()
elif choice == '2':
print('Pausing agent...')
controller.pause()
elif choice == '3':
print('Resuming agent...')
controller.resume()
elif choice == '4':
print('Stopping agent...')
controller.stop()
if agent_thread:
agent_thread.join()
agent_thread = None
elif choice == '5':
print('Exiting...')
if controller.running:
controller.stop()
if agent_thread:
agent_thread.join()
break
time.sleep(0.1) # Small delay to prevent CPU spinning
if __name__ == '__main__':
main()
|