lionagi 0.1.2__py3-none-any.whl → 0.2.0__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
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 +74 -0
  94. lionagi/core/work/work_function.py +92 -0
  95. lionagi/core/work/work_queue.py +81 -0
  96. lionagi/core/work/worker.py +195 -0
  97. lionagi/core/work/worklog.py +124 -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.0.dist-info/LICENSE +202 -0
  168. lionagi-0.2.0.dist-info/METADATA +272 -0
  169. lionagi-0.2.0.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.0.dist-info}/WHEEL +0 -0
  268. {lionagi-0.1.2.dist-info → lionagi-0.2.0.dist-info}/top_level.txt +0 -0
lionagi/core/form/form.py DELETED
@@ -1,302 +0,0 @@
1
- from typing import Any
2
- from pydantic import Field
3
-
4
- from lionagi.libs import convert, func_call
5
-
6
- from lionagi.core.generic import BaseComponent
7
- from lionagi.core.form.field_validator import validation_funcs
8
-
9
-
10
- non_prompt_words = [
11
- "id_",
12
- "node_id",
13
- "meta",
14
- "metadata",
15
- "timestamp",
16
- "content",
17
- "signature",
18
- "task",
19
- "template_name",
20
- "version",
21
- "description",
22
- "in_validation_kwargs",
23
- "out_validation_kwargs",
24
- "fix_input",
25
- "fix_output",
26
- "input_fields",
27
- "output_fields",
28
- "choices",
29
- "prompt_fields",
30
- "prompt_fields_annotation",
31
- "instruction_context",
32
- "instruction",
33
- "instruction_output_fields",
34
- "inputs",
35
- "outputs",
36
- "process",
37
- "_validate_field",
38
- "_process_input",
39
- "_process_response",
40
- "_validate_field_choices",
41
- "_validate_input_choices",
42
- "_validate_output_choices",
43
- ]
44
-
45
-
46
- class Form(BaseComponent):
47
- template_name: str = Field(
48
- default="default_form",
49
- description="The name of the prompt template.",
50
- )
51
- signature: str = Field("null", description="signature indicating inputs, outputs")
52
- version: str | float | int | None = Field(
53
- default=None, description="The version of the prompt template."
54
- )
55
- description: str | dict[str, Any] | None | Any = Field(
56
- default=None, description="The description of the prompt template."
57
- )
58
- task: str | dict[str, Any] | None = Field(
59
- default=None, description="The task associated with the prompt template."
60
- )
61
- out_validation_kwargs: dict[str, Any] = Field(
62
- default_factory=dict, description="validation kwargs for output"
63
- )
64
- in_validation_kwargs: dict[str, Any] = Field(
65
- default_factory=dict, description="validation kwargs for input"
66
- )
67
- fix_input: bool = Field(True, description="whether to fix input")
68
- fix_output: bool = Field(True, description="whether to fix output")
69
- input_fields: list[str] = Field(
70
- default_factory=list, description="Extracted input fields from the signature."
71
- )
72
- output_fields: list[str] = Field(
73
- default_factory=list, description="Extracted output fields from the signature."
74
- )
75
- choices: dict[str, list[str]] = Field(
76
- default_factory=dict, description="Choices available for each template field."
77
- )
78
-
79
- def __init__(self, **kwargs):
80
- super().__init__(**kwargs)
81
- self.input_fields, self.output_fields = self._get_input_output_fields(
82
- self.signature
83
- )
84
- self.process(in_=True)
85
-
86
- @property
87
- def prompt_fields(self):
88
- return self.input_fields + self.output_fields
89
-
90
- @property
91
- def instruction_context(self):
92
- a = "".join(
93
- f"""
94
- ## input: {i}:
95
- - description: {self.model_fields[i].description}
96
- - value: {str(self.__getattribute__(self.input_fields[idx]))}
97
- """
98
- for idx, i in enumerate(self.input_fields)
99
- )
100
- return a.replace(" ", "")
101
-
102
- @property
103
- def instruction(self):
104
- ccc = f"""
105
- 0. Your task is {self.task},
106
- 1. provided: {self.input_fields},
107
- 2. requested: {self.output_fields}
108
- ----------
109
- """
110
- return ccc.replace(" ", "")
111
-
112
- @property
113
- def instruction_output_fields(self):
114
- return {i: self.model_fields[i].description for i in self.output_fields}
115
-
116
- @property
117
- def inputs(self):
118
- return {i: getattr(self, i) for i in self.input_fields}
119
-
120
- @property
121
- def outputs(self):
122
- return {i: getattr(self, i) for i in self.output_fields}
123
-
124
- def process(self, in_=None, out_=None):
125
- if in_:
126
- self._process_input()
127
- self._validate_input_choices()
128
- if out_:
129
- self._process_response(out_)
130
- self._validate_output_choices()
131
- return self
132
-
133
- def _validate_field_choices(self, fields, fix_: bool = False):
134
- if len(self.choices) >= 1:
135
- for k, choices in self.choices.items():
136
- if k in fields and not self._validate_field(
137
- k, getattr(self, k), choices, fix_
138
- ):
139
- raise ValueError(
140
- f"Invalid choice for field {k}: {getattr(self, k)} is not in {choices}"
141
- )
142
-
143
- def _validate_input_choices(self):
144
- return self._validate_field_choices(self.input_fields, self.fix_input)
145
-
146
- def _validate_output_choices(self):
147
- return self._validate_field_choices(self.output_fields, self.fix_output)
148
-
149
- def _validate_field(self, k, v, choices=None, keys=None, fix_=False, **kwargs):
150
-
151
- str_ = self._prompt_fields_annotation[k]
152
-
153
- if keys:
154
- v_ = validation_funcs["dict"](v, keys=keys, fix_=fix_, **kwargs)
155
- setattr(self, k, v_)
156
- return True
157
-
158
- if choices:
159
- v_ = validation_funcs["enum"](v, choices=choices, fix_=fix_, **kwargs)
160
- if v_ not in choices:
161
- raise ValueError(f"{v} is not in chocies {choices}")
162
- setattr(self, k, v_)
163
- return True
164
-
165
- if "lionagi.core.form.action_form.actionrequest" in str_:
166
- self.__setattr__(k, validation_funcs["action"](v))
167
- return True
168
-
169
- if "bool" in str_ and "str" not in str_:
170
- self.__setattr__(k, validation_funcs["bool"](v, fix_=fix_, **kwargs))
171
- return True
172
-
173
- if any(i in str_ for i in ["int", "float", "number"]) and "str" not in str_:
174
- self.__setattr__(k, validation_funcs["number"](v, fix_=fix_, **kwargs))
175
- return True
176
-
177
- elif "str" in str_:
178
- self.__setattr__(k, validation_funcs["str"](v, fix_=fix_, **kwargs))
179
- return True
180
-
181
- return False
182
-
183
- def _process_input(self, fix_=False):
184
- kwargs = self.in_validation_kwargs.copy()
185
- for k, v in self.inputs.items():
186
- if k not in kwargs:
187
- kwargs = {k: {}}
188
-
189
- if self._field_has_choices(k):
190
- self.choices[k] = self.model_fields[k].json_schema_extra["choices"]
191
- if self._validate_field(
192
- k, v, choices=self.choices[k], fix_=fix_, **kwargs[k]
193
- ):
194
- continue
195
- else:
196
- raise ValueError(f"{k} has no choices")
197
-
198
- elif self._validate_field(k, v, fix_=fix_, **kwargs[k]):
199
- continue
200
- else:
201
- raise ValueError(f"failed to validate field {k}")
202
-
203
- def _get_field_attr(self, k, attr) -> Any:
204
- if not self._field_has_attr(k, attr):
205
- raise ValueError(f"field {k} has no attribute {attr}")
206
- field = self.model_fields[k]
207
- a = getattr(field, attr)
208
- if not a:
209
- try:
210
- a = field.json_schema_extra[attr]
211
- return a
212
- except Exception:
213
- return None
214
- return a
215
-
216
- def _field_has_attr(self, k, attr):
217
- a = False
218
-
219
- field = self.model_fields[k]
220
- a = hasattr(field, attr)
221
-
222
- if not a:
223
- try:
224
- a = (
225
- self.model_fields[k].json_schema_extra[attr] is not None
226
- and attr in self.model_fields[k].json_schema_extra
227
- )
228
- return a if isinstance(a, bool) else False
229
- except Exception:
230
- return False
231
-
232
- def _field_has_keys(self, k):
233
- return self._field_has_attr(k, "keys")
234
-
235
- def _field_has_choices(self, k):
236
- return self._field_has_attr(k, "choices")
237
-
238
- def _process_choices(self, k, v, fix_=False, kwargs=None):
239
- choices = self._get_field_attr(k, "choices")
240
-
241
- if self._validate_field(k, v, choices=choices, fix_=fix_, **kwargs):
242
- self.choices[k] = choices
243
- return True
244
- else:
245
- raise ValueError(f"{k} has no choices")
246
-
247
- def _process_keys(self, k, v, fix_=False, kwargs=None):
248
- keys = self._get_field_attr(k, "keys")
249
- if self._validate_field(k, v, keys=keys, fix_=fix_, **kwargs):
250
- return True
251
- else:
252
- raise ValueError(f"{k} has no keys")
253
-
254
- def _process_response(self, out_, fix_=fix_output):
255
- kwargs = self.out_validation_kwargs.copy()
256
- for k, v in out_.items():
257
- if k not in kwargs:
258
- kwargs = {k: {}}
259
-
260
- if self._field_has_choices(k):
261
- try:
262
- return self._process_choices(k, v, fix_=fix_, kwargs=kwargs[k])
263
- except Exception as e:
264
- raise ValueError(
265
- f"failed to process field {k} with value {v}"
266
- ) from e
267
-
268
- elif self._field_has_keys(k):
269
- try:
270
- return self._process_keys(k, v, fix_=fix_, kwargs=kwargs[k])
271
- except Exception as e:
272
- raise ValueError(
273
- f"failed to process field {k} with value {v}"
274
- ) from e
275
-
276
- elif self._validate_field(k, v, fix_=fix_, **kwargs[k]):
277
- continue
278
-
279
- else:
280
- raise ValueError(f"failed to validate field {k} with value {v}")
281
-
282
- @staticmethod
283
- def _get_input_output_fields(str_: str) -> Any:
284
- inputs, outputs = str_.split("->")
285
-
286
- input_fields = [convert.strip_lower(i) for i in inputs.split(",")]
287
- output_fields = [convert.strip_lower(o) for o in outputs.split(",")]
288
-
289
- return input_fields, output_fields
290
-
291
- @property
292
- def _prompt_fields_annotation(self):
293
- dict_ = {i: self.model_fields[i].annotation for i in self.prompt_fields}
294
- for k, v in dict_.items():
295
- if "|" in str(v):
296
- v = str(v)
297
- v = v.split("|")
298
- dict_[k] = func_call.lcall(v, convert.strip_lower)
299
- else:
300
- dict_[k] = [v.__name__]
301
-
302
- return dict_
@@ -1,214 +0,0 @@
1
- from abc import ABC
2
- from typing import Any
3
- from lionagi.core.form.field_validator import validation_funcs
4
-
5
-
6
- from lionagi.libs import convert, func_call
7
-
8
- non_prompt_words = [
9
- "id_",
10
- "node_id",
11
- "meta",
12
- "metadata",
13
- "timestamp",
14
- "content",
15
- "signature",
16
- "task",
17
- "template_name",
18
- "version",
19
- "description",
20
- "in_validation_kwargs",
21
- "out_validation_kwargs",
22
- "fix_input",
23
- "fix_output",
24
- "input_fields",
25
- "output_fields",
26
- "choices",
27
- "prompt_fields",
28
- "prompt_fields_annotation",
29
- "instruction_context",
30
- "instruction",
31
- "instruction_output_fields",
32
- "in_",
33
- "out",
34
- "process",
35
- "_validate_field",
36
- "_process_input",
37
- "_process_response",
38
- "_validate_field_choices",
39
- "_validate_input_choices",
40
- "_validate_output_choices",
41
- ]
42
-
43
-
44
- class PromptTemplateMixin(ABC):
45
- def _validate_field_choices(self, fields, fix_: bool = False):
46
- if len(self.choices) >= 1:
47
- for k, choices in self.choices.items():
48
- if k in fields and not self._validate_field(
49
- k, getattr(self, k), choices, fix_
50
- ):
51
- raise ValueError(
52
- f"Invalid choice for field {k}: {getattr(self, k)} is not in {choices}"
53
- )
54
-
55
- def _validate_input_choices(self):
56
- return self._validate_field_choices(self.input_fields, self.fix_input)
57
-
58
- def _validate_output_choices(self):
59
- return self._validate_field_choices(self.output_fields, self.fix_output)
60
-
61
- def _validate_field(self, k, v, choices=None, keys=None, fix_=False, **kwargs):
62
-
63
- str_ = self._prompt_fields_annotation[k]
64
-
65
- if keys:
66
- v_ = validation_funcs["dict"](v, keys=keys, fix_=fix_, **kwargs)
67
- setattr(self, k, v_)
68
- return True
69
-
70
- if choices:
71
- v_ = validation_funcs["enum"](v, choices=choices, fix_=fix_, **kwargs)
72
- if v_ not in choices:
73
- raise ValueError(f"{v} is not in chocies {choices}")
74
- setattr(self, k, v_)
75
- return True
76
-
77
- if "lionagi.core.prompt.action_template.actionrequest" in str_:
78
- self.__setattr__(k, validation_funcs["action"](v))
79
- return True
80
-
81
- if "bool" in str_ and "str" not in str_:
82
- self.__setattr__(k, validation_funcs["bool"](v, fix_=fix_, **kwargs))
83
- return True
84
-
85
- if any(i in str_ for i in ["int", "float", "number"]) and "str" not in str_:
86
- self.__setattr__(k, validation_funcs["number"](v, fix_=fix_, **kwargs))
87
- return True
88
-
89
- elif "str" in str_:
90
- self.__setattr__(k, validation_funcs["str"](v, fix_=fix_, **kwargs))
91
- return True
92
-
93
- return False
94
-
95
- def _process_input(self, fix_=False):
96
- kwargs = self.in_validation_kwargs.copy()
97
- for k, v in self.in_.items():
98
- if k not in kwargs:
99
- kwargs = {k: {}}
100
-
101
- if self._field_has_choices(k):
102
- self.choices[k] = self.model_fields[k].json_schema_extra["choices"]
103
- if self._validate_field(
104
- k, v, choices=self.choices[k], fix_=fix_, **kwargs[k]
105
- ):
106
- continue
107
- else:
108
- raise ValueError(f"{k} has no choices")
109
-
110
- elif self._validate_field(k, v, fix_=fix_, **kwargs[k]):
111
- continue
112
- else:
113
- raise ValueError(f"failed to validate field {k}")
114
-
115
- def _get_field_attr(self, k, attr) -> Any:
116
- if not self._field_has_attr(k, attr):
117
- raise ValueError(f"field {k} has no attribute {attr}")
118
- field = self.model_fields[k]
119
- a = getattr(field, attr)
120
- if not a:
121
- try:
122
- a = field.json_schema_extra[attr]
123
- return a
124
- except Exception:
125
- return None
126
- return a
127
-
128
- def _field_has_attr(self, k, attr):
129
- a = False
130
-
131
- field = self.model_fields[k]
132
- a = hasattr(field, attr)
133
-
134
- if not a:
135
- try:
136
- a = (
137
- self.model_fields[k].json_schema_extra[attr] is not None
138
- and attr in self.model_fields[k].json_schema_extra
139
- )
140
- return a if isinstance(a, bool) else False
141
- except Exception:
142
- return False
143
-
144
- def _field_has_keys(self, k):
145
- return self._field_has_attr(k, "keys")
146
-
147
- def _field_has_choices(self, k):
148
- return self._field_has_attr(k, "choices")
149
-
150
- def _process_choices(self, k, v, fix_=False, kwargs=None):
151
- choices = self._get_field_attr(k, "choices")
152
-
153
- if self._validate_field(k, v, choices=choices, fix_=fix_, **kwargs):
154
- self.choices[k] = choices
155
- return True
156
- else:
157
- raise ValueError(f"{k} has no choices")
158
-
159
- def _process_keys(self, k, v, fix_=False, kwargs=None):
160
- keys = self._get_field_attr(k, "keys")
161
- if self._validate_field(k, v, keys=keys, fix_=fix_, **kwargs):
162
- return True
163
- else:
164
- raise ValueError(f"{k} has no keys")
165
-
166
- def _process_response(self, out_, fix_=True):
167
- kwargs = self.out_validation_kwargs.copy()
168
- for k, v in out_.items():
169
- if k not in kwargs:
170
- kwargs = {k: {}}
171
-
172
- if self._field_has_choices(k):
173
- try:
174
- return self._process_choices(k, v, fix_=fix_, kwargs=kwargs[k])
175
- except Exception as e:
176
- raise ValueError(
177
- f"failed to process field {k} with value {v}"
178
- ) from e
179
-
180
- elif self._field_has_keys(k):
181
- try:
182
- return self._process_keys(k, v, fix_=fix_, kwargs=kwargs[k])
183
- except Exception as e:
184
- raise ValueError(
185
- f"failed to process field {k} with value {v}"
186
- ) from e
187
-
188
- elif self._validate_field(k, v, fix_=fix_, **kwargs[k]):
189
- continue
190
-
191
- else:
192
- raise ValueError(f"failed to validate field {k} with value {v}")
193
-
194
- @staticmethod
195
- def _get_input_output_fields(str_: str) -> Any:
196
- inputs, outputs = str_.split("->")
197
-
198
- input_fields = [convert.strip_lower(i) for i in inputs.split(",")]
199
- output_fields = [convert.strip_lower(o) for o in outputs.split(",")]
200
-
201
- return input_fields, output_fields
202
-
203
- @property
204
- def _prompt_fields_annotation(self):
205
- dict_ = {i: self.model_fields[i].annotation for i in self.prompt_fields}
206
- for k, v in dict_.items():
207
- if "|" in str(v):
208
- v = str(v)
209
- v = v.split("|")
210
- dict_[k] = func_call.lcall(v, convert.strip_lower)
211
- else:
212
- dict_[k] = [v.__name__]
213
-
214
- return dict_
@@ -1,13 +0,0 @@
1
- from lionagi.integrations.bridge.pydantic_.pydantic_bridge import Field
2
-
3
- from lionagi.core.form.form import Form
4
-
5
-
6
- class ScoredForm(Form):
7
- confidence_score: float | None = Field(
8
- -1,
9
- description="a numeric score between 0 to 1 formatted in num:0.2f",
10
- )
11
- reason: str | None = Field(
12
- default_factory=str, description="brief reason for the given output"
13
- )
@@ -1,26 +0,0 @@
1
- from pydantic import Field
2
-
3
- from lionagi.core.generic import Node
4
- from lionagi.core.tool.tool import Tool
5
-
6
-
7
- class ActionSelection(Node):
8
- action_kwargs: dict = Field(
9
- default_factory=dict,
10
- description="The arguments for the action",
11
- alias="action_arguments",
12
- )
13
- action: str = Field(
14
- "chat", description="The action to be performed", alias="action_type"
15
- )
16
-
17
-
18
- class ActionNode(ActionSelection):
19
- tools: list[Tool] | Tool | None = Field(
20
- default_factory=list,
21
- description="The tools to be used in the action",
22
- alias="tool",
23
- )
24
- instruction: Node = Field(
25
- ..., description="The instruction for the action", alias="instruct"
26
- )