waldiez 0.2.2__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.

Files changed (138) hide show
  1. waldiez/__init__.py +2 -0
  2. waldiez/__main__.py +2 -0
  3. waldiez/_version.py +3 -1
  4. waldiez/cli.py +13 -3
  5. waldiez/cli_extras.py +4 -3
  6. waldiez/conflict_checker.py +4 -3
  7. waldiez/exporter.py +28 -105
  8. waldiez/exporting/__init__.py +8 -9
  9. waldiez/exporting/agent/__init__.py +7 -0
  10. waldiez/exporting/agent/agent_exporter.py +279 -0
  11. waldiez/exporting/agent/utils/__init__.py +23 -0
  12. waldiez/exporting/agent/utils/agent_class_name.py +34 -0
  13. waldiez/exporting/agent/utils/agent_imports.py +50 -0
  14. waldiez/exporting/{agents → agent/utils}/code_execution.py +9 -11
  15. waldiez/exporting/{agents → agent/utils}/group_manager.py +47 -35
  16. waldiez/exporting/{agents → agent/utils}/rag_user/__init__.py +2 -0
  17. waldiez/exporting/{agents → agent/utils}/rag_user/chroma_utils.py +22 -17
  18. waldiez/exporting/{agents → agent/utils}/rag_user/mongo_utils.py +14 -10
  19. waldiez/exporting/{agents → agent/utils}/rag_user/pgvector_utils.py +12 -8
  20. waldiez/exporting/{agents → agent/utils}/rag_user/qdrant_utils.py +11 -8
  21. waldiez/exporting/{agents → agent/utils}/rag_user/rag_user.py +78 -55
  22. waldiez/exporting/{agents → agent/utils}/rag_user/vector_db.py +10 -8
  23. waldiez/exporting/agent/utils/swarm_agent.py +463 -0
  24. waldiez/exporting/{agents → agent/utils}/teachability.py +10 -6
  25. waldiez/exporting/{agents → agent/utils}/termination_message.py +7 -8
  26. waldiez/exporting/base/__init__.py +25 -0
  27. waldiez/exporting/base/agent_position.py +75 -0
  28. waldiez/exporting/base/base_exporter.py +118 -0
  29. waldiez/exporting/base/export_position.py +48 -0
  30. waldiez/exporting/base/import_position.py +23 -0
  31. waldiez/exporting/base/mixin.py +134 -0
  32. waldiez/exporting/base/utils/__init__.py +18 -0
  33. waldiez/exporting/{utils → base/utils}/comments.py +12 -55
  34. waldiez/exporting/{utils → base/utils}/naming.py +14 -4
  35. waldiez/exporting/base/utils/path_check.py +68 -0
  36. waldiez/exporting/{utils/object_string.py → base/utils/to_string.py} +21 -20
  37. waldiez/exporting/chats/__init__.py +5 -12
  38. waldiez/exporting/chats/chats_exporter.py +240 -0
  39. waldiez/exporting/chats/utils/__init__.py +15 -0
  40. waldiez/exporting/chats/utils/common.py +81 -0
  41. waldiez/exporting/chats/{nested.py → utils/nested.py} +125 -86
  42. waldiez/exporting/chats/utils/sequential.py +244 -0
  43. waldiez/exporting/chats/utils/single_chat.py +313 -0
  44. waldiez/exporting/chats/utils/swarm.py +207 -0
  45. waldiez/exporting/flow/__init__.py +5 -3
  46. waldiez/exporting/flow/flow_exporter.py +503 -0
  47. waldiez/exporting/flow/utils/__init__.py +47 -0
  48. waldiez/exporting/flow/utils/agent_utils.py +204 -0
  49. waldiez/exporting/flow/utils/chat_utils.py +71 -0
  50. waldiez/exporting/flow/utils/def_main.py +62 -0
  51. waldiez/exporting/flow/utils/flow_content.py +112 -0
  52. waldiez/exporting/flow/utils/flow_names.py +115 -0
  53. waldiez/exporting/flow/utils/importing_utils.py +179 -0
  54. waldiez/exporting/{utils → flow/utils}/logging_utils.py +34 -31
  55. waldiez/exporting/models/__init__.py +7 -242
  56. waldiez/exporting/models/models_exporter.py +192 -0
  57. waldiez/exporting/models/utils.py +166 -0
  58. waldiez/exporting/skills/__init__.py +7 -161
  59. waldiez/exporting/skills/skills_exporter.py +169 -0
  60. waldiez/exporting/skills/utils.py +281 -0
  61. waldiez/models/__init__.py +25 -7
  62. waldiez/models/agents/__init__.py +70 -0
  63. waldiez/models/agents/agent/__init__.py +11 -1
  64. waldiez/models/agents/agent/agent.py +9 -4
  65. waldiez/models/agents/agent/agent_data.py +3 -1
  66. waldiez/models/agents/agent/code_execution.py +2 -0
  67. waldiez/models/agents/agent/linked_skill.py +2 -0
  68. waldiez/models/agents/agent/nested_chat.py +2 -0
  69. waldiez/models/agents/agent/teachability.py +2 -0
  70. waldiez/models/agents/agent/termination_message.py +49 -13
  71. waldiez/models/agents/agents.py +15 -3
  72. waldiez/models/agents/assistant/__init__.py +2 -0
  73. waldiez/models/agents/assistant/assistant.py +2 -0
  74. waldiez/models/agents/assistant/assistant_data.py +2 -0
  75. waldiez/models/agents/group_manager/__init__.py +9 -1
  76. waldiez/models/agents/group_manager/group_manager.py +2 -0
  77. waldiez/models/agents/group_manager/group_manager_data.py +2 -0
  78. waldiez/models/agents/group_manager/speakers.py +49 -13
  79. waldiez/models/agents/rag_user/__init__.py +21 -4
  80. waldiez/models/agents/rag_user/rag_user.py +3 -1
  81. waldiez/models/agents/rag_user/rag_user_data.py +2 -0
  82. waldiez/models/agents/rag_user/retrieve_config.py +268 -17
  83. waldiez/models/agents/rag_user/vector_db_config.py +5 -3
  84. waldiez/models/agents/swarm_agent/__init__.py +49 -0
  85. waldiez/models/agents/swarm_agent/after_work.py +178 -0
  86. waldiez/models/agents/swarm_agent/on_condition.py +103 -0
  87. waldiez/models/agents/swarm_agent/on_condition_available.py +140 -0
  88. waldiez/models/agents/swarm_agent/on_condition_target.py +40 -0
  89. waldiez/models/agents/swarm_agent/swarm_agent.py +107 -0
  90. waldiez/models/agents/swarm_agent/swarm_agent_data.py +125 -0
  91. waldiez/models/agents/swarm_agent/update_system_message.py +144 -0
  92. waldiez/models/agents/user_proxy/__init__.py +2 -0
  93. waldiez/models/agents/user_proxy/user_proxy.py +2 -0
  94. waldiez/models/agents/user_proxy/user_proxy_data.py +2 -0
  95. waldiez/models/chat/__init__.py +21 -3
  96. waldiez/models/chat/chat.py +241 -7
  97. waldiez/models/chat/chat_data.py +192 -48
  98. waldiez/models/chat/chat_message.py +153 -144
  99. waldiez/models/chat/chat_nested.py +33 -53
  100. waldiez/models/chat/chat_summary.py +2 -0
  101. waldiez/models/common/__init__.py +6 -6
  102. waldiez/models/common/base.py +4 -1
  103. waldiez/models/common/method_utils.py +163 -83
  104. waldiez/models/flow/__init__.py +2 -0
  105. waldiez/models/flow/flow.py +176 -40
  106. waldiez/models/flow/flow_data.py +63 -2
  107. waldiez/models/flow/utils.py +172 -0
  108. waldiez/models/model/__init__.py +2 -0
  109. waldiez/models/model/model.py +25 -6
  110. waldiez/models/model/model_data.py +3 -1
  111. waldiez/models/skill/__init__.py +4 -1
  112. waldiez/models/skill/skill.py +30 -2
  113. waldiez/models/skill/skill_data.py +2 -0
  114. waldiez/models/waldiez.py +28 -4
  115. waldiez/runner.py +142 -228
  116. waldiez/running/__init__.py +33 -0
  117. waldiez/running/environment.py +83 -0
  118. waldiez/running/gen_seq_diagram.py +185 -0
  119. waldiez/running/running.py +300 -0
  120. {waldiez-0.2.2.dist-info → waldiez-0.3.0.dist-info}/METADATA +32 -26
  121. waldiez-0.3.0.dist-info/RECORD +125 -0
  122. waldiez-0.3.0.dist-info/licenses/LICENSE +201 -0
  123. waldiez/exporting/agents/__init__.py +0 -5
  124. waldiez/exporting/agents/agent.py +0 -236
  125. waldiez/exporting/agents/agent_skills.py +0 -67
  126. waldiez/exporting/agents/llm_config.py +0 -53
  127. waldiez/exporting/chats/chats.py +0 -46
  128. waldiez/exporting/chats/helpers.py +0 -420
  129. waldiez/exporting/flow/def_main.py +0 -32
  130. waldiez/exporting/flow/flow.py +0 -189
  131. waldiez/exporting/utils/__init__.py +0 -36
  132. waldiez/exporting/utils/importing.py +0 -265
  133. waldiez/exporting/utils/method_utils.py +0 -35
  134. waldiez/exporting/utils/path_check.py +0 -51
  135. waldiez-0.2.2.dist-info/RECORD +0 -92
  136. waldiez-0.2.2.dist-info/licenses/LICENSE +0 -21
  137. {waldiez-0.2.2.dist-info → waldiez-0.3.0.dist-info}/WHEEL +0 -0
  138. {waldiez-0.2.2.dist-info → waldiez-0.3.0.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,179 @@
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
+ "Any",
19
+ "Callable",
20
+ "Dict",
21
+ "List",
22
+ "Optional",
23
+ "Tuple",
24
+ "Union",
25
+ ]
26
+ COMMON_AUTOGEN_IMPORTS = [
27
+ "from autogen import Agent",
28
+ "from autogen import ConversableAgent",
29
+ "from autogen import ChatResult",
30
+ "from autogen import GroupChat",
31
+ "from autogen import runtime_logging",
32
+ ]
33
+
34
+
35
+ def get_standard_imports() -> str:
36
+ """Get the standard imports.
37
+
38
+ Returns
39
+ -------
40
+ str
41
+ The standard imports.
42
+ """
43
+ builtin_imports = BUILTIN_IMPORTS.copy()
44
+ imports_string = "\n".join(builtin_imports) + "\n"
45
+ typing_imports = "from typing import " + ", ".join(TYPING_IMPORTS)
46
+ imports_string += typing_imports
47
+ return imports_string
48
+
49
+
50
+ def sort_imports(
51
+ all_imports: List[Tuple[str, ImportPosition]],
52
+ ) -> Tuple[List[str], List[str], List[str], List[str], bool]:
53
+ """Sort the imports.
54
+
55
+ Parameters
56
+ ----------
57
+ all_imports : List[Tuple[str, ImportPosition]]
58
+ All the imports.
59
+
60
+ Returns
61
+ -------
62
+ Tuple[List[str], List[str], List[str], List[str], bool]
63
+ The sorted imports and a flag if we got `import autogen`.
64
+ """
65
+ builtin_imports = []
66
+ third_party_imports = []
67
+ local_imports = []
68
+ autogen_imports = COMMON_AUTOGEN_IMPORTS.copy()
69
+ got_import_autogen = False
70
+ for import_string, position in all_imports:
71
+ if "import autogen" in import_string:
72
+ got_import_autogen = True
73
+ continue
74
+ if import_string.startswith("from autogen"):
75
+ autogen_imports.append(import_string)
76
+ continue
77
+ if position == ImportPosition.BUILTINS:
78
+ builtin_imports.append(import_string)
79
+ elif position == ImportPosition.THIRD_PARTY:
80
+ third_party_imports.append(import_string)
81
+ elif position == ImportPosition.LOCAL:
82
+ local_imports.append(import_string)
83
+ autogen_imports = list(set(autogen_imports))
84
+ return (
85
+ sorted(builtin_imports),
86
+ sorted(autogen_imports),
87
+ sorted(third_party_imports),
88
+ sorted(local_imports),
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
+ Returns
106
+ -------
107
+ str
108
+ The final imports string.
109
+ """
110
+ (
111
+ builtin_imports,
112
+ autogen_imports,
113
+ third_party_imports,
114
+ local_imports,
115
+ got_import_autogen,
116
+ ) = sort_imports(all_imports)
117
+ # Get the final imports string.
118
+ # making sure, there are two lines after each import section
119
+ # (builtin, third party, local)
120
+ final_string = "\n".join(builtin_imports) + "\n"
121
+ while not final_string.endswith("\n\n"):
122
+ final_string += "\n"
123
+ if is_async:
124
+ final_string += "\nimport anyio"
125
+ if got_import_autogen:
126
+ final_string += "\nimport autogen # type: ignore\n"
127
+ if autogen_imports:
128
+ final_string += "\n".join(autogen_imports) + "\n"
129
+ if third_party_imports:
130
+ final_string += "\n".join(third_party_imports) + "\n"
131
+ while not final_string.endswith("\n\n"):
132
+ final_string += "\n"
133
+ if local_imports:
134
+ final_string += "\n".join(local_imports) + "\n"
135
+ while not final_string.endswith("\n\n"):
136
+ final_string += "\n"
137
+ return final_string.replace("\n\n\n", "\n\n") # avoid too many newlines
138
+
139
+
140
+ def gather_imports(
141
+ model_imports: Optional[List[Tuple[str, ImportPosition]]],
142
+ skill_imports: Optional[List[Tuple[str, ImportPosition]]],
143
+ chat_imports: Optional[List[Tuple[str, ImportPosition]]],
144
+ agent_imports: Optional[List[Tuple[str, ImportPosition]]],
145
+ ) -> List[Tuple[str, ImportPosition]]:
146
+ """Gather all the imports.
147
+
148
+ Parameters
149
+ ----------
150
+ model_imports : Tuple[str, ImportPosition]
151
+ The model imports.
152
+ skill_imports : Tuple[str, ImportPosition]
153
+ The skill imports.
154
+ chat_imports : Tuple[str, ImportPosition]
155
+ The chat imports.
156
+ agent_imports : Tuple[str, ImportPosition]
157
+ The agent imports.
158
+
159
+ Returns
160
+ -------
161
+ Tuple[str, ImportPosition]
162
+ The gathered imports.
163
+ """
164
+ imports_string = get_standard_imports()
165
+ all_imports: List[Tuple[str, ImportPosition]] = [
166
+ (
167
+ imports_string,
168
+ ImportPosition.BUILTINS,
169
+ )
170
+ ]
171
+ if model_imports:
172
+ all_imports.extend(model_imports)
173
+ if skill_imports:
174
+ all_imports.extend(skill_imports)
175
+ if chat_imports:
176
+ all_imports.extend(chat_imports)
177
+ if agent_imports:
178
+ all_imports.extend(agent_imports)
179
+ return list(set(all_imports))
@@ -1,3 +1,5 @@
1
+ # SPDX-License-Identifier: Apache-2.0.
2
+ # Copyright (c) 2024 - 2025 Waldiez and contributors.
1
3
  """Logging related string generation functions.
2
4
 
3
5
  Functions
@@ -13,11 +15,6 @@ get_sqlite_to_csv_call_string
13
15
  """
14
16
 
15
17
 
16
- # Check issue:
17
- # Also check if in ag2 this still applies
18
- # https://github.com/microsoft/autogen/issues/2286
19
- # we cannot log new agents if they have code_execution enabled
20
- # we get `Path` is not JSON serializable (on code_executor)
21
18
  # pylint: disable=inconsistent-quotes
22
19
  def get_logging_start_string(tabs: int = 0) -> str:
23
20
  """Get the logging start string.
@@ -43,10 +40,10 @@ def get_logging_start_string(tabs: int = 0) -> str:
43
40
  ```
44
41
  """
45
42
  tab = " " * tabs
46
- content = f"{tab}runtime_logging.start(\n"
47
- content += f'{tab} logger_type="sqlite",\n'
48
- content += f'{tab} config={{"dbname": "flow.db"}},\n'
49
- content += f"{tab})\n"
43
+ content = f"{tab}runtime_logging.start(" + "\n"
44
+ content += f'{tab} logger_type="sqlite",' + "\n"
45
+ content += f'{tab} config={{"dbname": "flow.db"}},' + "\n"
46
+ content += f"{tab})" + "\n"
50
47
  return content
51
48
 
52
49
 
@@ -71,24 +68,24 @@ def get_logging_stop_string(tabs: int = 0) -> str:
71
68
  ```
72
69
  """
73
70
  tab = " " * tabs
74
- return f"{tab}runtime_logging.stop()\n"
71
+ return f"{tab}runtime_logging.stop()" + "\n"
75
72
 
76
73
 
77
74
  # pylint: disable=differing-param-doc,differing-type-doc
78
- def get_sqlite_to_csv_string() -> str:
79
- """Get the sqlite to csv conversion code string.
75
+ def get_sqlite_out() -> str:
76
+ """Get the sqlite to csv and json conversion code string.
80
77
 
81
78
  Returns
82
79
  -------
83
80
  str
84
- The sqlite to csv conversion code string.
81
+ The sqlite to csv and json conversion code string.
85
82
 
86
83
  Example
87
84
  -------
88
85
  ```python
89
- >>> get_sqlite_to_csv_string()
90
- def sqlite_to_csv(dbname: str, table: str, csv_file: str) -> None:
91
- \"\"\"Convert a sqlite table to a csv file.
86
+ >>> get_sqlite_outputs()
87
+ def get_sqlite_out(dbname: str, table: str, csv_file: str) -> None:
88
+ \"\"\"Convert a sqlite table to csv and json files.
92
89
 
93
90
  Parameters
94
91
  ----------
@@ -107,16 +104,19 @@ def get_sqlite_to_csv_string() -> str:
107
104
  data = [dict(zip(column_names, row)) for row in rows]
108
105
  conn.close()
109
106
  with open(csv_file, "w", newline="", encoding="utf-8") as file:
110
- _csv_writer = csv.DictWriter(file, fieldnames=column_names)
111
- _csv_writer.writeheader()
112
- _csv_writer.writerows(data)
107
+ csv_writer = csv.DictWriter(file, fieldnames=column_names)
108
+ csv_writer.writeheader()
109
+ csv_writer.writerows(data)
110
+ json_file = csv_file.replace(".csv", ".json")
111
+ with open(json_file, "w", encoding="utf-8") as file:
112
+ json.dump(data, file, indent=4, ensure_ascii=False)
113
113
  ```
114
114
  """
115
115
  content = "\n\n"
116
116
  content += (
117
- "def sqlite_to_csv(dbname: str, table: str, csv_file: str) -> None:\n"
117
+ "def get_sqlite_out(dbname: str, table: str, csv_file: str) -> None:\n"
118
118
  )
119
- content += ' """Convert a sqlite table to a csv file.\n\n'
119
+ content += ' """Convert a sqlite table to csv and json files.\n\n'
120
120
  content += " Parameters\n"
121
121
  content += " ----------\n"
122
122
  content += " dbname : str\n"
@@ -142,16 +142,19 @@ def get_sqlite_to_csv_string() -> str:
142
142
  ' with open(csv_file, "w", newline="", encoding="utf-8") as file:\n'
143
143
  )
