diff --git a/.gitignore b/.gitignore index 840f8923..06be6891 100644 --- a/.gitignore +++ b/.gitignore @@ -25,4 +25,5 @@ step_experiences/* build/* *.egg-info/* cookbook/simple_demo/test_appworld/data/* -cookbook/simple_demo/test_appworld/experiments/* \ No newline at end of file +cookbook/simple_demo/test_appworld/experiments/* +cookbook/simple_demo/test_appworld/exp_result/* \ No newline at end of file diff --git a/cookbook/simple_demo/test_appworld/appworld_react_agent.py b/cookbook/simple_demo/test_appworld/appworld_react_agent.py index 4a17f6ab..ecf15b15 100644 --- a/cookbook/simple_demo/test_appworld/appworld_react_agent.py +++ b/cookbook/simple_demo/test_appworld/appworld_react_agent.py @@ -1,4 +1,3 @@ -import json import os os.environ["APPWORLD_ROOT"] = "." @@ -8,6 +7,7 @@ load_dotenv("../../../.env") import re import time +import json from appworld import AppWorld, load_task_ids from jinja2 import Template @@ -16,18 +16,21 @@ from openai import OpenAI from prompt import PROMPT_TEMPLATE -@ray.remote + +# @ray.remote class AppworldReactAgent: """A minimal ReAct Agent for AppWorld tasks.""" def __init__(self, + index: int, task_id: str, experiment_name: str, model_name: str = "qwen3-8b", temperature: float = 0.9, - max_interactions: int = 50, + max_interactions: int = 30, max_response_size: int = 2000): + self.index: int = index self.task_id: str = task_id self.experiment_name: str = experiment_name self.model_name: str = model_name @@ -47,7 +50,6 @@ class AppworldReactAgent: model=self.model_name, messages=messages, temperature=self.temperature, - max_tokens=400, extra_body={"enable_thinking": False}, seed=0) @@ -109,9 +111,9 @@ class AppworldReactAgent: output = self.next_step(code) self.history.append({"role": "user", "content": output}) - logger.info(f"task_id={self.task_id} iteration={i} " - f"code=\n{code}\n output=\n{output}\n " - f"score={self.get_reward():.4f}") + logger.info(f"index={self.index} task_id={self.task_id} iteration={i} ") + # f"code=\n{code}\n output=\n{output}\n " + # f"score={self.get_reward():.4f}") if self.world.task_completed(): break @@ -127,7 +129,8 @@ class AppworldReactAgent: "uplift_score": uplift_score, "task_history": self.history, } - + # logger.info(f"result={json.dumps(result)}") + # p_bar.close() return result except Exception as e: @@ -138,7 +141,7 @@ class AppworldReactAgent: def main(): dataset_name = "train" task_ids = load_task_ids(dataset_name) - agent = AppworldReactAgent(task_id=task_ids[0], experiment_name=f"jinli_{dataset_name}") + agent = AppworldReactAgent(index=0, task_id=task_ids[0], experiment_name=f"jinli_{dataset_name}") result = agent.execute() logger.info(f"result={json.dumps(result)}") diff --git a/cookbook/simple_demo/test_appworld/run_appworld.py b/cookbook/simple_demo/test_appworld/run_appworld.py index 17a33bab..5d32b6ea 100644 --- a/cookbook/simple_demo/test_appworld/run_appworld.py +++ b/cookbook/simple_demo/test_appworld/run_appworld.py @@ -1,13 +1,15 @@ import os +import ray +from ray import logger +from tqdm import tqdm + os.environ["APPWORLD_ROOT"] = "." from dotenv import load_dotenv load_dotenv("../../../.env") import json -import time -from concurrent.futures import ProcessPoolExecutor from pathlib import Path from appworld import load_task_ids @@ -15,29 +17,40 @@ from appworld import load_task_ids from appworld_react_agent import AppworldReactAgent -def run_agent(dataset_name: str, max_workers: int, experiment_suffix: str): +def run_agent(dataset_name: str, experiment_suffix: str, multi_thread: bool = False): experiment_name = dataset_name + "_" + experiment_suffix path: Path = Path(f"./exp_result") path.mkdir(parents=True, exist_ok=True) task_ids = load_task_ids(dataset_name) result: list = [] - with ProcessPoolExecutor(max_workers=max_workers) as executor: - task_list: list = [] + + def dump_file(): + with open(path / f"{experiment_name}.jsonl", "w") as f: + for x in result: + f.write(json.dumps(x) + "\n") + + if multi_thread: + future_list: list = [] for index, task_id in enumerate(task_ids): - agent = AppworldReactAgent(task_id, experiment_name) - task = executor.submit(agent.execute) - task_list.append(task) - time.sleep(1) + actor = AppworldReactAgent.remote(index=index, task_id=task_id, experiment_name=experiment_name) + future = actor.execute.remote() + future_list.append(future) + logger.info("submit complete") - for task in task_list: - result.append(task.result(timeout=600)) + for future in future_list: + result.append(ray.get(future)) + dump_file() + + else: + for index, task_id in enumerate(task_ids): + agent = AppworldReactAgent(index=index, task_id=task_id, experiment_name=experiment_name) + result.append(agent.execute()) + dump_file() - with open(path / f"{experiment_name}.jsonl", "w") as f: - for result_item in result: - f.write(json.dumps(result_item) + "\n") if __name__ == "__main__": - run_agent(dataset_name="train", experiment_suffix="v1", max_workers=1) - # run_agent(dataset_name="dev", experiment_suffix="v1", max_workers=1) + # ray.init(num_cpus=8) + run_agent(dataset_name="train", experiment_suffix="v2") + # run_agent(dataset_name="dev", experiment_suffix="v2")