lionagi 0.0.316__py3-none-any.whl → 0.1.0__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. lionagi/core/__init__.py +19 -8
  2. lionagi/core/agent/__init__.py +0 -3
  3. lionagi/core/agent/base_agent.py +26 -30
  4. lionagi/core/branch/__init__.py +0 -4
  5. lionagi/core/branch/{base_branch.py → base.py} +13 -14
  6. lionagi/core/branch/branch.py +22 -20
  7. lionagi/core/branch/executable_branch.py +0 -347
  8. lionagi/core/branch/{branch_flow_mixin.py → flow_mixin.py} +6 -6
  9. lionagi/core/branch/util.py +1 -1
  10. lionagi/core/direct/__init__.py +10 -1
  11. lionagi/core/direct/cot.py +61 -26
  12. lionagi/core/direct/plan.py +10 -8
  13. lionagi/core/direct/predict.py +5 -5
  14. lionagi/core/direct/react.py +8 -8
  15. lionagi/core/direct/score.py +4 -4
  16. lionagi/core/direct/select.py +4 -4
  17. lionagi/core/direct/utils.py +7 -4
  18. lionagi/core/direct/vote.py +2 -2
  19. lionagi/core/execute/base_executor.py +50 -0
  20. lionagi/core/execute/branch_executor.py +233 -0
  21. lionagi/core/execute/instruction_map_executor.py +131 -0
  22. lionagi/core/execute/structure_executor.py +218 -0
  23. lionagi/core/flow/monoflow/ReAct.py +4 -4
  24. lionagi/core/flow/monoflow/chat.py +6 -6
  25. lionagi/core/flow/monoflow/chat_mixin.py +24 -34
  26. lionagi/core/flow/monoflow/followup.py +4 -4
  27. lionagi/core/flow/polyflow/__init__.py +1 -1
  28. lionagi/core/flow/polyflow/chat.py +15 -12
  29. lionagi/core/{prompt/action_template.py → form/action_form.py} +2 -2
  30. lionagi/core/{prompt → form}/field_validator.py +40 -31
  31. lionagi/core/form/form.py +302 -0
  32. lionagi/core/form/mixin.py +214 -0
  33. lionagi/core/{prompt/scored_template.py → form/scored_form.py} +2 -2
  34. lionagi/core/generic/__init__.py +37 -0
  35. lionagi/core/generic/action.py +26 -0
  36. lionagi/core/generic/component.py +457 -0
  37. lionagi/core/generic/condition.py +44 -0
  38. lionagi/core/generic/data_logger.py +305 -0
  39. lionagi/core/generic/edge.py +110 -0
  40. lionagi/core/generic/mail.py +90 -0
  41. lionagi/core/generic/mailbox.py +36 -0
  42. lionagi/core/generic/node.py +285 -0
  43. lionagi/core/generic/relation.py +70 -0
  44. lionagi/core/generic/signal.py +22 -0
  45. lionagi/core/generic/structure.py +362 -0
  46. lionagi/core/generic/transfer.py +20 -0
  47. lionagi/core/generic/work.py +40 -0
  48. lionagi/core/graph/graph.py +126 -0
  49. lionagi/core/graph/tree.py +190 -0
  50. lionagi/core/mail/__init__.py +0 -8
  51. lionagi/core/mail/mail_manager.py +12 -10
  52. lionagi/core/mail/schema.py +9 -2
  53. lionagi/core/messages/__init__.py +0 -3
  54. lionagi/core/messages/schema.py +17 -225
  55. lionagi/core/session/__init__.py +0 -3
  56. lionagi/core/session/session.py +25 -23
  57. lionagi/core/tool/__init__.py +3 -1
  58. lionagi/core/tool/tool.py +28 -0
  59. lionagi/core/tool/tool_manager.py +75 -75
  60. lionagi/integrations/chunker/chunk.py +7 -7
  61. lionagi/integrations/config/oai_configs.py +4 -4
  62. lionagi/integrations/loader/load.py +6 -6
  63. lionagi/integrations/loader/load_util.py +8 -8
  64. lionagi/libs/ln_api.py +3 -3
  65. lionagi/libs/ln_parse.py +43 -6
  66. lionagi/libs/ln_validate.py +288 -0
  67. lionagi/libs/sys_util.py +28 -6
  68. lionagi/tests/libs/test_async.py +0 -0
  69. lionagi/tests/libs/test_field_validators.py +353 -0
  70. lionagi/tests/test_core/test_base_branch.py +0 -1
  71. lionagi/tests/test_core/test_branch.py +3 -0
  72. lionagi/tests/test_core/test_session_base_util.py +1 -0
  73. lionagi/version.py +1 -1
  74. {lionagi-0.0.316.dist-info → lionagi-0.1.0.dist-info}/METADATA +1 -1
  75. lionagi-0.1.0.dist-info/RECORD +136 -0
  76. lionagi/core/prompt/prompt_template.py +0 -312
  77. lionagi/core/schema/__init__.py +0 -22
  78. lionagi/core/schema/action_node.py +0 -29
  79. lionagi/core/schema/base_mixin.py +0 -296
  80. lionagi/core/schema/base_node.py +0 -199
  81. lionagi/core/schema/condition.py +0 -24
  82. lionagi/core/schema/data_logger.py +0 -354
  83. lionagi/core/schema/data_node.py +0 -93
  84. lionagi/core/schema/prompt_template.py +0 -67
  85. lionagi/core/schema/structure.py +0 -912
  86. lionagi/core/tool/manual.py +0 -1
  87. lionagi-0.0.316.dist-info/RECORD +0 -121
  88. /lionagi/core/{branch/base → execute}/__init__.py +0 -0
  89. /lionagi/core/flow/{base/baseflow.py → baseflow.py} +0 -0
  90. /lionagi/core/flow/{base/__init__.py → mono_chat_mixin.py} +0 -0
  91. /lionagi/core/{prompt → form}/__init__.py +0 -0
  92. /lionagi/{tests/test_integrations → core/graph}/__init__.py +0 -0
  93. /lionagi/tests/{test_libs → integrations}/__init__.py +0 -0
  94. /lionagi/tests/{test_libs/test_async.py → libs/__init__.py} +0 -0
  95. /lionagi/tests/{test_libs → libs}/test_api.py +0 -0
  96. /lionagi/tests/{test_libs → libs}/test_convert.py +0 -0
  97. /lionagi/tests/{test_libs → libs}/test_func_call.py +0 -0
  98. /lionagi/tests/{test_libs → libs}/test_nested.py +0 -0
  99. /lionagi/tests/{test_libs → libs}/test_parse.py +0 -0
  100. /lionagi/tests/{test_libs → libs}/test_sys_util.py +0 -0
  101. {lionagi-0.0.316.dist-info → lionagi-0.1.0.dist-info}/LICENSE +0 -0
  102. {lionagi-0.0.316.dist-info → lionagi-0.1.0.dist-info}/WHEEL +0 -0
  103. {lionagi-0.0.316.dist-info → lionagi-0.1.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,214 @@
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,9 +1,9 @@
1
1
  from lionagi.integrations.bridge.pydantic_.pydantic_bridge import Field
2
2
 
3
- from .prompt_template import PromptTemplate
3
+ from lionagi.core.form.form import Form
4
4
 
5
5
 
6
- class ScoredTemplate(PromptTemplate):
6
+ class ScoredForm(Form):
7
7
  confidence_score: float | None = Field(
8
8
  -1,
9
9
  description="a numeric score between 0 to 1 formatted in num:0.2f",
@@ -0,0 +1,37 @@
1
+ from .component import BaseComponent, BaseNode
2
+ from .condition import Condition
3
+ from .data_logger import DataLogger, DLog
4
+ from .signal import Signal, Start
5
+ from .mail import Mail, Package
6
+ from .mailbox import MailBox
7
+ from .edge import Edge
8
+ from .relation import Relations
9
+ from .transfer import Transfer
10
+ from .work import Work, Worker
11
+ from .node import Node
12
+ from .structure import BaseStructure
13
+ from .action import ActionNode, ActionSelection
14
+
15
+
16
+ __all__ = [
17
+ "BaseComponent",
18
+ "BaseNode",
19
+ "BaseStructure",
20
+ "BaseWork",
21
+ "Condition",
22
+ "Edge",
23
+ "Mail",
24
+ "MailBox",
25
+ "Start",
26
+ "Package",
27
+ "Relations",
28
+ "Signal",
29
+ "Transfer",
30
+ "Node",
31
+ "Work",
32
+ "Worker",
33
+ "ActionNode",
34
+ "ActionSelection",
35
+ "DataLogger",
36
+ "DLog",
37
+ ]
@@ -0,0 +1,26 @@
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
+ )