Spaces:
Sleeping
Sleeping
File size: 12,609 Bytes
b1eddd6 | 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | """
DebateArenaEnv — True Multi-Agent Runner.
Two genuinely independent agent objects take turns against the SAME
environment server over HTTP. Neither agent sees the other's internal
state — they only see the shared observation returned by /step.
Architecture
------------
ProposerAgent ──┐
├──► DebateArenaEnv HTTP server (port 8000)
ChallengerAgent ──┘
Each agent:
- Has its own strategy (scripted here; replace with LLM calls on-site)
- Only knows its own role's allowed tools
- Sees only the shared observation (no access to opponent's reasoning)
- Scores are computed independently by the Judge (rubric)
This is the file to replace scripted agents with real LLM agents:
agent.decide(observation) → AgentAction
"""
from __future__ import annotations
import httpx
import json
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional
ENV_URL = "http://localhost:8000"
# ──────────────────────────────────────────────────────────────────────────────
# Agent base class
# ──────────────────────────────────────────────────────────────────────────────
@dataclass
class AgentMemory:
"""Each agent maintains its own private memory — not shared with opponent."""
role: str
topic_claim: str = ""
verified_facts: Dict[str, bool] = field(default_factory=dict)
my_arguments: List[str] = field(default_factory=list)
opponent_arguments: List[str] = field(default_factory=list)
round: int = 0
class BaseAgent:
"""
Abstract debate agent.
Override `decide()` to plug in a real LLM.
"""
def __init__(self, role: str, name: str) -> None:
self.role = role # "proposer" | "challenger"
self.name = name
self.memory = AgentMemory(role=role)
def observe(self, observation: Dict[str, Any]) -> None:
"""Update private memory from shared observation."""
self.memory.round = observation.get("round", 0)
topic = observation.get("topic", {})
if topic:
self.memory.topic_claim = topic.get("claim", "")
def decide(self, observation: Dict[str, Any]) -> Dict[str, Any]:
"""
Return the next tool call as {"tool": ..., "params": {...}}.
Override this with an LLM call for real agents.
"""
raise NotImplementedError
# ──────────────────────────────────────────────────────────────────────────────
# Scripted agents (replace decide() with LLM on-site)
# ──────────────────────────────────────────────────────────────────────────────
class ProposerAgent(BaseAgent):
"""
Scripted Proposer — uses the structured optimal strategy.
On-site: replace decide() with GPT-4o / fine-tuned Llama call.
"""
def __init__(self, topic_facts: Dict[str, bool], topic_keywords: List[str]) -> None:
super().__init__(role="proposer", name="ProposerAgent")
self._true_facts = [k for k, v in topic_facts.items() if v is True]
self._keywords = topic_keywords
self._phase = "verify" # internal FSM: verify → argue → update → concede → close
def decide(self, observation: Dict[str, Any]) -> Dict[str, Any]:
self.observe(observation)
phase = self._phase
if phase == "verify":
self._phase = "argue"
fact = self._true_facts[0] if self._true_facts else "EU_AI_Act_mandates_labelling"
self.memory.verified_facts[fact] = True
return {
"tool": "verify_fact",
"params": {"fact_key": fact, "role": "proposer"},
}
if phase == "argue":
self._phase = "update"
kw = self._keywords[0] if self._keywords else "the evidence"
arg = (
f"The motion is correct because evidence shows {kw}. "
f"Studies show this is supported by {self._true_facts[0]}. "
f"Therefore the conclusion follows logically. "
f"As a result, this policy is necessary."
)
self.memory.my_arguments.append(arg)
return {
"tool": "submit_argument",
"params": {
"argument": arg,
"facts_cited": self._true_facts[:2],
},
}
if phase == "update":
self._phase = "concede"
return {
"tool": "refine_position",
"params": {
"refined_claim": "A phased, audited rollout is more defensible.",
"reason": "The Challenger raised valid implementation concerns.",
},
}
if phase == "concede":
self._phase = "close"
return {
"tool": "concede_point",
"params": {
"sub_point": "Enforcement mechanisms are genuinely complex.",
"maintain_main": "The core obligation to label remains justified.",
},
}
# close
closing = (
"Because the evidence is clear and studies show the need, "
"therefore this motion stands. As a result, we urge adoption."
)
return {
"tool": "end_debate",
"params": {"closing_statement": closing, "role": "proposer"},
}
class ChallengerAgent(BaseAgent):
"""
Scripted Challenger — rebuts with structured counter-evidence.
On-site: replace decide() with a separate LLM instance.
"""
def __init__(self, topic_keywords: List[str]) -> None:
super().__init__(role="challenger", name="ChallengerAgent")
self._keywords = topic_keywords
self._has_rebutted = False
def decide(self, observation: Dict[str, Any]) -> Dict[str, Any]:
self.observe(observation)
# Only rebut once; then wait for proposer to close
if not self._has_rebutted:
self._has_rebutted = True
kw = self._keywords[0] if self._keywords else "the primary issue"
rebuttal = (
f"The Proposer makes a valid point on {kw}, "
f"however this means that implementation remains complex. "
f"Consequently, a phased approach would be better. "
f"Because evidence shows that rapid mandates often fail."
)
self.memory.opponent_arguments.append(rebuttal)
return {
"tool": "submit_rebuttal",
"params": {
"rebuttal": rebuttal,
"facts_cited": [],
"expose_fallacy": None,
},
}
# Challenger done — let Proposer close
return {} # no action this turn
# ──────────────────────────────────────────────────────────────────────────────
# Multi-agent runner
# ──────────────────────────────────────────────────────────────────────────────
def call_env(tool: str, params: Dict[str, Any]) -> Dict[str, Any]:
r = httpx.post(f"{ENV_URL}/step",
json={"tool": tool, "params": params},
timeout=10)
r.raise_for_status()
return r.json()
def run_multiagent_episode(topic_id: str) -> Dict[str, Any]:
"""
Run one full episode with two independent agents taking turns.
Returns episode summary with reward and rubric breakdown.
"""
# ── Reset shared environment ──────────────────────────────────────────────
r = httpx.post(f"{ENV_URL}/reset", json={"topic_id": topic_id}, timeout=10)
r.raise_for_status()
obs = r.json()["observation"]
topic = obs["topic"]
max_rounds = topic["max_rounds"]
print(f"\n{'═'*70}")
print(f" DEBATE: {topic_id.upper()} — {topic['domain']}")
print(f" Motion: {topic['claim'][:65]}…")
print(f"{'═'*70}")
# Import topic data for agent initialisation
from server.tasks import TOPIC_BANK
topic_obj = next(t for t in TOPIC_BANK if t.topic_id == topic_id)
# ── Instantiate two INDEPENDENT agents ────────────────────────────────────
proposer = ProposerAgent(topic_obj.known_facts, topic_obj.evidence_keywords)
challenger = ChallengerAgent(topic_obj.evidence_keywords)
print(f"\n Agents: [{proposer.name}] vs [{challenger.name}]")
print(f" Roles: proposer challenger\n")
turn = 0
last_result: Dict[str, Any] = {}
# ── Turn-based loop — agents alternate ────────────────────────────────────
# Order: Proposer verifies → Proposer argues → Challenger rebuts →
# Proposer updates → Proposer concedes → Proposer closes
turn_order = [proposer, proposer, challenger, proposer, proposer, proposer]
for agent in turn_order:
action = agent.decide(obs)
if not action:
continue # agent passed this turn
tool = action["tool"]
params = action["params"]
result = call_env(tool, params)
obs = result.get("observation", obs)
reward = result.get("reward", 0)
done = result.get("done", False)
tag = f"[{agent.name:<16}] {tool:<20}"
print(f" Turn {turn+1:02d} {tag} reward={reward:.3f}")
if result.get("result"):
print(f" └─ {result['result'][:80]}")
turn += 1
if done:
last_result = result
break
# ── Episode summary ───────────────────────────────────────────────────────
final_obs = last_result.get("observation", obs)
winner = final_obs.get("winner", "?")
final_reward = last_result.get("reward", 0)
print(f"\n{'─'*70}")
print(f" Winner: {winner.upper()} | Final reward: {final_reward:.3f}")
print(f"\n Rubric breakdown:")
for line in last_result.get("breakdown", "").split("\n"):
if line.strip():
print(f" {line}")
return {
"topic_id": topic_id,
"winner": winner,
"reward": final_reward,
"proposer": proposer.name,
"challenger": challenger.name,
"turns": turn,
}
# ──────────────────────────────────────────────────────────────────────────────
# Entry point
# ──────────────────────────────────────────────────────────────────────────────
if __name__ == "__main__":
print("DebateArenaEnv — Multi-Agent Runner")
print("Two independent agents, one shared environment server")
print(f"Environment: {ENV_URL}\n")
summaries = []
for topic_id in ["easy", "medium", "hard"]:
summary = run_multiagent_episode(topic_id)
summaries.append(summary)
print(f"\n{'═'*70}")
print(" MULTI-AGENT EPISODE SUMMARY")
print(f"{'═'*70}")
print(f" {'Topic':<10} {'Winner':<15} {'Reward':>8} {'Turns':>6}")
print(f" {'─'*50}")
for s in summaries:
print(f" {s['topic_id']:<10} {s['winner']:<15} {s['reward']:>8.3f} {s['turns']:>6}")
print(f"{'═'*70}\n")
|