mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-17 15:35:21 +02:00
Move test func & add comment
This commit is contained in:
parent
7f763b57d0
commit
3a705a0e89
25 changed files with 489 additions and 333 deletions
91
tests/metagpt/roles/minecraft/test_action_developer.py
Normal file
91
tests/metagpt/roles/minecraft/test_action_developer.py
Normal file
|
|
@ -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())
|
||||
21
tests/metagpt/test_minecraft_team.py
Normal file
21
tests/metagpt/test_minecraft_team.py
Normal file
|
|
@ -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())
|
||||
69
tests/metagpt/utils/minecraft/test_action_rsp_parser.py
Normal file
69
tests/metagpt/utils/minecraft/test_action_rsp_parser.py
Normal file
|
|
@ -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)}")
|
||||
Loading…
Add table
Add a link
Reference in a new issue