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.

Files changed (94) hide show
  1. waldiez/__init__.py +15 -0
  2. waldiez/__main__.py +6 -0
  3. waldiez/_version.py +3 -0
  4. waldiez/cli.py +162 -0
  5. waldiez/exporter.py +293 -0
  6. waldiez/exporting/__init__.py +14 -0
  7. waldiez/exporting/agents/__init__.py +5 -0
  8. waldiez/exporting/agents/agent.py +229 -0
  9. waldiez/exporting/agents/agent_skills.py +67 -0
  10. waldiez/exporting/agents/code_execution.py +67 -0
  11. waldiez/exporting/agents/group_manager.py +209 -0
  12. waldiez/exporting/agents/llm_config.py +53 -0
  13. waldiez/exporting/agents/rag_user/__init__.py +5 -0
  14. waldiez/exporting/agents/rag_user/chroma_utils.py +134 -0
  15. waldiez/exporting/agents/rag_user/mongo_utils.py +83 -0
  16. waldiez/exporting/agents/rag_user/pgvector_utils.py +93 -0
  17. waldiez/exporting/agents/rag_user/qdrant_utils.py +112 -0
  18. waldiez/exporting/agents/rag_user/rag_user.py +165 -0
  19. waldiez/exporting/agents/rag_user/vector_db.py +119 -0
  20. waldiez/exporting/agents/teachability.py +37 -0
  21. waldiez/exporting/agents/termination_message.py +45 -0
  22. waldiez/exporting/chats/__init__.py +14 -0
  23. waldiez/exporting/chats/chats.py +46 -0
  24. waldiez/exporting/chats/helpers.py +395 -0
  25. waldiez/exporting/chats/nested.py +264 -0
  26. waldiez/exporting/flow/__init__.py +5 -0
  27. waldiez/exporting/flow/def_main.py +37 -0
  28. waldiez/exporting/flow/flow.py +185 -0
  29. waldiez/exporting/models/__init__.py +193 -0
  30. waldiez/exporting/skills/__init__.py +128 -0
  31. waldiez/exporting/utils/__init__.py +34 -0
  32. waldiez/exporting/utils/comments.py +136 -0
  33. waldiez/exporting/utils/importing.py +267 -0
  34. waldiez/exporting/utils/logging_utils.py +203 -0
  35. waldiez/exporting/utils/method_utils.py +35 -0
  36. waldiez/exporting/utils/naming.py +127 -0
  37. waldiez/exporting/utils/object_string.py +81 -0
  38. waldiez/io_stream.py +181 -0
  39. waldiez/models/__init__.py +107 -0
  40. waldiez/models/agents/__init__.py +65 -0
  41. waldiez/models/agents/agent/__init__.py +21 -0
  42. waldiez/models/agents/agent/agent.py +190 -0
  43. waldiez/models/agents/agent/agent_data.py +162 -0
  44. waldiez/models/agents/agent/code_execution.py +71 -0
  45. waldiez/models/agents/agent/linked_skill.py +30 -0
  46. waldiez/models/agents/agent/nested_chat.py +73 -0
  47. waldiez/models/agents/agent/teachability.py +68 -0
  48. waldiez/models/agents/agent/termination_message.py +167 -0
  49. waldiez/models/agents/agents.py +129 -0
  50. waldiez/models/agents/assistant/__init__.py +6 -0
  51. waldiez/models/agents/assistant/assistant.py +41 -0
  52. waldiez/models/agents/assistant/assistant_data.py +29 -0
  53. waldiez/models/agents/group_manager/__init__.py +19 -0
  54. waldiez/models/agents/group_manager/group_manager.py +87 -0
  55. waldiez/models/agents/group_manager/group_manager_data.py +91 -0
  56. waldiez/models/agents/group_manager/speakers.py +211 -0
  57. waldiez/models/agents/rag_user/__init__.py +26 -0
  58. waldiez/models/agents/rag_user/rag_user.py +58 -0
  59. waldiez/models/agents/rag_user/rag_user_data.py +32 -0
  60. waldiez/models/agents/rag_user/retrieve_config.py +592 -0
  61. waldiez/models/agents/rag_user/vector_db_config.py +162 -0
  62. waldiez/models/agents/user_proxy/__init__.py +6 -0
  63. waldiez/models/agents/user_proxy/user_proxy.py +41 -0
  64. waldiez/models/agents/user_proxy/user_proxy_data.py +30 -0
  65. waldiez/models/chat/__init__.py +22 -0
  66. waldiez/models/chat/chat.py +129 -0
  67. waldiez/models/chat/chat_data.py +326 -0
  68. waldiez/models/chat/chat_message.py +304 -0
  69. waldiez/models/chat/chat_nested.py +160 -0
  70. waldiez/models/chat/chat_summary.py +110 -0
  71. waldiez/models/common/__init__.py +38 -0
  72. waldiez/models/common/base.py +63 -0
  73. waldiez/models/common/method_utils.py +165 -0
  74. waldiez/models/flow/__init__.py +9 -0
  75. waldiez/models/flow/flow.py +302 -0
  76. waldiez/models/flow/flow_data.py +87 -0
  77. waldiez/models/model/__init__.py +11 -0
  78. waldiez/models/model/model.py +169 -0
  79. waldiez/models/model/model_data.py +86 -0
  80. waldiez/models/skill/__init__.py +9 -0
  81. waldiez/models/skill/skill.py +129 -0
  82. waldiez/models/skill/skill_data.py +37 -0
  83. waldiez/models/waldiez.py +301 -0
  84. waldiez/py.typed +0 -0
  85. waldiez/runner.py +304 -0
  86. waldiez/stream/__init__.py +7 -0
  87. waldiez/stream/consumer.py +139 -0
  88. waldiez/stream/provider.py +339 -0
  89. waldiez/stream/server.py +412 -0
  90. waldiez-0.1.0.dist-info/METADATA +181 -0
  91. waldiez-0.1.0.dist-info/RECORD +94 -0
  92. waldiez-0.1.0.dist-info/WHEEL +4 -0
  93. waldiez-0.1.0.dist-info/entry_points.txt +2 -0
  94. waldiez-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,264 @@
