lionagi 0.1.2__py3-none-any.whl → 0.2.1__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.
Files changed (268) hide show
  1. lionagi/__init__.py +60 -5
  2. lionagi/core/__init__.py +0 -25
  3. lionagi/core/_setting/_setting.py +59 -0
  4. lionagi/core/action/__init__.py +14 -0
  5. lionagi/core/action/function_calling.py +136 -0
  6. lionagi/core/action/manual.py +1 -0
  7. lionagi/core/action/node.py +109 -0
  8. lionagi/core/action/tool.py +114 -0
  9. lionagi/core/action/tool_manager.py +356 -0
  10. lionagi/core/agent/base_agent.py +27 -13
  11. lionagi/core/agent/eval/evaluator.py +1 -0
  12. lionagi/core/agent/eval/vote.py +40 -0
  13. lionagi/core/agent/learn/learner.py +59 -0
  14. lionagi/core/agent/plan/unit_template.py +1 -0
  15. lionagi/core/collections/__init__.py +17 -0
  16. lionagi/core/{generic/data_logger.py → collections/_logger.py} +69 -55
  17. lionagi/core/collections/abc/__init__.py +53 -0
  18. lionagi/core/collections/abc/component.py +615 -0
  19. lionagi/core/collections/abc/concepts.py +297 -0
  20. lionagi/core/collections/abc/exceptions.py +150 -0
  21. lionagi/core/collections/abc/util.py +45 -0
  22. lionagi/core/collections/exchange.py +161 -0
  23. lionagi/core/collections/flow.py +426 -0
  24. lionagi/core/collections/model.py +419 -0
  25. lionagi/core/collections/pile.py +913 -0
  26. lionagi/core/collections/progression.py +236 -0
  27. lionagi/core/collections/util.py +64 -0
  28. lionagi/core/director/direct.py +314 -0
  29. lionagi/core/director/director.py +2 -0
  30. lionagi/core/{execute/branch_executor.py → engine/branch_engine.py} +134 -97
  31. lionagi/core/{execute/instruction_map_executor.py → engine/instruction_map_engine.py} +80 -55
  32. lionagi/{experimental/directive/evaluator → core/engine}/script_engine.py +17 -1
  33. lionagi/core/executor/base_executor.py +90 -0
  34. lionagi/core/{execute/structure_executor.py → executor/graph_executor.py} +62 -66
  35. lionagi/core/{execute → executor}/neo4j_executor.py +70 -67
  36. lionagi/core/generic/__init__.py +3 -33
  37. lionagi/core/generic/edge.py +29 -79
  38. lionagi/core/generic/edge_condition.py +16 -0
  39. lionagi/core/generic/graph.py +236 -0
  40. lionagi/core/generic/hyperedge.py +1 -0
  41. lionagi/core/generic/node.py +156 -221
  42. lionagi/core/generic/tree.py +48 -0
  43. lionagi/core/generic/tree_node.py +79 -0
  44. lionagi/core/mail/__init__.py +12 -0
  45. lionagi/core/mail/mail.py +25 -0
  46. lionagi/core/mail/mail_manager.py +139 -58
  47. lionagi/core/mail/package.py +45 -0
  48. lionagi/core/mail/start_mail.py +36 -0
  49. lionagi/core/message/__init__.py +19 -0
  50. lionagi/core/message/action_request.py +133 -0
  51. lionagi/core/message/action_response.py +135 -0
  52. lionagi/core/message/assistant_response.py +95 -0
  53. lionagi/core/message/instruction.py +234 -0
  54. lionagi/core/message/message.py +101 -0
  55. lionagi/core/message/system.py +86 -0
  56. lionagi/core/message/util.py +283 -0
  57. lionagi/core/report/__init__.py +4 -0
  58. lionagi/core/report/base.py +217 -0
  59. lionagi/core/report/form.py +231 -0
  60. lionagi/core/report/report.py +166 -0
  61. lionagi/core/report/util.py +28 -0
  62. lionagi/core/rule/_default.py +16 -0
  63. lionagi/core/rule/action.py +99 -0
  64. lionagi/core/rule/base.py +238 -0
  65. lionagi/core/rule/boolean.py +56 -0
  66. lionagi/core/rule/choice.py +47 -0
  67. lionagi/core/rule/mapping.py +96 -0
  68. lionagi/core/rule/number.py +71 -0
  69. lionagi/core/rule/rulebook.py +109 -0
  70. lionagi/core/rule/string.py +52 -0
  71. lionagi/core/rule/util.py +35 -0
  72. lionagi/core/session/branch.py +431 -0
  73. lionagi/core/session/directive_mixin.py +287 -0
  74. lionagi/core/session/session.py +229 -903
  75. lionagi/core/structure/__init__.py +1 -0
  76. lionagi/core/structure/chain.py +1 -0
  77. lionagi/core/structure/forest.py +1 -0
  78. lionagi/core/structure/graph.py +1 -0
  79. lionagi/core/structure/tree.py +1 -0
  80. lionagi/core/unit/__init__.py +5 -0
  81. lionagi/core/unit/parallel_unit.py +245 -0
  82. lionagi/core/unit/template/action.py +81 -0
  83. lionagi/core/unit/template/base.py +51 -0
  84. lionagi/core/unit/template/plan.py +84 -0
  85. lionagi/core/unit/template/predict.py +109 -0
  86. lionagi/core/unit/template/score.py +124 -0
  87. lionagi/core/unit/template/select.py +104 -0
  88. lionagi/core/unit/unit.py +362 -0
  89. lionagi/core/unit/unit_form.py +305 -0
  90. lionagi/core/unit/unit_mixin.py +1168 -0
  91. lionagi/core/unit/util.py +71 -0
  92. lionagi/core/validator/validator.py +364 -0
  93. lionagi/core/work/work.py +76 -0
  94. lionagi/core/work/work_function.py +101 -0
  95. lionagi/core/work/work_queue.py +103 -0
  96. lionagi/core/work/worker.py +258 -0
  97. lionagi/core/work/worklog.py +120 -0
  98. lionagi/experimental/compressor/base.py +46 -0
  99. lionagi/experimental/compressor/llm_compressor.py +247 -0
  100. lionagi/experimental/compressor/llm_summarizer.py +61 -0
  101. lionagi/experimental/compressor/util.py +70 -0
  102. lionagi/experimental/directive/__init__.py +19 -0
  103. lionagi/experimental/directive/parser/base_parser.py +69 -2
  104. lionagi/experimental/directive/{template_ → template}/base_template.py +17 -1
  105. lionagi/{libs/ln_tokenizer.py → experimental/directive/tokenizer.py} +16 -0
  106. lionagi/experimental/{directive/evaluator → evaluator}/ast_evaluator.py +16 -0
  107. lionagi/experimental/{directive/evaluator → evaluator}/base_evaluator.py +16 -0
  108. lionagi/experimental/knowledge/base.py +10 -0
  109. lionagi/experimental/memory/__init__.py +0 -0
  110. lionagi/experimental/strategies/__init__.py +0 -0
  111. lionagi/experimental/strategies/base.py +1 -0
  112. lionagi/integrations/bridge/langchain_/documents.py +4 -0
  113. lionagi/integrations/bridge/llamaindex_/index.py +30 -0
  114. lionagi/integrations/bridge/llamaindex_/llama_index_bridge.py +6 -0
  115. lionagi/integrations/chunker/chunk.py +161 -24
  116. lionagi/integrations/config/oai_configs.py +34 -3
  117. lionagi/integrations/config/openrouter_configs.py +14 -2
  118. lionagi/integrations/loader/load.py +122 -21
  119. lionagi/integrations/loader/load_util.py +6 -77
  120. lionagi/integrations/provider/_mapping.py +46 -0
  121. lionagi/integrations/provider/litellm.py +2 -1
  122. lionagi/integrations/provider/mlx_service.py +16 -9
  123. lionagi/integrations/provider/oai.py +91 -4
  124. lionagi/integrations/provider/ollama.py +6 -5
  125. lionagi/integrations/provider/openrouter.py +115 -8
  126. lionagi/integrations/provider/services.py +2 -2
  127. lionagi/integrations/provider/transformers.py +18 -22
  128. lionagi/integrations/storage/__init__.py +3 -3
  129. lionagi/integrations/storage/neo4j.py +52 -60
  130. lionagi/integrations/storage/storage_util.py +44 -46
  131. lionagi/integrations/storage/structure_excel.py +43 -26
  132. lionagi/integrations/storage/to_excel.py +11 -4
  133. lionagi/libs/__init__.py +22 -1
  134. lionagi/libs/ln_api.py +75 -20
  135. lionagi/libs/ln_context.py +37 -0
  136. lionagi/libs/ln_convert.py +21 -9
  137. lionagi/libs/ln_func_call.py +69 -28
  138. lionagi/libs/ln_image.py +107 -0
  139. lionagi/libs/ln_nested.py +26 -11
  140. lionagi/libs/ln_parse.py +82 -23
  141. lionagi/libs/ln_queue.py +16 -0
  142. lionagi/libs/ln_tokenize.py +164 -0
  143. lionagi/libs/ln_validate.py +16 -0
  144. lionagi/libs/special_tokens.py +172 -0
  145. lionagi/libs/sys_util.py +95 -24
  146. lionagi/lions/coder/code_form.py +13 -0
  147. lionagi/lions/coder/coder.py +50 -3
  148. lionagi/lions/coder/util.py +30 -25
  149. lionagi/tests/libs/test_func_call.py +23 -21
  150. lionagi/tests/libs/test_nested.py +36 -21
  151. lionagi/tests/libs/test_parse.py +1 -1
  152. lionagi/tests/test_core/collections/__init__.py +0 -0
  153. lionagi/tests/test_core/collections/test_component.py +206 -0
  154. lionagi/tests/test_core/collections/test_exchange.py +138 -0
  155. lionagi/tests/test_core/collections/test_flow.py +145 -0
  156. lionagi/tests/test_core/collections/test_pile.py +171 -0
  157. lionagi/tests/test_core/collections/test_progression.py +129 -0
  158. lionagi/tests/test_core/generic/test_edge.py +67 -0
  159. lionagi/tests/test_core/generic/test_graph.py +96 -0
  160. lionagi/tests/test_core/generic/test_node.py +106 -0
  161. lionagi/tests/test_core/generic/test_tree_node.py +73 -0
  162. lionagi/tests/test_core/test_branch.py +115 -294
  163. lionagi/tests/test_core/test_form.py +46 -0
  164. lionagi/tests/test_core/test_report.py +105 -0
  165. lionagi/tests/test_core/test_validator.py +111 -0
  166. lionagi/version.py +1 -1
  167. lionagi-0.2.1.dist-info/LICENSE +202 -0
  168. lionagi-0.2.1.dist-info/METADATA +272 -0
  169. lionagi-0.2.1.dist-info/RECORD +240 -0
  170. lionagi/core/branch/base.py +0 -653
  171. lionagi/core/branch/branch.py +0 -474
  172. lionagi/core/branch/flow_mixin.py +0 -96
  173. lionagi/core/branch/util.py +0 -323
  174. lionagi/core/direct/__init__.py +0 -19
  175. lionagi/core/direct/cot.py +0 -123
  176. lionagi/core/direct/plan.py +0 -164
  177. lionagi/core/direct/predict.py +0 -166
  178. lionagi/core/direct/react.py +0 -171
  179. lionagi/core/direct/score.py +0 -279
  180. lionagi/core/direct/select.py +0 -170
  181. lionagi/core/direct/sentiment.py +0 -1
  182. lionagi/core/direct/utils.py +0 -110
  183. lionagi/core/direct/vote.py +0 -64
  184. lionagi/core/execute/base_executor.py +0 -47
  185. lionagi/core/flow/baseflow.py +0 -23
  186. lionagi/core/flow/monoflow/ReAct.py +0 -240
  187. lionagi/core/flow/monoflow/__init__.py +0 -9
  188. lionagi/core/flow/monoflow/chat.py +0 -95
  189. lionagi/core/flow/monoflow/chat_mixin.py +0 -253
  190. lionagi/core/flow/monoflow/followup.py +0 -215
  191. lionagi/core/flow/polyflow/__init__.py +0 -1
  192. lionagi/core/flow/polyflow/chat.py +0 -251
  193. lionagi/core/form/action_form.py +0 -26
  194. lionagi/core/form/field_validator.py +0 -287
  195. lionagi/core/form/form.py +0 -302
  196. lionagi/core/form/mixin.py +0 -214
  197. lionagi/core/form/scored_form.py +0 -13
  198. lionagi/core/generic/action.py +0 -26
  199. lionagi/core/generic/component.py +0 -532
  200. lionagi/core/generic/condition.py +0 -46
  201. lionagi/core/generic/mail.py +0 -90
  202. lionagi/core/generic/mailbox.py +0 -36
  203. lionagi/core/generic/relation.py +0 -70
  204. lionagi/core/generic/signal.py +0 -22
  205. lionagi/core/generic/structure.py +0 -362
  206. lionagi/core/generic/transfer.py +0 -20
  207. lionagi/core/generic/work.py +0 -40
  208. lionagi/core/graph/graph.py +0 -126
  209. lionagi/core/graph/tree.py +0 -190
  210. lionagi/core/mail/schema.py +0 -63
  211. lionagi/core/messages/schema.py +0 -325
  212. lionagi/core/tool/__init__.py +0 -5
  213. lionagi/core/tool/tool.py +0 -28
  214. lionagi/core/tool/tool_manager.py +0 -283
  215. lionagi/experimental/report/form.py +0 -64
  216. lionagi/experimental/report/report.py +0 -138
  217. lionagi/experimental/report/util.py +0 -47
  218. lionagi/experimental/tool/function_calling.py +0 -43
  219. lionagi/experimental/tool/manual.py +0 -66
  220. lionagi/experimental/tool/schema.py +0 -59
  221. lionagi/experimental/tool/tool_manager.py +0 -138
  222. lionagi/experimental/tool/util.py +0 -16
  223. lionagi/experimental/validator/rule.py +0 -139
  224. lionagi/experimental/validator/validator.py +0 -56
  225. lionagi/experimental/work/__init__.py +0 -10
  226. lionagi/experimental/work/async_queue.py +0 -54
  227. lionagi/experimental/work/schema.py +0 -73
  228. lionagi/experimental/work/work_function.py +0 -67
  229. lionagi/experimental/work/worker.py +0 -56
  230. lionagi/experimental/work2/form.py +0 -371
  231. lionagi/experimental/work2/report.py +0 -289
  232. lionagi/experimental/work2/schema.py +0 -30
  233. lionagi/experimental/work2/tests.py +0 -72
  234. lionagi/experimental/work2/work_function.py +0 -89
  235. lionagi/experimental/work2/worker.py +0 -12
  236. lionagi/integrations/bridge/llamaindex_/get_index.py +0 -294
  237. lionagi/tests/test_core/generic/test_component.py +0 -89
  238. lionagi/tests/test_core/test_base_branch.py +0 -426
  239. lionagi/tests/test_core/test_chat_flow.py +0 -63
  240. lionagi/tests/test_core/test_mail_manager.py +0 -75
  241. lionagi/tests/test_core/test_prompts.py +0 -51
  242. lionagi/tests/test_core/test_session.py +0 -254
  243. lionagi/tests/test_core/test_session_base_util.py +0 -313
  244. lionagi/tests/test_core/test_tool_manager.py +0 -95
  245. lionagi-0.1.2.dist-info/LICENSE +0 -9
  246. lionagi-0.1.2.dist-info/METADATA +0 -174
  247. lionagi-0.1.2.dist-info/RECORD +0 -206
  248. /lionagi/core/{branch → _setting}/__init__.py +0 -0
  249. /lionagi/core/{execute → agent/eval}/__init__.py +0 -0
  250. /lionagi/core/{flow → agent/learn}/__init__.py +0 -0
  251. /lionagi/core/{form → agent/plan}/__init__.py +0 -0
  252. /lionagi/core/{branch/executable_branch.py → agent/plan/plan.py} +0 -0
  253. /lionagi/core/{graph → director}/__init__.py +0 -0
  254. /lionagi/core/{messages → engine}/__init__.py +0 -0
  255. /lionagi/{experimental/directive/evaluator → core/engine}/sandbox_.py +0 -0
  256. /lionagi/{experimental/directive/evaluator → core/executor}/__init__.py +0 -0
  257. /lionagi/{experimental/directive/template_ → core/rule}/__init__.py +0 -0
  258. /lionagi/{experimental/report → core/unit/template}/__init__.py +0 -0
  259. /lionagi/{experimental/tool → core/validator}/__init__.py +0 -0
  260. /lionagi/{experimental/validator → core/work}/__init__.py +0 -0
  261. /lionagi/experimental/{work2 → compressor}/__init__.py +0 -0
  262. /lionagi/{core/flow/mono_chat_mixin.py → experimental/directive/template/__init__.py} +0 -0
  263. /lionagi/experimental/directive/{schema.py → template/schema.py} +0 -0
  264. /lionagi/experimental/{work2/util.py → evaluator/__init__.py} +0 -0
  265. /lionagi/experimental/{work2/work.py → knowledge/__init__.py} +0 -0
  266. /lionagi/{tests/libs/test_async.py → experimental/knowledge/graph.py} +0 -0
  267. {lionagi-0.1.2.dist-info → lionagi-0.2.1.dist-info}/WHEEL +0 -0
  268. {lionagi-0.1.2.dist-info → lionagi-0.2.1.dist-info}/top_level.txt +0 -0
