waldiez 0.4.6__py3-none-any.whl → 0.4.8__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.

Files changed (244) hide show
  1. waldiez/__init__.py +5 -5
  2. waldiez/_version.py +1 -1
  3. waldiez/cli.py +112 -73
  4. waldiez/exporter.py +61 -19
  5. waldiez/exporting/__init__.py +25 -6
  6. waldiez/exporting/agent/__init__.py +7 -3
  7. waldiez/exporting/agent/code_execution.py +114 -0
  8. waldiez/exporting/agent/exporter.py +354 -0
  9. waldiez/exporting/agent/extras/__init__.py +15 -0
  10. waldiez/exporting/agent/extras/captain_agent_extras.py +315 -0
  11. waldiez/exporting/agent/extras/group/target.py +178 -0
  12. waldiez/exporting/agent/extras/group_manager_agent_extas.py +500 -0
  13. waldiez/exporting/agent/extras/group_member_extras.py +181 -0
  14. waldiez/exporting/agent/extras/handoffs/__init__.py +19 -0
  15. waldiez/exporting/agent/extras/handoffs/after_work.py +78 -0
  16. waldiez/exporting/agent/extras/handoffs/available.py +74 -0
  17. waldiez/exporting/agent/extras/handoffs/condition.py +158 -0
  18. waldiez/exporting/agent/extras/handoffs/handoff.py +171 -0
  19. waldiez/exporting/agent/extras/handoffs/target.py +189 -0
  20. waldiez/exporting/agent/extras/rag/__init__.py +10 -0
  21. waldiez/exporting/agent/{utils/rag_user/chroma_utils.py → extras/rag/chroma_extras.py} +16 -15
  22. waldiez/exporting/agent/{utils/rag_user/mongo_utils.py → extras/rag/mongo_extras.py} +10 -10
  23. waldiez/exporting/agent/{utils/rag_user/pgvector_utils.py → extras/rag/pgvector_extras.py} +13 -13
  24. waldiez/exporting/agent/{utils/rag_user/qdrant_utils.py → extras/rag/qdrant_extras.py} +13 -13
  25. waldiez/exporting/agent/{utils/rag_user/vector_db.py → extras/rag/vector_db_extras.py} +59 -46
  26. waldiez/exporting/agent/extras/rag_user_proxy_agent_extras.py +245 -0
  27. waldiez/exporting/agent/extras/reasoning_agent_extras.py +88 -0
  28. waldiez/exporting/agent/factory.py +95 -0
  29. waldiez/exporting/agent/processor.py +150 -0
  30. waldiez/exporting/agent/system_message.py +36 -0
  31. waldiez/exporting/agent/termination.py +50 -0
  32. waldiez/exporting/chats/__init__.py +7 -3
  33. waldiez/exporting/chats/exporter.py +97 -0
  34. waldiez/exporting/chats/factory.py +65 -0
  35. waldiez/exporting/chats/processor.py +226 -0
  36. waldiez/exporting/chats/utils/__init__.py +6 -5
  37. waldiez/exporting/chats/utils/common.py +11 -45
  38. waldiez/exporting/chats/utils/group.py +55 -0
  39. waldiez/exporting/chats/utils/nested.py +37 -52
  40. waldiez/exporting/chats/utils/sequential.py +72 -61
  41. waldiez/exporting/chats/utils/{single_chat.py → single.py} +48 -50
  42. waldiez/exporting/core/__init__.py +196 -0
  43. waldiez/exporting/core/constants.py +17 -0
  44. waldiez/exporting/core/content.py +69 -0
  45. waldiez/exporting/core/context.py +244 -0
  46. waldiez/exporting/core/enums.py +89 -0
  47. waldiez/exporting/core/errors.py +19 -0
  48. waldiez/exporting/core/exporter.py +390 -0
  49. waldiez/exporting/core/exporters.py +67 -0
  50. waldiez/exporting/core/extras/__init__.py +39 -0
  51. waldiez/exporting/core/extras/agent_extras/__init__.py +27 -0
  52. waldiez/exporting/core/extras/agent_extras/captain_extras.py +57 -0
  53. waldiez/exporting/core/extras/agent_extras/group_manager_extras.py +102 -0
  54. waldiez/exporting/core/extras/agent_extras/rag_user_extras.py +53 -0
  55. waldiez/exporting/core/extras/agent_extras/reasoning_extras.py +68 -0
  56. waldiez/exporting/core/extras/agent_extras/standard_extras.py +263 -0
  57. waldiez/exporting/core/extras/base.py +241 -0
  58. waldiez/exporting/core/extras/chat_extras.py +118 -0
  59. waldiez/exporting/core/extras/flow_extras.py +70 -0
  60. waldiez/exporting/core/extras/model_extras.py +73 -0
  61. waldiez/exporting/core/extras/path_resolver.py +93 -0
  62. waldiez/exporting/core/extras/serializer.py +138 -0
  63. waldiez/exporting/core/extras/tool_extras.py +82 -0
  64. waldiez/exporting/core/protocols.py +259 -0
  65. waldiez/exporting/core/result.py +705 -0
  66. waldiez/exporting/core/types.py +329 -0
  67. waldiez/exporting/core/utils/__init__.py +11 -0
  68. waldiez/exporting/core/utils/comment.py +33 -0
  69. waldiez/exporting/core/utils/llm_config.py +117 -0
  70. waldiez/exporting/core/validation.py +96 -0
  71. waldiez/exporting/flow/__init__.py +6 -2
  72. waldiez/exporting/flow/execution_generator.py +193 -0
  73. waldiez/exporting/flow/exporter.py +107 -0
  74. waldiez/exporting/flow/factory.py +94 -0
  75. waldiez/exporting/flow/file_generator.py +214 -0
  76. waldiez/exporting/flow/merger.py +387 -0
  77. waldiez/exporting/flow/orchestrator.py +411 -0
  78. waldiez/exporting/flow/utils/__init__.py +9 -36
  79. waldiez/exporting/flow/utils/common.py +206 -0
  80. waldiez/exporting/flow/utils/importing.py +373 -0
  81. waldiez/exporting/flow/utils/linting.py +200 -0
  82. waldiez/exporting/flow/utils/{logging_utils.py → logging.py} +23 -9
  83. waldiez/exporting/models/__init__.py +3 -1
  84. waldiez/exporting/models/exporter.py +233 -0
  85. waldiez/exporting/models/factory.py +66 -0
  86. waldiez/exporting/models/processor.py +139 -0
  87. waldiez/exporting/tools/__init__.py +11 -0
  88. waldiez/exporting/tools/exporter.py +207 -0
  89. waldiez/exporting/tools/factory.py +57 -0
  90. waldiez/exporting/tools/processor.py +248 -0
  91. waldiez/exporting/tools/registration.py +133 -0
  92. waldiez/io/__init__.py +128 -0
  93. waldiez/io/_ws.py +199 -0
  94. waldiez/io/models/__init__.py +60 -0
  95. waldiez/io/models/base.py +66 -0
  96. waldiez/io/models/constants.py +78 -0
  97. waldiez/io/models/content/__init__.py +23 -0
  98. waldiez/io/models/content/audio.py +43 -0
  99. waldiez/io/models/content/base.py +45 -0
  100. waldiez/io/models/content/file.py +43 -0
  101. waldiez/io/models/content/image.py +96 -0
  102. waldiez/io/models/content/text.py +37 -0
  103. waldiez/io/models/content/video.py +43 -0
  104. waldiez/io/models/user_input.py +269 -0
  105. waldiez/io/models/user_response.py +215 -0
  106. waldiez/io/mqtt.py +681 -0
  107. waldiez/io/redis.py +782 -0
  108. waldiez/io/structured.py +419 -0
  109. waldiez/io/utils.py +184 -0
  110. waldiez/io/ws.py +298 -0
  111. waldiez/logger.py +481 -0
  112. waldiez/models/__init__.py +108 -51
  113. waldiez/models/agents/__init__.py +34 -70
  114. waldiez/models/agents/agent/__init__.py +10 -4
  115. waldiez/models/agents/agent/agent.py +466 -65
  116. waldiez/models/agents/agent/agent_data.py +119 -47
  117. waldiez/models/agents/agent/agent_type.py +13 -2
  118. waldiez/models/agents/agent/code_execution.py +12 -12
  119. waldiez/models/agents/agent/human_input_mode.py +8 -0
  120. waldiez/models/agents/agent/{linked_skill.py → linked_tool.py} +7 -7
  121. waldiez/models/agents/agent/nested_chat.py +35 -7
  122. waldiez/models/agents/agent/termination_message.py +30 -22
  123. waldiez/models/agents/{swarm_agent → agent}/update_system_message.py +22 -22
  124. waldiez/models/agents/agents.py +58 -63
  125. waldiez/models/agents/assistant/assistant.py +4 -4
  126. waldiez/models/agents/assistant/assistant_data.py +13 -1
  127. waldiez/models/agents/{captain_agent → captain}/captain_agent.py +5 -5
  128. waldiez/models/agents/{captain_agent → captain}/captain_agent_data.py +5 -5
  129. waldiez/models/agents/extra_requirements.py +11 -16
  130. waldiez/models/agents/group_manager/group_manager.py +103 -13
  131. waldiez/models/agents/group_manager/group_manager_data.py +36 -14
  132. waldiez/models/agents/group_manager/speakers.py +77 -24
  133. waldiez/models/agents/{rag_user → rag_user_proxy}/__init__.py +16 -16
  134. waldiez/models/agents/rag_user_proxy/rag_user_proxy.py +64 -0
  135. waldiez/models/agents/{rag_user/rag_user_data.py → rag_user_proxy/rag_user_proxy_data.py} +6 -5
  136. waldiez/models/agents/{rag_user → rag_user_proxy}/retrieve_config.py +182 -114
  137. waldiez/models/agents/{rag_user → rag_user_proxy}/vector_db_config.py +13 -13
  138. waldiez/models/agents/reasoning/reasoning_agent.py +6 -6
  139. waldiez/models/agents/reasoning/reasoning_agent_data.py +110 -63
  140. waldiez/models/agents/reasoning/reasoning_agent_reason_config.py +38 -10
  141. waldiez/models/agents/user_proxy/user_proxy.py +11 -7
  142. waldiez/models/agents/user_proxy/user_proxy_data.py +2 -2
  143. waldiez/models/chat/__init__.py +2 -1
  144. waldiez/models/chat/chat.py +166 -87
  145. waldiez/models/chat/chat_data.py +99 -136
  146. waldiez/models/chat/chat_message.py +33 -23
  147. waldiez/models/chat/chat_nested.py +31 -30
  148. waldiez/models/chat/chat_summary.py +10 -8
  149. waldiez/models/common/__init__.py +52 -2
  150. waldiez/models/common/ag2_version.py +1 -1
  151. waldiez/models/common/base.py +38 -7
  152. waldiez/models/common/dict_utils.py +42 -17
  153. waldiez/models/common/handoff.py +459 -0
  154. waldiez/models/common/id_generator.py +19 -0
  155. waldiez/models/common/method_utils.py +130 -68
  156. waldiez/{exporting/base/utils → models/common}/naming.py +38 -61
  157. waldiez/models/common/waldiez_version.py +37 -0
  158. waldiez/models/flow/__init__.py +9 -2
  159. waldiez/models/flow/connection.py +18 -0
  160. waldiez/models/flow/flow.py +311 -215
  161. waldiez/models/flow/flow_data.py +207 -40
  162. waldiez/models/flow/info.py +85 -0
  163. waldiez/models/flow/naming.py +131 -0
  164. waldiez/models/model/__init__.py +7 -1
  165. waldiez/models/model/extra_requirements.py +3 -12
  166. waldiez/models/model/model.py +76 -21
  167. waldiez/models/model/model_data.py +108 -20
  168. waldiez/models/tool/__init__.py +16 -0
  169. waldiez/models/tool/extra_requirements.py +36 -0
  170. waldiez/models/{skill/skill.py → tool/tool.py} +88 -88
  171. waldiez/models/tool/tool_data.py +51 -0
  172. waldiez/models/tool/tool_type.py +8 -0
  173. waldiez/models/waldiez.py +97 -80
  174. waldiez/runner.py +114 -49
  175. waldiez/running/__init__.py +1 -1
  176. waldiez/running/environment.py +49 -68
  177. waldiez/running/gen_seq_diagram.py +16 -14
  178. waldiez/running/running.py +53 -34
  179. waldiez/utils/__init__.py +0 -4
  180. waldiez/utils/cli_extras/jupyter.py +5 -3
  181. waldiez/utils/cli_extras/runner.py +6 -4
  182. waldiez/utils/cli_extras/studio.py +6 -4
  183. waldiez/utils/conflict_checker.py +15 -9
  184. waldiez/utils/flaml_warnings.py +5 -5
  185. {waldiez-0.4.6.dist-info → waldiez-0.4.8.dist-info}/METADATA +235 -91
  186. waldiez-0.4.8.dist-info/RECORD +200 -0
  187. waldiez/exporting/agent/agent_exporter.py +0 -297
  188. waldiez/exporting/agent/utils/__init__.py +0 -23
  189. waldiez/exporting/agent/utils/captain_agent.py +0 -263
  190. waldiez/exporting/agent/utils/code_execution.py +0 -65
  191. waldiez/exporting/agent/utils/group_manager.py +0 -220
  192. waldiez/exporting/agent/utils/rag_user/__init__.py +0 -7
  193. waldiez/exporting/agent/utils/rag_user/rag_user.py +0 -209
  194. waldiez/exporting/agent/utils/reasoning.py +0 -36
  195. waldiez/exporting/agent/utils/swarm_agent.py +0 -469
  196. waldiez/exporting/agent/utils/teachability.py +0 -41
  197. waldiez/exporting/agent/utils/termination_message.py +0 -44
  198. waldiez/exporting/base/__init__.py +0 -25
  199. waldiez/exporting/base/agent_position.py +0 -75
  200. waldiez/exporting/base/base_exporter.py +0 -118
  201. waldiez/exporting/base/export_position.py +0 -48
  202. waldiez/exporting/base/import_position.py +0 -23
  203. waldiez/exporting/base/mixin.py +0 -137
  204. waldiez/exporting/base/utils/__init__.py +0 -18
  205. waldiez/exporting/base/utils/comments.py +0 -96
  206. waldiez/exporting/base/utils/path_check.py +0 -68
  207. waldiez/exporting/base/utils/to_string.py +0 -84
  208. waldiez/exporting/chats/chats_exporter.py +0 -240
  209. waldiez/exporting/chats/utils/swarm.py +0 -210
  210. waldiez/exporting/flow/flow_exporter.py +0 -528
  211. waldiez/exporting/flow/utils/agent_utils.py +0 -204
  212. waldiez/exporting/flow/utils/chat_utils.py +0 -71
  213. waldiez/exporting/flow/utils/def_main.py +0 -77
  214. waldiez/exporting/flow/utils/flow_content.py +0 -202
  215. waldiez/exporting/flow/utils/flow_names.py +0 -116
  216. waldiez/exporting/flow/utils/importing_utils.py +0 -227
  217. waldiez/exporting/models/models_exporter.py +0 -199
  218. waldiez/exporting/models/utils.py +0 -174
  219. waldiez/exporting/skills/__init__.py +0 -9
  220. waldiez/exporting/skills/skills_exporter.py +0 -176
  221. waldiez/exporting/skills/utils.py +0 -369
  222. waldiez/models/agents/agent/teachability.py +0 -70
  223. waldiez/models/agents/rag_user/rag_user.py +0 -60
  224. waldiez/models/agents/swarm_agent/__init__.py +0 -50
  225. waldiez/models/agents/swarm_agent/after_work.py +0 -179
  226. waldiez/models/agents/swarm_agent/on_condition.py +0 -105
  227. waldiez/models/agents/swarm_agent/on_condition_available.py +0 -142
  228. waldiez/models/agents/swarm_agent/on_condition_target.py +0 -40
  229. waldiez/models/agents/swarm_agent/swarm_agent.py +0 -107
  230. waldiez/models/agents/swarm_agent/swarm_agent_data.py +0 -124
  231. waldiez/models/flow/utils.py +0 -232
  232. waldiez/models/skill/__init__.py +0 -16
  233. waldiez/models/skill/extra_requirements.py +0 -36
  234. waldiez/models/skill/skill_data.py +0 -53
  235. waldiez/models/skill/skill_type.py +0 -8
  236. waldiez/utils/pysqlite3_checker.py +0 -308
  237. waldiez/utils/rdps_checker.py +0 -122
  238. waldiez-0.4.6.dist-info/RECORD +0 -149
  239. /waldiez/models/agents/{captain_agent → captain}/__init__.py +0 -0
  240. /waldiez/models/agents/{captain_agent → captain}/captain_agent_lib_entry.py +0 -0
  241. {waldiez-0.4.6.dist-info → waldiez-0.4.8.dist-info}/WHEEL +0 -0
  242. {waldiez-0.4.6.dist-info → waldiez-0.4.8.dist-info}/entry_points.txt +0 -0
  243. {waldiez-0.4.6.dist-info → waldiez-0.4.8.dist-info}/licenses/LICENSE +0 -0
  244. {waldiez-0.4.6.dist-info → waldiez-0.4.8.dist-info}/licenses/NOTICE.md +0 -0