144
144
  content += (
145
- " _csv_writer = csv.DictWriter(file, fieldnames=column_names)\n"
145
+ " csv_writer = csv.DictWriter(file, fieldnames=column_names)\n"
146
146
  )
147
- content += " _csv_writer.writeheader()\n"
148
- content += " _csv_writer.writerows(data)\n"
147
+ content += " csv_writer.writeheader()\n"
148
+ content += " csv_writer.writerows(data)\n"
149
+ content += ' json_file = csv_file.replace(".csv", ".json")\n'
150
+ content += ' with open(json_file, "w", encoding="utf-8") as file:\n'
151
+ content += " json.dump(data, file, indent=4, ensure_ascii=False)\n"
149
152
  content += "\n\n"
150
153
  return content
151
154
 
152
155
 
153
- def get_sqlite_to_csv_call_string(tabs: int = 0) -> str:
154
- """Get the sqlite to csv conversion call string.
156
+ def get_sqlite_out_call(tabs: int = 0) -> str:
157
+ """Get the sqlite to csv and json conversion call string.
155
158
 
156
159
  Parameters
157
160
  ----------
@@ -166,7 +169,7 @@ def get_sqlite_to_csv_call_string(tabs: int = 0) -> str:
166
169
  Example
167
170
  -------
168
171
  ```python
169
- >>> get_sqlite_to_csv_call_string()
172
+ >>> get_sqlite_out_call()
170
173
  if not os.path.exists("logs"):
171
174
  os.makedirs("logs")
172
175
  for table in [
@@ -179,7 +182,7 @@ def get_sqlite_to_csv_call_string(tabs: int = 0) -> str:
179
182
  "function_calls",
180
183
  ]:
181
184
  dest = os.path.join("logs", f"{table}.csv")
182
- sqlite_to_csv("flow.db", table, dest)
185
+ get_sqlite_out("flow.db", table, dest)
183
186
  ```
