File size: 15,973 Bytes
9f3bc09 |
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 |
import asyncio
import json
import os
import argparse
from typing import Dict, List, Optional
from tqdm import tqdm
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.messages import TextMessage
from autogen_agentchat.ui import Console
from autogen_core import CancellationToken
from autogen_ext.models.openai import AzureOpenAIChatCompletionClient
from autogen_core.models import ModelFamily
from prompts import thought_prompt, sys_prompt, identity_prompt
from tools import *
from utils import extract_json, identity_mapping_dict, setup_logging_and_config, process_qa, rag_url_dict
from datetime import datetime
# max steps for the agent to generate the answer
MAX_STEPS = 20
class AgentResponse:
"""Class to represent agent response structure."""
def __init__(self, thoughts: str, response: str):
self.thoughts = thoughts
self.response = response
def parse_arguments() -> argparse.Namespace:
"""Parse command line arguments."""
parser = argparse.ArgumentParser(
description="Run EgoLife QA agent with chain-of-thought reasoning"
)
# Model configuration
parser.add_argument(
"--model",
type=str,
default="gpt-4.1",
help="Model to use for the agent"
)
parser.add_argument(
"--api_version",
type=str,
default="2024-09-01-preview",
help="API version for the model"
)
# Data configuration
parser.add_argument(
"--data_path",
type=str,
default="./egor1-bench/QA-egolife/",
help="Path to the data directory"
)
parser.add_argument(
"--identity",
type=str,
default="A1",
help="Identity to use for the agent"
)
# Output configuration
parser.add_argument(
"--result_dir",
type=str,
default="./results",
help="Directory to save results"
)
parser.add_argument(
"--log_dir",
type=str,
default="./logs",
help="Directory to save logs"
)
parser.add_argument(
"--cache_dir",
type=str,
default="./cache",
help="Directory for caching"
)
# Processing options
parser.add_argument(
"--explicit_answer",
action="store_true",
help="Use explicit answer termination"
)
parser.add_argument(
"--observation_type",
type=str,
default="all_actions",
choices=["single", "all", "all_actions", "null"],
help="Type of observation to include in prompts"
)
# Resume and specific data options
parser.add_argument(
"--resume",
action="store_true",
help="Resume processing from error files"
)
parser.add_argument(
"--gen_specific_data",
action="store_true",
help="Process only specific data IDs"
)
parser.add_argument(
"--specific_data_path",
type=str,
default="./data_statistics/error_list_results_aobs_gpt-41_A1.txt",
help="Path to specific data list file (.txt or .json)"
)
return parser.parse_args()
def load_json_data(file_path: str) -> List[Dict]:
"""Load JSON data from file with error handling."""
try:
with open(file_path, "r", encoding="utf-8") as f:
return json.load(f)
except (json.JSONDecodeError, FileNotFoundError, UnicodeDecodeError) as e:
raise ValueError(f"Failed to load {file_path}: {e}")
def load_specific_data_ids(file_path: str) -> List[int]:
"""Load specific data IDs from a text or JSON file."""
if file_path.endswith(".txt"):
with open(file_path, "r", encoding="utf-8") as f:
data_str = f.readlines()[0].strip()
# Handle both comma-separated and bracket-enclosed formats
data_str = data_str.strip("[]")
return [int(x.strip()) for x in data_str.split(",") if x.strip()]
elif file_path.endswith(".json"):
error_data = load_json_data(file_path)
return [int(item["ID"]) for item in error_data]
else:
raise ValueError(f"Unsupported file format: {file_path}")
def load_resume_data_ids(identity: str) -> List[int]:
"""Load data IDs that need to be resumed from error files."""
error_ids = []
# Load NA errors
na_error_path = f"data_gen/errors/error_list_na_{identity}.json"
if os.path.exists(na_error_path):
try:
na_errors = load_json_data(na_error_path)
error_ids.extend([int(d["ID"]) for d in na_errors])
except Exception as e:
print(f"Warning: Could not load NA errors from {na_error_path}: {e}")
# Load no-answer errors (incomplete processing)
no_answer_path = f"data_gen/errors/error_list_no_answer_{identity}.json"
if os.path.exists(no_answer_path):
try:
no_answer_errors = load_json_data(no_answer_path)
error_ids.extend([
int(error_d["ID"])
for error_d in no_answer_errors
if len(error_d.get("cot", [])) < MAX_STEPS
])
except Exception as e:
print(f"Warning: Could not load no-answer errors from {no_answer_path}: {e}")
return list(set(error_ids)) # Remove duplicates
def filter_data_by_ids(data: List[Dict], target_ids: List[int]) -> List[Dict]:
"""Filter data to only include items with IDs in target_ids."""
return [item for item in data if item["ID"] in target_ids]
def setup_model_client(args: argparse.Namespace) -> AzureOpenAIChatCompletionClient:
"""Set up the model client based on arguments."""
endpoint, deployment, subscription_key = setup_logging_and_config(args.model)
if args.model == "gpt-4.1":
return AzureOpenAIChatCompletionClient(
azure_deployment=deployment,
azure_endpoint=endpoint,
model="gpt-41",
api_version="2025-01-01-preview",
api_key=subscription_key,
model_info={
"family": ModelFamily.GPT_41,
"function_calling": True,
"json_output": True,
"structured_output": True,
"vision": False,
}
)
else:
return AzureOpenAIChatCompletionClient(
azure_deployment=deployment,
azure_endpoint=endpoint,
model=args.model,
api_version=args.api_version,
api_key=subscription_key,
)
def get_tools(args: argparse.Namespace) -> List:
"""Get the appropriate tools based on arguments."""
if args.explicit_answer:
return [rag, video_llm, vlm, terminate_explicit]
else:
return [rag, video_llm, vlm, terminate]
def get_system_prompt(args: argparse.Namespace) -> str:
"""Get the appropriate system prompt based on version."""
return sys_prompt + f"\n{identity_prompt.format(identity=f'{args.identity}_{identity_mapping_dict[args.identity]}')}"
def create_resume_prompt(dp: Dict, base_prompt: List[TextMessage]) -> List[TextMessage]:
"""Create a prompt for resuming from previous CoT steps."""
prompt = base_prompt.copy()
if dp.get("cot"):
prompt.append(TextMessage(
source="assistant",
content=f'Previous observations: {dp["cot"]}'
))
prompt.append(TextMessage(
source="user",
content=(
"Now you are given the previous actions and observations you have made before, "
"continue to try your best to answer the question using different tools. "
f"You must provide an answer to the question before step {MAX_STEPS}."
)
))
return prompt
def update_prompt_with_observation(
prompt: List[TextMessage],
observation: Dict,
observation_type: str
) -> List[TextMessage]:
"""Update prompt with new observation based on observation type."""
if observation_type == "single":
# Replace with single latest observation
return [
prompt[0],
TextMessage(source="assistant", content=f'Previous observations: {observation}')
]
elif observation_type == "all":
# Append all previous observations
prompt.append(TextMessage(
source="assistant",
content=f'Previous observations (step-{observation["step"]}): {observation}'
))
elif observation_type == "all_actions":
# Append all previous actions
prompt.append(TextMessage(
source="assistant",
content=f'Previous actions (step-{observation["step"]}): {observation["tool"]}'
))
elif observation_type == "null":
# Don't append any observation
pass
return prompt
async def process_single_qa(
qa: Dict,
agent: AssistantAgent,
args: argparse.Namespace,
result_dir: str
) -> Optional[Dict]:
"""Process a single QA item with the agent."""
dp = process_qa(qa, args.explicit_answer)
dp_path = os.path.join(result_dir, f"{dp['ID']}.json")
# Handle existing files
if os.path.exists(dp_path) and not args.resume:
print(f"Overwriting {dp['ID']}")
if args.resume and os.path.exists(dp_path):
print(f"Resuming {dp['ID']}")
with open(dp_path, "r", encoding="utf-8") as f:
dp = json.load(f)
# Create initial prompt
base_prompt = [TextMessage(
content=dp["question"] + "\n\n" + thought_prompt,
source="user"
)]
# Set up for resume if needed
if args.resume and dp.get("cot") and len(dp["cot"]) > 0:
step = len(dp["cot"]) - 1
print(f"Resuming from {dp['ID']} at step {step}")
dp["cot"] = dp["cot"][:-1] # Remove last incomplete step
prompt = create_resume_prompt(dp, base_prompt)
else:
step = 0
prompt = base_prompt
# Main processing loop
while step < MAX_STEPS:
print(f"Step: {step}")
try:
result = await Console(agent.run_stream(
task=prompt,
cancellation_token=CancellationToken()
))
except Exception as e:
print(f"Error at step {step}: {e}")
return {
"id": dp["ID"],
"prompt": [msg.content for msg in prompt],
"error": str(e),
"step": step
}
step += 1
messages = result.messages
# Extract information from messages
thought = None
tool_call = None
tool_summary = None
for message in messages:
if message.type == "ThoughtEvent":
thought = message.content
elif message.type == "ToolCallRequestEvent":
tool_call = message.content[0]
elif message.type == "ToolCallSummaryMessage":
tool_summary = message.content
# Handle termination
if tool_call and "terminate" in tool_call.name.lower():
observation = {
"step": step,
"thought": thought,
"answer": extract_json(tool_call.arguments)["answer"]
}
dp['cot'].append(observation)
break
# Handle regular tool usage
else:
observation = {
"step": step,
"thought": thought,
"tool": {
"id": tool_call.id if tool_call else None,
"name": tool_call.name if tool_call else None,
"arguments": tool_call.arguments if tool_call else None
},
"observation": tool_summary
}
dp['cot'].append(observation)
prompt = update_prompt_with_observation(prompt, observation, args.observation_type)
# Save results
with open(dp_path, "w", encoding="utf-8") as f:
json.dump(dp, f, indent=4)
return None # No error
async def main() -> None:
"""Main function to orchestrate the QA processing."""
args = parse_arguments()
# Set up environment
os.environ["LOG_DIR"] = args.log_dir
if os.environ.get("IDENTITY") is None:
os.environ["IDENTITY"] = args.identity
if os.environ.get("RAG_URL") is None:
os.environ["RAG_URL"] = rag_url_dict[args.identity]
assert os.environ["RAG_URL"] is not None, "RAG_URL is not set"
cache_dir = os.environ.get("CACHE_DIR", args.cache_dir)
os.makedirs(args.result_dir, exist_ok=True)
os.makedirs(cache_dir, exist_ok=True)
os.makedirs(args.log_dir, exist_ok=True)
# Load data based on mode
data_path = os.path.join(
args.data_path,
f"EgoLifeQA_{args.identity}_{identity_mapping_dict[args.identity]}.json"
)
print(f"Current identity: {args.identity}")
try:
if args.gen_specific_data:
print("Loading specific data...")
# Use specific_data_path or fall back to specific_data_list for compatibility
specific_path = args.specific_data_path
if not os.path.exists(specific_path) and hasattr(args, 'specific_data_list'):
specific_path = args.specific_data_list
if os.path.exists(specific_path):
target_ids = load_specific_data_ids(specific_path)
else:
# Fallback to default path format
default_path = f"./data_statistics/error_list_{args.identity}.txt"
target_ids = load_specific_data_ids(default_path)
all_data = load_json_data(data_path)
egolife_qa_data = filter_data_by_ids(all_data, target_ids)
print(f"Loaded {len(egolife_qa_data)} specific items")
elif args.resume:
print("Loading resume data...")
target_ids = load_resume_data_ids(args.identity)
all_data = load_json_data(data_path)
egolife_qa_data = filter_data_by_ids(all_data, target_ids)
print(f"Loaded {len(egolife_qa_data)} items for resume")
else:
print(f"Loading all data from: {data_path}")
egolife_qa_data = load_json_data(data_path)
print(f"Loaded {len(egolife_qa_data)} items")
except Exception as e:
print(f"Error loading data: {e}")
return
if not egolife_qa_data:
print("No data to process.")
return
# Set up model and agent components
model_client = setup_model_client(args)
tools = get_tools(args)
sys_prompt_text = get_system_prompt(args)
# Process data
errors = []
for qa in tqdm(egolife_qa_data, desc="Processing QA items"):
# Create fresh agent for each QA to avoid state issues
agent = AssistantAgent(
name="egolife_qa_agent",
model_client=model_client,
tools=tools,
system_message=sys_prompt_text,
)
error = await process_single_qa(qa, agent, args, args.result_dir)
if error:
errors.append(error)
# Save error list if any
if errors:
error_file = os.path.join(args.log_dir, f"error_list_{args.identity}.json")
with open(error_file, "w", encoding="utf-8") as f:
json.dump(errors, f, indent=4)
print(f"Saved {len(errors)} errors to {error_file}")
else:
print("No errors encountered!")
# Cleanup
print("Processing complete!")
if cache_dir and os.path.exists(cache_dir):
try:
os.rmdir(cache_dir)
except OSError:
pass # Directory not empty or doesn't exist
await model_client.close()
if __name__ == "__main__":
asyncio.run(main()) |