hhhhdgfs commited on
Commit
4ad488c
·
verified ·
1 Parent(s): 2d24d14

Upload 2 files

Browse files
Files changed (2) hide show
  1. main.py +287 -0
  2. research_actions.py +136 -0
main.py ADDED
@@ -0,0 +1,287 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Filename: MetaGPT/examples/debate.py
3
+ Created Date: Tuesday, September 19th 2023, 6:52:25 pm
4
+ Author: garylin2099
5
+ @Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.1.3 of RFC 116, modify the data type of the `send_to`
6
+ value of the `Message` object; modify the argument type of `get_by_actions`.
7
+ """
8
+
9
+ import asyncio
10
+ import platform
11
+ from typing import Any
12
+
13
+ import fire
14
+
15
+ from metagpt.actions import Action, UserRequirement
16
+ from metagpt.logs import logger
17
+ from metagpt.roles import Role
18
+ from metagpt.schema import Message
19
+ from research_actions import CollectLinks, WebBrowseAndSummarize, ConductResearch
20
+ from metagpt.roles import Role
21
+ from metagpt.roles.role import RoleReactMode
22
+ import asyncio
23
+ import re
24
+
25
+
26
+
27
+
28
+
29
+
30
+ class RequestResearch(Action):
31
+ """Action: Request research from researcher"""
32
+
33
+ PROMPT_TEMPLATE: str = """
34
+ ## BACKGROUND
35
+ You are {name}, a {profile} preparing for a debate on: {topic}
36
+
37
+ ## TASK
38
+ What specific aspect of this topic would you like to research to strengthen your position?
39
+ Provide ONE specific research query (1-2 sentences) that would help you in this debate.
40
+ Focus on facts, statistics, examples, or evidence that would support your {profile} perspective.
41
+ """
42
+ name: str = "RequestResearch"
43
+
44
+ async def run(self, name: str, profile: str, topic: str) -> str:
45
+ prompt = self.PROMPT_TEMPLATE.format(name=name, profile=profile, topic=topic)
46
+ rsp = await self._aask(prompt)
47
+ return rsp
48
+
49
+
50
+ class SpeakAloud(Action):
51
+ """Action: Speak out aloud in a debate (quarrel)"""
52
+
53
+ PROMPT_TEMPLATE: str = """
54
+ ## BACKGROUND
55
+ Suppose you are {name}, you are in a debate with {opponent_name1} and {opponent_name2}. You are debating the topic:
56
+ {idea}
57
+ ## DEBATE HISTORY
58
+ Previous rounds:
59
+ {context}
60
+ ## RESEARCH INFORMATION
61
+ {research_info}
62
+ ## YOUR TURN
63
+ {instruction}
64
+ """
65
+ name: str = "SpeakAloud"
66
+
67
+ async def run(self, context: str, name: str, opponent_name1: str, opponent_name2: str, idea: str = "", profile: str = "", round_num: int = 1, research_info: str = "") -> str:
68
+ if round_num <= 3:
69
+ instruction = f"This is round {round_num} of 3 opening rounds. You should ONLY state your view on the topic, give your arguments and how you logically and rigorously arrived at your views. Do NOT rebut or respond to any of your opponents' arguments yet. Your viewpoint should be clear, concise, and extremely stereotypical of a {profile}. MANDATORY: Use specific facts, statistics, and evidence from your research information to support your arguments. Include proper citations in your response using [Source: URL or description] format."
70
+ else:
71
+ instruction = f"This is round {round_num}. You should defend your arguments, and attack and directly rebut your opponents' arguments if they differ from yours. Craft a strong, logically rigorous response in {name}'s rhetoric and viewpoints. MANDATORY: Support your arguments with specific evidence from your research and include citations using [Source: URL or description] format."
72
+
73
+ prompt = self.PROMPT_TEMPLATE.format(context=context, name=name, opponent_name1=opponent_name1, opponent_name2=opponent_name2, idea=idea, profile=profile, instruction=instruction, research_info=research_info)
74
+
75
+ rsp = await self._aask(prompt)
76
+ return rsp
77
+
78
+
79
+ class EvaluateDebate(Action):
80
+ """Action: Evaluate and summarize a debate into concise recommendations"""
81
+
82
+ PROMPT_TEMPLATE: str = """
83
+ ## ROLE
84
+ You are a neutral evaluator analyzing a debate to provide clear, actionable recommendations.
85
+
86
+ ## DEBATE TOPIC
87
+ {topic}
88
+
89
+ ## DEBATE CONTENT
90
+ {debate_content}
91
+
92
+ ## TASK
93
+ Write a concise evaluation (200-300 words) that:
94
+ 1. Summarizes key arguments from all participants
95
+ 2. Identifies core trade-offs and considerations
96
+ 3. Provides balanced, practical recommendations for decision-makers
97
+ 4. Focuses on actionable solutions
98
+
99
+ Your response should be structured and help stakeholders make informed decisions.
100
+ """
101
+
102
+ name: str = "EvaluateDebate"
103
+
104
+ async def run(self, topic: str, debate_content: str) -> str:
105
+ prompt = self.PROMPT_TEMPLATE.format(topic=topic, debate_content=debate_content)
106
+ rsp = await self._aask(prompt)
107
+ return rsp
108
+
109
+
110
+ class DebateEvaluator(Role):
111
+ name: str = "Evaluator"
112
+ profile: str = "Neutral Analyst"
113
+
114
+ def __init__(self, **data: Any):
115
+ super().__init__(**data)
116
+ self.set_actions([EvaluateDebate])
117
+
118
+ async def evaluate(self, topic: str, debate_messages: list) -> str:
119
+ todo = EvaluateDebate()
120
+ debate_content = "\n\n".join(f"{msg.sent_from}: {msg.content}" for msg in debate_messages)
121
+ evaluation = await todo.run(topic=topic, debate_content=debate_content)
122
+ return evaluation
123
+
124
+
125
+ class Researcher(Role):
126
+ """Researcher role with three actions"""
127
+ name: str = "Researcher"
128
+ profile: str = "Research Assistant"
129
+
130
+ def __init__(self, **data):
131
+ super().__init__(**data)
132
+ self.set_actions([CollectLinks, WebBrowseAndSummarize, ConductResearch])
133
+ self._set_react_mode(RoleReactMode.BY_ORDER.value, len(self.actions))
134
+
135
+ async def research_topic(self, topic: str) -> str:
136
+ """Conduct quick research on a topic"""
137
+ # Collect links (limited to 2 queries, 2 URLs each)
138
+ collect_action = CollectLinks()
139
+ links = await collect_action.run(topic, decomposition_nums=2, url_per_query=2)
140
+
141
+ # Browse and summarize (max 4 URLs total)
142
+ browse_action = WebBrowseAndSummarize()
143
+ summaries = []
144
+ url_count = 0
145
+ for query, urls in links.items():
146
+ if urls and url_count < 4:
147
+ remaining_urls = 4 - url_count
148
+ limited_urls = urls[:remaining_urls]
149
+ result = await browse_action.run(*limited_urls, query=query)
150
+ summaries.extend(result.values())
151
+ url_count += len(limited_urls)
152
+
153
+ # Conduct research
154
+ research_action = ConductResearch()
155
+ content = "\n---\n".join(summaries)
156
+ report = await research_action.run(topic, content)
157
+ return report
158
+
159
+
160
+ class Debator(Role):
161
+ name: str = ""
162
+ profile: str = ""
163
+ opponent_name1: str = ""
164
+ opponent_name2: str = ""
165
+ research_info: str = ""
166
+ research_count: int = 0
167
+ max_research: int = 1
168
+
169
+ def __init__(self, **data: Any):
170
+ super().__init__(**data)
171
+ self.set_actions([SpeakAloud])
172
+ self._watch([UserRequirement, SpeakAloud])
173
+
174
+ async def request_research(self, topic: str, researcher: Researcher) -> str:
175
+ """Request research from the researcher"""
176
+ if self.research_count >= self.max_research:
177
+ return "Research limit reached"
178
+
179
+ request_action = RequestResearch()
180
+ query = await request_action.run(name=self.name, profile=self.profile, topic=topic)
181
+
182
+ # Get research report
183
+ research_result = await researcher.research_topic(query)
184
+
185
+ self.research_info += f"\n\nResearch Query: {query}\nResearch Result: {research_result}"
186
+ self.research_count += 1
187
+ return research_result
188
+
189
+ async def _observe(self) -> int:
190
+ await super()._observe()
191
+ # accept messages sent (from opponent) to self, disregard own messages from the last round
192
+ self.rc.news = [msg for msg in self.rc.news if self.name in msg.send_to or msg.send_to == {self.name}]
193
+ return len(self.rc.news)
194
+
195
+ async def _act(self) -> Message:
196
+ logger.info(f"{self._setting}: to do {self.rc.todo}({self.rc.todo.name})")
197
+ todo = self.rc.todo
198
+
199
+ memories = self.get_memories()
200
+ context = "\n".join(f"{msg.sent_from}: {msg.content}" for msg in memories)
201
+
202
+ # Get the debate topic from the first message
203
+ topic = self.get_memories()[0].content if self.get_memories() else "debate topic"
204
+ # Count how many times this speaker has spoken
205
+ speaker_turns = len([m for m in memories if m.sent_from == self.name])
206
+
207
+ rsp = await todo.run(context=context, name=self.name, opponent_name1=self.opponent_name1, opponent_name2=self.opponent_name2, idea=topic, profile=self.profile, round_num=speaker_turns + 1, research_info=self.research_info)
208
+
209
+ msg = Message(
210
+ content=rsp,
211
+ role=self.profile,
212
+ cause_by=type(todo),
213
+ sent_from=self.name,
214
+ send_to={self.opponent_name1, self.opponent_name2}
215
+ )
216
+ self.rc.memory.add(msg)
217
+
218
+ return msg
219
+
220
+
221
+ async def debate(idea: str, investment: float = 3.0, n_round: int = 5):
222
+ """Run a debate and evaluate the results"""
223
+ School = Debator(name="Principal", profile="School", opponent_name1="John", opponent_name2 = "Mom")
224
+ Student = Debator(name="John", profile="Student", opponent_name1="Mom", opponent_name2 = "Principal")
225
+ Parent = Debator(name="Mom", profile="Parent", opponent_name1="Principal", opponent_name2 = "John")
226
+
227
+ # Create researcher
228
+ researcher = Researcher()
229
+
230
+ logger.info(f"Starting debate on: {idea}")
231
+
232
+ # Research phase - each debater gets 1 research only
233
+ debaters = [School, Student, Parent]
234
+ logger.info(f"\n=== Research Phase ===")
235
+ for debater in debaters:
236
+ logger.info(f"{debater.name} requesting research...")
237
+ await debater.request_research(idea, researcher)
238
+
239
+ # Start with Principal responding to the topic
240
+ current_speaker = School
241
+ second_speaker = Student
242
+ third_speaker = Parent
243
+
244
+ # Store all debate messages for evaluation
245
+ all_messages = []
246
+
247
+ # Initial message to start the debate
248
+ msg = Message(content=idea, role="user", send_to={"Principal"}, sent_from="User")
249
+
250
+ for round_num in range(n_round):
251
+ logger.info(f"\n=== Round {round_num + 1} ===\n{current_speaker.name}'s turn:")
252
+
253
+ # Current speaker responds
254
+ response = await current_speaker.run(msg)
255
+ logger.info(f"{current_speaker.name}: {response.content}")
256
+
257
+ # Store the response for evaluation
258
+ all_messages.append(response)
259
+
260
+ # Switch speakers for next round
261
+ current_speaker, second_speaker, third_speaker = third_speaker, current_speaker, second_speaker
262
+ msg = response
263
+
264
+ # Evaluate the debate
265
+ logger.info("\n=== EVALUATING DEBATE ===")
266
+ evaluator = DebateEvaluator()
267
+ evaluation = await evaluator.evaluate(idea, all_messages)
268
+ logger.info(f"\n=== EVALUATION RESULTS ===\n{evaluation}")
269
+
270
+ return evaluation
271
+
272
+
273
+ def main(idea: str, investment: float = 3.0, n_round: int = 6):
274
+ """
275
+ :param idea: Debate topic, such as "Topic: The U.S. should commit more in climate change fighting"
276
+ or "Trump: Climate change is a hoax"
277
+ :param investment: contribute a certain dollar amount to watch the debate
278
+ :param n_round: maximum rounds of the debate
279
+ :return:
280
+ """
281
+ if platform.system() == "Windows":
282
+ asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
283
+ asyncio.run(debate(idea, investment, n_round))
284
+
285
+
286
+ if __name__ == "__main__":
287
+ fire.Fire(main) # run as python debate.py --idea="TOPIC" --investment=3.0 --n_round=5
research_actions.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ """
3
+ Research actions for debate agents
4
+ Simplified version for Streamlit Cloud deployment
5
+ """
6
+
7
+ from typing import Dict, List
8
+ from metagpt.actions import Action
9
+ from duckduckgo_search import DDGS
10
+ import asyncio
11
+ import aiohttp
12
+ from bs4 import BeautifulSoup
13
+
14
+
15
+ class CollectLinks(Action):
16
+ """Collect search result links using DuckDuckGo"""
17
+
18
+ name: str = "CollectLinks"
19
+
20
+ async def run(self, topic: str, decomposition_nums: int = 2, url_per_query: int = 3) -> Dict[str, List[str]]:
21
+ """
22
+ Search for links related to the topic
23
+
24
+ Args:
25
+ topic: Search topic
26
+ decomposition_nums: Number of search queries (simplified to 1 for cloud)
27
+ url_per_query: Number of URLs per query
28
+
29
+ Returns:
30
+ Dict mapping queries to lists of URLs
31
+ """
32
+ try:
33
+ # Use DuckDuckGo search (free, no API key needed)
34
+ ddgs = DDGS()
35
+ results = ddgs.text(topic, max_results=url_per_query)
36
+
37
+ urls = []
38
+ for result in results:
39
+ if 'href' in result:
40
+ urls.append(result['href'])
41
+ elif 'link' in result:
42
+ urls.append(result['link'])
43
+
44
+ return {topic: urls[:url_per_query]}
45
+
46
+ except Exception as e:
47
+ # Fallback to empty results if search fails
48
+ return {topic: []}
49
+
50
+
51
+ class WebBrowseAndSummarize(Action):
52
+ """Browse web pages and extract content"""
53
+
54
+ name: str = "WebBrowseAndSummarize"
55
+
56
+ async def run(self, *urls: str, query: str = "") -> Dict[str, str]:
57
+ """
58
+ Fetch and summarize web pages
59
+
60
+ Args:
61
+ urls: URLs to fetch
62
+ query: Original search query
63
+
64
+ Returns:
65
+ Dict mapping URLs to their content summaries
66
+ """
67
+ results = {}
68
+
69
+ async with aiohttp.ClientSession() as session:
70
+ for url in urls:
71
+ try:
72
+ # Set timeout and user agent
73
+ headers = {
74
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
75
+ }
76
+
77
+ async with session.get(url, headers=headers, timeout=10) as response:
78
+ if response.status == 200:
79
+ html = await response.text()
80
+ soup = BeautifulSoup(html, 'lxml')
81
+
82
+ # Remove script and style elements
83
+ for script in soup(["script", "style"]):
84
+ script.decompose()
85
+
86
+ # Get text
87
+ text = soup.get_text(separator=' ', strip=True)
88
+
89
+ # Limit text length
90
+ text = text[:2000] if len(text) > 2000 else text
91
+
92
+ results[url] = f"[Source: {url}]\n{text}"
93
+ else:
94
+ results[url] = f"[Source: {url}]\nFailed to fetch content (HTTP {response.status})"
95
+
96
+ except asyncio.TimeoutError:
97
+ results[url] = f"[Source: {url}]\nTimeout while fetching content"
98
+ except Exception as e:
99
+ results[url] = f"[Source: {url}]\nError: {str(e)}"
100
+
101
+ return results
102
+
103
+
104
+ class ConductResearch(Action):
105
+ """Compile research from collected information"""
106
+
107
+ name: str = "ConductResearch"
108
+
109
+ async def run(self, topic: str, content: str) -> str:
110
+ """
111
+ Summarize research findings using LLM
112
+
113
+ Args:
114
+ topic: Research topic
115
+ content: Collected content from web sources
116
+
117
+ Returns:
118
+ Research summary
119
+ """
120
+ if not content or content.strip() == "":
121
+ return f"No research data available for topic: {topic}"
122
+
123
+ prompt = f"""Based on the following web search results about "{topic}", provide a concise research summary (200-300 words) with key facts, statistics, and relevant information.
124
+
125
+ Web Content:
126
+ {content[:3000]}
127
+
128
+ Research Summary:"""
129
+
130
+ try:
131
+ # Use LLM to summarize
132
+ rsp = await self._aask(prompt)
133
+ return rsp
134
+ except Exception as e:
135
+ # Fallback: return raw content summary
136
+ return f"Research on '{topic}':\n\n{content[:1000]}..."