@@ -1,170 +0,0 @@
1
- """
2
- This module contains the SelectTemplate class for selecting an item from given choices based on a given context.
3
-
4
- The SelectTemplate class is a subclass of ScoredTemplate and provides functionality for selecting an item from
5
- given choices using a language model. It includes fields for the input sentence, choices, selected answer,
6
- confidence score, and reason for the selection.
7
-
8
- The module also includes a function for selecting an item from given choices using the SelectTemplate class
9
- and a language model.
10
- """
11
-
12
- from enum import Enum
13
- from pydantic import Field
14
-
15
- from lionagi.libs import func_call, StringMatch
16
- from lionagi.core.form.scored_form import ScoredForm
17
- from lionagi.core.branch.branch import Branch
18
-
19
-
20
- class SelectTemplate(ScoredForm):
21
- """
22
- A class for selecting an item from given choices based on a given context.
23
-
24
- Attributes:
25
- template_name (str): The name of the select template (default: "default_select").
26
- sentence (str | list | dict): The given context.
27
- answer (Enum | str): The selected item from the given choices.
28
- signature (str): The signature indicating the input and output fields (default: "sentence -> answer").
29
-
30
- Methods:
31
- __init__(self, sentence=None, choices=None, instruction=None, reason=False, confidence_score=False, **kwargs):
32
- Initializes a new instance of the SelectTemplate class.
33
- """
34
-
35
- template_name: str = "default_select"
36
- sentence: str | list | dict = Field(
37
- default_factory=str, description="the given context"
38
- )
39
- answer: Enum | str = Field(
40
- default_factory=str, description="selection from given choices"
41
- )
42
- choices: list = Field(default_factory=list, description="the given choices")
43
-
44
- signature: str = "sentence -> answer"
45
-
46
- def __init__(
47
- self,
48
- sentence=None,
49
- choices=None,
50
- instruction=None,
51
- reason=False,
52
- confidence_score=False,
53
- **kwargs,
54
- ):
55
- """
56
- Initializes a new instance of the SelectTemplate class.
57
-
58
- Args:
59
- sentence (Optional[str | list | dict]): The given context.
60
- choices (Optional[list]): The list of choices to select from.
61
- instruction (Optional[str]): The instruction for selection.
62
- reason (bool): Whether to include the reason for the selection in the output (default: False).
63
- confidence_score (bool): Whether to include the confidence score in the output (default: False).
64
- **kwargs: Additional keyword arguments.
65
- """
66
- super().__init__(**kwargs)
67
-
68
- self.sentence = sentence
69
- self.choices = choices
70
- self.task = f"select 1 item, from provided choices {choices}."
71
- if instruction:
72
- self.task += f"objetive {instruction}."
73
-
74
- if reason:
75
- self.output_fields.append("reason")
76
-
77
- if confidence_score:
78
- self.output_fields.append("confidence_score")
79
-
80
-
81
- async def select(
82
- sentence,
83
- choices=None,
84
- instruction=None,
85
- confidence_score=False,
86
- reason=False,
87
- retries=2,
88
- delay=0.5,
89
- backoff_factor=2,
90
- default_value=None,
91
- timeout=None,
92
- branch_name=None,
93
- system=None,
94
- messages=None,
95
- service=None,
96
- sender=None,
97
- llmconfig=None,
98
- tools=None,
99
- datalogger=None,
100
- persist_path=None,
101
- tool_manager=None,
102
- **kwargs,
103
- ):
104
- """
105
- Selects an item from given choices based on a given context using a language model.
106
-
107
- Args:
108
- sentence (str | list | dict): The given context.
109
- choices (Optional[list]): The list of choices to select from.
110
- instruction (Optional[str]): The instruction for selection.
111
- confidence_score (bool): Whether to include the confidence score in the output (default: False).
112
- reason (bool): Whether to include the reason for the selection in the output (default: False).
113
- retries (int): The number of retries for the API call (default: 2).
114
- delay (float): The initial delay between retries in seconds (default: 0.5).
115
- backoff_factor (float): The backoff factor for exponential delay between retries (default: 2).
116
- default_value (Optional[Any]): The default value to return if the API call fails (default: None).
117
- timeout (Optional[float]): The timeout for the API call in seconds (default: None).
118
- branch_name (Optional[str]): The name of the branch to use for selection.
119
- system (Optional[Any]): The system configuration for the branch.
120
- messages (Optional[Any]): The messages to initialize the branch with.
121
- service (Optional[Any]): The service to use for selection.
122
- sender (Optional[str]): The sender of the selection request.
123
- llmconfig (Optional[Any]): The configuration for the language model.
124
- tools (Optional[Any]): The tools to use for selection.
125
- datalogger (Optional[Any]): The data logger for the branch.
126
- persist_path (Optional[str]): The path to persist the branch data.
127
- tool_manager (Optional[Any]): The tool manager for the branch.
128
- **kwargs: Additional keyword arguments for the API call.
129
-
130
- Returns:
131
- SelectTemplate: The select template with the selected item.
132
- """
133
- branch = Branch(
134
- name=branch_name,
135
- system=system,
136
- messages=messages,
137
- service=service,
138
- sender=sender,
139
- llmconfig=llmconfig,
140
- tools=tools,
141
- datalogger=datalogger,
142
- persist_path=persist_path,
143
- tool_manager=tool_manager,
144
- )
145
-
146
- _template = SelectTemplate(
147
- sentence=sentence,
148
- choices=choices,
149
- instruction=instruction,
150
- confidence_score=confidence_score,
151
- reason=reason,
152
- )
153
-
154
- await func_call.rcall(
155
- branch.chat,
156
- form=_template,
157
- retries=retries,
158
- delay=delay,
159
- backoff_factor=backoff_factor,
160
- default=default_value,
161
- timeout=timeout,
162
- **kwargs,
163
- )
164
-
165
- ans = _template.answer
166
-
167
- if ans not in _template.choices:
168
- _template.answer = StringMatch.choose_most_similar(ans, _template.choices)
169
-
170
- return _template
@@ -1 +0,0 @@
1
- # TODO: sentiment analysis
@@ -1,110 +0,0 @@
1
- from typing import Callable
2
- from lionagi.core.tool import func_to_tool, Tool
3
-
4
- # import contextlib
5
- # from lionagi.libs import ParseUtil, StringMatch, convert, func_call
6
-
7
-
8
- # def _parse_out(out_):
9
- # if isinstance(out_, str):
10
- # try:
11
- # out_ = ParseUtil.md_to_json(out_)
12
- # except Exception:
13
- # with contextlib.suppress(Exception):
14
- # out_ = ParseUtil.fuzzy_parse_json(out_.strip("```json").strip("```"))
15
- # return out_
16
-
17
-
18
- # def _handle_single_out(
19
- # out_,
20
- # default_key="answer",
21
- # choices=None,
22
- # to_type="dict",
23
- # to_type_kwargs=None,
24
- # to_default=True,
25
- # ):
26
-
27
- # if to_type_kwargs is None:
28
- # to_type_kwargs = {}
29
- # out_ = _parse_out(out_)
30
-
31
- # if default_key not in out_:
32
- # raise ValueError(f"Key {default_key} not found in output")
33
-
34
- # answer = out_[default_key]
35
-
36
- # if (
37
- # choices is not None
38
- # and answer not in choices
39
- # and convert.strip_lower(out_) in ["", "none", "null", "na", "n/a"]
40
- # ):
41
- # raise ValueError(f"Answer {answer} not in choices {choices}")
42
-
43
- # if to_type == "str":
44
- # answer = convert.to_str(answer, **to_type_kwargs)
45
-
46
- # elif to_type == "num":
47
- # answer = convert.to_num(answer, **to_type_kwargs)
48
-
49
- # out_[default_key] = answer
50
-
51
- # return answer if to_default else out_
52
-
53
-
54
- # def _handle_multi_out(
55
- # out_,
56
- # default_key="answer",
57
- # choices=None,
58
- # to_type="dict",
59
- # to_type_kwargs=None,
60
- # to_default=True,
61
- # include_mapping=False,
62
- # ):
63
- # if to_type_kwargs is None:
64
- # to_type_kwargs = {}
65
-
66
- # if include_mapping:
67
- # for i in out_:
68
- # i[default_key] = _handle_single_out(
69
- # i[default_key],
70
- # choices=choices,
71
- # default_key=default_key,
72
- # to_type=to_type,
73
- # to_type_kwargs=to_type_kwargs,
74
- # to_default=to_default,
75
- # )
76
- # else:
77
- # _out = []
78
- # for i in out_:
79
- # i = _handle_single_out(
80
- # i,
81
- # choices=choices,
82
- # default_key=default_key,
83
- # to_type=to_type,
84
- # to_type_kwargs=to_type_kwargs,
85
- # to_default=to_default,
86
- # )
87
- # _out.append(i)
88
- # return _out
89
-
90
- # return out_ if len(out_) > 1 else out_[0]
91
-
92
-
93
- def _process_tools(tool_obj, branch):
94
- if isinstance(tool_obj, Callable):
95
- _process_tool(tool_obj, branch)
96
- else:
97
- for i in tool_obj:
98
- _process_tool(i, branch)
99
-
100
-
101
- def _process_tool(tool_obj, branch):
102
- if (
103
- isinstance(tool_obj, Tool)
104
- and tool_obj.schema_["function"]["name"] not in branch.tool_manager.registry
105
- ):
106
- branch.register_tools(tool_obj)
107
- if isinstance(tool_obj, Callable):
108
- tool = func_to_tool(tool_obj)[0]
109
- if tool.schema_["function"]["name"] not in branch.tool_manager.registry:
110
- branch.register_tools(tool)
@@ -1,64 +0,0 @@
1
- """
2
- This module contains the vote function for generating and scoring multiple outputs and selecting the top-ranked ones.
3
-
4
- The vote function generates multiple outputs using a specified directive function (default: predict), scores each output
5
- using the score function, and returns the top-ranked output(s) based on the scores. It allows for customization of the
6
- number of generations, number of outputs to return, number of scorers, score range, and scorer instruction.
7
- """
8
-
9
- from lionagi.libs import func_call
10
- import numpy as np
11
- from lionagi.core.direct.predict import predict
12
- from lionagi.core.direct.score import score
13
-
14
-
15
- async def vote(
16
- sentence,
17
- directive=predict,
18
- num_generations=5,
19
- num_output=1,
20
- num_scorer=5,
21
- score_range=(0, 100),
22
- num_digit=2,
23
- scorer_instruction=None,
24
- **kwargs,
25
- ):
26
- """
27
- Generates and scores multiple outputs and returns the top-ranked output(s).
28
-
29
- Args:
30
- sentence (str): The input sentence or context.
31
- directive (function): The function used to generate outputs (default: predict).
32
- num_generations (int): The number of outputs to generate (default: 5).
33
- num_output (int): The number of top-ranked outputs to return (default: 1).
34
- num_scorer (int): The number of scorers to use for scoring each output (default: 5).
35
- score_range (tuple): The range of scores to assign (default: (0, 100)).
36
- num_digit (int): The number of digits after the decimal point for scores (default: 2).
37
- scorer_instruction (str): The instruction for the scorers (default: None).
38
- **kwargs: Additional keyword arguments to pass to the directive function.
39
-
40
- Returns:
41
- The top-ranked output if num_output is 1, or a list of top-ranked outputs if num_output is greater than 1.
42
- """
43
-
44
- async def _inner(i):
45
- out_ = await directive(sentence, **kwargs)
46
- score_ = await score(
47
- out_.answer,
48
- context=sentence,
49
- instruction=scorer_instruction,
50
- score_range=score_range,
51
- num_digit=num_digit,
52
- num_instances=num_scorer,
53
- return_template=False,
54
- )
55
-
56
- out_.__setattr__("score", score_)
57
- return out_
58
-
59
- _outs = await func_call.alcall(list(range(num_generations)), _inner)
60
-
61
- top_index = np.argsort([i.score for i in _outs])[-num_output:]
62
- final_output = list(np.array(_outs)[top_index])
63
-
64
- return final_output[0] if len(final_output) == 1 else final_output
@@ -1,47 +0,0 @@
1
- from collections import deque
2
- from abc import ABC, abstractmethod
3
- from typing import Any
4
-
5
- from pydantic import Field
6
-
7
- from lionagi.core.generic import BaseComponent
8
- from lionagi.core.mail.schema import BaseMail
9
-
10
-
11
- class BaseExecutor(BaseComponent, ABC):
12
- pending_ins: dict = Field(
13
- default_factory=dict, description="The pending incoming mails."
14
- )
15
- pending_outs: deque = Field(
16
- default_factory=deque, description="The pending outgoing mails."
17
- )
18
- execute_stop: bool = Field(
19
- False, description="A flag indicating whether to stop execution."
20
- )
21
- context: dict | str | list | None = Field(
22
- None, description="The context buffer for the next instruction."
23
- )
24
- execution_responses: list = Field(
25
- default_factory=list, description="The list of responses."
26
- )
27
- context_log: list = Field(default_factory=list, description="The context log.")
28
- verbose: bool = Field(
29
- True, description="A flag indicating whether to provide verbose output."
30
- )
31
-
32
- def send(self, recipient_id: str, category: str, package: Any) -> None:
33
- """
34
- Sends a mail to a recipient.
35
-
36
- Args:
37
- recipient_id (str): The ID of the recipient.
38
- category (str): The category of the mail.
39
- package (Any): The package to send in the mail.
40
- """
41
- mail = BaseMail(
42
- sender_id=self.id_,
43
- recipient_id=recipient_id,
44
- category=category,
45
- package=package,
46
- )
47
- self.pending_outs.append(mail)
@@ -1,23 +0,0 @@
1
- from abc import ABC
2
-
3
-
4
- class BaseFlow(ABC):
5
-
6
- @classmethod
7
- def class_name(cls) -> str:
8
- """
9
- Returns the class name of the flow.
10
- """
11
- return cls.__name__
12
-
13
-
14
- class BaseMonoFlow(BaseFlow):
15
-
16
- def __init__(self, branch) -> None:
17
- self.branch = branch
18
-
19
-
20
- class BasePolyFlow(BaseFlow):
21
-
22
- def __init__(self, session) -> None:
23
- self.session = session
@@ -1,240 +0,0 @@
1
- """
2
- This module contains the MonoReAct class for performing reasoning and action tasks with an LLM.
3
-
4
- The MonoReAct class allows for conducting a series of reasoning and action steps with an LLM, with the ability to
5
- process instructions, system messages, and invoke tools during the conversation. It extends the MonoChat class.
6
- """
7
-
8
- from typing import Callable
9
- from lionagi.core.flow.monoflow.chat import MonoChat
10
- from lionagi.core.tool import Tool
11
- from lionagi.core.messages.schema import Instruction
12
-
13
-
14
- class MonoReAct(MonoChat):
15
- """
16
- A class for performing reasoning and action tasks with an LLM, processing instructions and system messages,
17
- and optionally invoking tools.
18
-
19
- Attributes:
20
- REASON_PROMPT (str): The default prompt for reasoning steps.
21
- ACTION_PROMPT (str): The default prompt for action steps.
22
- OUTPUT_PROMPT (str): The default prompt for presenting the final output to the user.
23
-
24
- Methods:
25
- async ReAct(self, instruction, context=None, sender=None, system=None, tools=None,
26
- num_rounds=1, reason_prompt=None, action_prompt=None, output_prompt=None, **kwargs):
27
- Performs a series of reasoning and action steps with an LLM, processing instructions and system messages,
28
- and optionally invoking tools.
29
-
30
- _get_prompt(prompt=None, default=None, num_steps=None, instruction=None) -> str:
31
- Retrieves the appropriate prompt for the reasoning or action step based on the provided parameters.
32
-
33
- _create_followup_config(self, tools, tool_choice="auto", **kwargs) -> dict:
34
- Creates the configuration for the followup steps based on the provided tools and parameters.
35
-
36
- async _ReAct(self, instruction, context=None, sender=None, system=None, tools=None,
37
- num_rounds=1, auto=False, reason_prompt=None, action_prompt=None,
38
- output_prompt=None, out=True, **kwargs):
39
- Performs the actual reasoning and action steps with the LLM, processing instructions and system messages,
40
- and optionally invoking tools.
41
- """
42
-
43
- REASON_PROMPT = """
44
- You have {num_steps} steps left in the current task. If available, integrate previous tool responses.
45
- Perform reasoning and prepare an action plan according to available tools only. Apply divide and conquer technique.
46
- """
47
-
48
- ACTION_PROMPT = """
49
- You have {num_steps} steps left in the current task. invoke tool usage.
50
- """
51
-
52
- OUTPUT_PROMPT = "Notice: Present the final output to the user. Original user instruction: {instruction}"
53
-
54
- async def ReAct(
55
- self,
56
- instruction: Instruction | str | dict[str, dict | str],
57
- context=None,
58
- sender=None,
59
- system=None,
60
- tools=None,
61
- num_rounds: int = 1,
62
- reason_prompt=None,
63
- action_prompt=None,
64
- output_prompt=None,
65
- **kwargs,
66
- ):
67
- """
68
- Performs a series of reasoning and action steps with an LLM, processing instructions and system messages,
69
- and optionally invoking tools.
70
-
71
- Args:
72
- instruction (Instruction | str | dict[str, dict | str]): The instruction for the task.
73
- context (Optional[Any]): Additional context for the task.
74
- sender (Optional[str]): The sender of the task message.
75
- system (Optional[Any]): System message to be processed during the task.
76
- tools (Optional[Any]): Specifies tools to be invoked during the task.
77
- num_rounds (int): The number of reasoning and action rounds to perform (default: 1).
78
- reason_prompt (Optional[str]): The prompt to use for reasoning steps.
79
- action_prompt (Optional[str]): The prompt to use for action steps.
80
- output_prompt (Optional[str]): The prompt to use for presenting the final output to the user.
81
- **kwargs: Additional keyword arguments for the task.
82
-
83
- Returns:
84
- The result of the reasoning and action steps.
85
- """
86
- return await self._ReAct(
87
- instruction,
88
- context=context,
89
- sender=sender,
90
- system=system,
91
- tools=tools,
92
- num_rounds=num_rounds,
93
- reason_prompt=reason_prompt,
94
- action_prompt=action_prompt,
95
- output_prompt=output_prompt,
96
- **kwargs,
97
- )
98
-
99
- @staticmethod
100
- def _get_prompt(prompt=None, default=None, num_steps=None, instruction=None):
101
- """
102
- Retrieves the appropriate prompt for the reasoning or action step based on the provided parameters.
103
-
104
- Args:
105
- prompt (Optional[str]): The prompt to use for the step.
106
- default (Optional[str]): The default prompt to use if no specific prompt is provided.
107
- num_steps (Optional[int]): The number of remaining steps in the task.
108
- instruction (Optional[Any]): The original user instruction.
109
-
110
- Returns:
111
- str: The appropriate prompt for the reasoning or action step.
112
- """
113
-
114
- if prompt is not None:
115
- return prompt
116
-
117
- try:
118
- if num_steps is not None:
119
- return default.format(num_steps=num_steps)
120
- elif instruction is not None:
121
- return default.format(instruction=instruction)
122
- except (KeyError, ValueError):
123
- pass
124
-
125
- return default
126
-
127
- def _create_followup_config(self, tools, tool_choice="auto", **kwargs):
128
- """
129
- Creates the configuration for the followup chat based on the provided tools and parameters.
130
-
131
- Args:
132
- tools (Optional[Any]): Specifies tools to be invoked during the followup chat.
133
- tool_choice (str): The choice of tools to use (default: "auto").
134
- **kwargs: Additional keyword arguments for the followup chat configuration.
135
-
136
- Returns:
137
- dict: The configuration for the followup chat.
138
-
139
- Raises:
140
- ValueError: If no tools are found and registered.
141
- """
142
- if tools and isinstance(tools, list) and isinstance(tools[0], (Callable, Tool)):
143
- self.branch.tool_manager.register_tools(tools)
144
-
145
- if not self.branch.tool_manager.has_tools:
146
- raise ValueError("No tools found. You need to register tools.")
147
-
148
- if tools is None:
149
- tools = True
150
- config = self.branch.tool_manager.parse_tool(tools=tools, **kwargs)
151
- config["tool_parsed"] = True
152
- config["tool_choice"] = tool_choice
153
- return config
154
-
155
- async def _ReAct(
156
- self,
157
- instruction: Instruction | str | dict[str, dict | str],
158
- context=None,
159
- sender=None,
160
- system=None,
161
- tools=None,
162
- num_rounds: int = 1,
163
- auto=True,
164
- reason_prompt=None,
165
- action_prompt=None,
166
- output_prompt=None,
167
- out=True,
168
- **kwargs,
169
- ):
170
- """
171
- Performs the actual reasoning and action steps with the LLM, processing instructions and system messages,
172
- and optionally invoking tools.
173
-
174
- Args:
175
- instruction (Instruction | str | dict[str, dict | str]): The instruction for the task.
176
- context (Optional[Any]): Additional context for the task.
177
- sender (Optional[str]): The sender of the task message.
178
- system (Optional[Any]): System message to be processed during the task.
179
- tools (Optional[Any]): Specifies tools to be invoked during the task.
180
- num_rounds (int): The number of reasoning and action rounds to perform (default: 1).
181
- auto (bool): Flag indicating whether to automatically determine if the task is finished (default: False).
182
- reason_prompt (Optional[str]): The prompt to use for reasoning steps.
183
- action_prompt (Optional[str]): The prompt to use for action steps.
184
- output_prompt (Optional[str]): The prompt to use for presenting the final output to the user.
185
- out (bool): Flag indicating whether to return the output of the task (default: True).
186
- **kwargs: Additional keyword arguments for the task.
187
-
188
- Returns:
189
- The result of the reasoning and action steps.
190
- """
191
- config = self._create_followup_config(tools, **kwargs)
192
- kwargs.pop("tools", None)
193
-
194
- i = 0
195
- _out = ""
196
-
197
- while i < num_rounds:
198
- _prompt = self._get_prompt(
199
- prompt=reason_prompt,
200
- default=self.REASON_PROMPT,
201
- num_steps=(num_rounds - i) * 2,
202
- )
203
- _instruct = {"NOTICE": _prompt}
204
-
205
- # reason
206
- if i == 0:
207
- _instruct["TASK"] = instruction
208
-
209
- await self.chat(
210
- _instruct, context=context, sender=sender, system=system, **kwargs
211
- )
212
-
213
- elif i > 0:
214
- await self.chat(_instruct, sender=sender, **kwargs)
215
-
216
- # action
217
- _prompt = self._get_prompt(
218
- prompt=action_prompt,
219
- default=self.ACTION_PROMPT,
220
- num_steps=(num_rounds - i) * 2 - 1,
221
- )
222
-
223
- _out = await self.chat(_prompt, sender=sender, **config)
224
-
225
- if not self.branch._is_invoked():
226
- return _out if out else None
227
-
228
- i += 1
229
-
230
-
231
- if not self.branch._is_invoked():
232
- return _out if out else None
233
-
234
- _prompt = self._get_prompt(
235
- prompt=output_prompt,
236
- default=self.OUTPUT_PROMPT,
237
- instruction=instruction,
238
- )
239
- _out = await self.chat(_prompt, sender=sender, **kwargs)
240
- return _out if out else None
@@ -1,9 +0,0 @@
1
- from .chat import MonoChat
2
- from .followup import MonoFollowup
3
- from .ReAct import MonoReAct
4
-
5
- __all__ = [
6
- "MonoChat",
7
- "MonoFollowup",
8
- "MonoReAct",
9
- ]