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.
Files changed (157) hide show
  1. lionagi/core/__init__.py +19 -8
  2. lionagi/core/agent/__init__.py +0 -3
  3. lionagi/core/agent/base_agent.py +25 -30
  4. lionagi/core/branch/__init__.py +0 -4
  5. lionagi/core/branch/{base_branch.py → base.py} +12 -13
  6. lionagi/core/branch/branch.py +22 -19
  7. lionagi/core/branch/executable_branch.py +0 -347
  8. lionagi/core/branch/{branch_flow_mixin.py → flow_mixin.py} +5 -5
  9. lionagi/core/direct/__init__.py +10 -1
  10. lionagi/core/direct/cot.py +61 -26
  11. lionagi/core/direct/plan.py +10 -8
  12. lionagi/core/direct/predict.py +5 -5
  13. lionagi/core/direct/react.py +8 -8
  14. lionagi/core/direct/score.py +4 -4
  15. lionagi/core/direct/select.py +4 -4
  16. lionagi/core/direct/utils.py +7 -4
  17. lionagi/core/direct/vote.py +2 -2
  18. lionagi/core/execute/base_executor.py +47 -0
  19. lionagi/core/execute/branch_executor.py +296 -0
  20. lionagi/core/execute/instruction_map_executor.py +179 -0
  21. lionagi/core/execute/neo4j_executor.py +381 -0
  22. lionagi/core/execute/structure_executor.py +314 -0
  23. lionagi/core/flow/monoflow/ReAct.py +20 -20
  24. lionagi/core/flow/monoflow/chat.py +6 -6
  25. lionagi/core/flow/monoflow/chat_mixin.py +23 -33
  26. lionagi/core/flow/monoflow/followup.py +14 -15
  27. lionagi/core/flow/polyflow/chat.py +15 -12
  28. lionagi/core/{prompt/action_template.py → form/action_form.py} +2 -2
  29. lionagi/core/{prompt → form}/field_validator.py +40 -31
  30. lionagi/core/form/form.py +302 -0
  31. lionagi/core/form/mixin.py +214 -0
  32. lionagi/core/{prompt/scored_template.py → form/scored_form.py} +2 -2
  33. lionagi/core/generic/__init__.py +37 -0
  34. lionagi/core/generic/action.py +26 -0
  35. lionagi/core/generic/component.py +455 -0
  36. lionagi/core/generic/condition.py +44 -0
  37. lionagi/core/generic/data_logger.py +305 -0
  38. lionagi/core/generic/edge.py +162 -0
  39. lionagi/core/generic/mail.py +90 -0
  40. lionagi/core/generic/mailbox.py +36 -0
  41. lionagi/core/generic/node.py +285 -0
  42. lionagi/core/generic/relation.py +70 -0
  43. lionagi/core/generic/signal.py +22 -0
  44. lionagi/core/generic/structure.py +362 -0
  45. lionagi/core/generic/transfer.py +20 -0
  46. lionagi/core/generic/work.py +40 -0
  47. lionagi/core/graph/graph.py +126 -0
  48. lionagi/core/graph/tree.py +190 -0
  49. lionagi/core/mail/__init__.py +0 -8
  50. lionagi/core/mail/mail_manager.py +15 -12
  51. lionagi/core/mail/schema.py +9 -2
  52. lionagi/core/messages/__init__.py +0 -3
  53. lionagi/core/messages/schema.py +17 -225
  54. lionagi/core/session/__init__.py +0 -3
  55. lionagi/core/session/session.py +24 -22
  56. lionagi/core/tool/__init__.py +3 -1
  57. lionagi/core/tool/tool.py +28 -0
  58. lionagi/core/tool/tool_manager.py +75 -75
  59. lionagi/experimental/directive/evaluator/__init__.py +0 -0
  60. lionagi/experimental/directive/evaluator/ast_evaluator.py +115 -0
  61. lionagi/experimental/directive/evaluator/base_evaluator.py +202 -0
  62. lionagi/experimental/directive/evaluator/sandbox_.py +14 -0
  63. lionagi/experimental/directive/evaluator/script_engine.py +83 -0
  64. lionagi/experimental/directive/parser/__init__.py +0 -0
  65. lionagi/experimental/directive/parser/base_parser.py +215 -0
  66. lionagi/experimental/directive/schema.py +36 -0
  67. lionagi/experimental/directive/template_/__init__.py +0 -0
  68. lionagi/experimental/directive/template_/base_template.py +63 -0
  69. lionagi/experimental/tool/__init__.py +0 -0
  70. lionagi/experimental/tool/function_calling.py +43 -0
  71. lionagi/experimental/tool/manual.py +66 -0
  72. lionagi/experimental/tool/schema.py +59 -0
  73. lionagi/experimental/tool/tool_manager.py +138 -0
  74. lionagi/experimental/tool/util.py +16 -0
  75. lionagi/experimental/work/__init__.py +0 -0
  76. lionagi/experimental/work/_logger.py +25 -0
  77. lionagi/experimental/work/exchange.py +0 -0
  78. lionagi/experimental/work/schema.py +30 -0
  79. lionagi/experimental/work/tests.py +72 -0
  80. lionagi/experimental/work/util.py +0 -0
  81. lionagi/experimental/work/work_function.py +89 -0
  82. lionagi/experimental/work/worker.py +12 -0
  83. lionagi/integrations/bridge/autogen_/__init__.py +0 -0
  84. lionagi/integrations/bridge/autogen_/autogen_.py +124 -0
  85. lionagi/integrations/bridge/llamaindex_/get_index.py +294 -0
  86. lionagi/integrations/bridge/llamaindex_/llama_pack.py +227 -0
  87. lionagi/integrations/bridge/transformers_/__init__.py +0 -0
  88. lionagi/integrations/bridge/transformers_/install_.py +36 -0
  89. lionagi/integrations/chunker/chunk.py +7 -7
  90. lionagi/integrations/config/oai_configs.py +5 -5
  91. lionagi/integrations/config/ollama_configs.py +1 -1
  92. lionagi/integrations/config/openrouter_configs.py +1 -1
  93. lionagi/integrations/loader/load.py +6 -6
  94. lionagi/integrations/loader/load_util.py +8 -8
  95. lionagi/integrations/storage/__init__.py +3 -0
  96. lionagi/integrations/storage/neo4j.py +673 -0
  97. lionagi/integrations/storage/storage_util.py +289 -0
  98. lionagi/integrations/storage/to_csv.py +63 -0
  99. lionagi/integrations/storage/to_excel.py +67 -0
  100. lionagi/libs/ln_api.py +3 -3
  101. lionagi/libs/ln_knowledge_graph.py +405 -0
  102. lionagi/libs/ln_parse.py +43 -6
  103. lionagi/libs/ln_queue.py +101 -0
  104. lionagi/libs/ln_tokenizer.py +57 -0
  105. lionagi/libs/ln_validate.py +288 -0
  106. lionagi/libs/sys_util.py +29 -7
  107. lionagi/lions/__init__.py +0 -0
  108. lionagi/lions/coder/__init__.py +0 -0
  109. lionagi/lions/coder/add_feature.py +20 -0
  110. lionagi/lions/coder/base_prompts.py +22 -0
  111. lionagi/lions/coder/coder.py +121 -0
  112. lionagi/lions/coder/util.py +91 -0
  113. lionagi/lions/researcher/__init__.py +0 -0
  114. lionagi/lions/researcher/data_source/__init__.py +0 -0
  115. lionagi/lions/researcher/data_source/finhub_.py +191 -0
  116. lionagi/lions/researcher/data_source/google_.py +199 -0
  117. lionagi/lions/researcher/data_source/wiki_.py +96 -0
  118. lionagi/lions/researcher/data_source/yfinance_.py +21 -0
  119. lionagi/tests/integrations/__init__.py +0 -0
  120. lionagi/tests/libs/__init__.py +0 -0
  121. lionagi/tests/libs/test_async.py +0 -0
  122. lionagi/tests/libs/test_field_validators.py +353 -0
  123. lionagi/tests/libs/test_queue.py +67 -0
  124. lionagi/tests/test_core/test_base_branch.py +0 -1
  125. lionagi/tests/test_core/test_branch.py +2 -0
  126. lionagi/tests/test_core/test_session_base_util.py +1 -0
  127. lionagi/version.py +1 -1
  128. {lionagi-0.0.316.dist-info → lionagi-0.1.1.dist-info}/METADATA +1 -1
  129. lionagi-0.1.1.dist-info/RECORD +190 -0
  130. lionagi/core/prompt/prompt_template.py +0 -312
  131. lionagi/core/schema/__init__.py +0 -22
  132. lionagi/core/schema/action_node.py +0 -29
  133. lionagi/core/schema/base_mixin.py +0 -296
  134. lionagi/core/schema/base_node.py +0 -199
  135. lionagi/core/schema/condition.py +0 -24
  136. lionagi/core/schema/data_logger.py +0 -354
  137. lionagi/core/schema/data_node.py +0 -93
  138. lionagi/core/schema/prompt_template.py +0 -67
  139. lionagi/core/schema/structure.py +0 -912
  140. lionagi/core/tool/manual.py +0 -1
  141. lionagi-0.0.316.dist-info/RECORD +0 -121
  142. /lionagi/core/{branch/base → execute}/__init__.py +0 -0
  143. /lionagi/core/flow/{base/baseflow.py → baseflow.py} +0 -0
  144. /lionagi/core/flow/{base/__init__.py → mono_chat_mixin.py} +0 -0
  145. /lionagi/core/{prompt → form}/__init__.py +0 -0
  146. /lionagi/{tests/test_integrations → core/graph}/__init__.py +0 -0
  147. /lionagi/{tests/test_libs → experimental}/__init__.py +0 -0
  148. /lionagi/{tests/test_libs/test_async.py → experimental/directive/__init__.py} +0 -0
  149. /lionagi/tests/{test_libs → libs}/test_api.py +0 -0
  150. /lionagi/tests/{test_libs → libs}/test_convert.py +0 -0
  151. /lionagi/tests/{test_libs → libs}/test_func_call.py +0 -0
  152. /lionagi/tests/{test_libs → libs}/test_nested.py +0 -0
  153. /lionagi/tests/{test_libs → libs}/test_parse.py +0 -0
  154. /lionagi/tests/{test_libs → libs}/test_sys_util.py +0 -0
  155. {lionagi-0.0.316.dist-info → lionagi-0.1.1.dist-info}/LICENSE +0 -0
  156. {lionagi-0.0.316.dist-info → lionagi-0.1.1.dist-info}/WHEEL +0 -0
  157. {lionagi-0.0.316.dist-info → lionagi-0.1.1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,289 @@
1
+ import json
2
+ import inspect
3
+ import re
4
+
5
+ from lionagi.core import System, Instruction
6
+ from lionagi.core.tool import Tool
7
+ from lionagi.core.generic.action import ActionSelection
8
+ from lionagi.core.agent.base_agent import BaseAgent
9
+ from lionagi.core.generic.condition import Condition
10
+
11
+ from lionagi.core import func_to_tool
12
+
13
+
14
+ def output_node_list(structure):
15
+ """
16
+ Processes a structure object to extract and format all associated nodes into a summary list and detailed output dictionary.
17
+
18
+ This function traverses a structure, extracting key properties of nodes and organizing them by type into a dictionary for easy access and manipulation.
19
+
20
+ Args:
21
+ structure: The structure object containing nodes and potentially other nested structures.
22
+
23
+ Returns:
24
+ tuple: A tuple containing a summary list of all nodes and a dictionary categorizing nodes by type.
25
+ """
26
+ summary_list = []
27
+ output = {}
28
+
29
+ structure_output = {
30
+ "id": structure.id_,
31
+ "timestamp": structure.timestamp,
32
+ "type": structure.class_name(),
33
+ }
34
+ summary_list.append(structure_output.copy())
35
+ structure_output["head_nodes"] = [i.id_ for i in structure.get_heads()]
36
+ # structure_output['nodes'] = json.dumps([i for i in structure.internal_nodes.keys()])
37
+ # structure_output['edges'] = json.dumps([i for i in structure.internal_edges.keys()])
38
+ output[structure_output["type"]] = [structure_output]
39
+ for node in structure.internal_nodes.values():
40
+ node_output = {
41
+ "id": node.id_,
42
+ "timestamp": node.timestamp,
43
+ "type": node.class_name(),
44
+ }
45
+ summary_list.append(node_output.copy())
46
+ if isinstance(node, System) or isinstance(node, Instruction):
47
+ node_output["content"] = json.dumps(node.content)
48
+ node_output["sender"] = node.sender
49
+ node_output["recipient"] = node.recipient
50
+ elif isinstance(node, Tool):
51
+ node_output["function"] = inspect.getsource(node.func)
52
+ # node_output['manual'] = node.manual
53
+ node_output["parser"] = (
54
+ inspect.getsource(node.parser) if node.parser else None
55
+ )
56
+ elif isinstance(node, ActionSelection):
57
+ node_output["action"] = node.action
58
+ node_output["action_kwargs"] = json.dumps(node.action_kwargs)
59
+ elif isinstance(node, BaseAgent):
60
+ node_output["structure_id"] = node.structure.id_
61
+ node_output["output_parser"] = inspect.getsource(node.output_parser)
62
+ else:
63
+ raise ValueError("Not supported node type detected")
64
+ if node_output["type"] not in output:
65
+ output[node_output["type"]] = []
66
+ output[node_output["type"]].append(node_output)
67
+
68
+ return summary_list, output
69
+
70
+
71
+ def output_edge_list(structure):
72
+ """
73
+ Extracts and formats all edges from a given structure into a list and maps any associated condition classes.
74
+
75
+ This function collects edge data from a structure, including identifiers, timestamps, labels, and conditions. It also compiles any unique condition classes associated with these edges.
76
+
77
+ Args:
78
+ structure: The structure object containing edges.
79
+
80
+ Returns:
81
+ tuple: A tuple containing a list of all edges with their details and a list of unique condition classes.
82
+ """
83
+ edge_list = []
84
+ edge_cls_dict = {}
85
+ for edge in structure.internal_edges.values():
86
+ edge_output = {
87
+ "id": edge.id_,
88
+ "timestamp": edge.timestamp,
89
+ "head": edge.head,
90
+ "tail": edge.tail,
91
+ "label": edge.label,
92
+ "bundle": str(edge.bundle),
93
+ }
94
+ if edge.condition:
95
+ cls_name = edge.condition.__class__.__qualname__
96
+ cond = json.dumps({"class": cls_name, "args": edge.condition.model_dump()})
97
+ cls = edge.string_condition()
98
+ if cls is not None and cls_name not in edge_cls_dict:
99
+ edge_cls_dict.update({cls_name: cls})
100
+ else:
101
+ cond = None
102
+ edge_output["condition"] = cond
103
+ edge_list.append(edge_output)
104
+
105
+ edge_cls_list = []
106
+ for key, value in edge_cls_dict.items():
107
+ edge_cls_list.append({"class_name": key, "class": value})
108
+
109
+ return edge_list, edge_cls_list
110
+
111
+
112
+ class ParseNode:
113
+ """
114
+ Provides static methods for converting code strings to functional definitions, classes, and for parsing various types of structured nodes based on dictionary definitions.
115
+
116
+ This utility class facilitates the dynamic execution of code and the instantiation of objects from serialized data.
117
+ """
118
+
119
+ @staticmethod
120
+ def convert_to_def(function_code):
121
+ """
122
+ Converts a string containing a function definition into a callable function object.
123
+
124
+ Args:
125
+ function_code (str): The string code of the function to convert.
126
+
127
+ Returns:
128
+ function: The converted function as a callable.
129
+
130
+ Raises:
131
+ ValueError: If the function code is invalid or the function name cannot be detected.
132
+ """
133
+ import re
134
+
135
+ match = re.search(r"def (\w+)\(", function_code)
136
+ if match:
137
+ class_name = match.group(1)
138
+ try:
139
+ exec(function_code, globals())
140
+ func = globals()[class_name]
141
+ return func
142
+ except Exception as e:
143
+ raise ValueError(f"Failed to convert str to def. Error: {e}")
144
+ else:
145
+ raise ValueError("Failed to detect function name")
146
+
147
+ @staticmethod
148
+ def convert_to_class(cls_code):
149
+ """
150
+ Converts a string containing a class definition into a class object.
151
+
152
+ Args:
153
+ cls_code (str): The string code of the class to convert.
154
+
155
+ Returns:
156
+ class: The converted class.
157
+
158
+ Raises:
159
+ ValueError: If the class code is invalid or the class name cannot be detected.
160
+ """
161
+ import re
162
+
163
+ match = re.search(r"class (\w+)\s*(?:\(([^)]+)\))?:", cls_code)
164
+ if match:
165
+ class_name = match.group(1)
166
+ try:
167
+ exec(cls_code, globals())
168
+ cls = globals()[class_name]
169
+ return cls
170
+ except Exception as e:
171
+ raise ValueError(f"Failed to convert str to class. Error: {e}")
172
+ else:
173
+ raise ValueError("Failed to detect class name")
174
+
175
+ @staticmethod
176
+ def parse_system(info_dict):
177
+ """
178
+ Parses dictionary information into a System node object.
179
+
180
+ Args:
181
+ info_dict (dict): A dictionary containing properties of a system node.
182
+
183
+ Returns:
184
+ System: An instantiated System node filled with properties from info_dict.
185
+ """
186
+ node = System(" ")
187
+ node.id_ = info_dict["id"]
188
+ node.timestamp = info_dict["timestamp"]
189
+ node.content = json.loads(info_dict["content"])
190
+ node.sender = info_dict["sender"]
191
+ node.recipient = info_dict["recipient"]
192
+ return node
193
+
194
+ @staticmethod
195
+ def parse_instruction(info_dict):
196
+ """
197
+ Parses dictionary information into an Instruction node object.
198
+
199
+ Args:
200
+ info_dict (dict): A dictionary containing properties of an instruction node.
201
+
202
+ Returns:
203
+ Instruction: An instantiated Instruction node filled with properties from info_dict.
204
+ """
205
+ node = Instruction(" ")
206
+ node.id_ = info_dict["id"]
207
+ node.timestamp = info_dict["timestamp"]
208
+ node.content = json.loads(info_dict["content"])
209
+ node.sender = info_dict["sender"]
210
+ node.recipient = info_dict["recipient"]
211
+ return node
212
+
213
+ @staticmethod
214
+ def parse_actionSelection(info_dict):
215
+ """
216
+ Parses dictionary information into an ActionSelection node object.
217
+
218
+ Args:
219
+ info_dict (dict): A dictionary containing properties of an action selection node.
220
+
221
+ Returns:
222
+ ActionSelection: An instantiated ActionSelection node filled with properties from info_dict.
223
+ """
224
+ node = ActionSelection()
225
+ node.id_ = info_dict["id"]
226
+ node.action = info_dict["action"]
227
+ if "action_kwargs" in info_dict:
228
+ if info_dict["action_kwargs"]:
229
+ node.action_kwargs = json.loads(info_dict["action_kwargs"])
230
+ elif "actionKwargs" in info_dict:
231
+ if info_dict["actionKwargs"]:
232
+ node.action_kwargs = json.loads(info_dict["actionKwargs"])
233
+ return node
234
+
235
+ @staticmethod
236
+ def parse_tool(info_dict):
237
+ """
238
+ Parses dictionary information into a Tool node object, converting associated function code into a callable.
239
+
240
+ Args:
241
+ info_dict (dict): A dictionary containing properties and function code for a tool node.
242
+
243
+ Returns:
244
+ Tool: An instantiated Tool node with the function converted from code.
245
+
246
+ Raises:
247
+ ValueError: If unsafe code is detected in the function definition.
248
+ """
249
+ func_code = info_dict["function"]
250
+ if "import os" in func_code or "__" in func_code or "import sys" in func_code:
251
+ raise ValueError(
252
+ "Unsafe code detected in Tool function. Please double check or implement explicitly"
253
+ )
254
+
255
+ func = ParseNode.convert_to_def(func_code)
256
+ tool = func_to_tool(func)
257
+ if func.__doc__:
258
+ if re.search(r":param \w+:", func.__doc__):
259
+ tool = func_to_tool(func, docstring_style="reST")
260
+
261
+ tool = tool[0]
262
+ tool.id_ = info_dict["id"]
263
+ tool.timestamp = info_dict["timestamp"]
264
+ return tool
265
+
266
+ @staticmethod
267
+ def parse_condition(condition, cls_code):
268
+ """
269
+ Parses a condition dictionary and corresponding class code into a class instance representing the condition.
270
+
271
+ Args:
272
+ condition (dict): A dictionary containing the serialized form of the condition's arguments.
273
+ cls_code (str): The class code to instantiate the condition class.
274
+
275
+ Returns:
276
+ An instance of the condition class filled with properties from the condition dictionary.
277
+
278
+ Raises:
279
+ ValueError: If the condition or class code is invalid.
280
+ """
281
+ args = condition["args"]
282
+ cls = ParseNode.convert_to_class(cls_code)
283
+
284
+ init_params = {}
285
+ for key in inspect.signature(cls.__init__).parameters.keys():
286
+ if key == "self":
287
+ continue
288
+ init_params[key] = args[key]
289
+ return cls(**init_params)
@@ -0,0 +1,63 @@
1
+ import zipfile
2
+ import pandas as pd
3
+ from pathlib import Path
4
+
5
+ from lionagi.integrations.storage.storage_util import output_node_list, output_edge_list
6
+
7
+
8
+ def _output_csv(
9
+ node_list, node_dict, edge_list, edge_cls_list, zipname="structure_storage"
10
+ ):
11
+ """
12
+ Writes provided node and edge data into multiple CSV files and compresses them into a ZIP archive.
13
+
14
+ This helper function takes lists and dictionaries of nodes and edges, converts them to pandas DataFrames,
15
+ and then writes each DataFrame to a CSV file stored inside a ZIP archive. This includes a separate CSV
16
+ for each type of node and edge, as well as edge conditions if they exist.
17
+
18
+ Args:
19
+ node_list (list): A list of dictionaries where each dictionary contains attributes of a single node.
20
+ node_dict (dict): A dictionary of lists where each key represents a node type and the value is a list of
21
+ node attributes for nodes of that type.
22
+ edge_list (list): A list of dictionaries where each dictionary contains attributes of a single edge.
23
+ edge_cls_list (list): A list of dictionaries where each dictionary contains attributes of edge conditions.
24
+ zipname (str): The base name for the output ZIP file that will store the CSV files.
25
+
26
+ Returns:
27
+ None: This function does not return a value but outputs a ZIP file containing the CSVs.
28
+ """
29
+ tables = {"Nodes": pd.DataFrame(node_list), "Edges": pd.DataFrame(edge_list)}
30
+ if edge_cls_list:
31
+ tables["EdgesCondClass"] = pd.DataFrame(edge_cls_list)
32
+ for i in node_dict:
33
+ tables[i] = pd.DataFrame(node_dict[i])
34
+
35
+ zipname = zipname + ".zip"
36
+
37
+ with zipfile.ZipFile(zipname, "w") as zf:
38
+ for i in tables:
39
+ filename = i + ".csv"
40
+ with zf.open(filename, "w") as file:
41
+ tables[i].to_csv(file, index=False)
42
+
43
+
44
+ def to_csv(structure, filename="structure_storage"):
45
+ """
46
+ Converts a structure into a series of CSV files and stores them in a compressed ZIP archive.
47
+
48
+ This function processes a given structure to extract detailed node and edge information,
49
+ including conditions if applicable. These details are then saved into separate CSV files
50
+ for nodes, edges, and any edge conditions, which are subsequently bundled into a ZIP file.
51
+
52
+ Args:
53
+ structure: An object representing the structure to be serialized. This structure should
54
+ have methods to return lists of nodes and edges.
55
+ filename (str): The base name of the output ZIP file that will store the CSV files.
56
+
57
+ Returns:
58
+ None: This function does not return a value but outputs a ZIP file containing CSVs.
59
+ """
60
+ node_list, node_dict = output_node_list(structure)
61
+ edge_list, edge_cls_list = output_edge_list(structure)
62
+
63
+ _output_csv(node_list, node_dict, edge_list, edge_cls_list, filename)
@@ -0,0 +1,67 @@
1
+ import pandas as pd
2
+ from lionagi.libs import SysUtil
3
+
4
+ from lionagi.integrations.storage.storage_util import output_node_list, output_edge_list
5
+
6
+
7
+ def _output_excel(
8
+ node_list, node_dict, edge_list, edge_cls_list, filename="structure_storage"
9
+ ):
10
+ """
11
+ Writes provided node and edge data into multiple sheets of a single Excel workbook.
12
+
13
+ This helper function takes lists and dictionaries of nodes and edges, converts them into pandas DataFrames,
14
+ and then writes each DataFrame to a distinct sheet in an Excel workbook. This includes a separate sheet
15
+ for each type of node and edge, as well as edge conditions if they exist.
16
+
17
+ Args:
18
+ node_list (list): A list of dictionaries where each dictionary contains attributes of a single node.
19
+ node_dict (dict): A dictionary of lists where each key represents a node type and the value is a list of
20
+ node attributes for nodes of that type.
21
+ edge_list (list): A list of dictionaries where each dictionary contains attributes of a single edge.
22
+ edge_cls_list (list): A list of dictionaries where each dictionary contains attributes of edge conditions.
23
+ filename (str): The base name for the output Excel file. The '.xlsx' extension will be added
24
+ automatically if not included.
25
+
26
+ Returns:
27
+ None: This function does not return a value but writes data directly to an Excel workbook.
28
+
29
+ Raises:
30
+ ImportError: If the required 'openpyxl' library is not installed, which is necessary for pandas to write Excel files.
31
+ """
32
+ SysUtil.check_import("openpyxl")
33
+
34
+ tables = {"Nodes": pd.DataFrame(node_list), "Edges": pd.DataFrame(edge_list)}
35
+ if edge_cls_list:
36
+ tables["EdgesCondClass"] = pd.DataFrame(edge_cls_list)
37
+ for i in node_dict:
38
+ tables[i] = pd.DataFrame(node_dict[i])
39
+
40
+ filename = filename + ".xlsx"
41
+
42
+ with pd.ExcelWriter(filename) as writer:
43
+ for i in tables:
44
+ tables[i].to_excel(writer, sheet_name=i, index=False)
45
+
46
+
47
+ def to_excel(structure, filename="structure_storage"):
48
+ """
49
+ Converts a structure into a series of Excel sheets within a single workbook.
50
+
51
+ This function processes the specified structure to extract detailed node and edge information,
52
+ including conditions if applicable. These details are then saved into separate sheets within an
53
+ Excel workbook for nodes, edges, and any edge conditions.
54
+
55
+ Args:
56
+ structure: An object representing the structure to be serialized. This should have methods
57
+ to return lists of nodes and edges suitable for output.
58
+ filename (str): The base name of the output Excel file. The '.xlsx' extension will be added
59
+ automatically if not included.
60
+
61
+ Returns:
62
+ None: This function does not return a value but outputs an Excel workbook with multiple sheets.
63
+ """
64
+ node_list, node_dict = output_node_list(structure)
65
+ edge_list, edge_cls_list = output_edge_list(structure)
66
+
67
+ _output_excel(node_list, node_dict, edge_list, edge_cls_list, filename)
lionagi/libs/ln_api.py CHANGED
@@ -377,8 +377,8 @@ class APIUtil:
377
377
  num_tokens += 2 # every reply is primed with <im_start>assistant
378
378
  return num_tokens + completion_tokens
379
379
  else:
380
- prompt = payload["prompt"]
381
- if isinstance(prompt, str): # single prompt
380
+ prompt = payload["format_prompt"]
381
+ if isinstance(prompt, str): # single format_prompt
382
382
  prompt_tokens = len(encoding.encode(prompt))
383
383
  return prompt_tokens + completion_tokens
384
384
  elif isinstance(prompt, list): # multiple prompts
@@ -386,7 +386,7 @@ class APIUtil:
386
386
  return prompt_tokens + completion_tokens * len(prompt)
387
387
  else:
388
388
  raise TypeError(
389
- 'Expecting either string or list of strings for "prompt" field in completion request'
389
+ 'Expecting either string or list of strings for "format_prompt" field in completion request'
390
390
  )
391
391
  elif api_endpoint == "embeddings":
392
392
  input = payload["input"]