mirror of
https://github.com/velocitatem/PHANTOM.git
synced 2026-05-31 08:33:36 +00:00
13 agentic behavior runner v1 (#14)
* baseline setup of agent abstract * feat: new implementation of simple AI agent that can follow a goal and return * refactored import structure and created full tests * pytest setup a github workflow to run tests + more ignores * singularity for pushing * fixing builds of PDFs * inital structure of docs * init styles and docs * basic style implementation * 13 create outline for research paper draft (#18) * updated outline for paper from issue * extra paper sections and some formalization of series data * algorithms and acknowledgements * updated outline for paper from issue * Refactor docker-compose services to use individual Dockerfiles (#20) * Initial plan * Refactor services into individual Dockerfiles Co-authored-by: velocitatem <60182044+velocitatem@users.noreply.github.com> * Add EXPOSE directives to all Dockerfiles with port documentation Co-authored-by: velocitatem <60182044+velocitatem@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: velocitatem <60182044+velocitatem@users.noreply.github.com> * 2 nextjs scaffold with store mode shop and admin session experiment wiring event emission v1 (#17) * chore: cleaning gitignore * formating and env documentation * feat: context switching of hotel/airline depndent on env var via middleware * fixed alignment and building * wrong file * prods * fixed applying style * better session cookie management * tentative session storage with maybe using airtable * migrated api of ingestion * events and products apge * fixing build * 13 create outline for research paper draft (#18) * updated outline for paper from issue * extra paper sections and some formalization of series data * algorithms and acknowledgements * updated outline for paper from issue * upadted text formating * event unification * refactor tracking to ues callbacks instead of refs * implement a pricing display api with session passing * moved middleware to proxy according to new changes in Nextjs * refactoed kafka ingestion to go via backend not web-db * Refactor docker-compose services to use individual Dockerfiles (#20) * Initial plan * Refactor services into individual Dockerfiles Co-authored-by: velocitatem <60182044+velocitatem@users.noreply.github.com> * Add EXPOSE directives to all Dockerfiles with port documentation Co-authored-by: velocitatem <60182044+velocitatem@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: velocitatem <60182044+velocitatem@users.noreply.github.com> * fixing small bugs and adding exepriments to tracking * added some doc * fixing prod * prod kafka server logging * topic auto create * pytest setup a github workflow to run tests + more ignores * getting data from agents properly * proper pipeline to handle data and build matrices * fixing backend dumping * fixing agents and ignore * fixing import for tests --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
9bb6f842f4
commit
ab8b8787a8
1
experiments/agents/__init__.py
Normal file
1
experiments/agents/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Agentic behavior runner for PHANTOM research platform."""
|
||||
44
experiments/agents/agent.py
Normal file
44
experiments/agents/agent.py
Normal file
@@ -0,0 +1,44 @@
|
||||
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"
|
||||
|
||||
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()
|
||||
# https://github.com/browser-use/browser-use/blob/main/browser_use/agent/views.py#L301
|
||||
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__":
|
||||
import asyncio
|
||||
JTBD= "Name all the products on this site and try to find out more about each product by clicking into them (they might not open)"
|
||||
agent = get_agent(AgentTypes.GENERIC_BROWSER_USE_AGENT, goal=JTBD, url="http://localhost:3000/products", timeout=300)
|
||||
R=asyncio.run(agent.act())
|
||||
print(R)
|
||||
19
experiments/agents/base.py
Normal file
19
experiments/agents/base.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Optional
|
||||
|
||||
class Agent(ABC):
|
||||
"""Base interface for browser automation agents"""
|
||||
|
||||
def __init__(self, goal: str, url: str = "http://localhost:3000", timeout: int = 300):
|
||||
self.goal = goal
|
||||
self.url = url
|
||||
self.timeout = timeout
|
||||
self.result: Optional[str] = None
|
||||
|
||||
@abstractmethod
|
||||
async def act(self) -> str:
|
||||
"""Execute goal and return result text"""
|
||||
pass
|
||||
|
||||
def final_result(self) -> Optional[str]:
|
||||
return self.result
|
||||
30
experiments/agents/test.py
Normal file
30
experiments/agents/test.py
Normal file
@@ -0,0 +1,30 @@
|
||||
import pytest
|
||||
import asyncio
|
||||
from experiments.agents.agent import get_agent, AgentTypes
|
||||
import os
|
||||
|
||||
|
||||
def test_agent_init():
|
||||
agent = get_agent(AgentTypes.GENERIC_BROWSER_USE_AGENT, goal="test", url="http://example.com", timeout=100)
|
||||
assert agent.goal == "test"
|
||||
assert agent.url == "http://example.com"
|
||||
assert agent.timeout == 100
|
||||
|
||||
|
||||
def test_invalid_agent():
|
||||
with pytest.raises(ValueError):
|
||||
get_agent("invalid", goal="test")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.skipif("OPENAI_API_KEY" not in os.environ, reason="OPENAI_API_KEY not set")
|
||||
async def test_agent_execution():
|
||||
agent = get_agent(AgentTypes.GENERIC_BROWSER_USE_AGENT, goal="get page title", url="https://example.com", timeout=60)
|
||||
|
||||
result = await agent.act()
|
||||
assert result
|
||||
assert agent.final_result()
|
||||
assert agent.final_result().history[-1].result[-1].is_done == True
|
||||
assert isinstance(result, str)
|
||||
assert "example" in result.lower()
|
||||
assert len(result) > 0
|
||||
Reference in New Issue
Block a user