waldiez 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of waldiez might be problematic. Click here for more details.
- waldiez/__init__.py +15 -0
- waldiez/__main__.py +6 -0
- waldiez/_version.py +3 -0
- waldiez/cli.py +162 -0
- waldiez/exporter.py +293 -0
- waldiez/exporting/__init__.py +14 -0
- waldiez/exporting/agents/__init__.py +5 -0
- waldiez/exporting/agents/agent.py +229 -0
- waldiez/exporting/agents/agent_skills.py +67 -0
- waldiez/exporting/agents/code_execution.py +67 -0
- waldiez/exporting/agents/group_manager.py +209 -0
- waldiez/exporting/agents/llm_config.py +53 -0
- waldiez/exporting/agents/rag_user/__init__.py +5 -0
- waldiez/exporting/agents/rag_user/chroma_utils.py +134 -0
- waldiez/exporting/agents/rag_user/mongo_utils.py +83 -0
- waldiez/exporting/agents/rag_user/pgvector_utils.py +93 -0
- waldiez/exporting/agents/rag_user/qdrant_utils.py +112 -0
- waldiez/exporting/agents/rag_user/rag_user.py +165 -0
- waldiez/exporting/agents/rag_user/vector_db.py +119 -0
- waldiez/exporting/agents/teachability.py +37 -0
- waldiez/exporting/agents/termination_message.py +45 -0
- waldiez/exporting/chats/__init__.py +14 -0
- waldiez/exporting/chats/chats.py +46 -0
- waldiez/exporting/chats/helpers.py +395 -0
- waldiez/exporting/chats/nested.py +264 -0
- waldiez/exporting/flow/__init__.py +5 -0
- waldiez/exporting/flow/def_main.py +37 -0
- waldiez/exporting/flow/flow.py +185 -0
- waldiez/exporting/models/__init__.py +193 -0
- waldiez/exporting/skills/__init__.py +128 -0
- waldiez/exporting/utils/__init__.py +34 -0
- waldiez/exporting/utils/comments.py +136 -0
- waldiez/exporting/utils/importing.py +267 -0
- waldiez/exporting/utils/logging_utils.py +203 -0
- waldiez/exporting/utils/method_utils.py +35 -0
- waldiez/exporting/utils/naming.py +127 -0
- waldiez/exporting/utils/object_string.py +81 -0
- waldiez/io_stream.py +181 -0
- waldiez/models/__init__.py +107 -0
- waldiez/models/agents/__init__.py +65 -0
- waldiez/models/agents/agent/__init__.py +21 -0
- waldiez/models/agents/agent/agent.py +190 -0
- waldiez/models/agents/agent/agent_data.py +162 -0
- waldiez/models/agents/agent/code_execution.py +71 -0
- waldiez/models/agents/agent/linked_skill.py +30 -0
- waldiez/models/agents/agent/nested_chat.py +73 -0
- waldiez/models/agents/agent/teachability.py +68 -0
- waldiez/models/agents/agent/termination_message.py +167 -0
- waldiez/models/agents/agents.py +129 -0
- waldiez/models/agents/assistant/__init__.py +6 -0
- waldiez/models/agents/assistant/assistant.py +41 -0
- waldiez/models/agents/assistant/assistant_data.py +29 -0
- waldiez/models/agents/group_manager/__init__.py +19 -0
- waldiez/models/agents/group_manager/group_manager.py +87 -0
- waldiez/models/agents/group_manager/group_manager_data.py +91 -0
- waldiez/models/agents/group_manager/speakers.py +211 -0
- waldiez/models/agents/rag_user/__init__.py +26 -0
- waldiez/models/agents/rag_user/rag_user.py +58 -0
- waldiez/models/agents/rag_user/rag_user_data.py +32 -0
- waldiez/models/agents/rag_user/retrieve_config.py +592 -0
- waldiez/models/agents/rag_user/vector_db_config.py +162 -0
- waldiez/models/agents/user_proxy/__init__.py +6 -0
- waldiez/models/agents/user_proxy/user_proxy.py +41 -0
- waldiez/models/agents/user_proxy/user_proxy_data.py +30 -0
- waldiez/models/chat/__init__.py +22 -0
- waldiez/models/chat/chat.py +129 -0
- waldiez/models/chat/chat_data.py +326 -0
- waldiez/models/chat/chat_message.py +304 -0
- waldiez/models/chat/chat_nested.py +160 -0
- waldiez/models/chat/chat_summary.py +110 -0
- waldiez/models/common/__init__.py +38 -0
- waldiez/models/common/base.py +63 -0
- waldiez/models/common/method_utils.py +165 -0
- waldiez/models/flow/__init__.py +9 -0
- waldiez/models/flow/flow.py +302 -0
- waldiez/models/flow/flow_data.py +87 -0
- waldiez/models/model/__init__.py +11 -0
- waldiez/models/model/model.py +169 -0
- waldiez/models/model/model_data.py +86 -0
- waldiez/models/skill/__init__.py +9 -0
- waldiez/models/skill/skill.py +129 -0
- waldiez/models/skill/skill_data.py +37 -0
- waldiez/models/waldiez.py +301 -0
- waldiez/py.typed +0 -0
- waldiez/runner.py +304 -0
- waldiez/stream/__init__.py +7 -0
- waldiez/stream/consumer.py +139 -0
- waldiez/stream/provider.py +339 -0
- waldiez/stream/server.py +412 -0
- waldiez-0.1.0.dist-info/METADATA +181 -0
- waldiez-0.1.0.dist-info/RECORD +94 -0
- waldiez-0.1.0.dist-info/WHEEL +4 -0
- waldiez-0.1.0.dist-info/entry_points.txt +2 -0
- waldiez-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
"""Agent strings generation.."""
|
|
2
|
+
|
|
3
|
+
from typing import Dict, List, Set, Tuple
|
|
4
|
+
|
|
5
|
+
from waldiez.models import WaldiezAgent, WaldiezModel, WaldiezSkill
|
|
6
|
+
|
|
7
|
+
from ..utils import get_escaped_string
|
|
8
|
+
from .agent_skills import get_agent_skill_registrations
|
|
9
|
+
from .code_execution import get_agent_code_execution_config
|
|
10
|
+
from .group_manager import get_group_manager_extras
|
|
11
|
+
from .llm_config import get_agent_llm_config
|
|
12
|
+
from .rag_user import get_rag_user_extras
|
|
13
|
+
from .termination_message import get_is_termination_message
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def get_agent_class_name(agent: WaldiezAgent) -> str:
|
|
17
|
+
"""Get the agent class name.
|
|
18
|
+
|
|
19
|
+
Parameters
|
|
20
|
+
----------
|
|
21
|
+
agent : WaldiezAgent
|
|
22
|
+
The agent.
|
|
23
|
+
|
|
24
|
+
Returns
|
|
25
|
+
-------
|
|
26
|
+
str
|
|
27
|
+
The agent class name.
|
|
28
|
+
"""
|
|
29
|
+
if agent.agent_type == "assistant":
|
|
30
|
+
return "AssistantAgent"
|
|
31
|
+
if agent.agent_type == "user":
|
|
32
|
+
return "UserProxyAgent"
|
|
33
|
+
if agent.agent_type == "manager":
|
|
34
|
+
return "GroupChatManager"
|
|
35
|
+
if agent.agent_type == "rag_user":
|
|
36
|
+
return "RetrieveUserProxyAgent"
|
|
37
|
+
return "ConversableAgent" # pragma: no cover
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def get_agent_imports(agent_class: str) -> Set[str]:
|
|
41
|
+
"""Get the imports needed for the agent.
|
|
42
|
+
|
|
43
|
+
Parameters
|
|
44
|
+
----------
|
|
45
|
+
agent_class : str
|
|
46
|
+
The agent class name.
|
|
47
|
+
|
|
48
|
+
Returns
|
|
49
|
+
-------
|
|
50
|
+
Set[str]
|
|
51
|
+
The imports needed for the agent.
|
|
52
|
+
"""
|
|
53
|
+
imports = set()
|
|
54
|
+
if agent_class == "AssistantAgent":
|
|
55
|
+
imports.add("from autogen import AssistantAgent")
|
|
56
|
+
elif agent_class == "UserProxyAgent":
|
|
57
|
+
imports.add("from autogen import UserProxyAgent")
|
|
58
|
+
elif agent_class == "GroupChatManager":
|
|
59
|
+
imports.add("from autogen import GroupChatManager")
|
|
60
|
+
imports.add("from autogen import GroupChat")
|
|
61
|
+
elif agent_class == "RetrieveUserProxyAgent":
|
|
62
|
+
imports.add(
|
|
63
|
+
"from autogen.agentchat.contrib.retrieve_user_proxy_agent "
|
|
64
|
+
"import RetrieveUserProxyAgent"
|
|
65
|
+
)
|
|
66
|
+
return imports
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def get_system_message_arg(agent: WaldiezAgent) -> str:
|
|
70
|
+
"""Get the system message argument.
|
|
71
|
+
|
|
72
|
+
Parameters
|
|
73
|
+
----------
|
|
74
|
+
agent : WaldiezAgent
|
|
75
|
+
The agent.
|
|
76
|
+
|
|
77
|
+
Returns
|
|
78
|
+
-------
|
|
79
|
+
str
|
|
80
|
+
The system message argument.
|
|
81
|
+
"""
|
|
82
|
+
if not agent.data.system_message:
|
|
83
|
+
return ""
|
|
84
|
+
return (
|
|
85
|
+
"\n "
|
|
86
|
+
f'system_message="{get_escaped_string(agent.data.system_message)}",'
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
# pylint: disable=too-many-locals, unused-argument
|
|
91
|
+
def export_agent(
|
|
92
|
+
agent: WaldiezAgent,
|
|
93
|
+
agent_names: Dict[str, str],
|
|
94
|
+
model_names: Dict[str, str],
|
|
95
|
+
skill_names: Dict[str, str],
|
|
96
|
+
all_models: List[WaldiezModel],
|
|
97
|
+
all_skills: List[WaldiezSkill],
|
|
98
|
+
group_chat_members: List[WaldiezAgent],
|
|
99
|
+
) -> Tuple[str, str, Set[str]]:
|
|
100
|
+
"""Export the agent to a string.
|
|
101
|
+
|
|
102
|
+
If the agent's `is_termination_msg` is a method,
|
|
103
|
+
the function definition and content will be included in the string.
|
|
104
|
+
So it could be like:
|
|
105
|
+
```python
|
|
106
|
+
...
|
|
107
|
+
def is_termination_message_{agent_name}(message):
|
|
108
|
+
return ....
|
|
109
|
+
...
|
|
110
|
+
agent_name = AssistantAgent(
|
|
111
|
+
...
|
|
112
|
+
is_termination_msg=is_termination_message_{agent_name},
|
|
113
|
+
...
|
|
114
|
+
)
|
|
115
|
+
```
|
|
116
|
+
The same goes for any additional `before the agent` contents in the cases
|
|
117
|
+
of a group chat manager (define the `GroupChat` first),
|
|
118
|
+
a retrieve user proxy agent (define the retrieve/db config first),
|
|
119
|
+
or the agent's `code_execution`: define the
|
|
120
|
+
`DockerCommandLineCodeExecutor`/`LocalCommandLineCodeExecutor` first.
|
|
121
|
+
|
|
122
|
+
The agent's `skill_registrations` and/or nested chats if any,
|
|
123
|
+
should be added to the final string after all the agents are defined.
|
|
124
|
+
definition. Example:
|
|
125
|
+
```python
|
|
126
|
+
...
|
|
127
|
+
agent_name = AssistantAgent(
|
|
128
|
+
...
|
|
129
|
+
)
|
|
130
|
+
...
|
|
131
|
+
register_function(
|
|
132
|
+
{skill_name},
|
|
133
|
+
caller={caller_name},
|
|
134
|
+
executor={executor_name},
|
|
135
|
+
)
|
|
136
|
+
...
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Parameters
|
|
140
|
+
----------
|
|
141
|
+
agent : WaldiezAgent
|
|
142
|
+
The agent.
|
|
143
|
+
agent_names : Dict[str, str]
|
|
144
|
+
A mapping of agent id to agent name.
|
|
145
|
+
model_names : Dict[str, str]
|
|
146
|
+
A mapping of model id to model name.
|
|
147
|
+
skill_names : Dict[str, str]
|
|
148
|
+
A mapping of skill id to skill name.
|
|
149
|
+
all_models : List[WaldiezModel]
|
|
150
|
+
All the models in the flow.
|
|
151
|
+
all_skills : List[WaldiezSkill]
|
|
152
|
+
All the skills in the flow.
|
|
153
|
+
group_chat_members : List[WaldiezAgent]
|
|
154
|
+
The group chat members.
|
|
155
|
+
|
|
156
|
+
Returns
|
|
157
|
+
-------
|
|
158
|
+
Tuple[str, str, Set[str], Set[str]]
|
|
159
|
+
A tuple containing:
|
|
160
|
+
- The string representation of the agent (w additional content before),
|
|
161
|
+
- Extra content to be added after the agents are defined.
|
|
162
|
+
- Needed imports (autogen.x, db/RAG or code execution related) if any.
|
|
163
|
+
"""
|
|
164
|
+
imports: Set[str] = set()
|
|
165
|
+
agent_name = agent_names[agent.id]
|
|
166
|
+
before_agent_string = ""
|
|
167
|
+
after_agent_string = ""
|
|
168
|
+
before_manager, group_chat_arg = get_group_manager_extras(
|
|
169
|
+
agent, group_chat_members, agent_names
|
|
170
|
+
)
|
|
171
|
+
if before_manager:
|
|
172
|
+
before_agent_string += before_manager
|
|
173
|
+
before_rag, retrieve_arg, rag_imports = get_rag_user_extras(
|
|
174
|
+
agent, agent_name, model_names
|
|
175
|
+
)
|
|
176
|
+
if before_rag:
|
|
177
|
+
before_agent_string += before_rag
|
|
178
|
+
imports.update(rag_imports)
|
|
179
|
+
is_termination_message, termination_function = get_is_termination_message(
|
|
180
|
+
agent, agent_name
|
|
181
|
+
)
|
|
182
|
+
if termination_function:
|
|
183
|
+
before_agent_string += termination_function
|
|
184
|
+
executor, config_arg, coding_import = get_agent_code_execution_config(
|
|
185
|
+
agent=agent, agent_name=agent_name, skill_names=skill_names
|
|
186
|
+
)
|
|
187
|
+
if executor:
|
|
188
|
+
before_agent_string += executor
|
|
189
|
+
if coding_import:
|
|
190
|
+
imports.add(f"from autogen.coding import {coding_import}")
|
|
191
|
+
agent_class = get_agent_class_name(agent)
|
|
192
|
+
imports.update(get_agent_imports(agent_class))
|
|
193
|
+
if agent.data.skills:
|
|
194
|
+
imports.add("from autogen import register_function")
|
|
195
|
+
default_auto_reply: str = "None"
|
|
196
|
+
if agent.data.agent_default_auto_reply:
|
|
197
|
+
default_auto_reply = (
|
|
198
|
+
f'"{get_escaped_string(agent.data.agent_default_auto_reply)}"'
|
|
199
|
+
)
|
|
200
|
+
agent_llm_config_arg, llm_config_string = get_agent_llm_config(
|
|
201
|
+
agent=agent,
|
|
202
|
+
agent_name=agent_name,
|
|
203
|
+
all_models=all_models,
|
|
204
|
+
model_names=model_names,
|
|
205
|
+
)
|
|
206
|
+
before_agent_string += llm_config_string
|
|
207
|
+
agent_str = f"""{agent_name} = {agent_class}(
|
|
208
|
+
name="{agent_name}",
|
|
209
|
+
description="{agent.description}",
|
|
210
|
+
llm_config={agent_llm_config_arg},{(get_system_message_arg(agent))}
|
|
211
|
+
human_input_mode="{agent.data.human_input_mode}",
|
|
212
|
+
max_consecutive_auto_reply={agent.data.max_consecutive_auto_reply},
|
|
213
|
+
default_auto_reply={default_auto_reply},
|
|
214
|
+
code_execution_config={config_arg},
|
|
215
|
+
is_termination_msg={is_termination_message},{group_chat_arg}{retrieve_arg}
|
|
216
|
+
)
|
|
217
|
+
"""
|
|
218
|
+
agent_skill_registrations = get_agent_skill_registrations(
|
|
219
|
+
agent, agent_names, all_skills, skill_names
|
|
220
|
+
)
|
|
221
|
+
if before_agent_string:
|
|
222
|
+
agent_str = before_agent_string + "\n" + agent_str
|
|
223
|
+
if agent_skill_registrations:
|
|
224
|
+
after_agent_string = "\n" + agent_skill_registrations + "\n"
|
|
225
|
+
return (
|
|
226
|
+
agent_str,
|
|
227
|
+
after_agent_string,
|
|
228
|
+
imports,
|
|
229
|
+
)
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"""Agent skills related string generation functions."""
|
|
2
|
+
|
|
3
|
+
from typing import Dict, List
|
|
4
|
+
|
|
5
|
+
from waldiez.models import WaldiezAgent, WaldiezSkill
|
|
6
|
+
|
|
7
|
+
from ..utils import get_escaped_string
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def get_agent_skill_registrations(
|
|
11
|
+
agent: WaldiezAgent,
|
|
12
|
+
agent_names: Dict[str, str],
|
|
13
|
+
all_skills: List[WaldiezSkill],
|
|
14
|
+
skill_names: Dict[str, str],
|
|
15
|
+
) -> str:
|
|
16
|
+
"""Get the agent skill registrations.
|
|
17
|
+
|
|
18
|
+
example output:
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
>>> register_function(
|
|
22
|
+
{skill_name},
|
|
23
|
+
caller={agent_name},
|
|
24
|
+
executor={executor_agent_name},
|
|
25
|
+
name="{skill_name}",
|
|
26
|
+
description="{skill_description}",
|
|
27
|
+
)
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Parameters
|
|
31
|
+
----------
|
|
32
|
+
agent : WaldiezAgent
|
|
33
|
+
The agent.
|
|
34
|
+
agent_names : Dict[str, str]
|
|
35
|
+
A mapping of agent id to agent name.
|
|
36
|
+
all_skills : List[WaldiezSkill]
|
|
37
|
+
All the skills in the flow.
|
|
38
|
+
skill_names : Dict[str, str]
|
|
39
|
+
A mapping of skill id to skill name.
|
|
40
|
+
|
|
41
|
+
Returns
|
|
42
|
+
-------
|
|
43
|
+
str
|
|
44
|
+
The agent skill registrations.
|
|
45
|
+
"""
|
|
46
|
+
if not agent.data.skills or not all_skills:
|
|
47
|
+
return ""
|
|
48
|
+
content = ""
|
|
49
|
+
for linked_skill in agent.data.skills:
|
|
50
|
+
skill_name = skill_names[linked_skill.id]
|
|
51
|
+
waldiez_skill = next(
|
|
52
|
+
skill for skill in all_skills if skill.id == linked_skill.id
|
|
53
|
+
)
|
|
54
|
+
skill_description = (
|
|
55
|
+
waldiez_skill.description or f"Description of {skill_name}"
|
|
56
|
+
)
|
|
57
|
+
skill_description = get_escaped_string(skill_description)
|
|
58
|
+
content += (
|
|
59
|
+
f"register_function(\n"
|
|
60
|
+
f" {skill_name},\n"
|
|
61
|
+
f" caller={agent_names[agent.id]},\n"
|
|
62
|
+
f" executor={agent_names[linked_skill.executor_id]},\n"
|
|
63
|
+
f' name="{skill_name}",\n'
|
|
64
|
+
f' description="{skill_description}",\n'
|
|
65
|
+
f")\n\n"
|
|
66
|
+
)
|
|
67
|
+
return content
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"""Code execution related functions for exporting agents."""
|
|
2
|
+
|
|
3
|
+
from typing import Dict, Tuple
|
|
4
|
+
|
|
5
|
+
from waldiez.models import WaldiezAgent
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def get_agent_code_execution_config(
|
|
9
|
+
agent: WaldiezAgent, agent_name: str, skill_names: Dict[str, str]
|
|
10
|
+
) -> Tuple[str, str, str]:
|
|
11
|
+
"""Get the code execution config for the agent.
|
|
12
|
+
|
|
13
|
+
Parameters
|
|
14
|
+
----------
|
|
15
|
+
agent : WaldiezAgent
|
|
16
|
+
The agent.
|
|
17
|
+
agent_name : str
|
|
18
|
+
The agent name.
|
|
19
|
+
skill_names : Dict[str, str]
|
|
20
|
+
A mapping of skill id to skill name.
|
|
21
|
+
|
|
22
|
+
Returns
|
|
23
|
+
-------
|
|
24
|
+
Tuple[str, str, str, Set[str]]
|
|
25
|
+
- The executor content.
|
|
26
|
+
- The executor argument.
|
|
27
|
+
- The extra autogen.coding import if needed.
|
|
28
|
+
"""
|
|
29
|
+
if agent.data.code_execution_config is False:
|
|
30
|
+
return "", "False", ""
|
|
31
|
+
use_docker = agent.data.code_execution_config.use_docker
|
|
32
|
+
if use_docker is None:
|
|
33
|
+
use_docker = False
|
|
34
|
+
executor_class_name = (
|
|
35
|
+
"DockerCommandLineCodeExecutor"
|
|
36
|
+
if use_docker
|
|
37
|
+
else "LocalCommandLineCodeExecutor"
|
|
38
|
+
)
|
|
39
|
+
executor_content = f"{agent_name}_executor = {executor_class_name}(\n"
|
|
40
|
+
if agent.data.code_execution_config.work_dir:
|
|
41
|
+
wok_dir = agent.data.code_execution_config.work_dir.replace(
|
|
42
|
+
'"', '\\"'
|
|
43
|
+
).replace("\n", "\\n")
|
|
44
|
+
executor_content += f' work_dir="{wok_dir}",\n'
|
|
45
|
+
if agent.data.code_execution_config.timeout:
|
|
46
|
+
executor_content += (
|
|
47
|
+
f" timeout={agent.data.code_execution_config.timeout},\n"
|
|
48
|
+
)
|
|
49
|
+
if use_docker is False and agent.data.code_execution_config.functions:
|
|
50
|
+
function_names = []
|
|
51
|
+
for skill_id in agent.data.code_execution_config.functions:
|
|
52
|
+
skill_name = skill_names[skill_id]
|
|
53
|
+
function_names.append(skill_name)
|
|
54
|
+
if function_names:
|
|
55
|
+
# pylint: disable=inconsistent-quotes
|
|
56
|
+
executor_content += (
|
|
57
|
+
f" functions=[{', '.join(function_names)}],\n"
|
|
58
|
+
)
|
|
59
|
+
executor_content += ")\n\n"
|
|
60
|
+
# if (
|
|
61
|
+
# executor_content
|
|
62
|
+
# == f"{agent_name}_executor = {executor_class_name}(\n)\n\n"
|
|
63
|
+
# ):
|
|
64
|
+
# # empty executor?
|
|
65
|
+
# return "", "False", ""
|
|
66
|
+
executor_arg = f'{{"executor": {agent_name}_executor}}'
|
|
67
|
+
return executor_content, executor_arg, executor_class_name
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
"""Export group manger and group chat to string."""
|
|
2
|
+
|
|
3
|
+
from typing import Dict, List, Optional, Tuple
|
|
4
|
+
|
|
5
|
+
from waldiez.models import WaldiezAgent, WaldiezGroupManager
|
|
6
|
+
|
|
7
|
+
from ..utils import get_method_string, get_object_string
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def get_group_manager_extras(
|
|
11
|
+
agent: WaldiezAgent,
|
|
12
|
+
group_chat_members: List[WaldiezAgent],
|
|
13
|
+
agent_names: Dict[str, str],
|
|
14
|
+
) -> Tuple[str, str]:
|
|
15
|
+
"""Get the group manager extra string and custom selection method if any.
|
|
16
|
+
|
|
17
|
+
Parameters
|
|
18
|
+
----------
|
|
19
|
+
agent : WaldiezAgent
|
|
20
|
+
The agent.
|
|
21
|
+
group_chat_members : List[WaldiezAgent]
|
|
22
|
+
The group members.
|
|
23
|
+
agent_names : Dict[str, str]
|
|
24
|
+
The agent names.
|
|
25
|
+
|
|
26
|
+
Returns
|
|
27
|
+
-------
|
|
28
|
+
Tuple[str, str]
|
|
29
|
+
The content before the agent string and the group chat argument.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
group_chat_arg = ""
|
|
33
|
+
before_agent_string = ""
|
|
34
|
+
custom_speaker_selection: Optional[str] = None
|
|
35
|
+
if agent.agent_type == "manager" and isinstance(agent, WaldiezGroupManager):
|
|
36
|
+
group_chat_string, group_chat_name, custom_speaker_selection = (
|
|
37
|
+
_get_group_manager_extras(agent, group_chat_members, agent_names)
|
|
38
|
+
)
|
|
39
|
+
if group_chat_name:
|
|
40
|
+
group_chat_arg = f"\n groupchat={group_chat_name},"
|
|
41
|
+
if custom_speaker_selection:
|
|
42
|
+
before_agent_string += f"{custom_speaker_selection}\n\n"
|
|
43
|
+
if group_chat_string:
|
|
44
|
+
before_agent_string += group_chat_string
|
|
45
|
+
return before_agent_string, group_chat_arg
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _get_group_manager_extras(
|
|
49
|
+
agent: WaldiezGroupManager,
|
|
50
|
+
group_members: List[WaldiezAgent],
|
|
51
|
+
agent_names: Dict[str, str],
|
|
52
|
+
) -> Tuple[str, str, Optional[str]]:
|
|
53
|
+
"""Get the group manager extra string and custom selection method if any.
|
|
54
|
+
|
|
55
|
+
Parameters
|
|
56
|
+
----------
|
|
57
|
+
agent : WaldiezGroupManager
|
|
58
|
+
The agent.
|
|
59
|
+
group_members : List[WaldiezAgent]
|
|
60
|
+
The group members.
|
|
61
|
+
agent_names : Dict[str, str]
|
|
62
|
+
The agent names.
|
|
63
|
+
|
|
64
|
+
Returns
|
|
65
|
+
-------
|
|
66
|
+
str
|
|
67
|
+
The group chat definition string.
|
|
68
|
+
str
|
|
69
|
+
The group chat name.
|
|
70
|
+
Optional[str]
|
|
71
|
+
The custom selection method name and content if any.
|
|
72
|
+
"""
|
|
73
|
+
agent_name = agent_names[agent.id]
|
|
74
|
+
group_chat_name = f"{agent_name}_group_chat"
|
|
75
|
+
group_members_str = ", ".join(
|
|
76
|
+
agent_names[member.id] for member in group_members
|
|
77
|
+
)
|
|
78
|
+
group_chat_string = "\n" + f"{group_chat_name} = GroupChat(" + "\n"
|
|
79
|
+
group_chat_string += f" agents=[{group_members_str}]," + "\n"
|
|
80
|
+
group_chat_string += (
|
|
81
|
+
f" enable_clear_history={agent.data.enable_clear_history}," + "\n"
|
|
82
|
+
)
|
|
83
|
+
group_chat_string += (
|
|
84
|
+
f" send_introductions={agent.data.send_introductions}," + "\n"
|
|
85
|
+
)
|
|
86
|
+
group_chat_string += " messages=[]," + "\n"
|
|
87
|
+
if agent.data.max_round is not None:
|
|
88
|
+
group_chat_string += f" max_round={agent.data.max_round}," + "\n"
|
|
89
|
+
if agent.data.admin_name:
|
|
90
|
+
group_chat_string += f' admin_name="{agent.data.admin_name}",' + "\n"
|
|
91
|
+
extra_group_chat_string, method_name_and_content = (
|
|
92
|
+
_get_group_chat_speakers_string(agent, agent_names)
|
|
93
|
+
)
|
|
94
|
+
custom_selection_method: Optional[str] = None
|
|
95
|
+
group_chat_string += extra_group_chat_string
|
|
96
|
+
group_chat_string += ")\n\n"
|
|
97
|
+
if method_name_and_content:
|
|
98
|
+
method_name, method_content = method_name_and_content
|
|
99
|
+
custom_selection_method = get_method_string(
|
|
100
|
+
"custom_speaker_selection",
|
|
101
|
+
method_name,
|
|
102
|
+
method_content,
|
|
103
|
+
)
|
|
104
|
+
return group_chat_string, group_chat_name, custom_selection_method
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _get_group_chat_speakers_string(
|
|
108
|
+
agent: WaldiezGroupManager, agent_names: Dict[str, str]
|
|
109
|
+
) -> Tuple[str, Optional[Tuple[str, str]]]:
|
|
110
|
+
"""Get the group chat speakers string.
|
|
111
|
+
|
|
112
|
+
Parameters
|
|
113
|
+
----------
|
|
114
|
+
agent : WaldiezGroupManager
|
|
115
|
+
The agent.
|
|
116
|
+
agent_names : Dict[str, str]
|
|
117
|
+
The agent names.
|
|
118
|
+
|
|
119
|
+
Returns
|
|
120
|
+
-------
|
|
121
|
+
str
|
|
122
|
+
The group chat speakers string.
|
|
123
|
+
Optional[Tuple[str, str]]
|
|
124
|
+
The custom selection method name and content if any.
|
|
125
|
+
"""
|
|
126
|
+
speakers_string = ""
|
|
127
|
+
method_name_and_content: Optional[Tuple[str, str]] = None
|
|
128
|
+
if agent.data.speakers.max_retries_for_selecting is not None:
|
|
129
|
+
speakers_string += (
|
|
130
|
+
" max_retries_for_selecting_speaker="
|
|
131
|
+
f"{agent.data.speakers.max_retries_for_selecting},"
|
|
132
|
+
"\n"
|
|
133
|
+
)
|
|
134
|
+
if agent.data.speakers.selection_method != "custom":
|
|
135
|
+
speakers_string += (
|
|
136
|
+
" speaker_selection_method="
|
|
137
|
+
f'"{agent.data.speakers.selection_method}",'
|
|
138
|
+
"\n"
|
|
139
|
+
)
|
|
140
|
+
else:
|
|
141
|
+
agent_name = agent_names[agent.id]
|
|
142
|
+
method_name = f"custom_speaker_selection_method_{agent_name}"
|
|
143
|
+
method_name_and_content = (
|
|
144
|
+
method_name,
|
|
145
|
+
agent.data.speakers.custom_method_string or "",
|
|
146
|
+
)
|
|
147
|
+
speakers_string += f" speaker_selection_method={method_name}," "\n"
|
|
148
|
+
# selection_mode == "repeat":
|
|
149
|
+
if agent.data.speakers.selection_mode == "repeat":
|
|
150
|
+
speakers_string += _get_speakers_selection_repeat_string(
|
|
151
|
+
agent, agent_names
|
|
152
|
+
)
|
|
153
|
+
# selection_mode == "transition":
|
|
154
|
+
if (
|
|
155
|
+
agent.data.speakers.selection_mode == "transition"
|
|
156
|
+
and agent.data.speakers.allowed_or_disallowed_transitions
|
|
157
|
+
):
|
|
158
|
+
speakers_string += _get_speakers_selection_transition_string(
|
|
159
|
+
agent, agent_names
|
|
160
|
+
)
|
|
161
|
+
speakers_string = speakers_string.replace('"None"', "None")
|
|
162
|
+
return speakers_string, method_name_and_content
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _get_speakers_selection_repeat_string(
|
|
166
|
+
agent: WaldiezGroupManager, agent_names: Dict[str, str]
|
|
167
|
+
) -> str:
|
|
168
|
+
speakers_string = ""
|
|
169
|
+
if isinstance(agent.data.speakers.allow_repeat, bool):
|
|
170
|
+
speakers_string += (
|
|
171
|
+
" allow_repeat_speaker="
|
|
172
|
+
f"{agent.data.speakers.allow_repeat},"
|
|
173
|
+
"\n"
|
|
174
|
+
)
|
|
175
|
+
elif isinstance(agent.data.speakers.allow_repeat, list):
|
|
176
|
+
# get the names of the agents
|
|
177
|
+
allow_repeat = ", ".join(
|
|
178
|
+
agent_names[agent_id]
|
|
179
|
+
for agent_id in agent.data.speakers.allow_repeat
|
|
180
|
+
)
|
|
181
|
+
speakers_string += f" allow_repeat=[{allow_repeat}]," "\n"
|
|
182
|
+
return speakers_string
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def _get_speakers_selection_transition_string(
|
|
186
|
+
agent: WaldiezGroupManager, agent_names: Dict[str, str]
|
|
187
|
+
) -> str:
|
|
188
|
+
speakers_string = ""
|
|
189
|
+
allowed_or_disallowed_speaker_transitions = {}
|
|
190
|
+
for (
|
|
191
|
+
agent_id,
|
|
192
|
+
transitions,
|
|
193
|
+
) in agent.data.speakers.allowed_or_disallowed_transitions.items():
|
|
194
|
+
allowed_or_disallowed_speaker_transitions[agent_names[agent_id]] = [
|
|
195
|
+
agent_names[transition] for transition in transitions
|
|
196
|
+
]
|
|
197
|
+
transitions_string = get_object_string(
|
|
198
|
+
allowed_or_disallowed_speaker_transitions, 1
|
|
199
|
+
)
|
|
200
|
+
transitions_string = transitions_string.replace('"', "").replace("'", "")
|
|
201
|
+
speakers_string += (
|
|
202
|
+
" allowed_or_disallowed_speaker_transitions="
|
|
203
|
+
f"{transitions_string}," + "\n"
|
|
204
|
+
)
|
|
205
|
+
speakers_string += (
|
|
206
|
+
" speaker_transitions_type="
|
|
207
|
+
f'"{agent.data.speakers.transitions_type}",' + "\n"
|
|
208
|
+
)
|
|
209
|
+
return speakers_string
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""Get an agent's llm config argument."""
|
|
2
|
+
|
|
3
|
+
from typing import Dict, List, Tuple
|
|
4
|
+
|
|
5
|
+
from waldiez.models import WaldiezAgent, WaldiezModel
|
|
6
|
+
|
|
7
|
+
from ..models import export_agent_models
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def get_agent_llm_config(
|
|
11
|
+
agent: WaldiezAgent,
|
|
12
|
+
agent_name: str,
|
|
13
|
+
all_models: List[WaldiezModel],
|
|
14
|
+
model_names: Dict[str, str],
|
|
15
|
+
) -> Tuple[str, str]:
|
|
16
|
+
"""Get the llm config argument string for one agent.
|
|
17
|
+
|
|
18
|
+
Parameters
|
|
19
|
+
----------
|
|
20
|
+
agent : WaldiezAgent
|
|
21
|
+
The agent.
|
|
22
|
+
agent_name : str
|
|
23
|
+
The name of the agent.
|
|
24
|
+
all_models : List[WaldiezModel]
|
|
25
|
+
All the models in the flow.
|
|
26
|
+
model_names : Dict[str, str]
|
|
27
|
+
A mapping of model id to model name.
|
|
28
|
+
|
|
29
|
+
Returns
|
|
30
|
+
-------
|
|
31
|
+
Tuple[str, str]
|
|
32
|
+
The llm config argument string and extra content
|
|
33
|
+
to use before the argument (the variable definition if needed).
|
|
34
|
+
"""
|
|
35
|
+
content_before = ""
|
|
36
|
+
if not agent.data.model_ids:
|
|
37
|
+
# no models
|
|
38
|
+
return "False", content_before
|
|
39
|
+
if len(agent.data.model_ids) == 1:
|
|
40
|
+
# one model
|
|
41
|
+
model_id = agent.data.model_ids[0]
|
|
42
|
+
model_name = model_names[model_id]
|
|
43
|
+
return f"{model_name}_llm_config", content_before
|
|
44
|
+
arg = f"{agent_name}_llm_config"
|
|
45
|
+
content_before = "\n" + (
|
|
46
|
+
export_agent_models(
|
|
47
|
+
agent_model_ids=agent.data.model_ids,
|
|
48
|
+
all_models=all_models,
|
|
49
|
+
agent_name=agent_name,
|
|
50
|
+
)
|
|
51
|
+
+ "\n"
|
|
52
|
+
)
|
|
53
|
+
return arg, content_before
|