|
| 1 | +import os |
| 2 | +from typing import Dict, Any |
| 3 | +from openai import AsyncOpenAI |
| 4 | +from dotenv import load_dotenv |
| 5 | +from mas_arena.agents.base import AgentSystem, AgentSystemRegistry |
| 6 | + |
| 7 | +load_dotenv() |
| 8 | + |
| 9 | + |
| 10 | +class AutoGen(AgentSystem): |
| 11 | + |
| 12 | + def __init__(self, name: str = "autogen", config: Dict[str, Any] = None): |
| 13 | + """Initialize the AutoGen System""" |
| 14 | + super().__init__(name, config) |
| 15 | + self.config = config or {} |
| 16 | + |
| 17 | + self.model_name = self.config.get("model_name") or os.getenv("MODEL_NAME", "qwen-plus") |
| 18 | + |
| 19 | + self.num_rounds = self.config.get("num_rounds", 5) |
| 20 | + |
| 21 | + self.client = AsyncOpenAI(api_key=os.getenv("OPENAI_API_KEY"), base_url=os.getenv("OPENAI_API_BASE")) |
| 22 | + |
| 23 | + self.agents = [ |
| 24 | + { |
| 25 | + "name": "primary", |
| 26 | + "system_prompt": """You are a helpful AI assistant, skilled at generating creative and accurate content.""" |
| 27 | + }, |
| 28 | + { |
| 29 | + "name": "critic", |
| 30 | + "system_prompt": "Provide constructive feedback on the content provided. Respond with 'APPROVE' when the content meets high standards or your feedback has been addressed." |
| 31 | + } |
| 32 | + ] |
| 33 | + |
| 34 | + async def run_agent(self, problem: Dict[str, Any], **kwargs) -> Dict[str, Any]: |
| 35 | + |
| 36 | + problem_text = problem["problem"] |
| 37 | + messages = [ |
| 38 | + {"role": "user", "content": f"Problem: {problem_text}"} |
| 39 | + ] |
| 40 | + conversation_history = messages.copy() |
| 41 | + |
| 42 | + all_messages = [] |
| 43 | + final_answer = "" |
| 44 | + |
| 45 | + for _ in range(self.num_rounds): |
| 46 | + for n, agent in enumerate(self.agents): |
| 47 | + agent_name = agent["name"] |
| 48 | + agent_prompt = agent["system_prompt"] |
| 49 | + |
| 50 | + agent_messages = [ |
| 51 | + {"role": "system", "content": agent_prompt}, |
| 52 | + *conversation_history |
| 53 | + ] |
| 54 | + |
| 55 | + response = await self.client.chat.completions.create( |
| 56 | + model=self.model_name, |
| 57 | + messages=agent_messages |
| 58 | + ) |
| 59 | + |
| 60 | + response_content = response.choices[0].message.content |
| 61 | + |
| 62 | + ai_message = { |
| 63 | + 'content': response_content, |
| 64 | + 'name': agent_name, |
| 65 | + 'role': 'assistant', |
| 66 | + 'message_type': 'ai_response', |
| 67 | + 'usage_metadata': response.usage |
| 68 | + } |
| 69 | + |
| 70 | + conversation_history.append({"role": "assistant", "content": response_content, "name": agent_name}) |
| 71 | + |
| 72 | + if (agent_name == "primary"): |
| 73 | + final_answer = ai_message["content"] |
| 74 | + |
| 75 | + if agent_name == "critic" and "approve" in response_content.lower(): |
| 76 | + return { |
| 77 | + "messages": all_messages, |
| 78 | + "final_answer": final_answer |
| 79 | + } |
| 80 | + all_messages.append(ai_message) |
| 81 | + |
| 82 | + return { |
| 83 | + "messages": all_messages, |
| 84 | + "final_answer": final_answer |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | +AgentSystemRegistry.register("autogen", AutoGen) |
0 commit comments