1
+ """Nested chats exporting."""
2
+
3
+ from typing import Dict, List, Optional, Tuple
4
+
5
+ from waldiez.models import (
6
+ WaldiezAgent,
7
+ WaldiezAgentNestedChat,
8
+ WaldiezAgentNestedChatMessage,
9
+ WaldiezChat,
10
+ )
11
+
12
+ from ..utils import get_escaped_string, get_object_string
13
+
14
+
15
+ def get_nested_chat_trigger_agent_names(
16
+ all_chats: List[WaldiezChat],
17
+ nested_chat: WaldiezAgentNestedChat,
18
+ agent_names: Dict[str, str],
19
+ ) -> str:
20
+ """Get the trigger agent names for the nested chat.
21
+
22
+ Parameters
23
+ ----------
24
+ all_chats : List[WaldiezChat]
25
+ All the chats in the flow.
26
+ nested_chat : WaldiezAgentNestedChat
27
+ The nested chat.
28
+ agent_names : Dict[str, str]
29
+ A mapping of agent id to agent name.
30
+
31
+ Returns
32
+ -------
33
+ str
34
+ The trigger agent names.
35
+ """
36
+ trigger_agent_ids: List[str] = []
37
+ for message in nested_chat.triggered_by:
38
+ waldiez_chat = next(chat for chat in all_chats if chat.id == message.id)
39
+ if message.is_reply:
40
+ trigger_agent_ids.append(waldiez_chat.target)
41
+ else:
42
+ trigger_agent_ids.append(waldiez_chat.source)
43
+ agents = [agent_names[agent_id] for agent_id in trigger_agent_ids]
44
+ trigger_string = f'{[", ".join(agents)]}'
45
+ return trigger_string.replace("'", '"')
46
+
47
+
48
+ def get_nested_chat_message_string(
49
+ waldiez_chat: WaldiezChat,
50
+ message: WaldiezAgentNestedChatMessage,
51
+ agent: WaldiezAgent,
52
+ agent_names: Dict[str, str],
53
+ chat_names: Dict[str, str],
54
+ ) -> Tuple[str, Optional[str]]:
55
+ """Get the nested chat message string.
56
+
57
+ Parameters
58
+ ----------
59
+ waldiez_chat : WaldiezChat
60
+ The chat.
61
+ message : WaldiezAgentNestedChatMessage
62
+ The message.
63
+ agent : WaldiezAgent
64
+ The agent.
65
+ agent_names : Dict[str, str]
66
+ A mapping of agent id to agent name.
67
+ chat_names : Dict[str, str]
68
+ A mapping of chat id to chat name.
69
+
70
+ Returns
71
+ -------
72
+ Tuple[str, Optional[str]]
73
+ The message string and the method name if the message is a method.
74
+ """
75
+ sender_name: Optional[str] = None
76
+ sender_id = waldiez_chat.target if message.is_reply else waldiez_chat.source
77
+ recipient_id = (
78
+ waldiez_chat.source if message.is_reply else waldiez_chat.target
79
+ )
80
+ if sender_id != agent.id:
81
+ sender_name = agent_names[sender_id]
82
+ recipient_name = agent_names[recipient_id]
83
+ chat_dict = waldiez_chat.get_chat_args()
84
+ chat_dict["recipient"] = recipient_name
85
+ if sender_name:
86
+ chat_dict["sender"] = sender_name
87
+ message_value, message_source = get_chat_nested_string(
88
+ chat=waldiez_chat, is_reply=message.is_reply, chat_names=chat_names
89
+ )
90
+ chat_dict["message"] = message_value
91
+ message_dict_str = get_object_string(chat_dict, tabs=1)
92
+ if message_source:
93
+ # it's not a string, its the name of the function
94
+ message_dict_str = message_dict_str.replace(
95
+ f': "{message_value}"', f": {message_value}"
96
+ )
97
+ if sender_name:
98
+ message_dict_str = message_dict_str.replace(
99
+ f': "{sender_name}"', f": {sender_name}"
100
+ )
101
+ if recipient_name:
102
+ message_dict_str = message_dict_str.replace(
103
+ f': "{recipient_name}"', f": {recipient_name}"
104
+ )
105
+ return message_dict_str, message_source
106
+
107
+
108
+ def get_nested_chat_queue(
109
+ nested_chat: WaldiezAgentNestedChat,
110
+ agent: WaldiezAgent,
111
+ agent_names: Dict[str, str],
112
+ chat_names: Dict[str, str],
113
+ all_chats: List[WaldiezChat],
114
+ ) -> Tuple[str, List[str]]:
115
+ """Get the nested chat queue.
116
+
117
+ Parameters
118
+ ----------
119
+ nested_chat : WaldiezAgentNestedChat
120
+ The nested chat.
121
+ agent : WaldiezAgent
122
+ The agent.
123
+ agent_names : Dict[str, str]
124
+ A mapping of agent id to agent name.
125
+ chat_names : Dict[str, str]
126
+ A mapping of chat id to chat name.
127
+ all_chats : List[WaldiezChat]
128
+ All the chats in the flow.
129
+
130
+ Returns
131
+ -------
132
+ Tuple[str, List[str]]
133
+ The nested chat queue and the methods to include.
134
+ """
135
+ message_methods_to_include = []
136
+ chat_messages_str = "[\n"
137
+ for message in nested_chat.messages:
138
+ waldiez_chat = next(chat for chat in all_chats if chat.id == message.id)
139
+ message_str, message_source = get_nested_chat_message_string(
140
+ waldiez_chat=waldiez_chat,
141
+ message=message,
142
+ agent=agent,
143
+ agent_names=agent_names,
144
+ chat_names=chat_names,
145
+ )
146
+ if message_source:
147
+ message_methods_to_include.append(message_source)
148
+ chat_messages_str += f" {message_str}," + "\n"
149
+ chat_messages_str += "]"
150
+ if chat_messages_str == "[\n]":
151
+ return "", message_methods_to_include
152
+ return chat_messages_str, message_methods_to_include
153
+
154
+
155
+ def get_chat_nested_string(
156
+ chat: WaldiezChat,
157
+ is_reply: bool,
158
+ chat_names: Dict[str, str],
159
+ ) -> Tuple[str, Optional[str]]:
160
+ """Get the nested chat message.
161
+
162
+ Parameters
163
+ ----------
164
+ chat : WaldiezChat
165
+ The chat.
166
+ is_reply : bool
167
+ Whether to use the nested chat's reply message or not.
168
+ chat_names : Dict[str, str]
169
+ A mapping of chat id to chat name
170
+
171
+ Returns
172
+ -------
173
+ Tuple[str, Optional[str]]
174
+ If the message is a string, the message content and None.
175
+ If the message is a method, the method name and the method content.
176
+ If the message is None, 'None' and None.
177
+ """
178
+ message = (
179
+ chat.data.nested_chat.reply
180
+ if is_reply
181
+ else chat.data.nested_chat.message
182
+ )
183
+ if not message or message.type == "none" or message.content is None:
184
+ return "None", None
185
+ if message.type == "string":
186
+ return get_escaped_string(message.content), None
187
+ chat_name = chat_names[chat.id]
188
+ method_args = "recipient, messages, sender, config"
189
+ function_name = "nested_chat_reply" if is_reply else "nested_chat_message"
190
+ new_function_name = f"{function_name}_{chat_name}"
191
+ function_def = f"\ndef {new_function_name}({method_args}):"
192
+ attribute_name = "reply_content" if is_reply else "message_content"
193
+ function_content = getattr(chat.data.nested_chat, attribute_name)
194
+ return new_function_name, function_def + "\n" + function_content + "\n"
195
+
196
+
197
+ def export_nested_chat(
198
+ agent: WaldiezAgent,
199
+ all_chats: List[WaldiezChat],
200
+ chat_names: Dict[str, str],
201
+ agent_names: Dict[str, str],
202
+ ) -> str:
203
+ """Get the nested chat string.
204
+
205
+ Parameters
206
+ ----------
207
+ agent : WaldiezAgent
208
+ The agent.
209
+ all_chats : List[WaldiezChat]
210
+ All the chats in the flow.
211
+ chat_names : Dict[str, str]
212
+ The chat names.
213
+ agent_names : Dict[str, str]
214
+ The agent names.
215
+
216
+ Returns
217
+ -------
218
+ str
219
+ The nested chat string.
220
+ """
221
+ if not agent.data.nested_chats:
222
+ return ""
223
+ content = ""
224
+ extra_contents = []
225
+ agent_name = agent_names[agent.id]
226
+ use_suffix = len(agent.data.nested_chats) > 1
227
+ for index, entry in enumerate(agent.data.nested_chats):
228
+ trigger_names = get_nested_chat_trigger_agent_names(
229
+ all_chats=all_chats, nested_chat=entry, agent_names=agent_names
230
+ )
231
+ chat_queue, extra_methods = get_nested_chat_queue(
232
+ nested_chat=entry,
233
+ agent=agent,
234
+ agent_names=agent_names,
235
+ chat_names=chat_names,
236
+ all_chats=all_chats,
237
+ )
238
+ if not chat_queue:
239
+ continue
240
+ extra_contents.extend(extra_methods)
241
+ var_name = (
242
+ f"{agent_name}_chat_queue_{index}"
243
+ if use_suffix
244
+ else f"{agent_name}_chat_queue"
245
+ )
246
+ content += f"\n{var_name} = {chat_queue}" + "\n"
247
+ content += f"""\n
248
+ {agent_name}.register_nested_chats(
249
+ trigger={trigger_names},
250
+ chat_queue={var_name},
251
+ )\n
252
+ """
253
+ functions_string = "\n".join(sorted(extra_contents))
254
+ if functions_string:
255
+ functions_string = functions_string + "\n"
256
+ content = f"{functions_string}{content}"
257
+ return (
258
+ content.replace('"None"', "None")
259
+ .replace("'None'", "None")
260
+ .replace('"False"', "False")
261
+ .replace("'False'", "False")
262
+ .replace("'True'", "True")
263
+ .replace('"True"', "True")
264
+ )
@@ -0,0 +1,5 @@
1
+ """Export the entire flow to string."""
2
+
3
+ from .flow import export_flow
4
+
5
+ __all__ = ["export_flow"]
@@ -0,0 +1,37 @@
1
+ """Get the main function (if exporting to python)."""
2
+
3
+ from ..utils import (
4
+ get_logging_start_string,
5
+ get_logging_stop_string,
6
+ get_sqlite_to_csv_call_string,
7
+ )
8
+
9
+
10
+ def get_def_main(waldiez_chats: str) -> str:
11
+ """Get the main function.
12
+
13
+ When exporting to python, waldiez_chats string will be the
14
+ content of the main function. It contains either a
15
+ `{sender.initiate_chat(recipient, ...)}` (if there is only one chat)
16
+ or `initiate_chats([..])`, with the list of chats to initiate.
17
+
18
+ Parameters
19
+ ----------
20
+ waldiez_chats : str
21
+ The content of the main function.
22
+
23
+ Returns
24
+ -------
25
+ str
26
+ The main function.
27
+ """
28
+ content = """def main():
29
+ # type: () -> Union[ChatResult, List[ChatResult]]
30
+ \"\"\"Start chatting.\"\"\"
31
+ """
32
+ content += get_logging_start_string(1)
33
+ content += f" results = {waldiez_chats}" + "\n"
34
+ content += get_logging_stop_string(1) + "\n"
35
+ content += get_sqlite_to_csv_call_string(1) + "\n"
36
+ content += " return results\n"
37
+ return content
@@ -0,0 +1,185 @@
1
+ """Export the entire flow to string."""
2
+
3
+ from pathlib import Path
4
+ from typing import Dict, List, Optional, Set, Tuple
5
+
6
+ from waldiez.models import (
7
+ Waldiez,
8
+ WaldiezAgent,
9
+ WaldiezChat,
10
+ WaldiezModel,
11
+ WaldiezSkill,
12
+ )
13
+
14
+ from ..agents import export_agent
15
+ from ..chats import export_chats, export_nested_chat
16
+ from ..models import export_models
17
+ from ..skills import export_skills
18
+ from ..utils import (
19
+ get_comment,
20
+ get_imports_string,
21
+ get_logging_start_string,
22
+ get_logging_stop_string,
23
+ get_pylint_ignore_comment,
24
+ get_sqlite_to_csv_call_string,
25
+ get_sqlite_to_csv_string,
26
+ )
27
+ from .def_main import get_def_main
28
+
29
+
30
+ # pylint: disable=too-many-locals
31
+ def export_flow(
32
+ waldiez: Waldiez,
33
+ agents: Tuple[List[WaldiezAgent], Dict[str, str]],
34
+ chats: Tuple[List[WaldiezChat], Dict[str, str]],
35
+ models: Tuple[List[WaldiezModel], Dict[str, str]],
36
+ skills: Tuple[List[WaldiezSkill], Dict[str, str]],
37
+ output_dir: Optional[Path],
38
+ notebook: bool,
39
+ ) -> str:
40
+ """Export the entire flow to a string.
41
+
42
+ It contains the required imports, the model and skill definitions,
43
+ the agent definitions, the links between agents, models and skills,
44
+ the agents' nested chats, the chat definitions, and the actual call
45
+ to start the chat(s).
46
+
47
+ Parameters
48
+ ----------
49
+ waldiez : Waldiez
50
+ The Waldiez instance.
51
+ agents : Tuple[List[WaldiezAgent], Dict[str, str]]
52
+ The agents and their names.
53
+ chats : Tuple[List[WaldiezChat], Dict[str, str]]
54
+ The chats and their names.
55
+ models : Tuple[List[WaldiezModel], Dict[str, str]]
56
+ The models and their names.
57
+ skills : Tuple[List[WaldiezSkill], Dict[str, str]]
58
+ The skills and their names.
59
+ output_dir : Optional[Path]
60
+ The output directory.
61
+ notebook : bool
62
+ Whether the export is for a jupyter notebook or a python script.
63
+
64
+ Returns
65
+ -------
66
+ str
67
+ The flow string.
68
+ """
69
+ all_agents, agent_names = agents
70
+ all_models, model_names = models
71
+ all_skills, skill_names = skills
72
+ all_chats, chat_names = chats
73
+ agent_strings = ""
74
+ # we need to add `skipped_agent_strings` after the other agents are defined
75
+ # for example, a group_manager needs the group members to have been defined
76
+ skipped_agent_strings = ""
77
+ nested_chats_strings = ""
78
+ builtin_imports: Set[str] = {
79
+ "import csv",
80
+ "import os",
81
+ "import sqlite3",
82
+ }
83
+ other_imports: Set[str] = {
84
+ "from autogen import Agent",
85
+ "from autogen import ConversableAgent",
86
+ "from autogen import ChatResult",
87
+ "from autogen import runtime_logging",
88
+ }
89
+ skill_imports, _ = export_skills(
90
+ skills=all_skills,
91
+ skill_names=skill_names,
92
+ output_dir=output_dir,
93
+ )
94
+ if len(waldiez.chats) > 1:
95
+ other_imports.add("from autogen import initiate_chats")
96
+ for agent in all_agents:
97
+ agent_string, after_agent, agent_imports = export_agent(
98
+ agent=agent,
99
+ agent_names=agent_names,
100
+ model_names=model_names,
101
+ skill_names=skill_names,
102
+ all_models=all_models,
103
+ all_skills=all_skills,
104
+ group_chat_members=waldiez.flow.get_group_chat_members(agent.id),
105
+ )
106
+ other_imports.update(agent_imports)
107
+ if after_agent:
108
+ skipped_agent_strings += after_agent
109
+ if agent.agent_type == "manager":
110
+ skipped_agent_strings += agent_string
111
+ else:
112
+ agent_strings += agent_string
113
+ agent_nested_chats_string = export_nested_chat(
114
+ agent=agent,
115
+ agent_names=agent_names,
116
+ all_chats=all_chats,
117
+ chat_names=chat_names,
118
+ )
119
+ if agent_nested_chats_string:
120
+ nested_chats_strings += "\n" + agent_nested_chats_string
121
+ agent_strings += skipped_agent_strings
122
+ models_string = export_models(
123
+ all_models=all_models,
124
+ model_names=model_names,
125
+ notebook=notebook,
126
+ )
127
+ all_imports_string = get_imports_string(
128
+ imports=other_imports,
129
+ builtin_imports=builtin_imports,
130
+ skill_imports=skill_imports,
131
+ )
132
+ return _combine_strings(
133
+ waldiez=waldiez,
134
+ imports_string=all_imports_string,
135
+ agents_string=agent_strings,
136
+ nested_chats_string=nested_chats_strings,
137
+ models_string=models_string,
138
+ agent_names=agent_names,
139
+ chat_names=chat_names,
140
+ notebook=notebook,
141
+ )
142
+
143
+
144
+ # pylint: disable=too-many-arguments
145
+ def _combine_strings(
146
+ waldiez: Waldiez,
147
+ imports_string: str,
148
+ agents_string: str,
149
+ nested_chats_string: str,
150
+ models_string: str,
151
+ agent_names: Dict[str, str],
152
+ chat_names: Dict[str, str],
153
+ notebook: bool,
154
+ ) -> str:
155
+ content = get_pylint_ignore_comment(notebook)
156
+ content += imports_string
157
+ # content += get_comment("logging", notebook) + "\n"
158
+ # content += get_logging_start_string(tabs=0) + "\n\n"
159
+ content += models_string
160
+ content += get_comment("agents", notebook) + "\n"
161
+ content += agents_string
162
+ if nested_chats_string:
163
+ content += get_comment("nested", notebook) + "\n"
164
+ content += nested_chats_string
165
+ chats_content, additional_methods = export_chats(
166
+ main_chats=waldiez.chats,
167
+ agent_names=agent_names,
168
+ chat_names=chat_names,
169
+ tabs=0 if notebook else 1,
170
+ )
171
+ if additional_methods:
172
+ while not content.endswith("\n\n"): # pragma: no cover
173
+ content += "\n"
174
+ content += "\n" + additional_methods + "\n"
175
+ content += get_sqlite_to_csv_string()
176
+ content += get_comment("run", notebook) + "\n"
177
+ if not notebook:
178
+ content += get_def_main(chats_content)
179
+ else:
180
+ content += get_logging_start_string(tabs=0)
181
+ content += "\n" + chats_content + "\n"
182
+ content += get_logging_stop_string(tabs=0) + "\n"
183
+ content += get_sqlite_to_csv_call_string(tabs=0) + "\n"
184
+ content = content.replace("\n\n\n\n", "\n\n\n")
185
+ return content
@@ -0,0 +1,193 @@
1
+ """Model/LLM related string generation functions.
2
+
3
+ Functions
4
+ ---------
5
+ export_models
6
+ Get the string representations of the LLM configs.
7
+ """
8
+
9
+ from typing import Dict, List
10
+
11
+ from waldiez.models import WaldiezModel
12
+
13
+ from ..utils import get_comment, get_object_string
14
+
15
+
16
+ def export_models(
17
+ all_models: List[WaldiezModel],
18
+ model_names: Dict[str, str],
19
+ notebook: bool,
20
+ ) -> str:
21
+ """Get the string representations of the LLM configs.
22
+
23
+ Parameters
24
+ ----------
25
+ all_models : List[WaldiezModel]
26
+ The models.
27
+ model_names : Dict[str, str]
28
+ A mapping of model ids to model names.
29
+ notebook : bool
30
+ Whether to export the string for a jupyter notebook.
31
+
32
+ Returns
33
+ -------
34
+ str
35
+ The string representation of the models.
36
+
37
+ Example
38
+ -------
39
+ ```python
40
+ >>> from waldiez.models import WaldiezModel, WaldiezModelData
41
+ >>> model = WaldiezModel(
42
+ ... id="wm-1",
43
+ ... name="llama3.1" ,
44
+ ... description="A model for llamas :P.",
45
+ ... tags=["llama", "llama3.1"],
46
+ ... requirements=[],
47
+ ... data=WaldiezModelData(
48
+ ... base_url="https://example.com/v1",
49
+ ... api_key="1234567890",
50
+ ... api_type="openai",
51
+ ... temperature=0.5,
52
+ ... price={
53
+ ... "prompt_price_per_1k": 0.0001,
54
+ ... "completion_token_price_per_1k": 0.0002,
55
+ ... },
56
+ ... ),
57
+ ... )
58
+ >>> model_names = {"wm-1": "llama3_1"}
59
+ >>> export_models([model], model_names, True)
60
+
61
+ # # Models
62
+ llama3_1_llm_config = {
63
+ "config_list": [{
64
+ "model": "llama3.1",
65
+ "base_url": "https://example.com/v1",
66
+ "api_key": "1234567890",
67
+ "api_type": "openai",
68
+ "temperature": 0.5,
69
+ "price": [0.0001, 0.0002],
70
+ }]
71
+ }
72
+ ```
73
+ """
74
+ content = get_comment("models", notebook) + "\n"
75
+ if len(all_models) == 1:
76
+ only_model = all_models[0]
77
+ model_name = model_names[only_model.id]
78
+ llm_config = only_model.get_llm_config()
79
+ model_dict_str = get_object_string(llm_config, tabs=2)
80
+ content += f"{model_name}_llm_config = " + "{\n"
81
+ content += ' "config_list": [\n'
82
+ content += f" {model_dict_str}\n"
83
+ content += " ]\n"
84
+ content += "}\n"
85
+ else:
86
+ for model in all_models:
87
+ model_name = model_names[model.id]
88
+ llm_config = model.get_llm_config()
89
+ model_dict_str = get_object_string(llm_config, tabs=2)
90
+ content += f"{model_name}_llm_config = " + "{\n"
91
+ content += ' "config_list": [\n'
92
+ content += f" {model_dict_str}\n"
93
+ content += " ]\n"
94
+ content += "}\n"
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