waldiez 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of waldiez might be problematic. Click here for more details.
- waldiez/__init__.py +15 -0
- waldiez/__main__.py +6 -0
- waldiez/_version.py +3 -0
- waldiez/cli.py +162 -0
- waldiez/exporter.py +293 -0
- waldiez/exporting/__init__.py +14 -0
- waldiez/exporting/agents/__init__.py +5 -0
- waldiez/exporting/agents/agent.py +229 -0
- waldiez/exporting/agents/agent_skills.py +67 -0
- waldiez/exporting/agents/code_execution.py +67 -0
- waldiez/exporting/agents/group_manager.py +209 -0
- waldiez/exporting/agents/llm_config.py +53 -0
- waldiez/exporting/agents/rag_user/__init__.py +5 -0
- waldiez/exporting/agents/rag_user/chroma_utils.py +134 -0
- waldiez/exporting/agents/rag_user/mongo_utils.py +83 -0
- waldiez/exporting/agents/rag_user/pgvector_utils.py +93 -0
- waldiez/exporting/agents/rag_user/qdrant_utils.py +112 -0
- waldiez/exporting/agents/rag_user/rag_user.py +165 -0
- waldiez/exporting/agents/rag_user/vector_db.py +119 -0
- waldiez/exporting/agents/teachability.py +37 -0
- waldiez/exporting/agents/termination_message.py +45 -0
- waldiez/exporting/chats/__init__.py +14 -0
- waldiez/exporting/chats/chats.py +46 -0
- waldiez/exporting/chats/helpers.py +395 -0
- waldiez/exporting/chats/nested.py +264 -0
- waldiez/exporting/flow/__init__.py +5 -0
- waldiez/exporting/flow/def_main.py +37 -0
- waldiez/exporting/flow/flow.py +185 -0
- waldiez/exporting/models/__init__.py +193 -0
- waldiez/exporting/skills/__init__.py +128 -0
- waldiez/exporting/utils/__init__.py +34 -0
- waldiez/exporting/utils/comments.py +136 -0
- waldiez/exporting/utils/importing.py +267 -0
- waldiez/exporting/utils/logging_utils.py +203 -0
- waldiez/exporting/utils/method_utils.py +35 -0
- waldiez/exporting/utils/naming.py +127 -0
- waldiez/exporting/utils/object_string.py +81 -0
- waldiez/io_stream.py +181 -0
- waldiez/models/__init__.py +107 -0
- waldiez/models/agents/__init__.py +65 -0
- waldiez/models/agents/agent/__init__.py +21 -0
- waldiez/models/agents/agent/agent.py +190 -0
- waldiez/models/agents/agent/agent_data.py +162 -0
- waldiez/models/agents/agent/code_execution.py +71 -0
- waldiez/models/agents/agent/linked_skill.py +30 -0
- waldiez/models/agents/agent/nested_chat.py +73 -0
- waldiez/models/agents/agent/teachability.py +68 -0
- waldiez/models/agents/agent/termination_message.py +167 -0
- waldiez/models/agents/agents.py +129 -0
- waldiez/models/agents/assistant/__init__.py +6 -0
- waldiez/models/agents/assistant/assistant.py +41 -0
- waldiez/models/agents/assistant/assistant_data.py +29 -0
- waldiez/models/agents/group_manager/__init__.py +19 -0
- waldiez/models/agents/group_manager/group_manager.py +87 -0
- waldiez/models/agents/group_manager/group_manager_data.py +91 -0
- waldiez/models/agents/group_manager/speakers.py +211 -0
- waldiez/models/agents/rag_user/__init__.py +26 -0
- waldiez/models/agents/rag_user/rag_user.py +58 -0
- waldiez/models/agents/rag_user/rag_user_data.py +32 -0
- waldiez/models/agents/rag_user/retrieve_config.py +592 -0
- waldiez/models/agents/rag_user/vector_db_config.py +162 -0
- waldiez/models/agents/user_proxy/__init__.py +6 -0
- waldiez/models/agents/user_proxy/user_proxy.py +41 -0
- waldiez/models/agents/user_proxy/user_proxy_data.py +30 -0
- waldiez/models/chat/__init__.py +22 -0
- waldiez/models/chat/chat.py +129 -0
- waldiez/models/chat/chat_data.py +326 -0
- waldiez/models/chat/chat_message.py +304 -0
- waldiez/models/chat/chat_nested.py +160 -0
- waldiez/models/chat/chat_summary.py +110 -0
- waldiez/models/common/__init__.py +38 -0
- waldiez/models/common/base.py +63 -0
- waldiez/models/common/method_utils.py +165 -0
- waldiez/models/flow/__init__.py +9 -0
- waldiez/models/flow/flow.py +302 -0
- waldiez/models/flow/flow_data.py +87 -0
- waldiez/models/model/__init__.py +11 -0
- waldiez/models/model/model.py +169 -0
- waldiez/models/model/model_data.py +86 -0
- waldiez/models/skill/__init__.py +9 -0
- waldiez/models/skill/skill.py +129 -0
- waldiez/models/skill/skill_data.py +37 -0
- waldiez/models/waldiez.py +301 -0
- waldiez/py.typed +0 -0
- waldiez/runner.py +304 -0
- waldiez/stream/__init__.py +7 -0
- waldiez/stream/consumer.py +139 -0
- waldiez/stream/provider.py +339 -0
- waldiez/stream/server.py +412 -0
- waldiez-0.1.0.dist-info/METADATA +181 -0
- waldiez-0.1.0.dist-info/RECORD +94 -0
- waldiez-0.1.0.dist-info/WHEEL +4 -0
- waldiez-0.1.0.dist-info/entry_points.txt +2 -0
- waldiez-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"""Vector DB exporting utils for RAG user agents."""
|
|
2
|
+
|
|
3
|
+
# pylint: disable=line-too-long
|
|
4
|
+
from typing import Any, Set, Tuple
|
|
5
|
+
|
|
6
|
+
from waldiez.models import WaldiezRagUser
|
|
7
|
+
|
|
8
|
+
from .chroma_utils import get_chroma_db_args
|
|
9
|
+
from .mongo_utils import get_mongodb_db_args
|
|
10
|
+
from .pgvector_utils import get_pgvector_db_args
|
|
11
|
+
from .qdrant_utils import get_qdrant_db_args
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _get_metadata_arg(
|
|
15
|
+
agent: WaldiezRagUser,
|
|
16
|
+
) -> str:
|
|
17
|
+
"""Get the metadata arg.
|
|
18
|
+
|
|
19
|
+
Parameters
|
|
20
|
+
----------
|
|
21
|
+
agent : WaldiezRagUser
|
|
22
|
+
The agent.
|
|
23
|
+
|
|
24
|
+
Returns
|
|
25
|
+
-------
|
|
26
|
+
str
|
|
27
|
+
The metadata arg.
|
|
28
|
+
"""
|
|
29
|
+
metadata_arg = ""
|
|
30
|
+
if agent.retrieve_config.db_config.metadata:
|
|
31
|
+
tab = " "
|
|
32
|
+
indent = tab * 3
|
|
33
|
+
metadata_arg += f"{indent}metadata={{\n"
|
|
34
|
+
for key, value in agent.retrieve_config.db_config.metadata.items():
|
|
35
|
+
value_string: Any = f'"{value}"'
|
|
36
|
+
if str(value).isdigit():
|
|
37
|
+
value_string = int(value)
|
|
38
|
+
elif str(value).replace(".", "").isdigit():
|
|
39
|
+
value_string = float(value)
|
|
40
|
+
metadata_arg += f'{indent} "{key}": {value_string},\n'
|
|
41
|
+
metadata_arg += f"{indent}}},\n"
|
|
42
|
+
return metadata_arg
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def get_rag_user_vector_db_string(
|
|
46
|
+
agent: WaldiezRagUser,
|
|
47
|
+
agent_name: str,
|
|
48
|
+
) -> Tuple[str, str, Set[str]]:
|
|
49
|
+
"""Get the RAG user vector db string.
|
|
50
|
+
|
|
51
|
+
The vector db can be one of the following:
|
|
52
|
+
"vector_db": ChromaVectorDB(....)
|
|
53
|
+
"vector_db": QdrantVectorDB(....)
|
|
54
|
+
"vector_db": MongoDBAtlasVectorDB(....)
|
|
55
|
+
"vector_db": PGVectorDB(....)
|
|
56
|
+
|
|
57
|
+
If a custom embedding function is to be used,
|
|
58
|
+
it's name will be in the arg and its definition will be before the arg.
|
|
59
|
+
|
|
60
|
+
Parameters
|
|
61
|
+
----------
|
|
62
|
+
agent : WaldiezRagUser
|
|
63
|
+
The agent.
|
|
64
|
+
agent_name : str
|
|
65
|
+
The agent's name.
|
|
66
|
+
|
|
67
|
+
Returns
|
|
68
|
+
-------
|
|
69
|
+
Tuple[str, str, Set[str]]
|
|
70
|
+
The content before the arg if any, the arg and the related imports.
|
|
71
|
+
"""
|
|
72
|
+
before = ""
|
|
73
|
+
imports: Set[str] = set()
|
|
74
|
+
ef_body: str = ""
|
|
75
|
+
db_imports: Set[str] = set()
|
|
76
|
+
kwarg_string = ""
|
|
77
|
+
content_before = ""
|
|
78
|
+
vdb_class = "ChromaVectorDB"
|
|
79
|
+
if agent.retrieve_config.vector_db == "chroma":
|
|
80
|
+
imports.add(
|
|
81
|
+
"from autogen.agentchat.contrib.vectordb.chromadb import ChromaVectorDB"
|
|
82
|
+
)
|
|
83
|
+
kwarg_string, db_imports, ef_body, content_before = get_chroma_db_args(
|
|
84
|
+
agent, agent_name
|
|
85
|
+
)
|
|
86
|
+
if agent.retrieve_config.vector_db == "qdrant":
|
|
87
|
+
vdb_class = "QdrantVectorDB"
|
|
88
|
+
imports.add(
|
|
89
|
+
"from autogen.agentchat.contrib.vectordb.qdrant import QdrantVectorDB"
|
|
90
|
+
)
|
|
91
|
+
kwarg_string, db_imports, ef_body = get_qdrant_db_args(
|
|
92
|
+
agent, agent_name
|
|
93
|
+
)
|
|
94
|
+
if agent.retrieve_config.vector_db == "mongodb":
|
|
95
|
+
vdb_class = "MongoDBAtlasVectorDB"
|
|
96
|
+
imports.add(
|
|
97
|
+
"from autogen.agentchat.contrib.vectordb.mongo import MongoDBAtlasVectorDB"
|
|
98
|
+
)
|
|
99
|
+
kwarg_string, db_imports, ef_body = get_mongodb_db_args(
|
|
100
|
+
agent, agent_name
|
|
101
|
+
)
|
|
102
|
+
if agent.retrieve_config.vector_db == "pgvector":
|
|
103
|
+
imports.add(
|
|
104
|
+
"from autogen.agentchat.contrib.vectordb.pgvector import PGVectorDB"
|
|
105
|
+
)
|
|
106
|
+
vdb_class = "PGVectorDB"
|
|
107
|
+
kwarg_string, db_imports, ef_body = get_pgvector_db_args(
|
|
108
|
+
agent, agent_name
|
|
109
|
+
)
|
|
110
|
+
if content_before:
|
|
111
|
+
before += f"\n{content_before}"
|
|
112
|
+
if ef_body:
|
|
113
|
+
before += f"\n{ef_body}\n"
|
|
114
|
+
if db_imports:
|
|
115
|
+
imports.update(db_imports)
|
|
116
|
+
kwarg_string += _get_metadata_arg(agent)
|
|
117
|
+
vdb_arg = f"{vdb_class}(\n"
|
|
118
|
+
vdb_arg += kwarg_string + " )"
|
|
119
|
+
return before, vdb_arg, imports
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""Exporting teachability data for agents."""
|
|
2
|
+
|
|
3
|
+
from typing import Dict
|
|
4
|
+
|
|
5
|
+
from waldiez.models import WaldiezAgent
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def get_agent_teachability_string(
|
|
9
|
+
agent: WaldiezAgent,
|
|
10
|
+
agent_names: Dict[str, str],
|
|
11
|
+
) -> str:
|
|
12
|
+
"""Get the teachability string to use for the agent.
|
|
13
|
+
|
|
14
|
+
Parameters
|
|
15
|
+
----------
|
|
16
|
+
agent : WaldiezAgent
|
|
17
|
+
The agent.
|
|
18
|
+
agent_names : Dict[str, str]
|
|
19
|
+
A mapping of agent id to agent name.
|
|
20
|
+
|
|
21
|
+
Returns
|
|
22
|
+
-------
|
|
23
|
+
str
|
|
24
|
+
The teachability string
|
|
25
|
+
"""
|
|
26
|
+
if not agent.data.teachability.enabled:
|
|
27
|
+
return ""
|
|
28
|
+
agent_name = agent_names[agent.id]
|
|
29
|
+
teachability = agent.data.teachability
|
|
30
|
+
content = f"{agent_name}_teachability = teachability.Teachability(\n"
|
|
31
|
+
content += f" verbosity={teachability.verbosity},\n"
|
|
32
|
+
content += f" reset_db={teachability.reset_db},\n"
|
|
33
|
+
content += f" recall_threshold={teachability.recall_threshold},\n"
|
|
34
|
+
content += f" max_num_retrievals={teachability.max_num_retrievals},\n"
|
|
35
|
+
content += ")\n\n\n"
|
|
36
|
+
content += f"{agent_name}_teachability.add_to_agent({agent_name})"
|
|
37
|
+
return content
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""Get the `is_termination_message` check for the agent."""
|
|
2
|
+
|
|
3
|
+
from typing import Tuple
|
|
4
|
+
|
|
5
|
+
from waldiez.models import WaldiezAgent
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def get_is_termination_message(
|
|
9
|
+
agent: WaldiezAgent, agent_name: str
|
|
10
|
+
) -> Tuple[str, str]:
|
|
11
|
+
"""Get the `is_termination_message` argument and content (if any).
|
|
12
|
+
|
|
13
|
+
Parameters
|
|
14
|
+
----------
|
|
15
|
+
agent : WaldiezAgent
|
|
16
|
+
The agent.
|
|
17
|
+
agent_name : str
|
|
18
|
+
The agent name.
|
|
19
|
+
|
|
20
|
+
Returns
|
|
21
|
+
-------
|
|
22
|
+
Tuple[str, str]
|
|
23
|
+
- The termination function name or lambda or None.
|
|
24
|
+
- The termination function definition and content if any.
|
|
25
|
+
|
|
26
|
+
Raises
|
|
27
|
+
------
|
|
28
|
+
ValueError
|
|
29
|
+
If the termination type is invalid.
|
|
30
|
+
"""
|
|
31
|
+
if agent.data.termination.type == "none":
|
|
32
|
+
return "None", ""
|
|
33
|
+
if agent.data.termination.type == "keyword":
|
|
34
|
+
return agent.data.termination.string, ""
|
|
35
|
+
if agent.data.termination.type == "method":
|
|
36
|
+
method_name = f"is_termination_message_{agent_name}"
|
|
37
|
+
content = (
|
|
38
|
+
"\n\n"
|
|
39
|
+
+ f"def is_termination_message_{agent_name}(message):"
|
|
40
|
+
+ "\n"
|
|
41
|
+
+ f"{agent.data.termination.string}"
|
|
42
|
+
+ "\n\n"
|
|
43
|
+
)
|
|
44
|
+
return method_name, content
|
|
45
|
+
raise ValueError(f"Invalid termination type: {agent.data.termination.type}")
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"""Chat related string generation functions.
|
|
2
|
+
|
|
3
|
+
Functions
|
|
4
|
+
---------
|
|
5
|
+
export_chats
|
|
6
|
+
Get the chats content.
|
|
7
|
+
export_nested_chat
|
|
8
|
+
Get the 'register_nested_chats' content.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from .chats import export_chats
|
|
12
|
+
from .nested import export_nested_chat
|
|
13
|
+
|
|
14
|
+
__all__ = ["export_chats", "export_nested_chat"]
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""Export the chats content."""
|
|
2
|
+
|
|
3
|
+
from typing import Dict, List, Tuple
|
|
4
|
+
|
|
5
|
+
from waldiez.models import WaldiezAgent, WaldiezChat
|
|
6
|
+
|
|
7
|
+
from .helpers import export_multiple_chats_string, export_single_chat_string
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def export_chats(
|
|
11
|
+
main_chats: List[Tuple[WaldiezChat, WaldiezAgent, WaldiezAgent]],
|
|
12
|
+
agent_names: Dict[str, str],
|
|
13
|
+
chat_names: Dict[str, str],
|
|
14
|
+
tabs: int,
|
|
15
|
+
) -> Tuple[str, str]:
|
|
16
|
+
"""Get the chats content.
|
|
17
|
+
|
|
18
|
+
Parameters
|
|
19
|
+
----------
|
|
20
|
+
main_chats : List[Tuple[WaldiezChat, WaldiezAgent, WaldiezAgent]]
|
|
21
|
+
The main flow chats.
|
|
22
|
+
agent_names : Dict[str, str]
|
|
23
|
+
A mapping of agent id to agent name.
|
|
24
|
+
chat_names : Dict[str, str]
|
|
25
|
+
A mapping of chat id to chat name.
|
|
26
|
+
tabs : int
|
|
27
|
+
The number of tabs to use for indentation.
|
|
28
|
+
|
|
29
|
+
Returns
|
|
30
|
+
-------
|
|
31
|
+
Tuple[str, str]
|
|
32
|
+
The chats content and additional methods string if any.
|
|
33
|
+
"""
|
|
34
|
+
if len(main_chats) == 1:
|
|
35
|
+
return export_single_chat_string(
|
|
36
|
+
flow=main_chats[0],
|
|
37
|
+
agent_names=agent_names,
|
|
38
|
+
chat_names=chat_names,
|
|
39
|
+
tabs=tabs,
|
|
40
|
+
)
|
|
41
|
+
return export_multiple_chats_string(
|
|
42
|
+
main_chats=main_chats,
|
|
43
|
+
chat_names=chat_names,
|
|
44
|
+
agent_names=agent_names,
|
|
45
|
+
tabs=tabs,
|
|
46
|
+
)
|
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
"""Helper functions for exporting chat data to code.
|
|
2
|
+
|
|
3
|
+
Functions
|
|
4
|
+
---------
|
|
5
|
+
export_single_chat_string
|
|
6
|
+
Get the chat string when there is only one chat in the flow.
|
|
7
|
+
export_multiple_chats_string
|
|
8
|
+
Get the chats content, when there are more than one chats in the flow.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from typing import Any, Dict, List, Optional, Tuple
|
|
12
|
+
|
|
13
|
+
from waldiez.models import (
|
|
14
|
+
WaldiezAgent,
|
|
15
|
+
WaldiezChat,
|
|
16
|
+
WaldiezChatMessage,
|
|
17
|
+
WaldiezRagUser,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
from ..utils import get_escaped_string, get_object_string
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
# pylint: disable=line-too-long
|
|
24
|
+
def export_single_chat_string(
|
|
25
|
+
flow: Tuple[WaldiezChat, WaldiezAgent, WaldiezAgent],
|
|
26
|
+
agent_names: Dict[str, str],
|
|
27
|
+
chat_names: Dict[str, str],
|
|
28
|
+
tabs: int,
|
|
29
|
+
) -> Tuple[str, str]:
|
|
30
|
+
"""Get the chat string when there is only one chat in the flow.
|
|
31
|
+
|
|
32
|
+
Parameters
|
|
33
|
+
----------
|
|
34
|
+
flow : Tuple[WaldiezChat, WaldiezAgent, WaldiezAgent]
|
|
35
|
+
The chat flow.
|
|
36
|
+
agent_names : Dict[str, str]
|
|
37
|
+
A mapping of agent id to agent name.
|
|
38
|
+
chat_names : Dict[str, str]
|
|
39
|
+
A mapping of chat id to chat name.
|
|
40
|
+
tabs : int
|
|
41
|
+
The number of tabs to use for indentation.
|
|
42
|
+
|
|
43
|
+
Returns
|
|
44
|
+
-------
|
|
45
|
+
Tuple[str, str]
|
|
46
|
+
The chat string and additional methods string if any
|
|
47
|
+
|
|
48
|
+
Example
|
|
49
|
+
-------
|
|
50
|
+
```python
|
|
51
|
+
>>> from waldiez.models import WaldiezAgent, WaldiezChat, WaldiezChatData, WaldiezChatMessage
|
|
52
|
+
>>> chat = WaldiezChat(
|
|
53
|
+
... id="wc-1",
|
|
54
|
+
... name="chat1",
|
|
55
|
+
... description="A chat between two agents.",
|
|
56
|
+
... tags=["chat", "chat1"],
|
|
57
|
+
... requirements=[],
|
|
58
|
+
... data=WaldiezChatData(
|
|
59
|
+
... sender="wa-1",
|
|
60
|
+
... recipient="wa-2",
|
|
61
|
+
... message=WaldiezChatMessage(
|
|
62
|
+
... type="string",
|
|
63
|
+
... content="Hello, how are you?",
|
|
64
|
+
... ),
|
|
65
|
+
... ),
|
|
66
|
+
... )
|
|
67
|
+
>>> agent_names = {"wa-1": "agent1", "wa-2": "agent2"}
|
|
68
|
+
>>> chat_names = {"wc-1": "chat1"}
|
|
69
|
+
>>> export_single_chat_string((chat, agent1, agent2), agent_names, chat_names, 0)
|
|
70
|
+
agent1.initiate_chat(
|
|
71
|
+
agent2,
|
|
72
|
+
message="Hello, how are you?",
|
|
73
|
+
)
|
|
74
|
+
```
|
|
75
|
+
"""
|
|
76
|
+
tab = " " * tabs
|
|
77
|
+
chat, sender, recipient = flow
|
|
78
|
+
chat_args = chat.get_chat_args(sender=sender)
|
|
79
|
+
if not chat_args:
|
|
80
|
+
return _get_empty_simple_chat_string(
|
|
81
|
+
tab,
|
|
82
|
+
chat=chat,
|
|
83
|
+
sender=sender,
|
|
84
|
+
recipient=recipient,
|
|
85
|
+
agent_names=agent_names,
|
|
86
|
+
)
|
|
87
|
+
return _get_simple_chat_string(
|
|
88
|
+
chat=chat,
|
|
89
|
+
chat_args=chat_args,
|
|
90
|
+
sender=sender,
|
|
91
|
+
recipient=recipient,
|
|
92
|
+
agent_names=agent_names,
|
|
93
|
+
chat_names=chat_names,
|
|
94
|
+
tabs=tabs,
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def export_multiple_chats_string(
|
|
99
|
+
main_chats: List[Tuple[WaldiezChat, WaldiezAgent, WaldiezAgent]],
|
|
100
|
+
chat_names: Dict[str, str],
|
|
101
|
+
agent_names: Dict[str, str],
|
|
102
|
+
tabs: int,
|
|
103
|
+
) -> Tuple[str, str]:
|
|
104
|
+
"""Get the chats content, when there are more than one chats in the flow.
|
|
105
|
+
|
|
106
|
+
Parameters
|
|
107
|
+
----------
|
|
108
|
+
main_chats : List[Tuple[WaldiezChat, WaldiezAgent, WaldiezAgent]]
|
|
109
|
+
The main chats.
|
|
110
|
+
chat_names : Dict[str, str]
|
|
111
|
+
A mapping of chat id to chat name.
|
|
112
|
+
agent_names : Dict[str, str]
|
|
113
|
+
A mapping of agent id to agent name.
|
|
114
|
+
tabs : int
|
|
115
|
+
The number of tabs to use for indentation.
|
|
116
|
+
|
|
117
|
+
Returns
|
|
118
|
+
-------
|
|
119
|
+
Tuple[str, str]
|
|
120
|
+
The main chats content and additional methods string if any.
|
|
121
|
+
|
|
122
|
+
Example
|
|
123
|
+
-------
|
|
124
|
+
```python
|
|
125
|
+
>>> from waldiez.models import WaldiezAgent, WaldiezChat, WaldiezChatData, WaldiezChatMessage
|
|
126
|
+
>>> chat1 = WaldiezChat(
|
|
127
|
+
... id="wc-1",
|
|
128
|
+
... name="chat1",
|
|
129
|
+
... description="A chat between two agents.",
|
|
130
|
+
... tags=["chat", "chat1"],
|
|
131
|
+
... requirements=[],
|
|
132
|
+
... data=WaldiezChatData(
|
|
133
|
+
... sender="wa-1",
|
|
134
|
+
... recipient="wa-2",
|
|
135
|
+
... position=0,
|
|
136
|
+
... message=WaldiezChatMessage(
|
|
137
|
+
... type="string",
|
|
138
|
+
... content="Hello, how are you?",
|
|
139
|
+
... ),
|
|
140
|
+
... ),
|
|
141
|
+
... )
|
|
142
|
+
>>> chat2 = WaldiezChat(
|
|
143
|
+
... id="wc-2",
|
|
144
|
+
... name="chat2",
|
|
145
|
+
... description="A chat between two agents.",
|
|
146
|
+
... tags=["chat", "chat2"],
|
|
147
|
+
... requirements=[],
|
|
148
|
+
... data=WaldiezChatData(
|
|
149
|
+
... sender="wa-2",
|
|
150
|
+
... recipient="wa-1",
|
|
151
|
+
... position=1,
|
|
152
|
+
... message=WaldiezChatMessage(
|
|
153
|
+
... type="string",
|
|
154
|
+
... content="I am good, thank you. How about you?",
|
|
155
|
+
... ),
|
|
156
|
+
... ),
|
|
157
|
+
... )
|
|
158
|
+
>>> agent_names = {"wa-1": "agent1", "wa-2": "agent2"}
|
|
159
|
+
>>> chat_names = {"wc-1": "chat1", "wc-2": "chat2"}
|
|
160
|
+
>>> export_multiple_chats_string([(chat1, agent1, agent2), (chat2, agent2, agent1)], chat_names, agent_names, 0)
|
|
161
|
+
initiate_chats([
|
|
162
|
+
{
|
|
163
|
+
"sender": agent1,
|
|
164
|
+
"recipient": agent2,
|
|
165
|
+
"message": "Hello, how are you?",
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
"sender": agent2,
|
|
169
|
+
"recipient": agent1,
|
|
170
|
+
"message": "I am good, thank you. How about you?",
|
|
171
|
+
},
|
|
172
|
+
])
|
|
173
|
+
```
|
|
174
|
+
"""
|
|
175
|
+
tab = " " * tabs
|
|
176
|
+
content = "\n"
|
|
177
|
+
additional_methods_string = ""
|
|
178
|
+
content = "initiate_chats(["
|
|
179
|
+
for chat, sender, recipient in main_chats:
|
|
180
|
+
chat_string, additional_methods = _get_chat_dict_string(
|
|
181
|
+
chat=chat,
|
|
182
|
+
chat_names=chat_names,
|
|
183
|
+
sender=sender,
|
|
184
|
+
recipient=recipient,
|
|
185
|
+
agent_names=agent_names,
|
|
186
|
+
tabs=tabs + 1,
|
|
187
|
+
)
|
|
188
|
+
additional_methods_string += additional_methods
|
|
189
|
+
content += f"\n{tab} {chat_string}"
|
|
190
|
+
content += "\n" + " " * tabs + "])"
|
|
191
|
+
return content, additional_methods_string
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def _get_chat_message_string(
|
|
195
|
+
chat: WaldiezChat,
|
|
196
|
+
chat_names: Dict[str, str],
|
|
197
|
+
) -> Tuple[str, Optional[str]]:
|
|
198
|
+
"""Get the agent's message as a string.
|
|
199
|
+
|
|
200
|
+
Parameters
|
|
201
|
+
----------
|
|
202
|
+
chat : WaldiezChat
|
|
203
|
+
The chat.
|
|
204
|
+
chat_names : Dict[str, str]
|
|
205
|
+
A mapping of chat id to chat name with all the chats in the flow.
|
|
206
|
+
|
|
207
|
+
Returns
|
|
208
|
+
-------
|
|
209
|
+
Tuple[str, Optional[str]]
|
|
210
|
+
If the message is a string, the message content and None.
|
|
211
|
+
If the message is a method, the method name and the method content.
|
|
212
|
+
If the message is None, 'None' and None.
|
|
213
|
+
"""
|
|
214
|
+
if (
|
|
215
|
+
not chat.message
|
|
216
|
+
or chat.message.type == "none"
|
|
217
|
+
or chat.message.content is None
|
|
218
|
+
or chat.message_content is None
|
|
219
|
+
):
|
|
220
|
+
return "None", None
|
|
221
|
+
if chat.message.type == "string":
|
|
222
|
+
return chat.message.content, None
|
|
223
|
+
chat_name = chat_names[chat.id]
|
|
224
|
+
original_function_name = "callable_message"
|
|
225
|
+
method_args = "sender, recipient, context"
|
|
226
|
+
function_name = f"{original_function_name}_{chat_name}"
|
|
227
|
+
function_def = f"def {function_name}({method_args}):"
|
|
228
|
+
return function_name, function_def + "\n" + chat.message_content + "\n"
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def _get_chat_dict_string(
|
|
232
|
+
chat: WaldiezChat,
|
|
233
|
+
sender: WaldiezAgent,
|
|
234
|
+
recipient: WaldiezAgent,
|
|
235
|
+
chat_names: Dict[str, str],
|
|
236
|
+
agent_names: Dict[str, str],
|
|
237
|
+
tabs: int,
|
|
238
|
+
) -> Tuple[str, str]:
|
|
239
|
+
"""Get a chat dictionary string.
|
|
240
|
+
|
|
241
|
+
If the chat message is a separate method and not a string or a lambda,
|
|
242
|
+
we return the method string (definition and body) as well as the rest
|
|
243
|
+
of the arguments.
|
|
244
|
+
|
|
245
|
+
Parameters
|
|
246
|
+
----------
|
|
247
|
+
chat : WaldiezChat
|
|
248
|
+
The chat.
|
|
249
|
+
sender : WaldiezAgent
|
|
250
|
+
The sender.
|
|
251
|
+
recipient : WaldiezAgent
|
|
252
|
+
The recipient.
|
|
253
|
+
chat_names : Dict[str, str]
|
|
254
|
+
A mapping of chat id to chat name.
|
|
255
|
+
agent_names : Dict[str, str]
|
|
256
|
+
A mapping of agent id to agent name.
|
|
257
|
+
tabs : int
|
|
258
|
+
The number of tabs to use for indentation.
|
|
259
|
+
|
|
260
|
+
Returns
|
|
261
|
+
-------
|
|
262
|
+
Tuple[str, str]
|
|
263
|
+
The chat dictionary string and additional methods string if any.
|
|
264
|
+
"""
|
|
265
|
+
tab = " " * tabs
|
|
266
|
+
chat_args = chat.get_chat_args(sender=sender)
|
|
267
|
+
chat_string = "{"
|
|
268
|
+
chat_string += "\n" + f'{tab} "sender": {agent_names[sender.id]},'
|
|
269
|
+
chat_string += "\n" + f'{tab} "recipient": {agent_names[recipient.id]},'
|
|
270
|
+
additional_methods_string = ""
|
|
271
|
+
for key, value in chat_args.items():
|
|
272
|
+
if isinstance(value, str):
|
|
273
|
+
chat_string += "\n" + f'{tab} "{key}": "{value}",'
|
|
274
|
+
elif isinstance(value, dict):
|
|
275
|
+
chat_string += (
|
|
276
|
+
"\n"
|
|
277
|
+
f'{tab} "{key}": {get_object_string(value, tabs=tabs + 1)},'
|
|
278
|
+
)
|
|
279
|
+
else:
|
|
280
|
+
chat_string += "\n" + f'{tab} "{key}": {value},'
|
|
281
|
+
if (
|
|
282
|
+
sender.agent_type == "rag_user"
|
|
283
|
+
and isinstance(sender, WaldiezRagUser)
|
|
284
|
+
and chat.message.type == "rag_message_generator"
|
|
285
|
+
):
|
|
286
|
+
message = f"{agent_names[sender.id]}.message_generator"
|
|
287
|
+
chat_string += "\n" + f'{tab} "message": {message},'
|
|
288
|
+
chat_string += "\n" + tab + "},"
|
|
289
|
+
return chat_string, additional_methods_string
|
|
290
|
+
message, method_content = _get_chat_message_string(
|
|
291
|
+
chat=chat,
|
|
292
|
+
chat_names=chat_names,
|
|
293
|
+
)
|
|
294
|
+
if message and isinstance(chat.data.message, WaldiezChatMessage):
|
|
295
|
+
message = get_escaped_string(message)
|
|
296
|
+
if chat.data.message.type == "method":
|
|
297
|
+
if method_content:
|
|
298
|
+
additional_methods_string += "\n" + method_content
|
|
299
|
+
chat_string += "\n" + f'{tab} "message": {message},'
|
|
300
|
+
elif chat.data.message.type == "string" and chat.data.message.content:
|
|
301
|
+
chat_string += "\n" + f'{tab} "message": "{message}",'
|
|
302
|
+
chat_string += "\n" + tab + "},"
|
|
303
|
+
return chat_string, additional_methods_string
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
def _get_empty_simple_chat_string(
|
|
307
|
+
tab: str,
|
|
308
|
+
chat: WaldiezChat,
|
|
309
|
+
sender: WaldiezAgent,
|
|
310
|
+
recipient: WaldiezAgent,
|
|
311
|
+
agent_names: Dict[str, str],
|
|
312
|
+
) -> Tuple[str, str]:
|
|
313
|
+
content = tab
|
|
314
|
+
sender_name = agent_names[sender.id]
|
|
315
|
+
recipient_name = agent_names[recipient.id]
|
|
316
|
+
content += f"{sender_name}.initiate_chat(\n"
|
|
317
|
+
content += tab + f" {recipient_name},\n"
|
|
318
|
+
message_arg, _ = _get_chat_message(
|
|
319
|
+
tab=tab,
|
|
320
|
+
chat=chat,
|
|
321
|
+
chat_names={},
|
|
322
|
+
sender=sender,
|
|
323
|
+
sender_name=sender_name,
|
|
324
|
+
)
|
|
325
|
+
content += message_arg
|
|
326
|
+
content += tab + ")"
|
|
327
|
+
return content, ""
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
def _get_chat_message(
|
|
331
|
+
tab: str,
|
|
332
|
+
chat: WaldiezChat,
|
|
333
|
+
chat_names: Dict[str, str],
|
|
334
|
+
sender: WaldiezAgent,
|
|
335
|
+
sender_name: str,
|
|
336
|
+
) -> Tuple[str, str]:
|
|
337
|
+
additional_methods_string = ""
|
|
338
|
+
method_content: Optional[str] = None
|
|
339
|
+
if (
|
|
340
|
+
sender.agent_type == "rag_user"
|
|
341
|
+
and isinstance(sender, WaldiezRagUser)
|
|
342
|
+
and chat.message.type == "rag_message_generator"
|
|
343
|
+
):
|
|
344
|
+
message = f"{sender_name}.message_generator"
|
|
345
|
+
return f"\n{tab} message={message},", additional_methods_string
|
|
346
|
+
message, method_content = _get_chat_message_string(
|
|
347
|
+
chat=chat,
|
|
348
|
+
chat_names=chat_names,
|
|
349
|
+
)
|
|
350
|
+
if message and isinstance(chat.data.message, WaldiezChatMessage):
|
|
351
|
+
message = get_escaped_string(message)
|
|
352
|
+
if chat.data.message.type == "method":
|
|
353
|
+
additional_methods_string += (
|
|
354
|
+
method_content if method_content else ""
|
|
355
|
+
)
|
|
356
|
+
return f"\n{tab} message={message},", additional_methods_string
|
|
357
|
+
if chat.message.type == "string" and chat.data.message.content:
|
|
358
|
+
return f'\n{tab} message="{message}",', additional_methods_string
|
|
359
|
+
return "", additional_methods_string
|
|
360
|
+
return "", additional_methods_string # pragma: no cover
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
def _get_simple_chat_string(
|
|
364
|
+
chat: WaldiezChat,
|
|
365
|
+
sender: WaldiezAgent,
|
|
366
|
+
recipient: WaldiezAgent,
|
|
367
|
+
agent_names: Dict[str, str],
|
|
368
|
+
chat_names: Dict[str, str],
|
|
369
|
+
chat_args: Dict[str, Any],
|
|
370
|
+
tabs: int,
|
|
371
|
+
) -> Tuple[str, str]:
|
|
372
|
+
tab = " " * tabs
|
|
373
|
+
sender_name = agent_names[sender.id]
|
|
374
|
+
recipient_name = agent_names[recipient.id]
|
|
375
|
+
chat_string = f"{sender_name}.initiate_chat(\n"
|
|
376
|
+
chat_string += f"{tab} {recipient_name},"
|
|
377
|
+
for key, value in chat_args.items():
|
|
378
|
+
if isinstance(value, str):
|
|
379
|
+
chat_string += f'\n{tab} {key}="{value}",'
|
|
380
|
+
elif isinstance(value, dict):
|
|
381
|
+
chat_string += (
|
|
382
|
+
f"\n{tab} {key}={get_object_string(value, tabs + 1)},"
|
|
383
|
+
)
|
|
384
|
+
else:
|
|
385
|
+
chat_string += f"\n{tab} {key}={value},"
|
|
386
|
+
message_arg, additional_methods_string = _get_chat_message(
|
|
387
|
+
tab=tab,
|
|
388
|
+
chat=chat,
|
|
389
|
+
chat_names=chat_names,
|
|
390
|
+
sender=sender,
|
|
391
|
+
sender_name=sender_name,
|
|
392
|
+
)
|
|
393
|
+
chat_string += message_arg
|
|
394
|
+
chat_string += f"\n{tab})"
|
|
395
|
+
return chat_string, additional_methods_string
|