feat: new implementation of simple AI agent that can follow a goal and return

This commit is contained in:
2025-11-04 18:44:06 +01:00
parent 8072e9567e
commit 7249a812f5
3 changed files with 54 additions and 43 deletions

View File

@@ -1,26 +1,44 @@
from base import Agent
from os import environ
from base import Agent as BaseAgent
from browser_use import Browser, Agent, ChatOpenAI
from enum import Enum
class AgentTypes(str, Enum):
GENERIC_BROWSER_USE_AGENT = "generic_browser_use_agent"
class GenericComputerUseAgent(Agent):
# TODO: implement code using computer-use library
"""
async def do_job(jtbd):
browser = Browser(headless=True)
llm = ChatOpenAI(model="gpt-5-mini")
task = "You are an agent for doing anything on http://localhost:3000 (wait for the website to load) for the user. The user wants you to do the following job: "+jtbd + ". Use the browser to do it. Be careful to follow all instructions and complete the job fully. You have the power to purchase or to do anything."
agent = Agent(task=task, llm=llm, browser=browser)
return await agent.run()
def _build_prompt(goal : str, environment_url : str) -> str:
#TODO: Improve prompt engineering here and experiment with
return f"""You are an autonomous agent tasked with achieving the following goal: {goal}
You have access to a web browser to interact with the environment at {environment_url}.
Use the browser to navigate, gather information, and perform actions necessary to accomplish your goal.
Be thorough and ensure you complete the task fully."""
class GenericBrowserUseAgent(BaseAgent):
def __init__(self,
goal: str,
url: str = "http://localhost:3000",
timeout: int = 300,
llm_model: str = "gpt-5-mini",
headless: bool = True):
super().__init__(goal, url, timeout)
self.llm_model = ChatOpenAI(model=llm_model)
self.browser = Browser(headless=headless)
self.agent = Agent(task=_build_prompt(goal, url),
llm=self.llm_model,
browser=self.browser)
async def act(self) -> str:
self.result = await self.agent.run()
return self.result.final_result()
def get_agent(agent_type: AgentTypes, **kwargs) -> Agent:
if agent_type == AgentTypes.GENERIC_BROWSER_USE_AGENT:
return GenericBrowserUseAgent(**kwargs)
else:
raise ValueError(f"Unknown agent type: {agent_type}")
if __name__ == "__main__":
JTBD= "Book the best room"
R=asyncio.run(do_job(JTBD))
print(R.final_result())
"""
pass
def get_agent():
# construct
pass
import asyncio
JTBD= "Name of the company of this website"
agent = get_agent(AgentTypes.GENERIC_BROWSER_USE_AGENT, goal=JTBD, url="https://ie.edu", timeout=300)
R=asyncio.run(agent.act())
print(R)