@@ -1,227 +0,0 @@
1
- # SPDX-License-Identifier: Apache-2.0.
2
- # Copyright (c) 2024 - 2025 Waldiez and contributors.
3
- """Get the standard imports for the flow exporter."""
4
-
5
- from typing import List, Optional, Tuple
6
-
7
- from waldiez.exporting.base import ImportPosition
8
-
9
- BUILTIN_IMPORTS = [
10
- "import csv",
11
- "import json",
12
- "import os",
13
- "import sqlite3",
14
- "from dataclasses import asdict",
15
- "from pprint import pprint",
16
- ]
17
- TYPING_IMPORTS = [
18
- "Annotated",
19
- "Any",
20
- "Callable",
21
- "Dict",
22
- "List",
23
- "Literal",
24
- "Optional",
25
- "Set",
26
- "Tuple",
27
- "Union",
28
- ]
29
- COMMON_AUTOGEN_IMPORTS = [
30
- "from autogen import Agent",
31
- "from autogen import Cache",
32
- "from autogen import ConversableAgent",
33
- "from autogen import ChatResult",
34
- "from autogen import GroupChat",
35
- "from autogen import runtime_logging",
36
- ]
37
-
38
-
39
- def sort_imports(
40
- all_imports: List[Tuple[str, ImportPosition]],
41
- ) -> Tuple[List[str], List[str], List[str], List[str], bool]:
42
- """Sort the imports.
43
-
44
- Parameters
45
- ----------
46
- all_imports : List[Tuple[str, ImportPosition]]
47
- All the imports.
48
-
49
- Returns
50
- -------
51
- Tuple[List[str], List[str], List[str], List[str], bool]
52
- The sorted imports and a flag if we got `import autogen`.
53
- """
54
- builtin_imports: List[str] = []
55
- third_party_imports: List[str] = []
56
- local_imports: List[str] = []
57
- autogen_imports: List[str] = COMMON_AUTOGEN_IMPORTS.copy()
58
- got_import_autogen = False
59
- for import_string, position in all_imports:
60
- if "import autogen" in import_string:
61
- got_import_autogen = True
62
- continue
63
- if import_string.startswith("from autogen"):
64
- autogen_imports.append(import_string)
65
- continue
66
- if position == ImportPosition.BUILTINS:
67
- builtin_imports.append(import_string)
68
- elif position == ImportPosition.THIRD_PARTY:
69
- third_party_imports.append(import_string)
70
- elif position == ImportPosition.LOCAL:
71
- local_imports.append(import_string)
72
- autogen_imports = list(set(autogen_imports))
73
- third_party_imports = ensure_np_import(third_party_imports)
74
- sorted_builtins = sorted(
75
- [imp for imp in builtin_imports if imp.startswith("import ")]
76
- ) + sorted([imp for imp in builtin_imports if imp.startswith("from ")])
77
- sorted_third_party = sorted(
78
- [imp for imp in third_party_imports if imp.startswith("import ")]
79
- ) + sorted([imp for imp in third_party_imports if imp.startswith("from ")])
80
- sorted_locals = sorted(
81
- [imp for imp in local_imports if imp.startswith("import ")]
82
- ) + sorted([imp for imp in local_imports if imp.startswith("from ")])
83
-
84
- return (
85
- sorted_builtins,
86
- sorted(autogen_imports),
87
- sorted_third_party,
88
- sorted_locals,
89
- got_import_autogen,
90
- )
91
-
92
-
93
- def get_the_imports_string(
94
- all_imports: List[Tuple[str, ImportPosition]],
95
- is_async: bool,
96
- ) -> str:
97
- """Get the final imports string.
98
-
99
- Parameters
100
- ----------
101
- all_imports : List[Tuple[str, ImportPosition]]
102
- All the imports.
103
- is_async : bool
104
- If the flow is async.
105
-
106
- Returns
107
- -------
108
- str
109
- The final imports string.
110
- """
111
- (
112
- builtin_imports,
113
- autogen_imports,
114
- third_party_imports,
115
- local_imports,
116
- got_import_autogen,
117
- ) = sort_imports(all_imports)
118
- # Get the final imports string.
119
- # making sure, there are two lines after each import section
120
- # (builtin, third party, local)
121
- final_string = "\n".join(builtin_imports) + "\n"
122
- while not final_string.endswith("\n\n"):
123
- final_string += "\n"
124
- if is_async:
125
- final_string += (
126
- "\nimport aiofiles"
127
- "\nimport aiosqlite"
128
- "\nimport anyio"
129
- "\nimport nest_asyncio"
130
- "\nfrom aiocsv import AsyncDictWriter"
131
- )
132
- if got_import_autogen:
133
- final_string += "\nimport autogen # type: ignore\n"
134
- if autogen_imports:
135
- final_string += "\n".join(autogen_imports) + "\n"
136
- if third_party_imports:
137
- final_string += "\n".join(third_party_imports) + "\n"
138
- while not final_string.endswith("\n\n"):
139
- final_string += "\n"
140
- if local_imports:
141
- final_string += "\n".join(local_imports) + "\n"
142
- while not final_string.endswith("\n\n"):
143
- final_string += "\n"
144
- if is_async:
145
- final_string += "\nnest_asyncio.apply()\n"
146
- return final_string.replace("\n\n\n", "\n\n") # avoid too many newlines
147
-
148
-
149
- def ensure_np_import(third_party_imports: List[str]) -> List[str]:
150
- """Ensure numpy is imported.
151
-
152
- Parameters
153
- ----------
154
- third_party_imports : List[str]
155
- The third party imports.
156
-
157
- Returns
158
- -------
159
- List[str]
160
- The third party imports with numpy.
161
- """
162
- if (
163
- not third_party_imports
164
- or "import numpy as np" not in third_party_imports
165
- ):
166
- third_party_imports.append("import numpy as np")
167
- return third_party_imports
168
-
169
-
170
- def gather_imports(
171
- model_imports: Optional[List[Tuple[str, ImportPosition]]],
172
- skill_imports: Optional[List[Tuple[str, ImportPosition]]],
173
- chat_imports: Optional[List[Tuple[str, ImportPosition]]],
174
- agent_imports: Optional[List[Tuple[str, ImportPosition]]],
175
- ) -> List[Tuple[str, ImportPosition]]:
176
- """Gather all the imports.
177
-
178
- Parameters
179
- ----------
180
- model_imports : Tuple[str, ImportPosition]
181
- The model imports.
182
- skill_imports : Tuple[str, ImportPosition]
183
- The skill imports.
184
- chat_imports : Tuple[str, ImportPosition]
185
- The chat imports.
186
- agent_imports : Tuple[str, ImportPosition]
187
- The agent imports.
188
-
189
- Returns
190
- -------
191
- Tuple[str, ImportPosition]
192
- The gathered imports.
193
- """
194
- all_imports: List[Tuple[str, ImportPosition]] = []
195
- for import_statement in BUILTIN_IMPORTS:
196
- all_imports.append(
197
- (
198
- import_statement,
199
- ImportPosition.BUILTINS,
200
- )
201
- )
202
- if model_imports:
203
- all_imports.extend(model_imports)
204
- if skill_imports:
205
- all_imports.extend(skill_imports)
206
- if chat_imports:
207
- all_imports.extend(chat_imports)
208
- if agent_imports:
209
- all_imports.extend(agent_imports)
210
- # let's try to avoid this:
211
- # from typing import Annotated
212
- # from typing import Annotated, Any, Callable, Dict, ...Union
213
- all_typing_imports = TYPING_IMPORTS.copy()
214
- final_imports: List[Tuple[str, ImportPosition]] = []
215
- for import_statement, import_position in all_imports:
216
- if import_statement.startswith("from typing"):
217
- to_import = import_statement.split("import")[1].strip()
218
- if to_import:
219
- all_typing_imports.append(to_import)
220
- else:
221
- final_imports.append((import_statement, import_position))
222
- unique_typing_imports = list(set(all_typing_imports))
223
- one_typing_import = "from typing import " + ", ".join(
224
- sorted(unique_typing_imports)
225
- )
226
- final_imports.insert(1, (one_typing_import, ImportPosition.BUILTINS))
227
- return list(set(final_imports))
@@ -1,199 +0,0 @@
1
- # SPDX-License-Identifier: Apache-2.0.
2
- # Copyright (c) 2024 - 2025 Waldiez and contributors.
3
- """Export models (llm_configs)."""
4
-
5
- from pathlib import Path
6
- from typing import Dict, List, Optional, Tuple, Union
7
-
8
- from waldiez.models import WaldiezAgent, WaldiezModel
9
-
10
- from ..base import (
11
- AgentPosition,
12
- AgentPositions,
13
- BaseExporter,
14
- ExporterMixin,
15
- ExporterReturnType,
16
- ExportPosition,
17
- ImportPosition,
18
- )
19
- from .utils import export_models, get_agent_llm_config_arg
20
-
21
-
22
- class ModelsExporter(BaseExporter, ExporterMixin):
23
- """Models exporter."""
24
-
25
- _exported_string: Optional[str]
26
-
27
- def __init__(
28
- self,
29
- flow_name: str,
30
- agents: List[WaldiezAgent],
31
- agent_names: Dict[str, str],
32
- models: List[WaldiezModel],
33
- model_names: Dict[str, str],
34
- for_notebook: bool,
35
- cache_seed: Optional[int],
36
- output_dir: Optional[Union[str, Path]] = None,
37
- ) -> None:
38
- """Initialize the models exporter.
39
-
40
- Parameters
41
- ----------
42
- agents : List[WaldiezAgent]
43
- The agents.
44
- agent_names : Dict[str, str]
45
- The agent names.
46
- models : List[WaldiezModel]
47
- The models.
48
- model_names : Dict[str, str]
49
- The model names.
50
- for_notebook : bool
51
- Whether the export is for a notebook or not.
52
- cache_seed : Optional[int]
53
- The cache seed if any, by default None
54
- output_dir : Optional[Union[str, Path]], optional
55
- The output directory if any, by default None
56
- """
57
- self.for_notebook = for_notebook
58
- self.flow_name = flow_name
59
- self.agents = agents
60
- self.agent_names = agent_names
61
- self.models = models
62
- self.model_names = model_names
63
- if output_dir is not None and not isinstance(output_dir, Path):
64
- output_dir = Path(output_dir)
65
- self.cache_seed = cache_seed
66
- self.output_dir = output_dir
67
- self._exported_string = None
68
-
69
- def get_imports(self) -> Optional[List[Tuple[str, ImportPosition]]]:
70
- """Generate the imports string.
71
-
72
- Returns
73
- -------
74
- Optional[Tuple[str, ImportPosition]]
75
- The exported imports and the position of the imports.
76
- """
77
- if not self.output_dir:
78
- return None
79
- file_path = self.output_dir / f"{self.flow_name}_api_keys.py"
80
- if not file_path.exists():
81
- # might be because the models are not exported yet
82
- if not self._exported_string:
83
- self.generate()
84
- # if still not exported, return None
85
- if not file_path.exists(): # pragma: no cover
86
- return None
87
- import_string = f"from {self.flow_name}_api_keys import (" + "\n"
88
- import_string += f" get_{self.flow_name}_model_api_key," + "\n"
89
- import_string += ")\n"
90
- return [(import_string, ImportPosition.LOCAL)]
91
-
92
- def get_after_export(
93
- self,
94
- ) -> Optional[List[Tuple[str, Union[ExportPosition, AgentPosition]]]]:
95
- # fmt: off
96
- """Generate the after export strings.
97
-
98
- The arguments for the agent's initialization.
99
- example generated args:
100
- >>> agent1 = ConversableAgent(
101
- >>> ...
102
- >>> llm_config=False,
103
- >>> ...
104
- >>> )
105
-
106
- >>> agent2 = ConversableAgent(
107
- >>> ...
108
- >>> llm_config={
109
- >>> "config_list": [
110
- >>> model1_llm_config,
111
- >>> model2_llm_config,
112
- >>> ],
113
- >>> },
114
- >>> ...
115
- >>> )
116
-
117
- where `model1_llm_config` and `model2_llm_config`
118
- are the exported models using `self.generate()`
119
-
120
- Returns
121
- -------
122
- Optional[List[Tuple[str, Union[ExportPosition, AgentPosition]]]]
123
- The exported after export strings and their positions.
124
- """
125
- # fmt: on
126
- agent_llm_config_args: List[
127
- Tuple[str, Union[ExportPosition, AgentPosition]]
128
- ] = []
129
- for agent in self.agents:
130
- agent_llm_config_args.append(
131
- (
132
- get_agent_llm_config_arg(
133
- agent,
134
- all_models=self.models,
135
- model_names=self.model_names,
136
- cache_seed=self.cache_seed,
137
- ),
138
- AgentPosition(
139
- agent=agent, position=AgentPositions.AS_ARGUMENT
140
- ),
141
- )
142
- )
143
- return agent_llm_config_args
144
-
145
- def generate(self) -> str:
146
- """Export the models.
147
-
148
- Returns
149
- -------
150
- str
151
- The exported models.
152
- """
153
- if not self._exported_string: # pragma: no cover
154
- self._exported_string = export_models(
155
- flow_name=self.flow_name,
156
- all_models=self.models,
157
- model_names=self.model_names,
158
- output_dir=self.output_dir,
159
- serializer=self.serializer,
160
- )
161
- return self._exported_string
162
-
163
- def get_environment_variables(self) -> Optional[List[Tuple[str, str]]]:
164
- """Get the environment variables to set.
165
-
166
- Returns
167
- -------
168
- Optional[List[Tuple[str, str]]
169
- The environment variables to set.
170
- """
171
- env_vars = []
172
- for model in self.models:
173
- if model.api_key:
174
- env_vars.append((model.api_key_env_key, model.api_key))
175
- return env_vars
176
-
177
- def export(self) -> ExporterReturnType:
178
- """Export the models.
179
-
180
- Returns
181
- -------
182
- ExporterReturnType
183
- The exported models,
184
- the imports,
185
- the before export strings,
186
- the after export strings,
187
- and the environment variables.
188
- """
189
- exported_string = self.generate()
190
- imports = self.get_imports()
191
- after_export = self.get_after_export()
192
- result: ExporterReturnType = {
193
- "content": exported_string,
194
- "imports": imports,
195
- "before_export": None,
196
- "after_export": after_export,
197
- "environment_variables": None,
198
- }
199
- return result
@@ -1,174 +0,0 @@
1
- # SPDX-License-Identifier: Apache-2.0.
2
- # Copyright (c) 2024 - 2025 Waldiez and contributors.
3
- """Model/LLM related string generation functions.
4
-
5
- Functions
6
- ---------
7
- export_models
8
- Get the string representations of the LLM configs.
9
- """
10
-
11
- from pathlib import Path
12
- from typing import Callable, Dict, List, Optional
13
-
14
- from waldiez.models import WaldiezAgent, WaldiezModel
15
-
16
-
17
- def export_models(
18
- flow_name: str,
19
- all_models: List[WaldiezModel],
20
- model_names: Dict[str, str],
21
- serializer: Callable[..., str],
22
- output_dir: Optional[Path] = None,
23
- ) -> str:
24
- """Get the string representations of all the models in the flow.
25
-
26
- Parameters
27
- ----------
28
- flow_name : str
29
- The name of the flow.
30
- all_models : List[WaldiezModel]
31
- All the models in the flow.
32
- model_names : Dict[str, str]
33
- A mapping of model ids to model names.
34
- serializer : Callable[..., str]
35
- The serializer function.
36
- output_dir : Optional[Path]
37
- The output directory to write the api keys.
38
-
39
- Returns
40
- -------
41
- str
42
- The models' llm config string.
43
- """
44
- content = ""
45
- for model in all_models:
46
- model_name = model_names[model.id]
47
- model_config = model.get_llm_config()
48
- model_config["api_key"] = (
49
- f'get_{flow_name}_model_api_key("{model_name}")'
50
- )
51
- model_dict_str = serializer(model_config, tabs=0)
52
- model_dict_str = model_dict_str.replace(
53
- f'"get_{flow_name}_model_api_key("{model_name}")"',
54
- f'get_{flow_name}_model_api_key("{model_name}")',
55
- )
56
- content += "\n" + f"{model_name}_llm_config = {model_dict_str}" + "\n"
57
- if output_dir:
58
- write_api_keys(flow_name, all_models, model_names, output_dir)
59
- return content
60
-
61
-
62
- def get_agent_llm_config_arg(
63
- agent: WaldiezAgent,
64
- all_models: List[WaldiezModel],
65
- model_names: Dict[str, str],
66
- cache_seed: Optional[int],
67
- tabs: int = 1,
68
- ) -> str:
69
- """Get the string representation of the agent's llm config argument.
70
-
71
- Parameters
72
- ----------
73
- agent : WaldiezAgent
74
- The agent.
75
- all_models : List[WaldiezModel]
76
- All the models in the flow.
77
- model_names : Dict[str, str]
78
- A mapping of model ids to model names.
79
- cache_seed : Optional[int]
80
- The cache seed.
81
- tabs : int, optional
82
- The number of tabs for indentation, by default 1.
83
-
84
- Returns
85
- -------
86
- str
87
- The agent's llm config argument to use.
88
- """
89
- tab = " " * tabs if tabs > 0 else ""
90
- if not agent.data.model_ids:
91
- return f"{tab}llm_config=False," + "\n"
92
- content = f"{tab}llm_config=" + "{\n"
93
- content += f'{tab} "config_list": ['
94
- got_at_least_one_model = False
95
- temperature: Optional[float] = None
96
- for model_id in agent.data.model_ids:
97
- model = next((m for m in all_models if m.id == model_id), None)
98
- if model is not None:
99
- temperature = model.data.temperature
100
- model_name = model_names[model_id]
101
- content += "\n" + f"{tab} {model_name}_llm_config,"
102
- got_at_least_one_model = True
103
- if not got_at_least_one_model: # pragma: no cover
104
- return f"{tab}llm_config=False," + "\n"
105
- content += "\n" + f"{tab} ]," + "\n"
106
- content += f'{tab} "cache_seed": {cache_seed},' + "\n"
107
- if temperature is not None:
108
- content += f'{tab} "temperature": {temperature},' + "\n"
109
- content += tab + "},\n"
110
- return content
111
-
112
-
113
- def write_api_keys(
114
- flow_name: str,
115
- all_models: List[WaldiezModel],
116
- model_names: Dict[str, str],
117
- output_dir: Path,
118
- ) -> None:
119
- """Write the api keys to a separate file.
120
-
121
- Parameters
122
- ----------
123
- flow_name : str
124
- The name of the flow.
125
- all_models : List[WaldiezModel]
126
- All the models in the flow.
127
- model_names : Dict[str, str]
128
- A mapping of model ids to model names.
129
- output_dir : Path
130
- The output directory to write the api keys.
131
- """
132
- flow_name_upper = flow_name.upper()
133
- api_keys_content = f'''
134
- """API keys for the {flow_name} models."""
135
-
136
- import os
137
-
138
- __{flow_name_upper}_MODEL_API_KEYS__ = {{'''
139
- for model in all_models:
140
- model_name = model_names[model.id]
141
- key_env = model.api_key_env_key
142
- api_keys_content += (
143
- "\n" + f' "{model_name}": '
144
- f'{{"key": "{model.api_key}", "env_key": "{key_env}"}},'
145
- )
146
- api_keys_content += "\n}\n"
147
- api_keys_content += f'''
148
-
149
- def get_{flow_name}_model_api_key(model_name: str) -> str:
150
- """Get the api key for the model.
151
-
152
- Parameters
153
- ----------
154
- model_name : str
155
- The name of the model.
156
-
157
- Returns
158
- -------
159
- str
160
- The api key for the model.
161
- """
162
- entry = __{flow_name_upper}_MODEL_API_KEYS__.get(model_name, {{}})
163
- if not entry:
164
- return ""
165
- env_key = entry.get("env_key", "")
166
- if env_key:
167
- from_env = os.environ.get(env_key, "")
168
- if from_env:
169
- return from_env
170
- return entry.get("key", "")
171
- '''
172
- file_name = f"{flow_name}_api_keys.py"
173
- with open(output_dir / file_name, "w", encoding="utf-8", newline="\n") as f:
174
- f.write(api_keys_content)
@@ -1,9 +0,0 @@
1
- # SPDX-License-Identifier: Apache-2.0.
2
- # Copyright (c) 2024 - 2025 Waldiez and contributors.
3
- """Export skill."""
4
-
5
- from .skills_exporter import SkillsExporter
6
-
7
- __all__ = [
8
- "SkillsExporter",
9
- ]