lionagi 0.0.316__py3-none-any.whl → 0.1.1__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- lionagi/core/__init__.py +19 -8
- lionagi/core/agent/__init__.py +0 -3
- lionagi/core/agent/base_agent.py +25 -30
- lionagi/core/branch/__init__.py +0 -4
- lionagi/core/branch/{base_branch.py → base.py} +12 -13
- lionagi/core/branch/branch.py +22 -19
- lionagi/core/branch/executable_branch.py +0 -347
- lionagi/core/branch/{branch_flow_mixin.py → flow_mixin.py} +5 -5
- lionagi/core/direct/__init__.py +10 -1
- lionagi/core/direct/cot.py +61 -26
- lionagi/core/direct/plan.py +10 -8
- lionagi/core/direct/predict.py +5 -5
- lionagi/core/direct/react.py +8 -8
- lionagi/core/direct/score.py +4 -4
- lionagi/core/direct/select.py +4 -4
- lionagi/core/direct/utils.py +7 -4
- lionagi/core/direct/vote.py +2 -2
- lionagi/core/execute/base_executor.py +47 -0
- lionagi/core/execute/branch_executor.py +296 -0
- lionagi/core/execute/instruction_map_executor.py +179 -0
- lionagi/core/execute/neo4j_executor.py +381 -0
- lionagi/core/execute/structure_executor.py +314 -0
- lionagi/core/flow/monoflow/ReAct.py +20 -20
- lionagi/core/flow/monoflow/chat.py +6 -6
- lionagi/core/flow/monoflow/chat_mixin.py +23 -33
- lionagi/core/flow/monoflow/followup.py +14 -15
- lionagi/core/flow/polyflow/chat.py +15 -12
- lionagi/core/{prompt/action_template.py → form/action_form.py} +2 -2
- lionagi/core/{prompt → form}/field_validator.py +40 -31
- lionagi/core/form/form.py +302 -0
- lionagi/core/form/mixin.py +214 -0
- lionagi/core/{prompt/scored_template.py → form/scored_form.py} +2 -2
- lionagi/core/generic/__init__.py +37 -0
- lionagi/core/generic/action.py +26 -0
- lionagi/core/generic/component.py +455 -0
- lionagi/core/generic/condition.py +44 -0
- lionagi/core/generic/data_logger.py +305 -0
- lionagi/core/generic/edge.py +162 -0
- lionagi/core/generic/mail.py +90 -0
- lionagi/core/generic/mailbox.py +36 -0
- lionagi/core/generic/node.py +285 -0
- lionagi/core/generic/relation.py +70 -0
- lionagi/core/generic/signal.py +22 -0
- lionagi/core/generic/structure.py +362 -0
- lionagi/core/generic/transfer.py +20 -0
- lionagi/core/generic/work.py +40 -0
- lionagi/core/graph/graph.py +126 -0
- lionagi/core/graph/tree.py +190 -0
- lionagi/core/mail/__init__.py +0 -8
- lionagi/core/mail/mail_manager.py +15 -12
- lionagi/core/mail/schema.py +9 -2
- lionagi/core/messages/__init__.py +0 -3
- lionagi/core/messages/schema.py +17 -225
- lionagi/core/session/__init__.py +0 -3
- lionagi/core/session/session.py +24 -22
- lionagi/core/tool/__init__.py +3 -1
- lionagi/core/tool/tool.py +28 -0
- lionagi/core/tool/tool_manager.py +75 -75
- lionagi/experimental/directive/evaluator/__init__.py +0 -0
- lionagi/experimental/directive/evaluator/ast_evaluator.py +115 -0
- lionagi/experimental/directive/evaluator/base_evaluator.py +202 -0
- lionagi/experimental/directive/evaluator/sandbox_.py +14 -0
- lionagi/experimental/directive/evaluator/script_engine.py +83 -0
- lionagi/experimental/directive/parser/__init__.py +0 -0
- lionagi/experimental/directive/parser/base_parser.py +215 -0
- lionagi/experimental/directive/schema.py +36 -0
- lionagi/experimental/directive/template_/__init__.py +0 -0
- lionagi/experimental/directive/template_/base_template.py +63 -0
- lionagi/experimental/tool/__init__.py +0 -0
- lionagi/experimental/tool/function_calling.py +43 -0
- lionagi/experimental/tool/manual.py +66 -0
- lionagi/experimental/tool/schema.py +59 -0
- lionagi/experimental/tool/tool_manager.py +138 -0
- lionagi/experimental/tool/util.py +16 -0
- lionagi/experimental/work/__init__.py +0 -0
- lionagi/experimental/work/_logger.py +25 -0
- lionagi/experimental/work/exchange.py +0 -0
- lionagi/experimental/work/schema.py +30 -0
- lionagi/experimental/work/tests.py +72 -0
- lionagi/experimental/work/util.py +0 -0
- lionagi/experimental/work/work_function.py +89 -0
- lionagi/experimental/work/worker.py +12 -0
- lionagi/integrations/bridge/autogen_/__init__.py +0 -0
- lionagi/integrations/bridge/autogen_/autogen_.py +124 -0
- lionagi/integrations/bridge/llamaindex_/get_index.py +294 -0
- lionagi/integrations/bridge/llamaindex_/llama_pack.py +227 -0
- lionagi/integrations/bridge/transformers_/__init__.py +0 -0
- lionagi/integrations/bridge/transformers_/install_.py +36 -0
- lionagi/integrations/chunker/chunk.py +7 -7
- lionagi/integrations/config/oai_configs.py +5 -5
- lionagi/integrations/config/ollama_configs.py +1 -1
- lionagi/integrations/config/openrouter_configs.py +1 -1
- lionagi/integrations/loader/load.py +6 -6
- lionagi/integrations/loader/load_util.py +8 -8
- lionagi/integrations/storage/__init__.py +3 -0
- lionagi/integrations/storage/neo4j.py +673 -0
- lionagi/integrations/storage/storage_util.py +289 -0
- lionagi/integrations/storage/to_csv.py +63 -0
- lionagi/integrations/storage/to_excel.py +67 -0
- lionagi/libs/ln_api.py +3 -3
- lionagi/libs/ln_knowledge_graph.py +405 -0
- lionagi/libs/ln_parse.py +43 -6
- lionagi/libs/ln_queue.py +101 -0
- lionagi/libs/ln_tokenizer.py +57 -0
- lionagi/libs/ln_validate.py +288 -0
- lionagi/libs/sys_util.py +29 -7
- lionagi/lions/__init__.py +0 -0
- lionagi/lions/coder/__init__.py +0 -0
- lionagi/lions/coder/add_feature.py +20 -0
- lionagi/lions/coder/base_prompts.py +22 -0
- lionagi/lions/coder/coder.py +121 -0
- lionagi/lions/coder/util.py +91 -0
- lionagi/lions/researcher/__init__.py +0 -0
- lionagi/lions/researcher/data_source/__init__.py +0 -0
- lionagi/lions/researcher/data_source/finhub_.py +191 -0
- lionagi/lions/researcher/data_source/google_.py +199 -0
- lionagi/lions/researcher/data_source/wiki_.py +96 -0
- lionagi/lions/researcher/data_source/yfinance_.py +21 -0
- lionagi/tests/integrations/__init__.py +0 -0
- lionagi/tests/libs/__init__.py +0 -0
- lionagi/tests/libs/test_async.py +0 -0
- lionagi/tests/libs/test_field_validators.py +353 -0
- lionagi/tests/libs/test_queue.py +67 -0
- lionagi/tests/test_core/test_base_branch.py +0 -1
- lionagi/tests/test_core/test_branch.py +2 -0
- lionagi/tests/test_core/test_session_base_util.py +1 -0
- lionagi/version.py +1 -1
- {lionagi-0.0.316.dist-info → lionagi-0.1.1.dist-info}/METADATA +1 -1
- lionagi-0.1.1.dist-info/RECORD +190 -0
- lionagi/core/prompt/prompt_template.py +0 -312
- lionagi/core/schema/__init__.py +0 -22
- lionagi/core/schema/action_node.py +0 -29
- lionagi/core/schema/base_mixin.py +0 -296
- lionagi/core/schema/base_node.py +0 -199
- lionagi/core/schema/condition.py +0 -24
- lionagi/core/schema/data_logger.py +0 -354
- lionagi/core/schema/data_node.py +0 -93
- lionagi/core/schema/prompt_template.py +0 -67
- lionagi/core/schema/structure.py +0 -912
- lionagi/core/tool/manual.py +0 -1
- lionagi-0.0.316.dist-info/RECORD +0 -121
- /lionagi/core/{branch/base → execute}/__init__.py +0 -0
- /lionagi/core/flow/{base/baseflow.py → baseflow.py} +0 -0
- /lionagi/core/flow/{base/__init__.py → mono_chat_mixin.py} +0 -0
- /lionagi/core/{prompt → form}/__init__.py +0 -0
- /lionagi/{tests/test_integrations → core/graph}/__init__.py +0 -0
- /lionagi/{tests/test_libs → experimental}/__init__.py +0 -0
- /lionagi/{tests/test_libs/test_async.py → experimental/directive/__init__.py} +0 -0
- /lionagi/tests/{test_libs → libs}/test_api.py +0 -0
- /lionagi/tests/{test_libs → libs}/test_convert.py +0 -0
- /lionagi/tests/{test_libs → libs}/test_func_call.py +0 -0
- /lionagi/tests/{test_libs → libs}/test_nested.py +0 -0
- /lionagi/tests/{test_libs → libs}/test_parse.py +0 -0
- /lionagi/tests/{test_libs → libs}/test_sys_util.py +0 -0
- {lionagi-0.0.316.dist-info → lionagi-0.1.1.dist-info}/LICENSE +0 -0
- {lionagi-0.0.316.dist-info → lionagi-0.1.1.dist-info}/WHEEL +0 -0
- {lionagi-0.0.316.dist-info → lionagi-0.1.1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,190 @@
|
|
1
|
+
"""
|
2
|
+
This module provides classes for representing and working with tree structures.
|
3
|
+
|
4
|
+
The module includes the following classes:
|
5
|
+
- TreeLabel: An enumeration representing tree relationships (parent and child).
|
6
|
+
- TreeNode: A specialized node representing a node within a tree structure,
|
7
|
+
extending the basic node functionality with parent-child relationships.
|
8
|
+
- Tree: Represents a tree structure, extending the graph with tree-specific
|
9
|
+
functionalities like managing parent-child relationships.
|
10
|
+
"""
|
11
|
+
|
12
|
+
from enum import Enum
|
13
|
+
from pydantic import Field
|
14
|
+
|
15
|
+
from lionagi.core.generic import Node, Condition
|
16
|
+
from lionagi.core.graph.graph import Graph
|
17
|
+
|
18
|
+
|
19
|
+
class TreeLabel(str, Enum):
|
20
|
+
"""
|
21
|
+
Enumeration representing tree relationships.
|
22
|
+
|
23
|
+
Attributes:
|
24
|
+
PARENT (str): Represents the parent edge.
|
25
|
+
CHILD (str): Represents the child edge.
|
26
|
+
"""
|
27
|
+
|
28
|
+
PARENT = "parent"
|
29
|
+
CHILD = "child"
|
30
|
+
|
31
|
+
|
32
|
+
class TreeNode(Node):
|
33
|
+
"""
|
34
|
+
A specialized node representing a node within a tree structure.
|
35
|
+
|
36
|
+
Extends the basic node functionality with parent-child relationships.
|
37
|
+
|
38
|
+
Attributes:
|
39
|
+
parent (Node | None): The parent node. Defaults to None if the node has
|
40
|
+
no parent.
|
41
|
+
"""
|
42
|
+
|
43
|
+
parent: Node | None = Field(
|
44
|
+
default=None,
|
45
|
+
description="The parent node, as an instance of Node.",
|
46
|
+
)
|
47
|
+
|
48
|
+
@property
|
49
|
+
def children(self) -> list[str]:
|
50
|
+
"""
|
51
|
+
Retrieves the IDs of all child nodes.
|
52
|
+
|
53
|
+
Returns:
|
54
|
+
list[str]: A list containing the IDs of the child nodes.
|
55
|
+
"""
|
56
|
+
if not self.parent:
|
57
|
+
return list(self.related_nodes)
|
58
|
+
else:
|
59
|
+
return [
|
60
|
+
node_id for node_id in self.related_nodes if node_id != self.parent.id_
|
61
|
+
]
|
62
|
+
|
63
|
+
def relate_child(
|
64
|
+
self,
|
65
|
+
child: Node | list[Node],
|
66
|
+
condition: Condition | None = None,
|
67
|
+
bundle: bool = False,
|
68
|
+
) -> None:
|
69
|
+
"""
|
70
|
+
Establishes a parent-child relationship between this node and the given
|
71
|
+
child node(s).
|
72
|
+
|
73
|
+
Args:
|
74
|
+
child (Node | list[Node]): The child node or list of child nodes to
|
75
|
+
be related.
|
76
|
+
condition (Condition | None): The condition associated with the
|
77
|
+
relationship, if any.
|
78
|
+
bundle (bool): Indicates whether to bundle the relation into a
|
79
|
+
single transaction. Defaults to False.
|
80
|
+
"""
|
81
|
+
children = [child] if isinstance(child, Node) else child
|
82
|
+
for _child in children:
|
83
|
+
self.relate(
|
84
|
+
_child,
|
85
|
+
node_as="head",
|
86
|
+
label=TreeLabel.PARENT,
|
87
|
+
condition=condition,
|
88
|
+
bundle=bundle,
|
89
|
+
)
|
90
|
+
if isinstance(_child, TreeNode):
|
91
|
+
_child.parent = self
|
92
|
+
|
93
|
+
def relate_parent(
|
94
|
+
self,
|
95
|
+
parent: Node,
|
96
|
+
condition: Condition | None = None,
|
97
|
+
bundle: bool = False,
|
98
|
+
) -> None:
|
99
|
+
"""
|
100
|
+
Establishes a parent-child relationship between the given parent node
|
101
|
+
and this node.
|
102
|
+
|
103
|
+
Args:
|
104
|
+
parent (Node): The parent node to be related.
|
105
|
+
condition (Condition | None): The condition associated with the
|
106
|
+
relationship, if any.
|
107
|
+
bundle (bool): Indicates whether to bundle the relation into a
|
108
|
+
single transaction. Defaults to False.
|
109
|
+
"""
|
110
|
+
if self.parent:
|
111
|
+
self.unrelate(self.parent)
|
112
|
+
self.relate(
|
113
|
+
parent,
|
114
|
+
node_as="tail",
|
115
|
+
label=TreeLabel.PARENT,
|
116
|
+
condition=condition,
|
117
|
+
bundle=bundle,
|
118
|
+
)
|
119
|
+
self.parent = parent
|
120
|
+
|
121
|
+
def unrelate_parent(self):
|
122
|
+
"""
|
123
|
+
Removes the parent relationship of this node.
|
124
|
+
"""
|
125
|
+
self.unrelate(self.parent)
|
126
|
+
self.parent = None
|
127
|
+
|
128
|
+
def unrelate_child(self, child: Node | list[Node]):
|
129
|
+
"""
|
130
|
+
Removes the child relationship between this node and the given child
|
131
|
+
node(s).
|
132
|
+
|
133
|
+
Args:
|
134
|
+
child (Node | list[Node]): The child node or list of child nodes to
|
135
|
+
be unrelated.
|
136
|
+
"""
|
137
|
+
children: list[Node] = [child] if isinstance(child, Node) else child
|
138
|
+
for _child in children:
|
139
|
+
self.unrelate(_child)
|
140
|
+
if isinstance(_child, TreeNode):
|
141
|
+
_child.parent = None
|
142
|
+
|
143
|
+
|
144
|
+
class Tree(Graph):
|
145
|
+
"""
|
146
|
+
Represents a tree structure, extending the graph with tree-specific
|
147
|
+
functionalities.
|
148
|
+
|
149
|
+
Manages parent-child relationships within the tree.
|
150
|
+
|
151
|
+
Attributes:
|
152
|
+
root (TreeNode | None): The root node of the tree. Defaults to None.
|
153
|
+
"""
|
154
|
+
|
155
|
+
root: TreeNode | None = Field(
|
156
|
+
default=None, description="The root node of the tree graph."
|
157
|
+
)
|
158
|
+
|
159
|
+
def relate_parent_child(
|
160
|
+
self,
|
161
|
+
parent: TreeNode,
|
162
|
+
children: list[TreeNode],
|
163
|
+
condition: Condition | None = None,
|
164
|
+
bundle: bool = False,
|
165
|
+
) -> None:
|
166
|
+
"""
|
167
|
+
Establishes parent-child relationships between the given parent node and
|
168
|
+
child node(s).
|
169
|
+
|
170
|
+
Args:
|
171
|
+
parent (TreeNode): The parent node.
|
172
|
+
children (list[TreeNode]): A list of child nodes.
|
173
|
+
condition (Condition | None): The condition associated with the
|
174
|
+
relationships, if any.
|
175
|
+
bundle (bool): Indicates whether to bundle the relations into a
|
176
|
+
single transaction. Defaults to False.
|
177
|
+
"""
|
178
|
+
children = [children] if isinstance(children, TreeNode) else children
|
179
|
+
for child in children:
|
180
|
+
if child.id_ not in self.internal_nodes:
|
181
|
+
self.add_node(child)
|
182
|
+
|
183
|
+
for child in children:
|
184
|
+
parent.relate_child(child, condition=condition, bundle=bundle)
|
185
|
+
|
186
|
+
if parent.id_ not in self.internal_nodes:
|
187
|
+
self.add_node(parent)
|
188
|
+
|
189
|
+
if self.root is None:
|
190
|
+
self.root = parent
|
lionagi/core/mail/__init__.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
from collections import deque
|
2
2
|
from lionagi.libs import AsyncUtil
|
3
|
-
from
|
4
|
-
from .schema import BaseMail
|
3
|
+
from lionagi.core.generic import Node
|
4
|
+
from lionagi.core.mail.schema import BaseMail, MailCategory
|
5
5
|
|
6
6
|
|
7
7
|
class MailManager:
|
@@ -17,10 +17,11 @@ class MailManager:
|
|
17
17
|
and sender.
|
18
18
|
"""
|
19
19
|
|
20
|
-
def __init__(self, sources):
|
20
|
+
def __init__(self, sources=None):
|
21
21
|
self.sources = {}
|
22
22
|
self.mails = {}
|
23
|
-
|
23
|
+
if sources:
|
24
|
+
self.add_sources(sources)
|
24
25
|
self.execute_stop = False
|
25
26
|
|
26
27
|
def add_sources(self, sources):
|
@@ -34,18 +35,20 @@ class MailManager:
|
|
34
35
|
if v.id_ not in self.sources:
|
35
36
|
self.sources[v.id_] = v
|
36
37
|
self.mails[v.id_] = {}
|
38
|
+
else:
|
39
|
+
raise ValueError("Failed to add source, please input list or dict.")
|
37
40
|
|
38
41
|
@staticmethod
|
39
42
|
def create_mail(sender_id, recipient_id, category, package):
|
40
43
|
return BaseMail(sender_id, recipient_id, category, package)
|
41
44
|
|
42
|
-
def add_source(self, sources: list[
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
45
|
+
# def add_source(self, sources: list[Node]):
|
46
|
+
# for source in sources:
|
47
|
+
# if source.id_ in self.sources:
|
48
|
+
# # raise ValueError(f"Source {source.id_} exists, please input a different name.")
|
49
|
+
# continue
|
50
|
+
# self.sources[source.id_] = source
|
51
|
+
# self.mails[source.id_] = {}
|
49
52
|
|
50
53
|
def delete_source(self, source_id):
|
51
54
|
if source_id not in self.sources:
|
@@ -65,7 +68,7 @@ class MailManager:
|
|
65
68
|
f"Recipient source {mail_.recipient_id} does not exist"
|
66
69
|
)
|
67
70
|
if mail_.sender_id not in self.mails[mail_.recipient_id]:
|
68
|
-
self.mails[mail_.recipient_id]
|
71
|
+
self.mails[mail_.recipient_id].update({mail_.sender_id: deque()})
|
69
72
|
self.mails[mail_.recipient_id][mail_.sender_id].append(mail_)
|
70
73
|
|
71
74
|
def send(self, recipient_id):
|
lionagi/core/mail/schema.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
from collections import deque
|
2
2
|
from enum import Enum
|
3
3
|
|
4
|
-
from lionagi.core.
|
4
|
+
from lionagi.core.generic import Node
|
5
5
|
|
6
6
|
|
7
7
|
class MailCategory(str, Enum):
|
@@ -39,7 +39,7 @@ class BaseMail:
|
|
39
39
|
self.package = package
|
40
40
|
|
41
41
|
|
42
|
-
class StartMail(
|
42
|
+
class StartMail(Node):
|
43
43
|
|
44
44
|
def __init__(self, **kwargs):
|
45
45
|
super().__init__(**kwargs)
|
@@ -54,3 +54,10 @@ class StartMail(BaseRelatableNode):
|
|
54
54
|
package=start_mail_content,
|
55
55
|
)
|
56
56
|
self.pending_outs.append(start_mail)
|
57
|
+
|
58
|
+
|
59
|
+
class MailTransfer(Node):
|
60
|
+
def __init__(self):
|
61
|
+
super().__init__()
|
62
|
+
self.pending_ins = {}
|
63
|
+
self.pending_outs = deque()
|
lionagi/core/messages/schema.py
CHANGED
@@ -1,15 +1,12 @@
|
|
1
1
|
from enum import Enum
|
2
2
|
|
3
3
|
from lionagi.libs import nested, convert
|
4
|
-
from
|
5
|
-
from
|
4
|
+
from lionagi.core.generic import Node
|
5
|
+
from lionagi.core.form.form import Form
|
6
6
|
|
7
7
|
_message_fields = ["node_id", "timestamp", "role", "sender", "recipient", "content"]
|
8
8
|
|
9
9
|
|
10
|
-
# ToDo: actually implement the new message classes
|
11
|
-
|
12
|
-
|
13
10
|
class BranchColumns(list[str], Enum):
|
14
11
|
COLUMNS = _message_fields
|
15
12
|
|
@@ -94,14 +91,14 @@ class MessageType(dict, Enum):
|
|
94
91
|
}
|
95
92
|
|
96
93
|
|
97
|
-
class BaseMessage(
|
94
|
+
class BaseMessage(Node):
|
98
95
|
"""
|
99
96
|
Represents a message in a chatbot-like system, inheriting from BaseNode.
|
100
97
|
|
101
98
|
Attributes:
|
102
|
-
|
103
|
-
|
104
|
-
|
99
|
+
role (str | None): The role of the entity sending the message, e.g., 'user', 'system'.
|
100
|
+
sender (str | None): The identifier of the sender of the message.
|
101
|
+
content (Any): The actual content of the message.
|
105
102
|
"""
|
106
103
|
|
107
104
|
role: str | None = None
|
@@ -114,7 +111,7 @@ class BaseMessage(DataNode):
|
|
114
111
|
Constructs and returns a dictionary representation of the message.
|
115
112
|
|
116
113
|
Returns:
|
117
|
-
|
114
|
+
A dictionary representation of the message with 'role' and 'content' keys.
|
118
115
|
"""
|
119
116
|
return self._to_message()
|
120
117
|
|
@@ -124,7 +121,7 @@ class BaseMessage(DataNode):
|
|
124
121
|
Gets the 'content' field of the message.
|
125
122
|
|
126
123
|
Returns:
|
127
|
-
|
124
|
+
The 'content' part of the message.
|
128
125
|
"""
|
129
126
|
return self.msg["content"]
|
130
127
|
|
@@ -133,7 +130,7 @@ class BaseMessage(DataNode):
|
|
133
130
|
Constructs and returns a dictionary representation of the message.
|
134
131
|
|
135
132
|
Returns:
|
136
|
-
|
133
|
+
dict: A dictionary representation of the message with 'role' and 'content' keys.
|
137
134
|
"""
|
138
135
|
return {"role": self.role, "content": convert.to_str(self.content)}
|
139
136
|
|
@@ -187,16 +184,16 @@ class Instruction(BaseMessage):
|
|
187
184
|
return self.content["instruction"]
|
188
185
|
|
189
186
|
@classmethod
|
190
|
-
def
|
187
|
+
def from_form(
|
191
188
|
cls,
|
192
|
-
|
189
|
+
form: Form,
|
193
190
|
sender: str | None = None,
|
194
191
|
recipient=None,
|
195
192
|
):
|
196
193
|
return cls(
|
197
|
-
instruction=
|
198
|
-
context=
|
199
|
-
output_fields=
|
194
|
+
instruction=form.instruction,
|
195
|
+
context=form.instruction_context,
|
196
|
+
output_fields=form.instruction_output_fields,
|
200
197
|
sender=sender,
|
201
198
|
recipient=recipient,
|
202
199
|
)
|
@@ -297,13 +294,13 @@ class Response(BaseMessage):
|
|
297
294
|
Processes an action request response and extracts relevant information.
|
298
295
|
|
299
296
|
Args:
|
300
|
-
|
297
|
+
response (dict): The response dictionary containing tool calls and other information.
|
301
298
|
|
302
299
|
Returns:
|
303
|
-
|
300
|
+
list: A list of dictionaries, each representing a function call with action and arguments.
|
304
301
|
|
305
302
|
Raises:
|
306
|
-
|
303
|
+
ValueError: If the response does not conform to the expected format for action requests.
|
307
304
|
"""
|
308
305
|
try:
|
309
306
|
tool_count = 0
|
@@ -326,208 +323,3 @@ class Response(BaseMessage):
|
|
326
323
|
raise ValueError(
|
327
324
|
"Response message must be one of regular response or function calling"
|
328
325
|
)
|
329
|
-
|
330
|
-
|
331
|
-
# class BaseMessage(DataNode):
|
332
|
-
|
333
|
-
|
334
|
-
# def __init__():
|
335
|
-
# ...
|
336
|
-
|
337
|
-
# role: MessageRoleType = Field(..., alias=MessageField.ROLE.value)
|
338
|
-
# sender: str = Field(..., alias=MessageField.SENDER.value) # Customizable sender
|
339
|
-
# recipient: str | None = Field(None,
|
340
|
-
# alias=MessageField.RECIPIENT.value) # Optional recipient
|
341
|
-
|
342
|
-
# class Config:
|
343
|
-
# extra = 'allow'
|
344
|
-
# use_enum_values = True
|
345
|
-
# populate_by_name = True
|
346
|
-
|
347
|
-
# @model_validator(mode='before')
|
348
|
-
# def handle_extra_fields(cls, values):
|
349
|
-
# """Move undefined fields to metadata."""
|
350
|
-
# fields = set(values.keys())
|
351
|
-
# defined_fields = set(cls.model_fields.keys())
|
352
|
-
# extra_fields = fields - defined_fields
|
353
|
-
|
354
|
-
# if extra_fields:
|
355
|
-
# metadata = values.get('metadata', {})
|
356
|
-
# for field in extra_fields:
|
357
|
-
# if field in metadata:
|
358
|
-
# metadata[f"{field}_1"] = values.pop(field)
|
359
|
-
# else:
|
360
|
-
# metadata[field] = values.pop(field)
|
361
|
-
# values['metadata'] = metadata
|
362
|
-
# return values
|
363
|
-
|
364
|
-
# def _to_roled_message(self):
|
365
|
-
# return {
|
366
|
-
# MessageField.ROLE.value: self.role.value,
|
367
|
-
# MessageField.CONTENT.value: (
|
368
|
-
# json.dumps(self.content) if isinstance(self.content, dict)
|
369
|
-
# else self.content
|
370
|
-
# )
|
371
|
-
# }
|
372
|
-
|
373
|
-
# def to_pd_series(self, *args, **kwargs):
|
374
|
-
# msg_dict = self.to_dict(*args, **kwargs)
|
375
|
-
# if isinstance(to_dict(msg_dict['content']), dict):
|
376
|
-
# msg_dict['content'] = json.dumps(msg_dict['content'])
|
377
|
-
# return pd.Series(msg_dict)
|
378
|
-
|
379
|
-
# @classmethod
|
380
|
-
# def from_pd_series(cls, series: pd.Series, **kwargs):
|
381
|
-
# self = cls.from_dict(series.to_dict(**kwargs))
|
382
|
-
# if isinstance(self.content, str):
|
383
|
-
# try:
|
384
|
-
# self.content = to_dict(self.content)
|
385
|
-
# except:
|
386
|
-
# pass
|
387
|
-
# return self
|
388
|
-
|
389
|
-
|
390
|
-
# def __str__(self):
|
391
|
-
# content_preview = self.content[:50] + "..." if len(
|
392
|
-
# self.content) > 50 else self.content
|
393
|
-
# meta_preview = str(self.metadata)[:50] + "..." if len(
|
394
|
-
# str(self.metadata)) > 50 else str(self.metadata)
|
395
|
-
|
396
|
-
# return (
|
397
|
-
# f"Message({self.role.value or 'none'}, {self._sender or 'none'}, "
|
398
|
-
# f"{content_preview or 'none'}, {self.recipient or 'none'},"
|
399
|
-
# f"{self.timestamp or 'none'}, {meta_preview or 'none'}"
|
400
|
-
# )
|
401
|
-
|
402
|
-
# class Instruction(BaseMessage):
|
403
|
-
|
404
|
-
# def __init__(self, instruction: Any, context: Any = None,
|
405
|
-
# sender: str | None = None, recipient: str | None | Any = None,
|
406
|
-
# metadata: dict | None = None, relation: list | None | Any = None,
|
407
|
-
# **kwargs):
|
408
|
-
# super().__init__(
|
409
|
-
# role=MessageType.INSTRUCTION.value[MessageField.ROLE.value],
|
410
|
-
# sender=sender or MessageType.INSTRUCTION.value[MessageField.SENDER.value],
|
411
|
-
# content={MessageType.INSTRUCTION.value["content_key"]: instruction},
|
412
|
-
# recipient=recipient or MessageType.SYSTEM.value[MessageField.RECIPIENT.value],
|
413
|
-
# metadata=metadata or {}, relation=relation or [], **kwargs
|
414
|
-
# )
|
415
|
-
# if context:
|
416
|
-
# self.content.update({MessageType.CONTEXT.value["content_key"]: context})
|
417
|
-
|
418
|
-
|
419
|
-
# class System(BaseMessage):
|
420
|
-
|
421
|
-
# def __init__(self, system: Any, sender: str | None = None, recipient: Optional[
|
422
|
-
# str] = None, metadata: Optional[dict] = None, relation: Optional[list] = None,
|
423
|
-
# **kwargs):
|
424
|
-
# super().__init__(
|
425
|
-
# role=MessageType.SYSTEM.value[MessageField.ROLE.value],
|
426
|
-
# sender=sender or MessageType.SYSTEM.value[MessageField.SENDER.value],
|
427
|
-
# content={MessageType.SYSTEM.value["content_key"]: system},
|
428
|
-
# recipient=recipient or MessageType.SYSTEM.value[MessageField.RECIPIENT.value],
|
429
|
-
# metadata=metadata or {}, relation=relation or [], **kwargs
|
430
|
-
# )
|
431
|
-
|
432
|
-
|
433
|
-
# class ActionRequest(BaseMessage):
|
434
|
-
|
435
|
-
# def __init__(self, action_request: Any, sender: str | None = None,
|
436
|
-
# recipient: Optional[
|
437
|
-
# str] = None, metadata: Optional[dict] = None,
|
438
|
-
# relation: Optional[list] = None,
|
439
|
-
# **kwargs
|
440
|
-
# ):
|
441
|
-
|
442
|
-
# super().__init__(
|
443
|
-
# role=MessageType.ACTION_REQUEST.value[MessageField.ROLE.value],
|
444
|
-
# sender=sender or MessageType.ACTION_REQUEST.value[MessageField.SENDER.value],
|
445
|
-
# content={MessageType.ACTION_REQUEST.value["content_key"]: action_request},
|
446
|
-
# recipient=recipient or MessageType.ACTION_REQUEST.value[
|
447
|
-
# MessageField.RECIPIENT.value],
|
448
|
-
# metadata=metadata or {}, relation=relation or [], **kwargs
|
449
|
-
# )
|
450
|
-
|
451
|
-
# @classmethod
|
452
|
-
# def from_response(cls, response, sender=None, recipient=None, metadata=None,
|
453
|
-
# relation=None,
|
454
|
-
# **kwargs):
|
455
|
-
# return cls(action_request=response, sender=sender, recipient=recipient,
|
456
|
-
# metadata=metadata,
|
457
|
-
# relation=relation, **kwargs)
|
458
|
-
|
459
|
-
# @staticmethod
|
460
|
-
# def _handle_action_request(response):
|
461
|
-
# try:
|
462
|
-
# tool_count = 0
|
463
|
-
# func_list = []
|
464
|
-
# while tool_count < len(response['tool_calls']):
|
465
|
-
# _path = ['tool_calls', tool_count, 'type']
|
466
|
-
|
467
|
-
# if nget(response, _path) == 'function':
|
468
|
-
# _path1 = ['tool_calls', tool_count, 'function', 'name']
|
469
|
-
# _path2 = ['tool_calls', tool_count, 'function', 'arguments']
|
470
|
-
|
471
|
-
# func_content = {
|
472
|
-
# "action": ("action_" + nget(response, _path1)),
|
473
|
-
# "arguments": nget(response, _path2)
|
474
|
-
# }
|
475
|
-
# func_list.append(func_content)
|
476
|
-
# tool_count += 1
|
477
|
-
# return func_list
|
478
|
-
# except:
|
479
|
-
# raise ValueError(
|
480
|
-
# "Response message must be one of regular response or function calling"
|
481
|
-
# )
|
482
|
-
|
483
|
-
|
484
|
-
# class ActionResponse(BaseMessage):
|
485
|
-
|
486
|
-
# def __init__(self, action_response: Any, sender: str | None = None,
|
487
|
-
# recipient: Optional[
|
488
|
-
# str] = None, metadata: Optional[dict] = None,
|
489
|
-
# relation: Optional[list] = None,
|
490
|
-
# **kwargs
|
491
|
-
# ):
|
492
|
-
# super().__init__(
|
493
|
-
# role=MessageType.ACTION_RESPONSE.value[MessageField.ROLE.value],
|
494
|
-
# sender=sender or MessageType.ACTION_RESPONSE.value[MessageField.SENDER.value],
|
495
|
-
# content={MessageType.ACTION_RESPONSE.value["content_key"]: action_response},
|
496
|
-
# recipient=recipient or MessageType.ACTION_RESPONSE.value[
|
497
|
-
# MessageField.RECIPIENT.value],
|
498
|
-
# metadata=metadata or {}, relation=relation or [], **kwargs
|
499
|
-
# )
|
500
|
-
|
501
|
-
# @classmethod
|
502
|
-
# def from_response(cls, response, sender=None, recipient=None, metadata=None,
|
503
|
-
# relation=None,
|
504
|
-
# **kwargs):
|
505
|
-
# return cls(action_response=response, sender=sender, recipient=recipient,
|
506
|
-
# metadata=metadata,
|
507
|
-
# relation=relation, **kwargs)
|
508
|
-
|
509
|
-
|
510
|
-
# class AssistantResponse(BaseMessage):
|
511
|
-
|
512
|
-
# def __init__(self, response: Any, sender: str | None = None,
|
513
|
-
# recipient: Optional[
|
514
|
-
# str] = None, metadata: Optional[dict] = None,
|
515
|
-
# relation: Optional[list] = None,
|
516
|
-
# **kwargs
|
517
|
-
# ):
|
518
|
-
# super().__init__(
|
519
|
-
# role='assistant',
|
520
|
-
# sender=sender or MessageType.RESPONSE.value[MessageField.SENDER.value],
|
521
|
-
# content={MessageType.RESPONSE.value["content_key"]: response},
|
522
|
-
# recipient=recipient or MessageType.RESPONSE.value[
|
523
|
-
# MessageField.RECIPIENT.value],
|
524
|
-
# metadata=metadata or {}, relation=relation or [], **kwargs
|
525
|
-
# )
|
526
|
-
|
527
|
-
# @classmethod
|
528
|
-
# def from_response(cls, response, sender=None, recipient=None, metadata=None,
|
529
|
-
# relation=None,
|
530
|
-
# **kwargs):
|
531
|
-
# return cls(response=response, sender=sender, recipient=recipient,
|
532
|
-
# metadata=metadata,
|
533
|
-
# relation=relation, **kwargs)
|
lionagi/core/session/__init__.py
CHANGED