184
187
  """
185
188
  table_names = [
@@ -197,8 +200,8 @@ def get_sqlite_to_csv_call_string(tabs: int = 0) -> str:
197
200
  content += tab + ' os.makedirs("logs")\n'
198
201
  content += tab + "for table in [\n"
199
202
  for table in table_names:
200
- content += tab + f' "{table}",\n'
203
+ content += tab + f' "{table}",' + "\n"
201
204
  content += tab + "]:\n"
202
- content += tab + ' dest = os.path.join("logs", f"{table}.csv")\n'
203
- content += tab + ' sqlite_to_csv("flow.db", table, dest)\n'
205
+ content += tab + ' dest = os.path.join("logs", f"{table}.csv")' + "\n"
206
+ content += tab + ' get_sqlite_out("flow.db", table, dest)\n'
204
207
  return content
@@ -1,244 +1,9 @@
1
- """Model/LLM related string generation functions.
1
+ # SPDX-License-Identifier: Apache-2.0.
2
+ # Copyright (c) 2024 - 2025 Waldiez and contributors.
3
+ """Models (llm_configs) exporter."""
2
4
 
3
- Functions
4
- ---------
5
- export_models
6
- Get the string representations of the LLM configs.
7
- """
5
+ from .models_exporter import ModelsExporter
8
6
 
9
- from pathlib import Path
10
- from typing import Dict, List, Optional
11
-
12
- from waldiez.models import WaldiezModel
13
-
14
- from ..utils import get_comment, get_object_string
15
-
16
-
17
- def export_models(
18
- all_models: List[WaldiezModel],
19
- model_names: Dict[str, str],
20
- notebook: bool,
21
- output_dir: Optional[Path] = None,
22
- ) -> str:
23
- """Get the string representations of the LLM configs.
24
-
25
- Parameters
26
- ----------
27
- all_models : List[WaldiezModel]
28
- The models.
29
- model_names : Dict[str, str]
30
- A mapping of model ids to model names.
31
- notebook : bool
32
- Whether to export the string for a jupyter notebook.
33
- output_dir : Optional[Path]
34
- The output directory to write the api keys.
35
- Returns
36
- -------
37
- str
38
- The string representation of the models.
39
-
40
- Example
41
- -------
42
- ```python
43
- >>> from waldiez.models import WaldiezModel, WaldiezModelData
44
- >>> api_key = "1234567890"
45
- ... model = WaldiezModel(
46
- ... id="wm-1",
47
- ... name="llama3.1" ,
48
- ... description="A model for llamas :P.",
49
- ... tags=["llama", "llama3.1"],
50
- ... requirements=[],
51
- ... data=WaldiezModelData(
52
- ... base_url="https://example.com/v1",
53
- ... api_key=api_key,
54
- ... api_type="openai",
55
- ... temperature=0.5,
56
- ... price={
57
- ... "prompt_price_per_1k": 0.0001,
58
- ... "completion_token_price_per_1k": 0.0002,
59
- ... },
60
- ... ),
61
- ... )
62
- >>> model_names = {"wm-1": "llama3_1"}
63
- >>> export_models([model], model_names, True)
64
-
65
- # # Models
66
- llama3_1_llm_config = {
67
- "config_list": [{
68
- "model": "llama3_1",
69
- "base_url": "https://example.com/v1",
70
- "api_key": get_model_api_key("llama3_1"),
71
- "api_type": "openai",
72
- "temperature": 0.5,
73
- "price": [0.0001, 0.0002],
74
- }]
75
- }
76
- ```
77
- """
78
- content = get_comment("models", notebook) + "\n"
79
- for model in all_models:
80
- model_name = model_names[model.id]
81
- llm_config = model.get_llm_config()
82
- llm_config["api_key"] = f'get_model_api_key("{model_name}")'
83
- model_dict_str = get_object_string(llm_config, tabs=2)
84
- model_dict_str = model_dict_str.replace(
85
- f'"get_model_api_key("{model_name}")"',
86
- f'get_model_api_key("{model_name}")',
87
- )
88
- content += f"{model_name}_llm_config = " + "{\n"
89
- content += ' "config_list": [\n'
90
- content += f" {model_dict_str}\n"
91
- content += " ]\n"
92
- content += "}\n"
93
- if output_dir:
94
- write_api_keys(all_models, model_names, output_dir)
95
- return content
96
-
97
-
98
- def export_agent_models(
99
- agent_model_ids: List[str],
100
- all_models: List[WaldiezModel],
101
- agent_name: str,
102
- ) -> str:
103
- """Get the string representations of the agent's registered models.
104
-
105
- Parameters
106
- ----------
107
- agent_model_ids : List[str]
108
- The model ids registered to the agent.
109
- all_models : List[WaldiezModel]
110
- All the models in the flow.
111
- agent_name : str
112
- The name of the agent.
113
-
114
- Returns
115
- -------
116
- str
117
- The agent's llm config string.
118
-
119
- Example
120
- -------
121
- ```python
122
- >>> from waldiez.models import WaldiezModel, WaldiezModelData
123
- >>> model1 = WaldiezModel(
124
- ... id="wm-1",
125
- ... name="llama3.1" ,
126
- ... description="A model for llamas :P.",
127
- ... tags=["llama", "llama3.1"],
128
- ... requirements=[],
129
- ... data=WaldiezModelData(
130
- ... base_url="https://example.com/v1",
131
- ... api_key="1234567890",
132
- ... api_type="openai",
133
- ... temperature=0.5,
134
- ... price={
135
- ... "prompt_price_per_1k": 0.0001,
136
- ... "completion_token_price_per_1k": 0.0002,
137
- ... },
138
- ... ),
139
- ... )
140
- >>> model2 = WaldiezModel(
141
- ... id="wm-2",
142
- ... name="llama3.2" ,
143
- ... description="A model for llamas :P.",
144
- ... tags=["llama", "llama3.2"],
145
- ... requirements=[],
146
- ... data=WaldiezModelData(
147
- ... base_url="https://example.com/v1",
148
- ... api_key="1234567890",
149
- ... api_type="openai",
150
- ... temperature=0.5,
151
- ... price={
152
- ... "prompt_price_per_1k": 0.0001,
153
- ... "completion_token_price_per_1k": 0.0002,
154
- ... },
155
- ... ),
156
- ... )
157
- >>> all_models = [model1, model2]
158
- >>> agent_model_ids = ["wm-1", "wm-2"]
159
- >>> export_agent_models(agent_model_ids, all_models, "llama_agent")
160
-
161
- llama_agent_llm_config = {
162
- "config_list": [
163
- {
164
- "model": "llama3.1",
165
- "base_url": "https://example.com/v1",
166
- "api_key": "1234567890",
167
- "api_type": "openai",
168
- "temperature": 0.5,
169
- "price": [0.0001, 0.0002],
170
- },
171
- {
172
- "model": "llama3.2",
173
- "base_url": "https://example.com/v1",
174
- "api_key": "1234567890",
175
- "api_type": "openai",
176
- "temperature": 0.5,
177
- "price": [0.0001, 0.0002],
178
- },
179
- ]
180
- }
181
- ```
182
- """
183
- content = f"{agent_name}_llm_config = " + "{\n"
184
- content += ' "config_list": [\n'
185
- for model_id in agent_model_ids:
186
- model = next((m for m in all_models if m.id == model_id), None)
187
- if model is not None:
188
- llm_config = model.get_llm_config()
189
- model_dict_str = get_object_string(llm_config, tabs=2)
190
- content += f" {model_dict_str},\n"
191
- content += " ]\n"
192
- content += "}\n"
193
- return content
194
-
195
-
196
- def write_api_keys(
197
- all_models: List[WaldiezModel],
198
- model_names: Dict[str, str],
199
- output_dir: Path,
200
- ) -> None:
201
- """Write the api keys to a separate file.
202
-
203
- Parameters
204
- ----------
205
- all_models : List[WaldiezModel]
206
- All the models in the flow.
207
- model_names : Dict[str, str]
208
- A mapping of model ids to model names.
209
- output_dir : Path
210
- The output directory to write the api keys.
211
- """
212
- # example call: llama3_1_api_key = get_model_api_key("llama3_1")
213
- api_keys_content = '''
214
- """API keys for the models."""
215
-
216
- __ALL_MODEL_API_KEYS__ = {
217
- '''
218
- for model in all_models:
219
- model_name = model_names[model.id]
220
- api_keys_content += f' "{model_name}": "{model.api_key}",\n'
221
- api_keys_content += "}\n"
222
-
223
- api_keys_content += '''
224
-
225
- def get_model_api_key(model_name: str) -> str:
226
- """Get the api key for the model.
227
-
228
- Parameters
229
- ----------
230
- model_name : str
231
- The name of the model.
232
-
233
- Returns
234
- -------
235
- str
236
- The api key for the model.
237
- """
238
- return __ALL_MODEL_API_KEYS__.get(model_name, "")
239
- '''
240
-
241
- with open(
242
- output_dir / "waldiez_api_keys.py", "w", encoding="utf-8", newline="\n"
243
- ) as f:
244
- f.write(api_keys_content)
7
+ __all__ = [
8
+ "ModelsExporter",
9
+ ]