File size: 3,562 Bytes
78f0f06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from autogen.agentchat import (
    Agent,
    AssistantAgent,
    UserProxyAgent,
)
from typing import Dict, List, Optional, Union
import os
from dotenv import load_dotenv
import autogen

# https://github.com/PanoEvJ/Tiny_Agents/blob/main/src/backup/agentchat_groupchat.ipynb
load_dotenv()


class AgentSpawner:  # Group chat GroupChatSpawner class
    def __init__(
        self,
        name: List[str],
        system_message: List[str],
        human_input_mode: List[str],
        llm_config: Dict[str, any],
        agent_type: List[str],
    ):
        self.name = name
        self.llm_config = llm_config
        self.system_message = system_message
        self.human_input_mode = human_input_mode
        self.llm_config = llm_config
        self.agent_type = agent_type

    def spawn(self) -> List[Agent]:
        agents = []
        for index, ag_type in enumerate(self.agent_type):
            if ag_type == "assistant":
                agents.append(
                    autogen.AssistantAgent(
                        name=self.name[index],
                        system_message=self.system_message[index],
                        # human_input_mode=self.human_input_mode[index],
                        llm_config=self.llm_config,
                    )
                )
            elif ag_type == "userproxy":
                agents.append(
                    autogen.UserProxyAgent(
                        name=self.name[index],
                        human_input_mode=self.human_input_mode[index],
                        system_message=self.system_message[index],
                        llm_config=self.llm_config,
                    )
                )
        return agents


def combine_description_and_skills(
    data: Dict[str, Dict[str, any]], llm_config
) -> AgentSpawner:
    agent_type = []
    names = []
    system_message = []
    human_input_mode = []
    for key, values in data.items():
        agent_type.append(values["agent_type"])
        names.append(values["name"])
        human_input_mode.append(values["human_input_mode"])
        system_message.append(
            values["description"] + " with skillset in " + " ".join(values["skills"])
        )

    # You need to define llm_config according to your needs
    # llm_config = {}

    return AgentSpawner(
        name=names,
        system_message=system_message,
        human_input_mode=human_input_mode,
        llm_config=llm_config,
        agent_type=agent_type,
    )


json_data = {
    "1": {
        "name": "sales",
        "agent_type": "assistant",
        "description": "sales agents",
        "skills": ["sales", "customer service", "communication"],
        "human_input_mode": "TERMINATE",
    },
    "2": {
        "name": "marketing",
        "agent_type": "assistant",
        "description": "marketing agents",
        "skills": ["marketing", "communication"],
        "human_input_mode": "",
    },
    "3": {
        "name": "engineer",
        "agent_type": "assistant",
        "description": "engineers",
        "skills": ["python", "linux", "communication"],
        "human_input_mode": "",
    },
}
config_list_gpt4 = [
    {
        "model": "gpt-4",
        "api_key": os.getenv("OPENAI_API_KEY"),
        # "api_key": str(os.environ["OPENAI_API_KEY"]),
    },
    {
        "model": "gpt-4",
        "api_key": os.getenv("OPENAI_API_KEY"),
    },
]
llm_config = {"config_list": config_list_gpt4}
agent_spawner = combine_description_and_skills(json_data, llm_config)

all_agents = agent_spawner.spawn()
print(all_agents)