waldiez 0.2.1__py3-none-any.whl → 0.3.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 +2 -0
- waldiez/__main__.py +2 -0
- waldiez/_version.py +3 -1
- waldiez/cli.py +13 -3
- waldiez/cli_extras.py +25 -27
- waldiez/conflict_checker.py +4 -3
- waldiez/exporter.py +28 -105
- waldiez/exporting/__init__.py +8 -9
- waldiez/exporting/agent/__init__.py +7 -0
- waldiez/exporting/agent/agent_exporter.py +279 -0
- waldiez/exporting/agent/utils/__init__.py +23 -0
- waldiez/exporting/agent/utils/agent_class_name.py +34 -0
- waldiez/exporting/agent/utils/agent_imports.py +50 -0
- waldiez/exporting/{agents → agent/utils}/code_execution.py +9 -11
- waldiez/exporting/{agents → agent/utils}/group_manager.py +47 -35
- waldiez/exporting/{agents → agent/utils}/rag_user/__init__.py +2 -0
- waldiez/exporting/{agents → agent/utils}/rag_user/chroma_utils.py +22 -17
- waldiez/exporting/{agents → agent/utils}/rag_user/mongo_utils.py +14 -10
- waldiez/exporting/{agents → agent/utils}/rag_user/pgvector_utils.py +12 -8
- waldiez/exporting/{agents → agent/utils}/rag_user/qdrant_utils.py +11 -8
- waldiez/exporting/{agents → agent/utils}/rag_user/rag_user.py +78 -55
- waldiez/exporting/{agents → agent/utils}/rag_user/vector_db.py +10 -8
- waldiez/exporting/agent/utils/swarm_agent.py +463 -0
- waldiez/exporting/{agents → agent/utils}/teachability.py +10 -6
- waldiez/exporting/{agents → agent/utils}/termination_message.py +7 -8
- waldiez/exporting/base/__init__.py +25 -0
- waldiez/exporting/base/agent_position.py +75 -0
- waldiez/exporting/base/base_exporter.py +118 -0
- waldiez/exporting/base/export_position.py +48 -0
- waldiez/exporting/base/import_position.py +23 -0
- waldiez/exporting/base/mixin.py +134 -0
- waldiez/exporting/base/utils/__init__.py +18 -0
- waldiez/exporting/{utils → base/utils}/comments.py +12 -55
- waldiez/exporting/{utils → base/utils}/naming.py +14 -4
- waldiez/exporting/base/utils/path_check.py +68 -0
- waldiez/exporting/{utils/object_string.py → base/utils/to_string.py} +21 -20
- waldiez/exporting/chats/__init__.py +5 -12
- waldiez/exporting/chats/chats_exporter.py +240 -0
- waldiez/exporting/chats/utils/__init__.py +15 -0
- waldiez/exporting/chats/utils/common.py +81 -0
- waldiez/exporting/chats/{nested.py → utils/nested.py} +125 -86
- waldiez/exporting/chats/utils/sequential.py +244 -0
- waldiez/exporting/chats/utils/single_chat.py +313 -0
- waldiez/exporting/chats/utils/swarm.py +207 -0
- waldiez/exporting/flow/__init__.py +5 -3
- waldiez/exporting/flow/flow_exporter.py +503 -0
- waldiez/exporting/flow/utils/__init__.py +47 -0
- waldiez/exporting/flow/utils/agent_utils.py +204 -0
- waldiez/exporting/flow/utils/chat_utils.py +71 -0
- waldiez/exporting/flow/utils/def_main.py +62 -0
- waldiez/exporting/flow/utils/flow_content.py +112 -0
- waldiez/exporting/flow/utils/flow_names.py +115 -0
- waldiez/exporting/flow/utils/importing_utils.py +179 -0
- waldiez/exporting/{utils → flow/utils}/logging_utils.py +34 -31
- waldiez/exporting/models/__init__.py +7 -242
- waldiez/exporting/models/models_exporter.py +192 -0
- waldiez/exporting/models/utils.py +166 -0
- waldiez/exporting/skills/__init__.py +7 -161
- waldiez/exporting/skills/skills_exporter.py +169 -0
- waldiez/exporting/skills/utils.py +281 -0
- waldiez/models/__init__.py +25 -7
- waldiez/models/agents/__init__.py +70 -0
- waldiez/models/agents/agent/__init__.py +11 -1
- waldiez/models/agents/agent/agent.py +9 -4
- waldiez/models/agents/agent/agent_data.py +3 -1
- waldiez/models/agents/agent/code_execution.py +2 -0
- waldiez/models/agents/agent/linked_skill.py +2 -0
- waldiez/models/agents/agent/nested_chat.py +2 -0
- waldiez/models/agents/agent/teachability.py +2 -0
- waldiez/models/agents/agent/termination_message.py +49 -13
- waldiez/models/agents/agents.py +15 -3
- waldiez/models/agents/assistant/__init__.py +2 -0
- waldiez/models/agents/assistant/assistant.py +2 -0
- waldiez/models/agents/assistant/assistant_data.py +2 -0
- waldiez/models/agents/group_manager/__init__.py +9 -1
- waldiez/models/agents/group_manager/group_manager.py +2 -0
- waldiez/models/agents/group_manager/group_manager_data.py +2 -0
- waldiez/models/agents/group_manager/speakers.py +49 -13
- waldiez/models/agents/rag_user/__init__.py +21 -4
- waldiez/models/agents/rag_user/rag_user.py +3 -1
- waldiez/models/agents/rag_user/rag_user_data.py +2 -0
- waldiez/models/agents/rag_user/retrieve_config.py +268 -17
- waldiez/models/agents/rag_user/vector_db_config.py +5 -3
- waldiez/models/agents/swarm_agent/__init__.py +49 -0
- waldiez/models/agents/swarm_agent/after_work.py +178 -0
- waldiez/models/agents/swarm_agent/on_condition.py +103 -0
- waldiez/models/agents/swarm_agent/on_condition_available.py +140 -0
- waldiez/models/agents/swarm_agent/on_condition_target.py +40 -0
- waldiez/models/agents/swarm_agent/swarm_agent.py +107 -0
- waldiez/models/agents/swarm_agent/swarm_agent_data.py +125 -0
- waldiez/models/agents/swarm_agent/update_system_message.py +144 -0
- waldiez/models/agents/user_proxy/__init__.py +2 -0
- waldiez/models/agents/user_proxy/user_proxy.py +2 -0
- waldiez/models/agents/user_proxy/user_proxy_data.py +2 -0
- waldiez/models/chat/__init__.py +21 -3
- waldiez/models/chat/chat.py +241 -7
- waldiez/models/chat/chat_data.py +192 -48
- waldiez/models/chat/chat_message.py +153 -144
- waldiez/models/chat/chat_nested.py +33 -53
- waldiez/models/chat/chat_summary.py +2 -0
- waldiez/models/common/__init__.py +6 -6
- waldiez/models/common/base.py +4 -1
- waldiez/models/common/method_utils.py +163 -83
- waldiez/models/flow/__init__.py +2 -0
- waldiez/models/flow/flow.py +176 -40
- waldiez/models/flow/flow_data.py +63 -2
- waldiez/models/flow/utils.py +172 -0
- waldiez/models/model/__init__.py +2 -0
- waldiez/models/model/model.py +25 -6
- waldiez/models/model/model_data.py +3 -1
- waldiez/models/skill/__init__.py +4 -1
- waldiez/models/skill/skill.py +30 -2
- waldiez/models/skill/skill_data.py +2 -0
- waldiez/models/waldiez.py +28 -4
- waldiez/runner.py +142 -228
- waldiez/running/__init__.py +33 -0
- waldiez/running/environment.py +83 -0
- waldiez/running/gen_seq_diagram.py +185 -0
- waldiez/running/running.py +300 -0
- {waldiez-0.2.1.dist-info → waldiez-0.3.0.dist-info}/METADATA +36 -30
- waldiez-0.3.0.dist-info/RECORD +125 -0
- waldiez-0.3.0.dist-info/licenses/LICENSE +201 -0
- waldiez/exporting/agents/__init__.py +0 -5
- waldiez/exporting/agents/agent.py +0 -236
- waldiez/exporting/agents/agent_skills.py +0 -67
- waldiez/exporting/agents/llm_config.py +0 -53
- waldiez/exporting/chats/chats.py +0 -46
- waldiez/exporting/chats/helpers.py +0 -420
- waldiez/exporting/flow/def_main.py +0 -32
- waldiez/exporting/flow/flow.py +0 -189
- waldiez/exporting/utils/__init__.py +0 -36
- waldiez/exporting/utils/importing.py +0 -265
- waldiez/exporting/utils/method_utils.py +0 -35
- waldiez/exporting/utils/path_check.py +0 -51
- waldiez-0.2.1.dist-info/RECORD +0 -92
- waldiez-0.2.1.dist-info/licenses/LICENSE +0 -21
- {waldiez-0.2.1.dist-info → waldiez-0.3.0.dist-info}/WHEEL +0 -0
- {waldiez-0.2.1.dist-info → waldiez-0.3.0.dist-info}/entry_points.txt +0 -0
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0.
|
|
2
|
+
# Copyright (c) 2024 - 2025 Waldiez and contributors.
|
|
3
|
+
"""Utility functions for exporting sequential chats."""
|
|
4
|
+
|
|
5
|
+
from typing import Callable, Dict, List, Tuple
|
|
6
|
+
|
|
7
|
+
from waldiez.models import (
|
|
8
|
+
WaldiezAgent,
|
|
9
|
+
WaldiezChat,
|
|
10
|
+
WaldiezChatMessage,
|
|
11
|
+
WaldiezRagUser,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
from .common import get_chat_message_string, update_summary_chat_args
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def export_sequential_chat(
|
|
18
|
+
main_chats: List[Tuple[WaldiezChat, WaldiezAgent, WaldiezAgent]],
|
|
19
|
+
chat_names: Dict[str, str],
|
|
20
|
+
agent_names: Dict[str, str],
|
|
21
|
+
serializer: Callable[..., str],
|
|
22
|
+
string_escape: Callable[[str], str],
|
|
23
|
+
tabs: int,
|
|
24
|
+
is_async: bool,
|
|
25
|
+
) -> Tuple[str, str]:
|
|
26
|
+
"""Get the chats content, when there are more than one chats in the flow.
|
|
27
|
+
|
|
28
|
+
Parameters
|
|
29
|
+
----------
|
|
30
|
+
main_chats : List[Tuple[WaldiezChat, WaldiezAgent, WaldiezAgent]]
|
|
31
|
+
The main chats.
|
|
32
|
+
chat_names : Dict[str, str]
|
|
33
|
+
A mapping of chat id to chat name.
|
|
34
|
+
agent_names : Dict[str, str]
|
|
35
|
+
A mapping of agent id to agent name.
|
|
36
|
+
serializer : Callable[..., str]
|
|
37
|
+
The serializer function to escape quotes in a string.
|
|
38
|
+
string_escape : Callable[[str], str]
|
|
39
|
+
The string escape function.
|
|
40
|
+
tabs : int
|
|
41
|
+
The number of tabs to use for indentation.
|
|
42
|
+
is_async : bool
|
|
43
|
+
Whether the chat is asynchronous.
|
|
44
|
+
|
|
45
|
+
Returns
|
|
46
|
+
-------
|
|
47
|
+
Tuple[str, str]
|
|
48
|
+
The main chats content and additional methods string if any.
|
|
49
|
+
|
|
50
|
+
Example
|
|
51
|
+
-------
|
|
52
|
+
```python
|
|
53
|
+
>>> from waldiez.models import (
|
|
54
|
+
... WaldiezAgent,
|
|
55
|
+
... WaldiezChat,
|
|
56
|
+
... WaldiezChatData,
|
|
57
|
+
... WaldiezChatMessage,
|
|
58
|
+
... )
|
|
59
|
+
>>> chat1 = WaldiezChat(
|
|
60
|
+
... id="wc-1",
|
|
61
|
+
... name="chat1",
|
|
62
|
+
... description="A chat between two agents.",
|
|
63
|
+
... tags=["chat", "chat1"],
|
|
64
|
+
... requirements=[],
|
|
65
|
+
... data=WaldiezChatData(
|
|
66
|
+
... sender="wa-1",
|
|
67
|
+
... recipient="wa-2",
|
|
68
|
+
... position=0,
|
|
69
|
+
... message=WaldiezChatMessage(
|
|
70
|
+
... type="string",
|
|
71
|
+
... content="Hello, how are you?",
|
|
72
|
+
... ),
|
|
73
|
+
... ),
|
|
74
|
+
... )
|
|
75
|
+
>>> chat2 = WaldiezChat(
|
|
76
|
+
... id="wc-2",
|
|
77
|
+
... name="chat2",
|
|
78
|
+
... description="A chat between two agents.",
|
|
79
|
+
... tags=["chat", "chat2"],
|
|
80
|
+
... requirements=[],
|
|
81
|
+
... data=WaldiezChatData(
|
|
82
|
+
... sender="wa-2",
|
|
83
|
+
... recipient="wa-1",
|
|
84
|
+
... position=1,
|
|
85
|
+
... message=WaldiezChatMessage(
|
|
86
|
+
... type="string",
|
|
87
|
+
... content="I am good, thank you. How about you?",
|
|
88
|
+
... ),
|
|
89
|
+
... ),
|
|
90
|
+
... )
|
|
91
|
+
>>> agent_names = {"wa-1": "agent1", "wa-2": "agent2"}
|
|
92
|
+
>>> chat_names = {"wc-1": "chat1", "wc-2": "chat2"}
|
|
93
|
+
>>> serializer = lambda x: x.replace('"', "\"").replace("\n", "\\n")
|
|
94
|
+
>>> export_sequential_chat(
|
|
95
|
+
... main_chats=[(chat1, agent1, agent2), (chat2, agent2, agent1)],
|
|
96
|
+
... chat_names=chat_names,
|
|
97
|
+
... agent_names=agent_names,
|
|
98
|
+
... serializer=serializer,
|
|
99
|
+
... tabs=0,
|
|
100
|
+
... is_async=False,
|
|
101
|
+
... )
|
|
102
|
+
results = initiate_chats([
|
|
103
|
+
{
|
|
104
|
+
"sender": agent1,
|
|
105
|
+
"recipient": agent2,
|
|
106
|
+
"message": "Hello, how are you?",
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
"sender": agent2,
|
|
110
|
+
"recipient": agent1,
|
|
111
|
+
"message": "I am good, thank you. How about you?",
|
|
112
|
+
},
|
|
113
|
+
])
|
|
114
|
+
```
|
|
115
|
+
"""
|
|
116
|
+
tab = " " * tabs if tabs > 0 else ""
|
|
117
|
+
content = "\n"
|
|
118
|
+
additional_methods_string = ""
|
|
119
|
+
content += _get_initiate_chats_line(tab, is_async)
|
|
120
|
+
for chat, sender, recipient in main_chats:
|
|
121
|
+
chat_string, additional_methods = _get_chat_dict_string(
|
|
122
|
+
chat=chat,
|
|
123
|
+
chat_names=chat_names,
|
|
124
|
+
sender=sender,
|
|
125
|
+
recipient=recipient,
|
|
126
|
+
agent_names=agent_names,
|
|
127
|
+
serializer=serializer,
|
|
128
|
+
string_escape=string_escape,
|
|
129
|
+
tabs=tabs + 1,
|
|
130
|
+
)
|
|
131
|
+
additional_methods_string += additional_methods
|
|
132
|
+
content += "\n" + f"{tab} {chat_string}"
|
|
133
|
+
content += "\n" + " " * tabs + "])\n"
|
|
134
|
+
return content, additional_methods_string
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
# pylint: disable=too-many-locals
|
|
138
|
+
def _get_chat_dict_string(
|
|
139
|
+
chat: WaldiezChat,
|
|
140
|
+
sender: WaldiezAgent,
|
|
141
|
+
recipient: WaldiezAgent,
|
|
142
|
+
chat_names: Dict[str, str],
|
|
143
|
+
agent_names: Dict[str, str],
|
|
144
|
+
serializer: Callable[..., str],
|
|
145
|
+
string_escape: Callable[[str], str],
|
|
146
|
+
tabs: int,
|
|
147
|
+
) -> Tuple[str, str]:
|
|
148
|
+
"""Get a chat dictionary string.
|
|
149
|
+
|
|
150
|
+
If the chat message is a separate method and not a string or a lambda,
|
|
151
|
+
we return the method string (definition and body) as well as the rest
|
|
152
|
+
of the arguments.
|
|
153
|
+
|
|
154
|
+
Parameters
|
|
155
|
+
----------
|
|
156
|
+
chat : WaldiezChat
|
|
157
|
+
The chat.
|
|
158
|
+
sender : WaldiezAgent
|
|
159
|
+
The sender.
|
|
160
|
+
recipient : WaldiezAgent
|
|
161
|
+
The recipient.
|
|
162
|
+
chat_names : Dict[str, str]
|
|
163
|
+
A mapping of chat id to chat name.
|
|
164
|
+
agent_names : Dict[str, str]
|
|
165
|
+
A mapping of agent id to agent name.
|
|
166
|
+
serializer : Callable[[str], str]
|
|
167
|
+
The function to serialize the dictionaries or lists.
|
|
168
|
+
string_escape : Callable[[str], str]
|
|
169
|
+
The function to escape the string.
|
|
170
|
+
tabs : int
|
|
171
|
+
The number of tabs to use for indentation.
|
|
172
|
+
|
|
173
|
+
Returns
|
|
174
|
+
-------
|
|
175
|
+
Tuple[str, str]
|
|
176
|
+
The chat dictionary string and additional methods string if any.
|
|
177
|
+
"""
|
|
178
|
+
tab = " " * tabs
|
|
179
|
+
chat_args = chat.get_chat_args(for_queue=True, sender=sender)
|
|
180
|
+
chat_args = update_summary_chat_args(chat_args, string_escape)
|
|
181
|
+
chat_string = "{"
|
|
182
|
+
chat_string += "\n" + f'{tab} "sender": {agent_names[sender.id]},'
|
|
183
|
+
chat_string += "\n" + f'{tab} "recipient": {agent_names[recipient.id]},'
|
|
184
|
+
additional_methods_string = ""
|
|
185
|
+
for key, value in chat_args.items():
|
|
186
|
+
if isinstance(value, str):
|
|
187
|
+
chat_string += "\n" + f'{tab} "{key}": "{value}",'
|
|
188
|
+
elif isinstance(value, dict):
|
|
189
|
+
chat_string += (
|
|
190
|
+
"\n" + f'{tab} "{key}": {serializer(value, tabs=tabs + 1)},'
|
|
191
|
+
)
|
|
192
|
+
else:
|
|
193
|
+
chat_string += "\n" + f'{tab} "{key}": {value},'
|
|
194
|
+
if (
|
|
195
|
+
sender.agent_type == "rag_user"
|
|
196
|
+
and isinstance(sender, WaldiezRagUser)
|
|
197
|
+
and chat.message.type == "rag_message_generator"
|
|
198
|
+
):
|
|
199
|
+
message = f"{agent_names[sender.id]}.message_generator"
|
|
200
|
+
chat_string += "\n" + f'{tab} "message": {message},'
|
|
201
|
+
chat_string += "\n" + tab + "},"
|
|
202
|
+
return chat_string, additional_methods_string
|
|
203
|
+
message, method_content = get_chat_message_string(
|
|
204
|
+
sender=sender,
|
|
205
|
+
chat=chat,
|
|
206
|
+
chat_names=chat_names,
|
|
207
|
+
string_escape=string_escape,
|
|
208
|
+
)
|
|
209
|
+
if message and isinstance(chat.data.message, WaldiezChatMessage):
|
|
210
|
+
message = string_escape(message)
|
|
211
|
+
if chat.data.message.type == "method":
|
|
212
|
+
if method_content:
|
|
213
|
+
additional_methods_string += "\n" + method_content
|
|
214
|
+
chat_string += "\n" + f'{tab} "message": {message},'
|
|
215
|
+
elif chat.data.message.type == "string" and chat.data.message.content:
|
|
216
|
+
chat_string += "\n" + f'{tab} "message": "{message}",'
|
|
217
|
+
chat_string += "\n" + tab + "},"
|
|
218
|
+
return chat_string, additional_methods_string
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def _get_initiate_chats_line(
|
|
222
|
+
tab: str,
|
|
223
|
+
is_async: bool,
|
|
224
|
+
) -> str:
|
|
225
|
+
"""Get the initiate chats line.
|
|
226
|
+
|
|
227
|
+
Parameters
|
|
228
|
+
----------
|
|
229
|
+
tab : str
|
|
230
|
+
The tab string.
|
|
231
|
+
is_async : bool
|
|
232
|
+
Whether the chat is asynchronous.
|
|
233
|
+
|
|
234
|
+
Returns
|
|
235
|
+
-------
|
|
236
|
+
str
|
|
237
|
+
The initiate chats line.
|
|
238
|
+
"""
|
|
239
|
+
results_is = f"{tab}results = "
|
|
240
|
+
initiate = "initiate_chats"
|
|
241
|
+
if is_async:
|
|
242
|
+
results_is += "await "
|
|
243
|
+
initiate = "a_initiate_chats"
|
|
244
|
+
return results_is + initiate + "(["
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0.
|
|
2
|
+
# Copyright (c) 2024 - 2025 Waldiez and contributors.
|
|
3
|
+
# flake8: noqa E501
|
|
4
|
+
# pylint: disable=line-too-long
|
|
5
|
+
"""Utilities for exporting a single chat in a flow."""
|
|
6
|
+
|
|
7
|
+
from typing import Any, Callable, Dict, Optional, Tuple
|
|
8
|
+
|
|
9
|
+
from waldiez.models import (
|
|
10
|
+
WaldiezAgent,
|
|
11
|
+
WaldiezChat,
|
|
12
|
+
WaldiezChatMessage,
|
|
13
|
+
WaldiezRagUser,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
from .common import get_chat_message_string, update_summary_chat_args
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def export_single_chat(
|
|
20
|
+
sender: WaldiezAgent,
|
|
21
|
+
recipient: WaldiezAgent,
|
|
22
|
+
chat: WaldiezChat,
|
|
23
|
+
agent_names: Dict[str, str],
|
|
24
|
+
chat_names: Dict[str, str],
|
|
25
|
+
serializer: Callable[[str], str],
|
|
26
|
+
string_escape: Callable[[str], str],
|
|
27
|
+
tabs: int,
|
|
28
|
+
is_async: bool,
|
|
29
|
+
) -> Tuple[str, str]:
|
|
30
|
+
"""Get the chat string when there is only one chat in the flow.
|
|
31
|
+
|
|
32
|
+
Parameters
|
|
33
|
+
----------
|
|
34
|
+
sender : WaldiezAgent
|
|
35
|
+
The sender.
|
|
36
|
+
recipient : WaldiezAgent
|
|
37
|
+
The recipient.
|
|
38
|
+
chat : WaldiezChat
|
|
39
|
+
The chat.
|
|
40
|
+
agent_names : Dict[str, str]
|
|
41
|
+
A mapping of agent id to agent name.
|
|
42
|
+
chat_names : Dict[str, str]
|
|
43
|
+
A mapping of chat id to chat name.
|
|
44
|
+
serializer : Callable[[str], str]
|
|
45
|
+
The serializer function to escape quotes in a string.
|
|
46
|
+
string_escape : Callable[[str], str]
|
|
47
|
+
The string escape function.
|
|
48
|
+
tabs : int
|
|
49
|
+
The number of tabs to use for indentation.
|
|
50
|
+
is_async : bool
|
|
51
|
+
Whether the chat is asynchronous.
|
|
52
|
+
Returns
|
|
53
|
+
-------
|
|
54
|
+
Tuple[str, str]
|
|
55
|
+
The chat string and additional methods string if any
|
|
56
|
+
|
|
57
|
+
Example
|
|
58
|
+
-------
|
|
59
|
+
```python
|
|
60
|
+
>>> from waldiez.models import WaldiezAgent, WaldiezChat, WaldiezChatData, WaldiezChatMessage
|
|
61
|
+
>>> chat = WaldiezChat(
|
|
62
|
+
... id="wc-1",
|
|
63
|
+
... name="chat1",
|
|
64
|
+
... description="A chat between two agents.",
|
|
65
|
+
... tags=["chat", "chat1"],
|
|
66
|
+
... requirements=[],
|
|
67
|
+
... data=WaldiezChatData(
|
|
68
|
+
... sender="wa-1",
|
|
69
|
+
... recipient="wa-2",
|
|
70
|
+
... message=WaldiezChatMessage(
|
|
71
|
+
... type="string",
|
|
72
|
+
... content="Hello, how are you?",
|
|
73
|
+
... ),
|
|
74
|
+
... ),
|
|
75
|
+
... )
|
|
76
|
+
>>> agent_names = {"wa-1": "agent1", "wa-2": "agent2"}
|
|
77
|
+
>>> chat_names = {"wc-1": "chat1"}
|
|
78
|
+
>>> export_single_chat_string(
|
|
79
|
+
... sender=agent1,
|
|
80
|
+
... recipient=agent2,
|
|
81
|
+
... chat=chat,
|
|
82
|
+
... agent_names=agent_names,
|
|
83
|
+
... chat_names=chat_names,
|
|
84
|
+
... tabs=0,
|
|
85
|
+
... )
|
|
86
|
+
agent1.initiate_chat(
|
|
87
|
+
agent2,
|
|
88
|
+
message="Hello, how are you?",
|
|
89
|
+
)
|
|
90
|
+
```
|
|
91
|
+
"""
|
|
92
|
+
tab = " " * tabs if tabs > 0 else ""
|
|
93
|
+
chat_args = chat.get_chat_args(for_queue=False, sender=sender)
|
|
94
|
+
chat_args = update_summary_chat_args(chat_args, string_escape)
|
|
95
|
+
if not chat_args:
|
|
96
|
+
return get_empty_simple_chat_string(
|
|
97
|
+
chat=chat,
|
|
98
|
+
sender=sender,
|
|
99
|
+
recipient=recipient,
|
|
100
|
+
agent_names=agent_names,
|
|
101
|
+
string_escape=string_escape,
|
|
102
|
+
tab=tab,
|
|
103
|
+
is_async=is_async,
|
|
104
|
+
)
|
|
105
|
+
return get_simple_chat_string(
|
|
106
|
+
chat=chat,
|
|
107
|
+
chat_args=chat_args,
|
|
108
|
+
sender=sender,
|
|
109
|
+
recipient=recipient,
|
|
110
|
+
agent_names=agent_names,
|
|
111
|
+
chat_names=chat_names,
|
|
112
|
+
serializer=serializer,
|
|
113
|
+
string_escape=string_escape,
|
|
114
|
+
tabs=tabs,
|
|
115
|
+
is_async=is_async,
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
# pylint: disable=too-many-locals
|
|
120
|
+
def get_simple_chat_string(
|
|
121
|
+
chat: WaldiezChat,
|
|
122
|
+
sender: WaldiezAgent,
|
|
123
|
+
recipient: WaldiezAgent,
|
|
124
|
+
agent_names: Dict[str, str],
|
|
125
|
+
chat_names: Dict[str, str],
|
|
126
|
+
chat_args: Dict[str, Any],
|
|
127
|
+
serializer: Callable[..., str],
|
|
128
|
+
string_escape: Callable[[str], str],
|
|
129
|
+
tabs: int,
|
|
130
|
+
is_async: bool,
|
|
131
|
+
) -> Tuple[str, str]:
|
|
132
|
+
"""Get the chat string when there are chat arguments.
|
|
133
|
+
|
|
134
|
+
Parameters
|
|
135
|
+
----------
|
|
136
|
+
chat : WaldiezChat
|
|
137
|
+
The chat.
|
|
138
|
+
sender : WaldiezAgent
|
|
139
|
+
The sender.
|
|
140
|
+
recipient : WaldiezAgent
|
|
141
|
+
The recipient.
|
|
142
|
+
agent_names : Dict[str, str]
|
|
143
|
+
A mapping of agent id to agent name.
|
|
144
|
+
chat_names : Dict[str, str]
|
|
145
|
+
A mapping of chat id to chat name.
|
|
146
|
+
chat_args : Dict[str, Any]
|
|
147
|
+
The chat arguments.
|
|
148
|
+
serializer : Callable[[str], str]
|
|
149
|
+
The serializer function to escape quotes in a string.
|
|
150
|
+
string_escape : Callable[[str], str]
|
|
151
|
+
The string escape function.
|
|
152
|
+
tabs : int
|
|
153
|
+
The number of tabs to use for indentation.
|
|
154
|
+
is_async : bool
|
|
155
|
+
Whether the chat is asynchronous.
|
|
156
|
+
Returns
|
|
157
|
+
-------
|
|
158
|
+
Tuple[str, str]
|
|
159
|
+
The chat string and additional methods string if any.
|
|
160
|
+
"""
|
|
161
|
+
tab = " " * tabs
|
|
162
|
+
sender_name = agent_names[sender.id]
|
|
163
|
+
initiate = "initiate_chat"
|
|
164
|
+
if is_async:
|
|
165
|
+
sender_name = f"await {sender_name}"
|
|
166
|
+
initiate = "a_initiate_chat"
|
|
167
|
+
recipient_name = agent_names[recipient.id]
|
|
168
|
+
chat_string = "\n" + f"{tab}results = {sender_name}.{initiate}(" + "\n"
|
|
169
|
+
chat_string += f"{tab} {recipient_name},"
|
|
170
|
+
for key, value in chat_args.items():
|
|
171
|
+
if isinstance(value, str):
|
|
172
|
+
chat_string += "\n" + f'{tab} {key}="{value}",'
|
|
173
|
+
elif isinstance(value, dict):
|
|
174
|
+
chat_string += (
|
|
175
|
+
"\n" + f"{tab} {key}={serializer(value, tabs + 1)},"
|
|
176
|
+
)
|
|
177
|
+
else:
|
|
178
|
+
chat_string += "\n" + f"{tab} {key}={value},"
|
|
179
|
+
message_arg, additional_methods_string = get_chat_message(
|
|
180
|
+
tab=tab,
|
|
181
|
+
chat=chat,
|
|
182
|
+
chat_names=chat_names,
|
|
183
|
+
sender=sender,
|
|
184
|
+
sender_name=sender_name,
|
|
185
|
+
string_escape=string_escape,
|
|
186
|
+
)
|
|
187
|
+
chat_string += message_arg
|
|
188
|
+
chat_string += "\n" + f"{tab})" + "\n"
|
|
189
|
+
return chat_string, additional_methods_string
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def get_empty_simple_chat_string(
|
|
193
|
+
chat: WaldiezChat,
|
|
194
|
+
sender: WaldiezAgent,
|
|
195
|
+
recipient: WaldiezAgent,
|
|
196
|
+
agent_names: Dict[str, str],
|
|
197
|
+
string_escape: Callable[[str], str],
|
|
198
|
+
tab: str,
|
|
199
|
+
is_async: bool,
|
|
200
|
+
) -> Tuple[str, str]:
|
|
201
|
+
"""Get the chat string when there are no chat arguments.
|
|
202
|
+
|
|
203
|
+
Parameters
|
|
204
|
+
----------
|
|
205
|
+
chat : WaldiezChat
|
|
206
|
+
The chat.
|
|
207
|
+
sender : WaldiezAgent
|
|
208
|
+
The sender.
|
|
209
|
+
recipient : WaldiezAgent
|
|
210
|
+
The recipient.
|
|
211
|
+
agent_names : Dict[str, str]
|
|
212
|
+
A mapping of agent id to agent name.
|
|
213
|
+
string_escape : Callable[[str], str]
|
|
214
|
+
The string escape function.
|
|
215
|
+
tab : str
|
|
216
|
+
The tab string.
|
|
217
|
+
is_async : bool
|
|
218
|
+
Whether the chat is asynchronous.
|
|
219
|
+
Returns
|
|
220
|
+
-------
|
|
221
|
+
Tuple[str, str]
|
|
222
|
+
The chat string and additional methods string if any
|
|
223
|
+
"""
|
|
224
|
+
sender_name = agent_names[sender.id]
|
|
225
|
+
if is_async:
|
|
226
|
+
sender_name = f"await {sender_name}"
|
|
227
|
+
recipient_name = agent_names[recipient.id]
|
|
228
|
+
initiate = "a_initiate_chat" if is_async else "initiate_chat"
|
|
229
|
+
content = "\n" + f"{tab}results = {sender_name}.{initiate}(" + "\n"
|
|
230
|
+
content += f"{tab} {recipient_name}," + "\n"
|
|
231
|
+
message_arg, _ = get_chat_message(
|
|
232
|
+
tab=tab,
|
|
233
|
+
chat=chat,
|
|
234
|
+
chat_names={},
|
|
235
|
+
sender=sender,
|
|
236
|
+
sender_name=sender_name,
|
|
237
|
+
string_escape=string_escape,
|
|
238
|
+
)
|
|
239
|
+
content += message_arg
|
|
240
|
+
content += f"{tab})" + "\n"
|
|
241
|
+
return content, ""
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def get_chat_message(
|
|
245
|
+
tab: str,
|
|
246
|
+
chat: WaldiezChat,
|
|
247
|
+
chat_names: Dict[str, str],
|
|
248
|
+
sender: WaldiezAgent,
|
|
249
|
+
sender_name: str,
|
|
250
|
+
string_escape: Callable[[str], str],
|
|
251
|
+
) -> Tuple[str, str]:
|
|
252
|
+
"""Get the chat message string.
|
|
253
|
+
|
|
254
|
+
Parameters
|
|
255
|
+
----------
|
|
256
|
+
tab : str
|
|
257
|
+
The tab string.
|
|
258
|
+
chat : WaldiezChat
|
|
259
|
+
The chat.
|
|
260
|
+
chat_names : Dict[str, str]
|
|
261
|
+
A mapping of chat id to chat name.
|
|
262
|
+
sender : WaldiezAgent
|
|
263
|
+
The sender.
|
|
264
|
+
sender_name : str
|
|
265
|
+
The sender name.
|
|
266
|
+
string_escape : Callable[[str], str]
|
|
267
|
+
The string escape function.
|
|
268
|
+
|
|
269
|
+
Returns
|
|
270
|
+
-------
|
|
271
|
+
Tuple[str, str]
|
|
272
|
+
The message argument and additional methods string if any.
|
|
273
|
+
"""
|
|
274
|
+
additional_methods_string = ""
|
|
275
|
+
method_content: Optional[str] = None
|
|
276
|
+
if (
|
|
277
|
+
sender.agent_type == "rag_user"
|
|
278
|
+
and isinstance(sender, WaldiezRagUser)
|
|
279
|
+
and chat.message.type == "rag_message_generator"
|
|
280
|
+
and chat.message.use_carryover is False
|
|
281
|
+
):
|
|
282
|
+
message = f"{sender_name}.message_generator"
|
|
283
|
+
return "\n" + f"{tab} message={message},", additional_methods_string
|
|
284
|
+
message, method_content = get_chat_message_string(
|
|
285
|
+
sender=sender,
|
|
286
|
+
chat=chat,
|
|
287
|
+
chat_names=chat_names,
|
|
288
|
+
string_escape=string_escape,
|
|
289
|
+
)
|
|
290
|
+
if message and isinstance(chat.data.message, WaldiezChatMessage):
|
|
291
|
+
message = string_escape(message)
|
|
292
|
+
if chat.data.message.type == "method":
|
|
293
|
+
additional_methods_string += (
|
|
294
|
+
method_content if method_content else ""
|
|
295
|
+
)
|
|
296
|
+
return (
|
|
297
|
+
"\n" + f"{tab} message={message},",
|
|
298
|
+
additional_methods_string,
|
|
299
|
+
)
|
|
300
|
+
if chat.message.type == "string" and chat.data.message.content:
|
|
301
|
+
return (
|
|
302
|
+
"\n" + f'{tab} message="{message}",',
|
|
303
|
+
additional_methods_string,
|
|
304
|
+
)
|
|
305
|
+
if chat.message.type == "rag_message_generator":
|
|
306
|
+
additional_methods_string += (
|
|
307
|
+
method_content if method_content else ""
|
|
308
|
+
)
|
|
309
|
+
return (
|
|
310
|
+
"\n" + f"{tab} message={message},",
|
|
311
|
+
additional_methods_string,
|
|
312
|
+
)
|
|
313
|
+
return "", additional_methods_string # pragma: no cover
|