File size: 5,418 Bytes
fa70ae5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# LLM agent player using HuggingFace's smolagent
from wiki_run_engine import WikiRunEnvironment
from rich.console import Console

console = Console()

try:
    from smolagent import Agent, AgentConfig
except ImportError:
    console.print("[red]smolagent package not found. Please install with 'uv pip install smolagent'[/red]")
    raise

class AgentPlayer:
    def __init__(self, wiki_data_path, model_name="HuggingFaceH4/zephyr-7b-beta"):
        """Initialize agent player"""
        self.env = WikiRunEnvironment(wiki_data_path)
        
        # Initialize LLM agent
        config = AgentConfig(
            model=model_name,
            output_parser="json"
        )
        self.agent = Agent(config)
        
    def play(self, start_article=None, target_article=None, max_steps=20):
        """Play a game of Wiki Run using the LLM agent"""
        # Reset environment
        state = self.env.reset(start_article, target_article)
        
        console.print("[bold]Agent Wiki Run[/bold]")
        console.print(f"Starting article: [cyan]{state['current_article']}[/cyan]")
        console.print(f"Target article: [red]{state['target_article']}[/red]")
        console.print()
        
        steps = 0
        while not state['is_complete'] and steps < max_steps:
            console.print(f"[bold]Step {steps + 1}:[/bold]")
            console.print(f"Current article: [cyan]{state['current_article']}[/cyan]")
            
            # Create prompt for agent
            prompt = self._create_agent_prompt(state)
            
            # Get agent's decision
            tool_result = self.agent.run(
                prompt,
                tools=[
                    {
                        "name": "choose_next_article",
                        "description": "Choose the next Wikipedia article to navigate to",
                        "parameters": {
                            "type": "object",
                            "properties": {
                                "article": {
                                    "type": "string",
                                    "description": "The title of the next article to navigate to"
                                },
                                "reasoning": {
                                    "type": "string",
                                    "description": "Explanation of why this article was chosen"
                                }
                            },
                            "required": ["article", "reasoning"]
                        }
                    }
                ]
            )
            
            # Extract agent's choice
            choice = tool_result.get("choose_next_article", {})
            next_article = choice.get("article", "")
            reasoning = choice.get("reasoning", "")
            
            console.print(f"[yellow]Agent reasoning: {reasoning}[/yellow]")
            console.print(f"Agent chooses: [green]{next_article}[/green]")
            
            # Verify the choice is valid
            if next_article in state['available_links']:
                state, message = self.env.step(next_article)
                if message:
                    console.print(f"[bold]{message}[/bold]")
            else:
                console.print("[red]Invalid choice! Agent selected an article that's not in the available links.[/red]")
                # Choose a random valid link as fallback
                import random
                next_article = random.choice(state['available_links'])
                console.print(f"[yellow]Falling back to random choice: {next_article}[/yellow]")
                state, _ = self.env.step(next_article)
                
            steps += 1
            console.print()
            
        # Game complete
        if state['is_complete']:
            console.print("[bold green]Success! Agent reached the target article![/bold green]")
        else:
            console.print("[bold red]Failed to reach target within step limit.[/bold red]")
            
        console.print(f"Steps taken: [bold]{state['steps_taken']}[/bold]")
        console.print(f"Path: [italic]{' → '.join(state['path_taken'])}[/italic]")
        
        return state
    
    def _create_agent_prompt(self, state):
        """Create prompt for the agent"""
        current = state['current_article']
        target = state['target_article']
        links = state['available_links']
        
        prompt = f"""You are playing the Wiki Run game. Your goal is to navigate from the current Wikipedia article to the target article using only the available links.

Current article: {current}
Target article: {target}

Available links (choose one):
{', '.join(links)}

Choose the link that you think will get you closest to the target article. Consider:
1. Direct connections to the target
2. Articles that might be in the same category as the target
3. General articles that might have many links to other topics

Use the choose_next_article tool to make your selection."""
        
        return prompt

if __name__ == "__main__":
    import sys
    
    if len(sys.argv) < 2:
        console.print("[red]Please provide the path to Wikipedia data[/red]")
        console.print("Usage: python agent.py <wiki_data_path>")
        sys.exit(1)
        
    wiki_data_path = sys.argv[1]
    agent = AgentPlayer(wiki_data_path)
    agent.play()