demo_helloagents.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """Minimal end-to-end MADF discussion powered by HelloAgents."""
  2. from app.agent.agent import ModeratorAgent, ParticipantAgent
  3. from app.agent.memory import SharedMemory
  4. def _consume(stream):
  5. return "".join(token for token in stream if token)
  6. def run_demo(topic="人工智能应该如何参与公共决策?"):
  7. persona = {
  8. "name": "林衡",
  9. "title": "公共政策研究者",
  10. "bio": "长期研究技术治理、公共参与和算法问责。",
  11. "theories": ["审议民主", "算法问责", "风险治理"],
  12. "stance": "技术可以辅助决策,但不能替代公共责任。",
  13. "system_prompt": "你是公共政策研究者林衡,表达具体、审慎并回应他人。",
  14. }
  15. moderator = ModeratorAgent(topic)
  16. participant = ParticipantAgent(persona["name"], persona, 1, topic)
  17. memory = SharedMemory(1)
  18. opening = _consume(moderator.opening([persona]))
  19. memory.add_message(moderator.name, opening)
  20. context = memory.get_context_str() + "\n主持人点名请林衡发表观点。"
  21. thought = participant.think(context) or {"action": "apply_to_speak", "mind": "回应主持人的问题。"}
  22. speech = _consume(participant.speak(thought, context))
  23. memory.add_message(participant.name, speech)
  24. summary = _consume(moderator.periodic_summary(memory.get_messages_for_summary()))
  25. memory.add_summary(summary)
  26. closing = _consume(moderator.closing(memory.get_summaries()))
  27. return {
  28. "topic": topic,
  29. "opening": opening,
  30. "thought": thought,
  31. "speech": speech,
  32. "summary": summary,
  33. "closing": closing,
  34. }
  35. if __name__ == "__main__":
  36. transcript = run_demo()
  37. for key in ("opening", "speech", "summary", "closing"):
  38. print(f"\n[{key}]\n{transcript[key]}")