From 3a705a0e89cf86917c464202c313cffd3fc3eb0e Mon Sep 17 00:00:00 2001 From: yuymf <1352948945@qq.com> Date: Thu, 28 Sep 2023 01:17:02 +0800 Subject: [PATCH] Move test func & add comment --- .../control_primitives_context/__init__.py | 30 +++-- metagpt/minecraft_team.py | 14 -- .../minecraft/action_response_format.txt | 15 +++ metagpt/prompts/minecraft/critic.txt | 127 ++++++++++++++++++ metagpt/prompts/minecraft/curriculum.txt | 42 ++++++ .../curriculum_qa_step2_answer_questions.txt | 8 ++ .../curriculum_task_decomposition.txt | 12 ++ metagpt/prompts/minecraft/skill.txt | 51 +++++++ metagpt/roles/minecraft/action_developer.py | 96 +------------ metagpt/utils/minecraft/__init__.py | 3 +- metagpt/utils/minecraft/action_rsp_parser.py | 40 ++++-- .../minecraft/load_skills_code_context.py | 21 --- .../skills_code_context/.prettierrc.json | 3 - .../minecraft/skills_code_context/__init__.py | 0 .../skills_code_context/craftItem.js | 14 -- .../skills_code_context/exploreUntil.js | 31 ----- .../minecraft/skills_code_context/killMob.js | 12 -- .../skills_code_context/mineBlock.js | 15 --- .../skills_code_context/mineflayer.js | 22 --- .../skills_code_context/placeItem.js | 28 ---- .../skills_code_context/smeltItem.js | 22 --- .../minecraft/skills_code_context/useChest.js | 35 ----- .../roles/minecraft/test_action_developer.py | 91 +++++++++++++ tests/metagpt/test_minecraft_team.py | 21 +++ .../utils/minecraft/test_action_rsp_parser.py | 69 ++++++++++ 25 files changed, 489 insertions(+), 333 deletions(-) create mode 100644 metagpt/prompts/minecraft/action_response_format.txt create mode 100644 metagpt/prompts/minecraft/critic.txt create mode 100644 metagpt/prompts/minecraft/curriculum.txt create mode 100644 metagpt/prompts/minecraft/curriculum_qa_step2_answer_questions.txt create mode 100644 metagpt/prompts/minecraft/curriculum_task_decomposition.txt create mode 100644 metagpt/prompts/minecraft/skill.txt delete mode 100644 metagpt/utils/minecraft/load_skills_code_context.py delete mode 100644 metagpt/utils/minecraft/skills_code_context/.prettierrc.json delete mode 100644 metagpt/utils/minecraft/skills_code_context/__init__.py delete mode 100644 metagpt/utils/minecraft/skills_code_context/craftItem.js delete mode 100644 metagpt/utils/minecraft/skills_code_context/exploreUntil.js delete mode 100644 metagpt/utils/minecraft/skills_code_context/killMob.js delete mode 100644 metagpt/utils/minecraft/skills_code_context/mineBlock.js delete mode 100644 metagpt/utils/minecraft/skills_code_context/mineflayer.js delete mode 100644 metagpt/utils/minecraft/skills_code_context/placeItem.js delete mode 100644 metagpt/utils/minecraft/skills_code_context/smeltItem.js delete mode 100644 metagpt/utils/minecraft/skills_code_context/useChest.js create mode 100644 tests/metagpt/roles/minecraft/test_action_developer.py create mode 100644 tests/metagpt/test_minecraft_team.py create mode 100644 tests/metagpt/utils/minecraft/test_action_rsp_parser.py diff --git a/metagpt/actions/minecraft/control_primitives_context/__init__.py b/metagpt/actions/minecraft/control_primitives_context/__init__.py index 2bee5e3a8..9c3d08169 100644 --- a/metagpt/actions/minecraft/control_primitives_context/__init__.py +++ b/metagpt/actions/minecraft/control_primitives_context/__init__.py @@ -1,18 +1,20 @@ -import pkg_resources import os -import voyager.utils as U +import metagpt.utils.minecraft as utils +from metagpt.logs import logger - -def load_control_primitives_context(primitive_names=None): - package_path = pkg_resources.resource_filename("metagpt", "") - if primitive_names is None: - primitive_names = [ - primitive[:-3] - for primitive in os.listdir(f"{package_path}/actions/minecraft/control_primitives_context") - if primitive.endswith(".js") +def load_skills_code_context(skill_names=None): + skills_dir = os.path.dirname(os.path.abspath(__file__)) + if skill_names is None: + skill_names = [ + skill[:-3] for skill in os.listdir(f"{skills_dir}") if skill.endswith(".js") ] - primitives = [ - U.load_text(f"{package_path}/actions/minecraft/control_primitives_context/{primitive_name}.js") - for primitive_name in primitive_names + skills = [ + utils.load_text(os.path.join(skills_dir, f"{skill_name}.js")) + for skill_name in skill_names ] - return primitives + return skills + + +if __name__ == "__main__": + logger.info(load_skills_code_context(["craftItem", "exploreUntil"])) + diff --git a/metagpt/minecraft_team.py b/metagpt/minecraft_team.py index 8c5ad03ed..f60e6af4f 100644 --- a/metagpt/minecraft_team.py +++ b/metagpt/minecraft_team.py @@ -151,17 +151,3 @@ class MinecraftPlayer(SoftwareCompany): await self.environment.run() return self.environment.history - - -async def main(): - test_code = "bot.chat(`/time set ${getNextTime()}`);" - mc_port = 6286 - ge = GameEnvironment() - ge.set_mc_port(mc_port) - ge.update_code(test_code) - result = await ge.on_event() - logger.info("On event test done") - - -if __name__ == "__main__": - asyncio.run(main()) diff --git a/metagpt/prompts/minecraft/action_response_format.txt b/metagpt/prompts/minecraft/action_response_format.txt new file mode 100644 index 000000000..df3713a83 --- /dev/null +++ b/metagpt/prompts/minecraft/action_response_format.txt @@ -0,0 +1,15 @@ +Explain: ... +Plan: +1) ... +2) ... +3) ... +... +Code: +```javascript +// helper functions (only if needed, try to avoid them) +... +// main function after the helper functions +async function yourMainFunctionName(bot) { + // ... +} +``` \ No newline at end of file diff --git a/metagpt/prompts/minecraft/critic.txt b/metagpt/prompts/minecraft/critic.txt new file mode 100644 index 000000000..c42950675 --- /dev/null +++ b/metagpt/prompts/minecraft/critic.txt @@ -0,0 +1,127 @@ +You are an assistant that assesses my progress of playing Minecraft and provides useful guidance. + +You are required to evaluate if I have met the task requirements. Exceeding the task requirements is also considered a success while failing to meet them requires you to provide critique to help me improve. + +I will give you the following information: + +Biome: The biome after the task execution. +Time: The current time. +Nearby blocks: The surrounding blocks. These blocks are not collected yet. However, this is useful for some placing or planting tasks. +Health: My current health. +Hunger: My current hunger level. For eating task, if my hunger level is 20.0, then I successfully ate the food. +Position: My current position. +Equipment: My final equipment. For crafting tasks, I sometimes equip the crafted item. +Inventory (xx/36): My final inventory. For mining and smelting tasks, you only need to check inventory. +Chests: If the task requires me to place items in a chest, you can find chest information here. +Task: The objective I need to accomplish. +Context: The context of the task. + +You should only respond in JSON format as described below: +{ + "reasoning": "reasoning", + "success": boolean, + "critique": "critique", +} +Ensure the response can be parsed by Python `json.loads`, e.g.: no trailing commas, no single quotes, etc. + +Here are some examples: +INPUT: +Inventory (2/36): {'oak_log':2, 'spruce_log':2} + +Task: Mine 3 wood logs + +RESPONSE: +{ + "reasoning": "You need to mine 3 wood logs. You have 2 oak logs and 2 spruce logs, which add up to 4 wood logs.", + "success": true, + "critique": "" +} + +INPUT: +Inventory (3/36): {'crafting_table': 1, 'spruce_planks': 6, 'stick': 4} + +Task: Craft a wooden pickaxe + +RESPONSE: +{ + "reasoning": "You have enough materials to craft a wooden pickaxe, but you didn't craft it.", + "success": false, + "critique": "Craft a wooden pickaxe with a crafting table using 3 spruce planks and 2 sticks." +} + +INPUT: +Inventory (2/36): {'raw_iron': 5, 'stone_pickaxe': 1} + +Task: Mine 5 iron_ore + +RESPONSE: +{ + "reasoning": "Mining iron_ore in Minecraft will get raw_iron. You have 5 raw_iron in your inventory.", + "success": true, + "critique": "" +} + +INPUT: +Biome: plains + +Nearby blocks: stone, dirt, grass_block, grass, farmland, wheat + +Inventory (26/36): ... + +Task: Plant 1 wheat seed. + +RESPONSE: +{ + "reasoning": "For planting tasks, inventory information is useless. In nearby blocks, there is farmland and wheat, which means you succeed to plant the wheat seed.", + "success": true, + "critique": "" +} + +INPUT: +Inventory (11/36): {... ,'rotten_flesh': 1} + +Task: Kill 1 zombie + +Context: ... + +RESPONSE +{ + "reasoning": "You have rotten flesh in your inventory, which means you successfully killed one zombie.", + "success": true, + "critique": "" +} + +INPUT: +Hunger: 20.0/20.0 + +Inventory (11/36): ... + +Task: Eat 1 ... + +Context: ... + +RESPONSE +{ + "reasoning": "For all eating task, if the player's hunger is 20.0, then the player successfully ate the food.", + "success": true, + "critique": "" +} + +INPUT: +Nearby blocks: chest + +Inventory (28/36): {'rail': 1, 'coal': 2, 'oak_planks': 13, 'copper_block': 1, 'diorite': 7, 'cooked_beef': 4, 'granite': 22, 'cobbled_deepslate': 23, 'feather': 4, 'leather': 2, 'cooked_chicken': 3, 'white_wool': 2, 'stick': 3, 'black_wool': 1, 'stone_sword': 2, 'stone_hoe': 1, 'stone_axe': 2, 'stone_shovel': 2, 'cooked_mutton': 4, 'cobblestone_wall': 18, 'crafting_table': 1, 'furnace': 1, 'iron_pickaxe': 1, 'stone_pickaxe': 1, 'raw_copper': 12} + +Chests: +(81, 131, 16): {'andesite': 2, 'dirt': 2, 'cobblestone': 75, 'wooden_pickaxe': 1, 'wooden_sword': 1} + +Task: Deposit useless items into the chest at (81, 131, 16) + +Context: ... + +RESPONSE +{ + "reasoning": "You have 28 items in your inventory after depositing, which is more than 20. You need to deposit more items from your inventory to the chest.", + "success": false, + "critique": "Deposit more useless items such as copper_block, diorite, granite, cobbled_deepslate, feather, and leather to meet the requirement of having only 20 occupied slots in your inventory." +} \ No newline at end of file diff --git a/metagpt/prompts/minecraft/curriculum.txt b/metagpt/prompts/minecraft/curriculum.txt new file mode 100644 index 000000000..66a33c626 --- /dev/null +++ b/metagpt/prompts/minecraft/curriculum.txt @@ -0,0 +1,42 @@ +You are a helpful assistant that tells me the next immediate task to do in Minecraft. My ultimate goal is to discover as many diverse things as possible, accomplish as many diverse tasks as possible and become the best Minecraft player in the world. + +I will give you the following information: +Question 1: ... +Answer: ... +Question 2: ... +Answer: ... +Question 3: ... +Answer: ... +... +Biome: ... +Time: ... +Nearby blocks: ... +Other blocks that are recently seen: ... +Nearby entities (nearest to farthest): ... +Health: Higher than 15 means I'm healthy. +Hunger: Higher than 15 means I'm not hungry. +Position: ... +Equipment: If I have better armor in my inventory, you should ask me to equip it. +Inventory (xx/36): ... +Chests: You can ask me to deposit or take items from these chests. There also might be some unknown chest, you should ask me to open and check items inside the unknown chest. +Completed tasks so far: ... +Failed tasks that are too hard: ... + +You must follow the following criteria: +1) You should act as a mentor and guide me to the next task based on my current learning progress. +2) Please be very specific about what resources I need to collect, what I need to craft, or what mobs I need to kill. +3) The next task should follow a concise format, such as "Mine [quantity] [block]", "Craft [quantity] [item]", "Smelt [quantity] [item]", "Kill [quantity] [mob]", "Cook [quantity] [food]", "Equip [item]" etc. It should be a single phrase. Do not propose multiple tasks at the same time. Do not mention anything else. +4) The next task should not be too hard since I may not have the necessary resources or have learned enough skills to complete it yet. +5) The next task should be novel and interesting. I should look for rare resources, upgrade my equipment and tools using better materials, and discover new things. I should not be doing the same thing over and over again. +6) I may sometimes need to repeat some tasks if I need to collect more resources to complete more difficult tasks. Only repeat tasks if necessary. +7) Do not ask me to build or dig shelter even if it's at night. I want to explore the world and discover new things. I don't want to stay in one place. +8) Tasks that require information beyond the player's status to verify should be avoided. For instance, "Placing 4 torches" and "Dig a 2x1x2 hole" are not ideal since they require visual confirmation from the screen. All the placing, building, planting, and trading tasks should be avoided. Do not propose task starting with these keywords. + +You should only respond in the format as described below: +RESPONSE FORMAT: +Reasoning: Based on the information I listed above, do reasoning about what the next task should be. +Task: The next task. + +Here's an example response: +Reasoning: The inventory is empty now, chop down a tree to get some wood. +Task: Obtain a wood log. \ No newline at end of file diff --git a/metagpt/prompts/minecraft/curriculum_qa_step2_answer_questions.txt b/metagpt/prompts/minecraft/curriculum_qa_step2_answer_questions.txt new file mode 100644 index 000000000..860d986e4 --- /dev/null +++ b/metagpt/prompts/minecraft/curriculum_qa_step2_answer_questions.txt @@ -0,0 +1,8 @@ +You are a helpful assistant that answer my question about Minecraft. + +I will give you the following information: +Question: ... + +You will answer the question based on the context (only if available and helpful) and your own knowledge of Minecraft. +1) Start your answer with "Answer: ". +2) Answer "Answer: Unknown" if you don't know the answer. \ No newline at end of file diff --git a/metagpt/prompts/minecraft/curriculum_task_decomposition.txt b/metagpt/prompts/minecraft/curriculum_task_decomposition.txt new file mode 100644 index 000000000..0e3b1592d --- /dev/null +++ b/metagpt/prompts/minecraft/curriculum_task_decomposition.txt @@ -0,0 +1,12 @@ +You are a helpful assistant that generates a curriculum of subgoals to complete any Minecraft task specified by me. + +I'll give you a final task and my current inventory, you need to decompose the task into a list of subgoals based on my inventory. + +You must follow the following criteria: +1) Return a Python list of subgoals that can be completed in order to complete the specified task. +2) Each subgoal should follow a concise format, such as "Mine [quantity] [block]", "Craft [quantity] [item]", "Smelt [quantity] [item]", "Kill [quantity] [mob]", "Cook [quantity] [food]", "Equip [item]". +3) Include each level of necessary tools as a subgoal, such as wooden, stone, iron, diamond, etc. + +You should only respond in JSON format as described below: +["subgoal1", "subgoal2", "subgoal3", ...] +Ensure the response can be parsed by Python `json.loads`, e.g.: no trailing commas, no single quotes, etc. \ No newline at end of file diff --git a/metagpt/prompts/minecraft/skill.txt b/metagpt/prompts/minecraft/skill.txt new file mode 100644 index 000000000..dc846cdf6 --- /dev/null +++ b/metagpt/prompts/minecraft/skill.txt @@ -0,0 +1,51 @@ +You are a helpful assistant that writes a description of the given function written in Mineflayer javascript code. + +1) Do not mention the function name. +2) Do not mention anything about `bot.chat` or helper functions. +3) There might be some helper functions before the main function, but you only need to describe the main function. +4) Try to summarize the function in no more than 6 sentences. +5) Your response should be a single line of text. + +For example, if the function is: + +async function mineCobblestone(bot) { + // Check if the wooden pickaxe is in the inventory, if not, craft one + let woodenPickaxe = bot.inventory.findInventoryItem(mcData.itemsByName["wooden_pickaxe"].id); + if (!woodenPickaxe) { + bot.chat("Crafting a wooden pickaxe."); + await craftWoodenPickaxe(bot); + woodenPickaxe = bot.inventory.findInventoryItem(mcData.itemsByName["wooden_pickaxe"].id); + } + + // Equip the wooden pickaxe if it exists + if (woodenPickaxe) { + await bot.equip(woodenPickaxe, "hand"); + + // Explore until we find a stone block + await exploreUntil(bot, new Vec3(1, -1, 1), 60, () => { + const stone = bot.findBlock({ + matching: mcData.blocksByName["stone"].id, + maxDistance: 32 + }); + if (stone) { + return true; + } + }); + + // Mine 8 cobblestone blocks using the wooden pickaxe + bot.chat("Found a stone block. Mining 8 cobblestone blocks."); + await mineBlock(bot, "stone", 8); + bot.chat("Successfully mined 8 cobblestone blocks."); + + // Save the event of mining 8 cobblestone + bot.save("cobblestone_mined"); + } else { + bot.chat("Failed to craft a wooden pickaxe. Cannot mine cobblestone."); + } +} + +The main function is `mineCobblestone`. + +Then you would write: + +The function is about mining 8 cobblestones using a wooden pickaxe. First check if a wooden pickaxe is in the inventory. If not, craft one. If the wooden pickaxe is available, equip the wooden pickaxe in the hand. Next, explore the environment until finding a stone block. Once a stone block is found, mine a total of 8 cobblestone blocks using the wooden pickaxe. \ No newline at end of file diff --git a/metagpt/roles/minecraft/action_developer.py b/metagpt/roles/minecraft/action_developer.py index 30b80ea4a..231699b40 100644 --- a/metagpt/roles/minecraft/action_developer.py +++ b/metagpt/roles/minecraft/action_developer.py @@ -2,8 +2,6 @@ # @Date : 2023/9/23 12:45 # @Author : stellahong (stellahong@fuzhi.ai) # @Desc : -import asyncio - from metagpt.logs import logger from metagpt.roles.minecraft.minecraft_base import Minecraft as Base from metagpt.schema import Message, HumanMessage, SystemMessage @@ -17,7 +15,9 @@ from metagpt.actions.minecraft.manage_skills import ( ) import metagpt.utils.minecraft as utils from metagpt.config import CONFIG -from metagpt.minecraft_team import GameEnvironment +from metagpt.actions.minecraft.control_primitives_context import ( + load_skills_code_context, +) @agent_registry.register("action_developer") @@ -62,7 +62,7 @@ class ActionDeveloper(Base): "useChest", "mineflayer", ] - programs = "\n\n".join(utils.load_skills_code_context(base_skills) + skills) + programs = "\n\n".join(load_skills_code_context(base_skills) + skills) response_format = utils.load_prompt("action_response_format") system_action_prompt = action_template.format( programs=programs, response_format=response_format @@ -193,7 +193,7 @@ class ActionDeveloper(Base): instruct_content="generate_action_code", role=self.profile, ) - logger.info(msg) + # logger.info(msg) return msg async def _act(self) -> Message: @@ -231,89 +231,3 @@ class ActionDeveloper(Base): return msg raise ValueError(f"Unknown todo type: {type(todo)}") - - -async def main(): - events = [ - [ - "observe", - { - "voxels": ["grass_block", "dirt", "grass"], - "status": { - "health": 20, - "food": 20, - "saturation": 5, - "oxygen": 20, - "position": {"x": 0.5, "y": 84, "z": -207.5}, - "velocity": {"x": 0, "y": -0.0784000015258789, "z": 0}, - "yaw": 3.141592653589793, - "pitch": 0, - "onGround": True, - "equipment": [None, None, None, None, None, None], - "name": "bot", - "isInWater": False, - "isInLava": False, - "isCollidedHorizontally": False, - "isCollidedVertically": True, - "biome": "plains", - "entities": { - "chicken": 29.071822119730644, - "sheep": 20.361212992763768, - }, - "timeOfDay": "day", - "inventoryUsed": 0, - "elapsedTime": 41, - }, - "inventory": {}, - "nearbyChests": {}, - "blockRecords": ["grass_block", "dirt", "grass"], - }, - ] - ] - code = """ - collectBamboo.code async function collectBamboo(bot) { - // Equip the iron sword - const ironSword = bot.inventory.findInventoryItem(mcData.itemsByName.iron_sword.id); - await bot.equip(ironSword, "hand"); - - // Find bamboo plants using the exploreUntil function - const bambooPlants = await exploreUntil(bot, new Vec3(1, 0, 1), 60, () => { - const bambooPlants = bot.findBlocks({ - matching: block => block.name === "bamboo", - maxDistance: 32, - count: 10 - }); - return bambooPlants.length >= 10 ? bambooPlants : null; - }); - if (!bambooPlants) { - bot.chat("Could not find enough bamboo plants."); - return; - } - - // Break 10 bamboo plants using the iron sword - for (const bambooPlant of bambooPlants) { - const block = bot.blockAt(bambooPlant); - await bot.dig(block); - } - bot.chat("Broke 10 bamboo plants."); - - // Collect the dropped bamboo items - for (const bambooPlant of bambooPlants) { - await bot.pathfinder.goto(new GoalBlock(bambooPlant.x, bambooPlant.y, bambooPlant.z)); - } - bot.chat("Collected 10 bamboo."); - } - """ - ad = ActionDeveloper() - ge = GameEnvironment() - ad.set_memory(shared_memory=ge) - msg = ad.encapsule_message(events=events, code=code) - logger.info(f"Encapsuled_message: {msg}") - - parsed_result = await ad.generate_action_code(msg) - - logger.info(f"Parsed_code_updating: {parsed_result}") - - -if __name__ == "__main__": - asyncio.run(main()) diff --git a/metagpt/utils/minecraft/__init__.py b/metagpt/utils/minecraft/__init__.py index e61f130c4..6ee04c606 100644 --- a/metagpt/utils/minecraft/__init__.py +++ b/metagpt/utils/minecraft/__init__.py @@ -5,5 +5,4 @@ from .load_prompts import load_prompt from .json_utils import * from .file_utils import * -from .load_skills_code_context import load_skills_code_context -from .action_rsp_parser import parse_action_response +from .action_rsp_parser import parse_js_code, parse_action_response diff --git a/metagpt/utils/minecraft/action_rsp_parser.py b/metagpt/utils/minecraft/action_rsp_parser.py index d2f150e44..91ccc141d 100644 --- a/metagpt/utils/minecraft/action_rsp_parser.py +++ b/metagpt/utils/minecraft/action_rsp_parser.py @@ -2,28 +2,47 @@ import re import time from javascript import require +def parse_js_code(msg: str): + ''' + Extract and Parse JavaScript code blocks + ''' + babel = require("@babel/core") + code_pattern = re.compile(r"```(?:javascript|js)(.*?)```", re.DOTALL) + code = "\n".join(code_pattern.findall(msg)) + parsed = babel.parse(code) + return parsed def parse_action_response(msg: str): """ + Input: + ''' + Explain: ... + Plan: ... + Code: + ```javascript + ... + ``` + ''' + Return: { "program_code": program_code, "program_name": main_function["name"], "exec_code": exec_code, - } + } or + + "{error}" + Refer to @ https://github.com/MineDojo/Voyager/blob/main/voyager/agents/action.py """ retry = 3 - error = None + error = None # 3 times failed return error + babel_generator = require("@babel/generator").default while retry > 0: try: - babel = require("@babel/core") - babel_generator = require("@babel/generator").default - - code_pattern = re.compile(r"```(?:javascript|js)(.*?)```", re.DOTALL) - code = "\n".join(code_pattern.findall(msg)) - parsed = babel.parse(code) + parsed = parse_js_code(msg) + # Collect func list: check if func & async functions = [] assert len(list(parsed.program.body)) > 0, "No functions found" for i, node in enumerate(parsed.program.body): @@ -42,7 +61,8 @@ def parse_action_response(msg: str): "params": list(node["params"]), } ) - # find the last async function + + # Ensure main_function is the last async function main_function = None for function in reversed(functions): if function["type"] == "AsyncFunctionDeclaration": @@ -55,6 +75,8 @@ def parse_action_response(msg: str): len(main_function["params"]) == 1 and main_function["params"][0].name == "bot" ), f"Main function {main_function['name']} must take a single argument named 'bot'" + + # Split to program_code & exec_code for output program_code = "\n\n".join(function["body"] for function in functions) exec_code = f"await {main_function['name']}(bot);" return { diff --git a/metagpt/utils/minecraft/load_skills_code_context.py b/metagpt/utils/minecraft/load_skills_code_context.py deleted file mode 100644 index ffaaa0052..000000000 --- a/metagpt/utils/minecraft/load_skills_code_context.py +++ /dev/null @@ -1,21 +0,0 @@ -import os -import metagpt.utils.minecraft as utils -from metagpt.logs import logger - - -def load_skills_code_context(skill_names=None): - current_dir = os.path.dirname(os.path.abspath(__file__)) - skills_dir = os.path.join(current_dir, "skills_code_context") - if skill_names is None: - skill_names = [ - skill[:-3] for skill in os.listdir(f"{skills_dir}") if skill.endswith(".js") - ] - skills = [ - utils.load_text(os.path.join(skills_dir, f"{skill_name}.js")) - for skill_name in skill_names - ] - return skills - - -if __name__ == "__main__": - logger.info(load_skills_code_context(["craftItem", "exploreUntil"])) diff --git a/metagpt/utils/minecraft/skills_code_context/.prettierrc.json b/metagpt/utils/minecraft/skills_code_context/.prettierrc.json deleted file mode 100644 index 0a02bcefd..000000000 --- a/metagpt/utils/minecraft/skills_code_context/.prettierrc.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "tabWidth": 4 -} diff --git a/metagpt/utils/minecraft/skills_code_context/__init__.py b/metagpt/utils/minecraft/skills_code_context/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/metagpt/utils/minecraft/skills_code_context/craftItem.js b/metagpt/utils/minecraft/skills_code_context/craftItem.js deleted file mode 100644 index 806811d46..000000000 --- a/metagpt/utils/minecraft/skills_code_context/craftItem.js +++ /dev/null @@ -1,14 +0,0 @@ -// Craft 8 oak_planks from 2 oak_log (do the recipe 2 times): craftItem(bot, "oak_planks", 2); -// You must place a crafting table before calling this function -async function craftItem(bot, name, count = 1) { - const item = mcData.itemsByName[name]; - const craftingTable = bot.findBlock({ - matching: mcData.blocksByName.crafting_table.id, - maxDistance: 32, - }); - await bot.pathfinder.goto( - new GoalLookAtBlock(craftingTable.position, bot.world) - ); - const recipe = bot.recipesFor(item.id, null, 1, craftingTable)[0]; - await bot.craft(recipe, count, craftingTable); -} diff --git a/metagpt/utils/minecraft/skills_code_context/exploreUntil.js b/metagpt/utils/minecraft/skills_code_context/exploreUntil.js deleted file mode 100644 index 55c62a453..000000000 --- a/metagpt/utils/minecraft/skills_code_context/exploreUntil.js +++ /dev/null @@ -1,31 +0,0 @@ -/* -Explore until find an iron_ore, use Vec3(0, -1, 0) because iron ores are usually underground -await exploreUntil(bot, new Vec3(0, -1, 0), 60, () => { - const iron_ore = bot.findBlock({ - matching: mcData.blocksByName["iron_ore"].id, - maxDistance: 32, - }); - return iron_ore; -}); - -Explore until find a pig, use Vec3(1, 0, 1) because pigs are usually on the surface -let pig = await exploreUntil(bot, new Vec3(1, 0, 1), 60, () => { - const pig = bot.nearestEntity((entity) => { - return ( - entity.name === "pig" && - entity.position.distanceTo(bot.entity.position) < 32 - ); - }); - return pig; -}); -*/ -async function exploreUntil(bot, direction, maxTime = 60, callback) { - /* - Implementation of this function is omitted. - direction: Vec3, can only contain value of -1, 0 or 1 - maxTime: number, the max time for exploration - callback: function, early stop condition, will be called each second, exploration will stop if return value is not null - - Return: null if explore timeout, otherwise return the return value of callback - */ -} diff --git a/metagpt/utils/minecraft/skills_code_context/killMob.js b/metagpt/utils/minecraft/skills_code_context/killMob.js deleted file mode 100644 index 670ca9753..000000000 --- a/metagpt/utils/minecraft/skills_code_context/killMob.js +++ /dev/null @@ -1,12 +0,0 @@ -// Kill a pig and collect the dropped item: killMob(bot, "pig", 300); -async function killMob(bot, mobName, timeout = 300) { - const entity = bot.nearestEntity( - (entity) => - entity.name === mobName && - entity.position.distanceTo(bot.entity.position) < 32 - ); - await bot.pvp.attack(entity); - await bot.pathfinder.goto( - new GoalBlock(entity.position.x, entity.position.y, entity.position.z) - ); -} diff --git a/metagpt/utils/minecraft/skills_code_context/mineBlock.js b/metagpt/utils/minecraft/skills_code_context/mineBlock.js deleted file mode 100644 index c6a7559e6..000000000 --- a/metagpt/utils/minecraft/skills_code_context/mineBlock.js +++ /dev/null @@ -1,15 +0,0 @@ -// Mine 3 cobblestone: mineBlock(bot, "stone", 3); -async function mineBlock(bot, name, count = 1) { - const blocks = bot.findBlocks({ - matching: (block) => { - return block.name === name; - }, - maxDistance: 32, - count: count, - }); - const targets = []; - for (let i = 0; i < Math.min(blocks.length, count); i++) { - targets.push(bot.blockAt(blocks[i])); - } - await bot.collectBlock.collect(targets, { ignoreNoPath: true }); -} diff --git a/metagpt/utils/minecraft/skills_code_context/mineflayer.js b/metagpt/utils/minecraft/skills_code_context/mineflayer.js deleted file mode 100644 index 43217885c..000000000 --- a/metagpt/utils/minecraft/skills_code_context/mineflayer.js +++ /dev/null @@ -1,22 +0,0 @@ -await bot.pathfinder.goto(goal); // A very useful function. This function may change your main-hand equipment. -// Following are some Goals you can use: -new GoalNear(x, y, z, range); // Move the bot to a block within the specified range of the specified block. `x`, `y`, `z`, and `range` are `number` -new GoalXZ(x, z); // Useful for long-range goals that don't have a specific Y level. `x` and `z` are `number` -new GoalGetToBlock(x, y, z); // Not get into the block, but get directly adjacent to it. Useful for fishing, farming, filling bucket, and beds. `x`, `y`, and `z` are `number` -new GoalFollow(entity, range); // Follow the specified entity within the specified range. `entity` is `Entity`, `range` is `number` -new GoalPlaceBlock(position, bot.world, {}); // Position the bot in order to place a block. `position` is `Vec3` -new GoalLookAtBlock(position, bot.world, {}); // Path into a position where a blockface of the block at position is visible. `position` is `Vec3` - -// These are other Mineflayer functions you can use: -bot.isABed(bedBlock); // Return true if `bedBlock` is a bed -bot.blockAt(position); // Return the block at `position`. `position` is `Vec3` - -// These are other Mineflayer async functions you can use: -await bot.equip(item, destination); // Equip the item in the specified destination. `item` is `Item`, `destination` can only be "hand", "head", "torso", "legs", "feet", "off-hand" -await bot.consume(); // Consume the item in the bot's hand. You must equip the item to consume first. Useful for eating food, drinking potions, etc. -await bot.fish(); // Let bot fish. Before calling this function, you must first get to a water block and then equip a fishing rod. The bot will automatically stop fishing when it catches a fish -await bot.sleep(bedBlock); // Sleep until sunrise. You must get to a bed block first -await bot.activateBlock(block); // This is the same as right-clicking a block in the game. Useful for buttons, doors, etc. You must get to the block first -await bot.lookAt(position); // Look at the specified position. You must go near the position before you look at it. To fill bucket with water, you must lookAt first. `position` is `Vec3` -await bot.activateItem(); // This is the same as right-clicking to use the item in the bot's hand. Useful for using buckets, etc. You must equip the item to activate first -await bot.useOn(entity); // This is the same as right-clicking an entity in the game. Useful for shearing sheep, equipping harnesses, etc. You must get to the entity first diff --git a/metagpt/utils/minecraft/skills_code_context/placeItem.js b/metagpt/utils/minecraft/skills_code_context/placeItem.js deleted file mode 100644 index 99e06089c..000000000 --- a/metagpt/utils/minecraft/skills_code_context/placeItem.js +++ /dev/null @@ -1,28 +0,0 @@ -// Place a crafting_table near the player, Vec3(1, 0, 0) is just an example, you shouldn't always use that: placeItem(bot, "crafting_table", bot.entity.position.offset(1, 0, 0)); -async function placeItem(bot, name, position) { - const item = bot.inventory.findInventoryItem(mcData.itemsByName[name].id); - // find a reference block - const faceVectors = [ - new Vec3(0, 1, 0), - new Vec3(0, -1, 0), - new Vec3(1, 0, 0), - new Vec3(-1, 0, 0), - new Vec3(0, 0, 1), - new Vec3(0, 0, -1), - ]; - let referenceBlock = null; - let faceVector = null; - for (const vector of faceVectors) { - const block = bot.blockAt(position.minus(vector)); - if (block?.name !== "air") { - referenceBlock = block; - faceVector = vector; - break; - } - } - // You must first go to the block position you want to place - await bot.pathfinder.goto(new GoalPlaceBlock(position, bot.world, {})); - // You must equip the item right before calling placeBlock - await bot.equip(item, "hand"); - await bot.placeBlock(referenceBlock, faceVector); -} diff --git a/metagpt/utils/minecraft/skills_code_context/smeltItem.js b/metagpt/utils/minecraft/skills_code_context/smeltItem.js deleted file mode 100644 index 0a3c76257..000000000 --- a/metagpt/utils/minecraft/skills_code_context/smeltItem.js +++ /dev/null @@ -1,22 +0,0 @@ -// Smelt 1 raw_iron into 1 iron_ingot using 1 oak_planks as fuel: smeltItem(bot, "raw_iron", "oak_planks"); -// You must place a furnace before calling this function -async function smeltItem(bot, itemName, fuelName, count = 1) { - const item = mcData.itemsByName[itemName]; - const fuel = mcData.itemsByName[fuelName]; - const furnaceBlock = bot.findBlock({ - matching: mcData.blocksByName.furnace.id, - maxDistance: 32, - }); - await bot.pathfinder.goto( - new GoalLookAtBlock(furnaceBlock.position, bot.world) - ); - const furnace = await bot.openFurnace(furnaceBlock); - for (let i = 0; i < count; i++) { - await furnace.putFuel(fuel.id, null, 1); - await furnace.putInput(item.id, null, 1); - // Wait 12 seconds for the furnace to smelt the item - await bot.waitForTicks(12 * 20); - await furnace.takeOutput(); - } - await furnace.close(); -} diff --git a/metagpt/utils/minecraft/skills_code_context/useChest.js b/metagpt/utils/minecraft/skills_code_context/useChest.js deleted file mode 100644 index e80af3fd9..000000000 --- a/metagpt/utils/minecraft/skills_code_context/useChest.js +++ /dev/null @@ -1,35 +0,0 @@ -// Get a torch from chest at (30, 65, 100): getItemFromChest(bot, new Vec3(30, 65, 100), {"torch": 1}); -// This function will work no matter how far the bot is from the chest. -async function getItemFromChest(bot, chestPosition, itemsToGet) { - await moveToChest(bot, chestPosition); - const chestBlock = bot.blockAt(chestPosition); - const chest = await bot.openContainer(chestBlock); - for (const name in itemsToGet) { - const itemByName = mcData.itemsByName[name]; - const item = chest.findContainerItem(itemByName.id); - await chest.withdraw(item.type, null, itemsToGet[name]); - } - await closeChest(bot, chestBlock); -} -// Deposit a torch into chest at (30, 65, 100): depositItemIntoChest(bot, new Vec3(30, 65, 100), {"torch": 1}); -// This function will work no matter how far the bot is from the chest. -async function depositItemIntoChest(bot, chestPosition, itemsToDeposit) { - await moveToChest(bot, chestPosition); - const chestBlock = bot.blockAt(chestPosition); - const chest = await bot.openContainer(chestBlock); - for (const name in itemsToDeposit) { - const itemByName = mcData.itemsByName[name]; - const item = bot.inventory.findInventoryItem(itemByName.id); - await chest.deposit(item.type, null, itemsToDeposit[name]); - } - await closeChest(bot, chestBlock); -} -// Check the items inside the chest at (30, 65, 100): checkItemInsideChest(bot, new Vec3(30, 65, 100)); -// You only need to call this function once without any action to finish task of checking items inside the chest. -async function checkItemInsideChest(bot, chestPosition) { - await moveToChest(bot, chestPosition); - const chestBlock = bot.blockAt(chestPosition); - await bot.openContainer(chestBlock); - // You must close the chest after opening it if you are asked to open a chest - await closeChest(bot, chestBlock); -} diff --git a/tests/metagpt/roles/minecraft/test_action_developer.py b/tests/metagpt/roles/minecraft/test_action_developer.py new file mode 100644 index 000000000..badb5166b --- /dev/null +++ b/tests/metagpt/roles/minecraft/test_action_developer.py @@ -0,0 +1,91 @@ +import asyncio + +from metagpt.minecraft_team import GameEnvironment +from metagpt.roles.minecraft.action_developer import ActionDeveloper +from metagpt.logs import logger + + +async def main(): + events = [ + [ + "observe", + { + "voxels": ["grass_block", "dirt", "grass"], + "status": { + "health": 20, + "food": 20, + "saturation": 5, + "oxygen": 20, + "position": {"x": 0.5, "y": 84, "z": -207.5}, + "velocity": {"x": 0, "y": -0.0784000015258789, "z": 0}, + "yaw": 3.141592653589793, + "pitch": 0, + "onGround": True, + "equipment": [None, None, None, None, None, None], + "name": "bot", + "isInWater": False, + "isInLava": False, + "isCollidedHorizontally": False, + "isCollidedVertically": True, + "biome": "plains", + "entities": { + "chicken": 29.071822119730644, + "sheep": 20.361212992763768, + }, + "timeOfDay": "day", + "inventoryUsed": 0, + "elapsedTime": 41, + }, + "inventory": {}, + "nearbyChests": {}, + "blockRecords": ["grass_block", "dirt", "grass"], + }, + ] + ] + code = """ + async function collectBamboo(bot) { + // Equip the iron sword + const ironSword = bot.inventory.findInventoryItem(mcData.itemsByName.iron_sword.id); + await bot.equip(ironSword, "hand"); + + // Find bamboo plants using the exploreUntil function + const bambooPlants = await exploreUntil(bot, new Vec3(1, 0, 1), 60, () => { + const bambooPlants = bot.findBlocks({ + matching: block => block.name === "bamboo", + maxDistance: 32, + count: 10 + }); + return bambooPlants.length >= 10 ? bambooPlants : null; + }); + if (!bambooPlants) { + bot.chat("Could not find enough bamboo plants."); + return; + } + + // Break 10 bamboo plants using the iron sword + for (const bambooPlant of bambooPlants) { + const block = bot.blockAt(bambooPlant); + await bot.dig(block); + } + bot.chat("Broke 10 bamboo plants."); + + // Collect the dropped bamboo items + for (const bambooPlant of bambooPlants) { + await bot.pathfinder.goto(new GoalBlock(bambooPlant.x, bambooPlant.y, bambooPlant.z)); + } + bot.chat("Collected 10 bamboo."); + } + """ + ad = ActionDeveloper() + ge = GameEnvironment() + ad.set_memory(shared_memory=ge) + msg = ad.encapsule_message(events=events, code=code) + logger.info(f"Encapsuled_message: {msg}") + + parsed_result = await ad.generate_action_code(msg) + + logger.info(f"Parsed_code_updating: {parsed_result}") + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/tests/metagpt/test_minecraft_team.py b/tests/metagpt/test_minecraft_team.py new file mode 100644 index 000000000..da0126d8d --- /dev/null +++ b/tests/metagpt/test_minecraft_team.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# @Date : 2023/09/28 00:03 +# @Author : yuymf +# @Desc : +import asyncio +from metagpt.logs import logger +from metagpt.minecraft_team import GameEnvironment + + +async def main(): + test_code = "bot.chat(`/time set ${getNextTime()}`);" + mc_port = 2745 + ge = GameEnvironment() + ge.set_mc_port(mc_port) + ge.update_code(test_code) + result = await ge.on_event() + logger.info("On event test done") + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/tests/metagpt/utils/minecraft/test_action_rsp_parser.py b/tests/metagpt/utils/minecraft/test_action_rsp_parser.py new file mode 100644 index 000000000..e41ce8dd4 --- /dev/null +++ b/tests/metagpt/utils/minecraft/test_action_rsp_parser.py @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- +# @Date : 2023/09/28 00:08 +# @Author : yuymf +# @Desc : +from metagpt.utils.minecraft import parse_js_code, parse_action_response +from metagpt.logs import logger +from typing import Any + + +if __name__ == "__main__": + msg = ''' + Explain: The code from the last round is a function called `collectBamboo` that is supposed to collect bamboo plants. It equips an iron sword, finds bamboo plants using the `exploreUntil` function, breaks 10 bamboo plants using the iron sword, and then collects the dropped bamboo items. + + Plan: + 1) Check if the bot has an iron sword in its inventory. If not, collect the necessary materials and craft an iron sword using the `craftItem` function. + 2) Use the `exploreUntil` function to find at least 10 bamboo plants. If the function times out or cannot find enough bamboo plants, return and chat "Could not find enough bamboo plants." + 3) Equip the iron sword. + 4) Iterate over the found bamboo plants and break them using the iron sword. + 5) Chat "Broke 10 bamboo plants." + 6) Iterate over the found bamboo plants and collect the dropped bamboo items. + 7) Chat "Collected 10 bamboo." + + Code: + ```javascript + async function collectBamboo(bot) { + // Check if the bot has an iron sword + const ironSword = bot.inventory.findInventoryItem(mcData.itemsByName.iron_sword.id); + if (!ironSword) { + // Collect the necessary materials to craft an iron sword + await mineBlock(bot, "iron_ore", 3); + await smeltItem(bot, "iron_ore", "oak_planks", 3); + await craftItem(bot, "iron_sword", 1); + } + + // Find bamboo plants using the exploreUntil function + const bambooPlants = await exploreUntil(bot, new Vec3(1, 0, 1), 60, () => { + const bambooPlants = bot.findBlocks({ + matching: block => block.name === "bamboo", + maxDistance: 32, + count: 10 + }); + return bambooPlants.length >= 10 ? bambooPlants : null; + }); + if (!bambooPlants) { + bot.chat("Could not find enough bamboo plants."); + return; + } + + // Equip the iron sword + await bot.equip(ironSword, "hand"); + + // Break 10 bamboo plants using the iron sword + for (const bambooPlant of bambooPlants) { + const block = bot.blockAt(bambooPlant); + await bot.dig(block); + } + bot.chat("Broke 10 bamboo plants."); + + // Collect the dropped bamboo items + for (const bambooPlant of bambooPlants) { + await bot.pathfinder.goto(new GoalBlock(bambooPlant.x, bambooPlant.y, bambooPlant.z)); + } + bot.chat("Collected 10 bamboo."); + } + ``` + ''' + + logger.info(f"Parse_js_code result is HERE: {parse_js_code(msg)}") + logger.info(f"Parse_action_response result is HERE: {parse_action_response(msg)}") \ No newline at end of file