Spaces:
Configuration error
Configuration error
| """ | |
| Student Agent for Text Adventure Games | |
| This is your submission file. Implement the StudentAgent class to play | |
| text adventure games using the MCP server you also implement. | |
| Your agent should: | |
| 1. Connect to the MCP server via the provided client | |
| 2. Use the ReAct pattern (Thought -> Action -> Observation) | |
| 3. Call MCP tools to interact with the game | |
| 4. Maximize the game score within the step limit | |
| Required method: | |
| async def run(self, client, game, max_steps, seed, verbose) -> RunResult | |
| The 'client' is a FastMCP Client already connected to your MCP server. | |
| Use it to call tools like: await client.call_tool("play_action", {"action": "look"}) | |
| Tips: | |
| - Start by looking around and understanding your environment | |
| - Keep track of visited locations to avoid loops | |
| - Pick up useful items (lamp, sword, etc.) | |
| - The seed parameter should be used to set your LLM's seed for reproducibility | |
| """ | |
| import json | |
| import os | |
| import re | |
| from dataclasses import dataclass, field | |
| from typing import Optional | |
| from dotenv import load_dotenv | |
| from huggingface_hub import InferenceClient | |
| import random | |
| # Load environment variables | |
| load_dotenv() | |
| # ============================================================================= | |
| # LLM Configuration - DO NOT MODIFY | |
| # ============================================================================= | |
| # Model to use (fixed for fair evaluation) | |
| LLM_MODEL = "Qwen/Qwen2.5-72B-Instruct" | |
| # Initialize the LLM client (uses HF_TOKEN from environment) | |
| _hf_token = os.getenv("HF_TOKEN") | |
| if not _hf_token: | |
| raise ValueError("HF_TOKEN not found. Set it in your .env file.") | |
| LLM_CLIENT = InferenceClient(token=_hf_token) | |
| def call_llm(prompt: str, system_prompt: str, seed: int, max_tokens: int = 300) -> str: | |
| """ | |
| Call the LLM with the given prompt. Use this function in your agent. | |
| Args: | |
| prompt: The user prompt (current game state, history, etc.) | |
| system_prompt: The system prompt (instructions for the agent) | |
| seed: Random seed for reproducibility | |
| max_tokens: Maximum tokens in response (default: 300) | |
| Returns: | |
| The LLM's response text | |
| Example: | |
| response = call_llm( | |
| prompt="You are in a forest. What do you do?", | |
| system_prompt=SYSTEM_PROMPT, | |
| seed=42, | |
| ) | |
| """ | |
| messages = [ | |
| {"role": "system", "content": system_prompt}, | |
| {"role": "user", "content": prompt}, | |
| ] | |
| response = LLM_CLIENT.chat.completions.create( | |
| model=LLM_MODEL, | |
| messages=messages, | |
| temperature=0.0, # Deterministic for reproducibility | |
| max_tokens=max_tokens, | |
| seed=seed, | |
| ) | |
| return response.choices[0].message.content | |
| class RunResult: | |
| """Result of running the agent. Do not modify this class.""" | |
| final_score: int | |
| max_score: int | |
| moves: int | |
| locations_visited: set[str] | |
| game_completed: bool | |
| error: Optional[str] = None | |
| history: list[tuple[str, str, str]] = field(default_factory=list) | |
| # ============================================================================= | |
| # System Prompt - Customize this for your agent | |
| # ============================================================================= | |
| SYSTEM_PROMPT = """You are playing a classic text adventure game. | |
| GOAL: Explore the world, solve puzzles, and maximize your score. | |
| AVAILABLE TOOLS (use via MCP): | |
| - play_action: Execute a game command (north, take lamp, open mailbox, etc.) | |
| - memory: Get current game state and history (if implemented) | |
| - inventory: Check what you're carrying (if implemented) | |
| - get_map: Get a map of explored locations | |
| - add_interactive: Add an interactive object to memory (position is automatically tracked, but you cann add it manutally if it is not in the current location) | |
| - get_interactives: Get list of visited interactive objects, with their positions. | |
| - set_current_objective: Set a new current objective | |
| VALID GAME COMMANDS for play_action: | |
| - Movement: north, south, east, west, up, down, enter, exit | |
| - Objects: take <item>, drop <item>, open <thing>, close <thing>, examine <thing> | |
| - Light: turn on lamp, turn off lamp | |
| - Combat: attack <enemy> with <weapon> | |
| - Other: inventory, look, read <thing>, wait | |
| FORBIDDEN (will NOT work): check, inspect, search, grab, use, help | |
| RESPOND IN THIS EXACT FORMAT (no markdown): | |
| THOUGHT: <brief reasoning about what to do next> | |
| TOOL: <tool_name> | |
| ARGS: <JSON arguments> | |
| Examples: | |
| THOUGHT: I need to see what's around me. | |
| TOOL: play_action | |
| ARGS: {"action": "look"} | |
| THOUGHT: I found a locked door. I might find a key to open it. | |
| TOOL: add_interactive | |
| ARGS: {"object":"locked door"} | |
| THOUGHT:I have picked up a key, this might be useful to open a door I saw earlier. | |
| TOOL: get_interactives | |
| ARGS: {} | |
| STRATEGY: | |
| 1. Start by looking around and checking memory | |
| 2. Explore systematically | |
| 3. Always pick up useful items (key, lamp, sword, etc.) | |
| 4. Open containers (mailbox, window, etc.) | |
| 5. Try to use items to interact with the environment (attack, read, use key, etc). Check you | |
| 6. Check your inventory to see if the items you have can be useful. | |
| DO NOT repeat the same action multiple times in a row. | |
| Try to follow the current objective, but adapt if you find new information. | |
| """ | |
| # ============================================================================= | |
| # Student Agent - IMPLEMENT THIS CLASS | |
| # ============================================================================= | |
| class StudentAgent: | |
| def __init__(self): | |
| """Initialize the agent state.""" | |
| self.history: list[dict] = [] | |
| self.recent_actions: list[str] = [] | |
| self.score: int = 0 | |
| self.visited_directions = {} | |
| self.current_location = None | |
| async def run( | |
| self, | |
| client, | |
| game: str, | |
| max_steps: int, | |
| seed: int, | |
| verbose: bool = False, | |
| ) -> RunResult: | |
| """Run the agent for a game session.""" | |
| locations_visited = set() | |
| history = [] | |
| moves = 0 | |
| # Get list of available tools | |
| tools = await client.list_tools() | |
| tool_names = [t.name for t in tools] | |
| # Get initial observation | |
| result = await client.call_tool("play_action", {"action": "look"}) | |
| observation = self._extract_result(result) | |
| # Track initial location | |
| location = observation.split("\n")[0] if observation else "Unknown" | |
| self.current_location = location | |
| locations_visited.add(location) | |
| if verbose: | |
| print(f"\n{observation}") | |
| # Main ReAct loop | |
| for step in range(1, max_steps + 1): | |
| # Build prompt with context | |
| prompt = self._build_prompt(observation) | |
| # Call LLM for reasoning (use step-based seed for variety) | |
| #print(prompt) | |
| objective_result = await client.call_tool("get_current_objective", {}) | |
| current_objective = self._extract_result(objective_result) | |
| additional = '' | |
| if step % 9 == 0: | |
| additional += "If in the last turns you have encountered places you may need to go back later write it down using the add_interactive tool." | |
| additional += " If you have found new items, check if they are useful for places you have visited in the past using the get_interactives tool. " | |
| if step % 20 == 0: | |
| additional += "You should update the current objective using the set_current_objective tool. Think about a medium to long term objective." | |
| if verbose: | |
| print(additional) | |
| response = call_llm(prompt+current_objective+additional, SYSTEM_PROMPT, seed + step) | |
| #print(f"\n[LLM RESPONSE]\n{response}\n") | |
| # Parse the response | |
| thought, tool_name, tool_args = self._parse_response(response, tool_names) | |
| if verbose: | |
| print(f"\n--- Step {step} ---") | |
| print(f"[THOUGHT] {thought}") | |
| print(f"[TOOL] {tool_name}({tool_args})") | |
| # Validate and fix common issues | |
| tool_name, tool_args = self._validate_tool_call(tool_name, tool_args, tool_names) | |
| # Loop detection | |
| if tool_name == "play_action": | |
| action = tool_args.get("action", "look") | |
| self.recent_actions.append(action) | |
| if len(self.recent_actions) > 5: | |
| self.recent_actions = self.recent_actions[-5:] | |
| # Detect loops - if same action 3 times, force "look" or a random direction | |
| if len(self.recent_actions) >= 3 and len(set(self.recent_actions[-3:])) == 1: | |
| if self.recent_actions[-1] != "look": | |
| if verbose: | |
| print(f"[WARNING] Loop detected - forcing 'look'") | |
| tool_args = {"action": "look"} | |
| self.recent_actions.append("look") | |
| elif self.recent_actions[-1] == "look": | |
| if verbose: | |
| print(f"[WARNING] Repeated 'look' - trying a random direction") | |
| tool_args = {"action": random.choice(["north", "south", "east", "west", "up", "down"])} | |
| self.recent_actions.append(tool_args["action"]) | |
| if action in ["north", "south", "east", "west", "up", "down"]: | |
| if not self.visited_directions.get(self.current_location): | |
| self.visited_directions[self.current_location] = [] | |
| available = [d for d in ["north", "south", "east", "west", "up", "down"] if d not in self.visited_directions[self.current_location]] | |
| if not available: | |
| available = ["north", "south", "east", "west", "up", "down"] | |
| if action in available: | |
| self.visited_directions[self.current_location].append(action) | |
| if action not in available: | |
| tool_args = {"action": available[0]} | |
| self.visited_directions[self.current_location].append(available[0]) | |
| if verbose: | |
| print(f"[INFO] You've been {action} from {self.current_location} before. Forcing new direction: {available[0]} ") | |
| moves += 1 | |
| # Execute the tool | |
| try: | |
| result = await client.call_tool(tool_name, tool_args) | |
| observation = self._extract_result(result) | |
| if verbose: | |
| print(f"[RESULT] {observation[:200]}...") | |
| except Exception as e: | |
| observation = f"Error: {e}" | |
| if verbose: | |
| print(f"[ERROR] {e}") | |
| # Track location | |
| if action in ["north", "south", "east", "west", "up", "down"]: | |
| location = observation.split("\n")[0] if observation else "Unknown" | |
| if len(location.split(' ')) < 4: | |
| locations_visited.add(location) | |
| self.current_location = location | |
| # Update history | |
| self.history.append({ | |
| "step": step, | |
| "thought": thought, | |
| "tool": tool_name, | |
| "args": tool_args, | |
| "result": observation[:200] | |
| }) | |
| if len(self.history) > 10: | |
| self.history = self.history[-10:] | |
| # Track score from observation | |
| self._update_score(observation) | |
| # Record in result history | |
| history.append((thought, f"{tool_name}({tool_args})", observation[:200])) | |
| # Check for game over | |
| if self._is_game_over(observation): | |
| if verbose: | |
| print("\n*** GAME OVER ***") | |
| break | |
| return RunResult( | |
| final_score=self.score, | |
| max_score=350, | |
| moves=moves, | |
| locations_visited=locations_visited, | |
| game_completed=self._is_game_over(observation), | |
| history=history, | |
| ) | |
| def _build_prompt(self, observation: str) -> str: | |
| """Build the prompt for the LLM with context.""" | |
| parts = [] | |
| parts.append(f"Current Score: {self.score}") | |
| # Recent history | |
| if self.history: | |
| parts.append("\nRecent actions:") | |
| for entry in self.history: | |
| action = entry.get("args", {}).get("action", entry["tool"]) | |
| result_short = entry["result"] | |
| parts.append(f" > {action} -> {result_short}") | |
| # Warn about repeated actions | |
| if self.recent_actions and len(set(self.recent_actions[-3:])) == 1: | |
| parts.append(f"\n[WARNING: You've been doing '{self.recent_actions[-1]}' repeatedly. TRY SOMETHING DIFFERENT!]") | |
| parts.append(f"\nCurrent situation:\n{observation}") | |
| parts.append("\nWhat do you do next?") | |
| return "\n".join(parts) | |
| def _parse_response(self, response: str, valid_tools: list[str]) -> tuple[str, str, dict]: | |
| """Parse the LLM response to extract thought, tool, and arguments.""" | |
| thought = "No reasoning provided" | |
| tool_name = "play_action" | |
| tool_args = {"action": "look"} | |
| lines = response.strip().split("\n") | |
| for line in lines: | |
| line_clean = line.strip() | |
| line_upper = line_clean.upper() | |
| if line_upper.startswith("THOUGHT:"): | |
| thought = line_clean.split(":", 1)[1].strip() | |
| elif line_upper.startswith("TOOL:"): | |
| raw_tool = line_clean.split(":", 1)[1].strip().lower() | |
| raw_tool = raw_tool.replace("**", "").replace("*", "").replace("`", "") | |
| raw_tool = raw_tool.split()[0] if raw_tool else "play_action" | |
| tool_name = raw_tool | |
| elif line_upper.startswith("ARGS:"): | |
| args_part = line_clean.split(":", 1)[1].strip() | |
| try: | |
| args_part = args_part.replace("'", '"') | |
| tool_args = json.loads(args_part) | |
| except json.JSONDecodeError: | |
| match = re.search(r'"action"\s*:\s*"([^"]+)"', args_part) | |
| if match: | |
| tool_args = {"action": match.group(1)} | |
| else: | |
| tool_args = {"action": "look"} | |
| return thought, tool_name, tool_args | |
| def _validate_tool_call(self, tool_name: str, tool_args: dict, valid_tools: list[str]) -> tuple[str, dict]: | |
| """Validate and fix common tool call issues.""" | |
| # Fix tool name | |
| if tool_name not in valid_tools: | |
| if tool_name in ["action", "do", "command"]: | |
| tool_name = "play_action" | |
| elif tool_name in ["map", "location"]: | |
| tool_name = "get_map" | |
| elif tool_name in ["mem", "state", "status"]: | |
| tool_name = "memory" | |
| elif tool_name in ["inv", "items"]: | |
| tool_name = "inventory" | |
| elif tool_name in ["interactive", "interactives", "get_interactive", "get_interactive_objects"]: | |
| tool_name = "get_interactives" | |
| elif tool_name in ["add_object", "add_interactives", "add_interactive_object"]: | |
| tool_name = "add_interactive" | |
| elif tool_name in ["set_objective", "objective"]: | |
| tool_name = "set_current_objective" | |
| else: | |
| tool_name = "play_action" | |
| # Fix action verbs | |
| if tool_name == "play_action": | |
| action = tool_args.get("action", "look") | |
| invalid_verb_map = { | |
| "check": "examine", | |
| "inspect": "examine", | |
| "search": "look", | |
| "grab": "take", | |
| "pick": "take", | |
| "use": "examine", | |
| "investigate": "examine", | |
| } | |
| words = action.lower().split() | |
| if words and words[0] in invalid_verb_map: | |
| words[0] = invalid_verb_map[words[0]] | |
| action = " ".join(words) | |
| action = action.lower().strip() | |
| action = action.replace("**", "").replace("*", "").replace("`", "") | |
| action = " ".join(action.split()) | |
| tool_args["action"] = action | |
| if tool_name == "add_interactive": | |
| if "obj" in tool_args: | |
| tool_args["object"] = tool_args.pop("obj") | |
| if "object" not in tool_args and len(tool_args) > 0: | |
| tool_args = {"object": str(list(tool_args.values())[0])} | |
| return tool_name, tool_args | |
| def _extract_result(self, result) -> str: | |
| """Extract text from MCP tool result.""" | |
| if hasattr(result, 'content') and result.content: | |
| return result.content[0].text | |
| if isinstance(result, list) and result: | |
| return result[0].text if hasattr(result[0], 'text') else str(result[0]) | |
| return str(result) | |
| def _update_score(self, text: str) -> None: | |
| """Update score from game text.""" | |
| patterns = [ | |
| r'Score:\s*(\d+)', | |
| r'score[:\s]+(\d+)', | |
| r'\[Score:\s*(\d+)', | |
| ] | |
| for pattern in patterns: | |
| match = re.search(pattern, text, re.IGNORECASE) | |
| if match: | |
| self.score = max(self.score, int(match.group(1))) | |
| def _is_game_over(self, text: str) -> bool: | |
| """Check if the game is over.""" | |
| game_over_phrases = [ | |
| "game over", | |
| "you have died", | |
| "you are dead", | |
| "*** you have died ***", | |
| ] | |
| text_lower = text.lower() | |
| return any(phrase in text_lower for phrase in game_over_phrases) | |
| # ============================================================================= | |
| # Local Testing | |
| # ============================================================================= | |
| async def test_agent(): | |
| """Test the agent locally.""" | |
| from fastmcp import Client | |
| agent = StudentAgent() | |
| async with Client("mcp_server.py") as client: | |
| result = await agent.run( | |
| client=client, | |
| game="zork1", | |
| max_steps=20, | |
| seed=42, | |
| verbose=True, | |
| ) | |
| print(f"\n{'=' * 50}") | |
| print(f"Final Score: {result.final_score}") | |
| print(f"Moves: {result.moves}") | |
| print(f"Locations: {len(result.locations_visited)}") | |
| if __name__ == "__main__": | |
| import asyncio | |
| asyncio.run(test_agent()) |