-
Notifications
You must be signed in to change notification settings - Fork 75
Dev branch for the ToolUseAgent #239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
TLSDC
wants to merge
30
commits into
main
Choose a base branch
from
tlsdc/tool_use_agent
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
9ee2367
moving the browsergym.experiment.benchmark module to agentlab
TLSDC c2e2b9c
added comment for new parameter
TLSDC 596fcd2
BaseMessages take into account 'input_text' key too (for xray)
TLSDC f9d7b91
convenient array to base64 function
TLSDC 73ba428
tool agent embryo
TLSDC c11db49
Merge branch 'main' of github.com:ServiceNow/AgentLab into tlsdc/tool…
TLSDC 6604dbc
added the MessageBuilder class, which should help interfacing APIs
TLSDC ef6f648
claude
TLSDC 4e973ac
adding markdown display for MessageBuilder in xray
TLSDC 54ec412
changed LLM structure to be more versatile
TLSDC 0fc43cc
unified claude and openai response apis
TLSDC 19cdaf9
i dont think this is relevant anymore
TLSDC 5b3f469
backtracking from moving bgym.benchmarks etc
TLSDC 087ad75
defaulting to claude bc it's better
TLSDC 8a17470
kind of forced to comment this to avoid circular imports atm
TLSDC 5f675ba
Merge branch 'main' of github.com:ServiceNow/AgentLab into tlsdc/tool…
TLSDC 234be09
parametrized env output to agent_args
TLSDC 544908e
fixing broken import in test
TLSDC 16cc3cd
Add pricing tracking for Anthropic model and refactor pricing functions
recursix c674094
Enhance ToolUseAgent with token counting and improved message handlin…
recursix 528b513
Update action in ClaudeResponseModel to None for improved clarity
recursix 417893c
typo
recursix c676eab
typo
recursix ab2d331
Remove unnecessary import of anthropic for cleaner code
recursix bf57591
moving some utils to agent_utils.py
amanjaiswal73892 ce72b41
Fix: Formatting ang Darglint
amanjaiswal73892 fe05d75
Refactor: Simplify message builder methods and add support for chat c…
amanjaiswal73892 97a39cc
added vllm-support-for-tool-use-agent
amanjaiswal73892 7d8a08c
Moving some functions to llm utils.py
amanjaiswal73892 ffd5c5e
Merge pull request #248 from ServiceNow/aj/tool_use_agent_chat_comple…
amanjaiswal73892 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
import json | ||
import logging | ||
from copy import deepcopy as copy | ||
from dataclasses import asdict, dataclass | ||
from typing import TYPE_CHECKING, Any | ||
|
||
import bgym | ||
from browsergym.core.observation import extract_screenshot | ||
|
||
from agentlab.agents.agent_args import AgentArgs | ||
from agentlab.llm.llm_utils import image_to_png_base64_url | ||
from agentlab.llm.response_api import OpenAIResponseModelArgs | ||
from agentlab.llm.tracking import cost_tracker_decorator | ||
|
||
if TYPE_CHECKING: | ||
from openai.types.responses import Response | ||
|
||
|
||
@dataclass | ||
class ToolUseAgentArgs(AgentArgs): | ||
temperature: float = 0.1 | ||
model_args: OpenAIResponseModelArgs = None | ||
|
||
def __post_init__(self): | ||
try: | ||
self.agent_name = f"ToolUse-{self.model_args.model_name}".replace("/", "_") | ||
except AttributeError: | ||
pass | ||
|
||
def make_agent(self) -> bgym.Agent: | ||
return ToolUseAgent( | ||
temperature=self.temperature, | ||
model_args=self.model_args, | ||
) | ||
|
||
def set_reproducibility_mode(self): | ||
self.temperature = 0 | ||
|
||
def prepare(self): | ||
return self.model_args.prepare_server() | ||
|
||
def close(self): | ||
return self.model_args.close_server() | ||
|
||
|
||
class ToolUseAgent(bgym.Agent): | ||
def __init__( | ||
self, | ||
temperature: float, | ||
model_args: OpenAIResponseModelArgs, | ||
): | ||
self.temperature = temperature | ||
self.chat = model_args.make_model() | ||
self.model_args = model_args | ||
|
||
self.action_set = bgym.HighLevelActionSet(["coord"], multiaction=False) | ||
|
||
self.tools = self.action_set.to_tool_description() | ||
|
||
# self.tools.append( | ||
# { | ||
# "type": "function", | ||
# "name": "chain_of_thought", | ||
# "description": "A tool that allows the agent to think step by step. Every other action must ALWAYS be preceeded by a call to this tool.", | ||
# "parameters": { | ||
# "type": "object", | ||
# "properties": { | ||
# "thoughts": { | ||
# "type": "string", | ||
# "description": "The agent's reasoning process.", | ||
# }, | ||
# }, | ||
# "required": ["thoughts"], | ||
# }, | ||
# } | ||
# ) | ||
|
||
self.llm = model_args.make_model(extra_kwargs={"tools": self.tools}) | ||
|
||
self.messages = [] | ||
|
||
def obs_preprocessor(self, obs): | ||
page = obs.pop("page", None) | ||
if page is not None: | ||
obs["screenshot"] = extract_screenshot(page) | ||
else: | ||
raise ValueError("No page found in the observation.") | ||
|
||
return obs | ||
|
||
@cost_tracker_decorator | ||
def get_action(self, obs: Any) -> tuple[str, dict]: | ||
|
||
if len(self.messages) == 0: | ||
system_message = { | ||
"role": "system", | ||
"content": "You are an agent. Based on the observation, you will decide which action to take to accomplish your goal.", | ||
} | ||
goal_object = [el for el in obs["goal_object"]] | ||
for content in goal_object: | ||
if content["type"] == "text": | ||
content["type"] = "input_text" | ||
elif content["type"] == "image_url": | ||
content["type"] = "input_image" | ||
goal_message = {"role": "user", "content": goal_object} | ||
goal_message["content"].append( | ||
{ | ||
"type": "input_image", | ||
"image_url": image_to_png_base64_url(obs["screenshot"]), | ||
} | ||
) | ||
self.messages.append(system_message) | ||
self.messages.append(goal_message) | ||
else: | ||
if obs["last_action_error"] == "": | ||
self.messages.append( | ||
{ | ||
"type": "function_call_output", | ||
"call_id": self.previous_call_id, | ||
"output": "Function call executed, see next observation.", | ||
} | ||
) | ||
self.messages.append( | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{ | ||
"type": "input_image", | ||
"image_url": image_to_png_base64_url(obs["screenshot"]), | ||
} | ||
], | ||
} | ||
) | ||
else: | ||
self.messages.append( | ||
{ | ||
"type": "function_call_output", | ||
"call_id": self.previous_call_id, | ||
"output": f"Function call failed: {obs['last_action_error']}", | ||
} | ||
) | ||
|
||
response: "Response" = self.llm( | ||
messages=self.messages, | ||
temperature=self.temperature, | ||
) | ||
|
||
action = "noop()" | ||
think = "" | ||
for output in response.output: | ||
if output.type == "function_call": | ||
arguments = json.loads(output.arguments) | ||
action = f"{output.name}({", ".join([f"{k}={v}" for k, v in arguments.items()])})" | ||
self.previous_call_id = output.call_id | ||
self.messages.append(output) | ||
break | ||
elif output.type == "reasoning": | ||
if len(output.summary) > 0: | ||
think += output.summary[0].text + "\n" | ||
self.messages.append(output) | ||
|
||
return ( | ||
action, | ||
bgym.AgentInfo( | ||
think=think, | ||
chat_messages=[], | ||
stats={}, | ||
), | ||
) | ||
|
||
|
||
MODEL_CONFIG = OpenAIResponseModelArgs( | ||
model_name="o4-mini-2025-04-16", | ||
max_total_tokens=200_000, | ||
max_input_tokens=200_000, | ||
max_new_tokens=100_000, | ||
temperature=0.1, | ||
vision_support=True, | ||
) | ||
|
||
AGENT_CONFIG = ToolUseAgentArgs( | ||
temperature=0.1, | ||
model_args=MODEL_CONFIG, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .base import Benchmark, HighLevelActionSetArgs | ||
from .configs import DEFAULT_BENCHMARKS |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.