lionagi 0.2.6__py3-none-any.whl → 0.2.8__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
Files changed (172) hide show
  1. lionagi/__init__.py +11 -25
  2. lionagi/core/action/README.md +20 -0
  3. lionagi/core/action/__init__.py +1 -2
  4. lionagi/core/action/function_calling.py +2 -27
  5. lionagi/core/action/node.py +1 -16
  6. lionagi/core/action/tool.py +4 -17
  7. lionagi/core/action/tool_manager.py +8 -31
  8. lionagi/core/agent/README.md +1 -0
  9. lionagi/core/agent/base_agent.py +3 -27
  10. lionagi/core/agent/eval/README.md +1 -0
  11. lionagi/core/collections/README.md +23 -0
  12. lionagi/core/collections/__init__.py +2 -3
  13. lionagi/core/collections/_logger.py +0 -17
  14. lionagi/core/collections/abc/README.md +63 -0
  15. lionagi/core/collections/abc/__init__.py +18 -18
  16. lionagi/core/collections/abc/component.py +5 -21
  17. lionagi/core/collections/abc/concepts.py +1 -17
  18. lionagi/core/collections/abc/exceptions.py +0 -16
  19. lionagi/core/collections/exchange.py +1 -16
  20. lionagi/core/collections/flow.py +5 -25
  21. lionagi/core/collections/model.py +8 -21
  22. lionagi/core/collections/pile.py +317 -35
  23. lionagi/core/collections/progression.py +4 -17
  24. lionagi/core/collections/util.py +2 -2
  25. lionagi/core/director/README.md +1 -0
  26. lionagi/core/director/direct.py +1 -17
  27. lionagi/core/engine/branch_engine.py +6 -6
  28. lionagi/core/engine/instruction_map_engine.py +4 -4
  29. lionagi/core/engine/script_engine.py +2 -16
  30. lionagi/core/executor/base_executor.py +1 -1
  31. lionagi/core/executor/graph_executor.py +6 -9
  32. lionagi/core/executor/neo4j_executor.py +7 -9
  33. lionagi/core/generic/README.md +0 -0
  34. lionagi/core/generic/__init__.py +1 -2
  35. lionagi/core/generic/edge.py +7 -3
  36. lionagi/core/generic/edge_condition.py +3 -2
  37. lionagi/core/generic/graph.py +5 -6
  38. lionagi/core/generic/node.py +4 -4
  39. lionagi/core/generic/tree.py +2 -1
  40. lionagi/core/generic/tree_node.py +2 -0
  41. lionagi/core/mail/__init__.py +0 -1
  42. lionagi/core/mail/mail.py +2 -1
  43. lionagi/core/mail/mail_manager.py +8 -6
  44. lionagi/core/mail/package.py +2 -0
  45. lionagi/core/mail/start_mail.py +2 -2
  46. lionagi/core/message/__init__.py +4 -5
  47. lionagi/core/message/action_request.py +5 -31
  48. lionagi/core/message/action_response.py +3 -23
  49. lionagi/core/message/assistant_response.py +2 -17
  50. lionagi/core/message/instruction.py +2 -48
  51. lionagi/core/message/message.py +2 -17
  52. lionagi/core/message/system.py +2 -17
  53. lionagi/core/message/util.py +7 -26
  54. lionagi/core/report/base.py +4 -26
  55. lionagi/core/report/form.py +7 -28
  56. lionagi/core/report/report.py +3 -26
  57. lionagi/core/report/util.py +2 -17
  58. lionagi/core/rule/_default.py +3 -2
  59. lionagi/core/rule/action.py +4 -19
  60. lionagi/core/rule/base.py +4 -19
  61. lionagi/core/rule/boolean.py +2 -2
  62. lionagi/core/rule/choice.py +3 -2
  63. lionagi/core/rule/mapping.py +7 -21
  64. lionagi/core/rule/number.py +3 -1
  65. lionagi/core/rule/rulebook.py +2 -70
  66. lionagi/core/rule/string.py +2 -13
  67. lionagi/core/rule/util.py +0 -35
  68. lionagi/core/session/branch.py +20 -36
  69. lionagi/core/session/directive_mixin.py +1 -16
  70. lionagi/core/session/session.py +8 -24
  71. lionagi/core/unit/__init__.py +1 -2
  72. lionagi/core/unit/parallel_unit.py +4 -21
  73. lionagi/core/unit/template/action.py +0 -16
  74. lionagi/core/unit/template/base.py +0 -16
  75. lionagi/core/unit/template/plan.py +1 -16
  76. lionagi/core/unit/template/predict.py +0 -16
  77. lionagi/core/unit/template/score.py +1 -17
  78. lionagi/core/unit/template/select.py +1 -16
  79. lionagi/core/unit/unit.py +6 -21
  80. lionagi/core/unit/unit_form.py +6 -19
  81. lionagi/core/unit/unit_mixin.py +11 -29
  82. lionagi/core/unit/util.py +1 -0
  83. lionagi/core/validator/validator.py +8 -22
  84. lionagi/core/work/work.py +2 -19
  85. lionagi/core/work/work_edge.py +3 -3
  86. lionagi/core/work/work_function.py +1 -18
  87. lionagi/core/work/work_function_node.py +0 -5
  88. lionagi/core/work/work_queue.py +1 -16
  89. lionagi/core/work/work_task.py +4 -4
  90. lionagi/core/work/worker.py +5 -20
  91. lionagi/core/work/worker_engine.py +5 -5
  92. lionagi/core/work/worklog.py +1 -17
  93. lionagi/experimental/compressor/base.py +1 -0
  94. lionagi/experimental/compressor/llm_compressor.py +7 -4
  95. lionagi/experimental/directive/README.md +1 -0
  96. lionagi/experimental/directive/parser/base_parser.py +2 -17
  97. lionagi/experimental/directive/parser/base_syntax.txt +200 -0
  98. lionagi/experimental/directive/template/base_template.py +1 -17
  99. lionagi/experimental/directive/tokenizer.py +0 -16
  100. lionagi/experimental/evaluator/README.md +1 -0
  101. lionagi/experimental/evaluator/ast_evaluator.py +0 -16
  102. lionagi/experimental/evaluator/base_evaluator.py +1 -17
  103. lionagi/experimental/knowledge/base.py +1 -1
  104. lionagi/integrations/bridge/__init__.py +1 -1
  105. lionagi/integrations/bridge/langchain_/documents.py +1 -1
  106. lionagi/integrations/bridge/llamaindex_/node_parser.py +2 -1
  107. lionagi/integrations/bridge/llamaindex_/textnode.py +2 -1
  108. lionagi/integrations/bridge/pydantic_/pydantic_bridge.py +1 -1
  109. lionagi/integrations/bridge/transformers_/install_.py +1 -0
  110. lionagi/integrations/chunker/chunk.py +5 -6
  111. lionagi/integrations/loader/load.py +3 -3
  112. lionagi/integrations/loader/load_util.py +2 -2
  113. lionagi/integrations/provider/__init__.py +0 -1
  114. lionagi/integrations/provider/_mapping.py +6 -5
  115. lionagi/integrations/provider/mlx_service.py +4 -3
  116. lionagi/integrations/provider/oai.py +1 -17
  117. lionagi/integrations/provider/ollama.py +1 -1
  118. lionagi/integrations/provider/openrouter.py +1 -0
  119. lionagi/integrations/provider/transformers.py +2 -2
  120. lionagi/integrations/storage/neo4j.py +1 -1
  121. lionagi/integrations/storage/storage_util.py +3 -4
  122. lionagi/integrations/storage/structure_excel.py +5 -4
  123. lionagi/integrations/storage/to_csv.py +3 -2
  124. lionagi/integrations/storage/to_excel.py +2 -2
  125. lionagi/libs/__init__.py +15 -19
  126. lionagi/libs/ln_api.py +9 -26
  127. lionagi/libs/ln_async.py +3 -2
  128. lionagi/libs/ln_convert.py +1 -18
  129. lionagi/libs/ln_func_call.py +3 -20
  130. lionagi/libs/ln_image.py +4 -1
  131. lionagi/libs/ln_knowledge_graph.py +5 -2
  132. lionagi/libs/ln_nested.py +2 -18
  133. lionagi/libs/ln_parse.py +4 -19
  134. lionagi/libs/ln_queue.py +2 -17
  135. lionagi/libs/ln_tokenize.py +3 -1
  136. lionagi/libs/ln_validate.py +2 -18
  137. lionagi/libs/sys_util.py +0 -16
  138. lionagi/lions/coder/code_form.py +3 -1
  139. lionagi/lions/coder/coder.py +1 -1
  140. lionagi/lions/coder/util.py +3 -2
  141. lionagi/lions/researcher/data_source/finhub_.py +1 -0
  142. lionagi/lions/researcher/data_source/google_.py +2 -2
  143. lionagi/lions/researcher/data_source/wiki_.py +1 -1
  144. lionagi/tests/libs/test_api.py +2 -1
  145. lionagi/tests/libs/test_convert.py +2 -1
  146. lionagi/tests/libs/test_field_validators.py +9 -8
  147. lionagi/tests/libs/test_func_call.py +2 -2
  148. lionagi/tests/libs/test_nested.py +1 -0
  149. lionagi/tests/libs/test_queue.py +1 -0
  150. lionagi/tests/libs/test_sys_util.py +4 -4
  151. lionagi/tests/test_core/collections/test_component.py +6 -4
  152. lionagi/tests/test_core/collections/test_exchange.py +2 -1
  153. lionagi/tests/test_core/collections/test_flow.py +2 -1
  154. lionagi/tests/test_core/collections/test_pile.py +3 -2
  155. lionagi/tests/test_core/collections/test_progression.py +3 -2
  156. lionagi/tests/test_core/generic/test_edge.py +4 -2
  157. lionagi/tests/test_core/generic/test_graph.py +3 -2
  158. lionagi/tests/test_core/generic/test_node.py +2 -1
  159. lionagi/tests/test_core/generic/test_tree_node.py +2 -1
  160. lionagi/tests/test_core/mail/test_mail.py +2 -1
  161. lionagi/tests/test_core/test_branch.py +3 -2
  162. lionagi/tests/test_core/test_form.py +1 -0
  163. lionagi/tests/test_core/test_report.py +1 -0
  164. lionagi/tests/test_core/test_validator.py +4 -3
  165. lionagi/version.py +1 -1
  166. lionagi-0.2.8.dist-info/METADATA +66 -0
  167. lionagi-0.2.8.dist-info/RECORD +267 -0
  168. {lionagi-0.2.6.dist-info → lionagi-0.2.8.dist-info}/WHEEL +1 -2
  169. lionagi-0.2.6.dist-info/METADATA +0 -277
  170. lionagi-0.2.6.dist-info/RECORD +0 -258
  171. lionagi-0.2.6.dist-info/top_level.txt +0 -1
  172. {lionagi-0.2.6.dist-info → lionagi-0.2.8.dist-info}/LICENSE +0 -0
@@ -1,50 +1,43 @@
1
- """
2
- Copyright 2024 HaiyangLi
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- """
16
-
17
- """
18
- This module defines the Pile class, a versatile container for managing
19
- collections of Element objects. It supports structured access and
20
- manipulation, including retrieval, addition, and deletion of elements.
21
- """
1
+ from __future__ import annotations
22
2
 
3
+ import asyncio
23
4
  from collections.abc import Iterable
24
- from typing import TypeVar, Type, Any, Generic
5
+ from functools import wraps
6
+ from typing import Any, AsyncIterator, Callable, Generic, Type, TypeVar
25
7
 
26
8
  from pydantic import Field, field_validator
27
9
 
28
- from lionagi.libs.ln_convert import is_same_dtype, to_df
29
- from lionagi.libs.ln_func_call import bcall, alcall, CallDecorator as cd
10
+ from lionagi.libs.ln_convert import is_same_dtype, to_df, to_dict
11
+ from lionagi.libs.ln_func_call import CallDecorator as cd
12
+ from lionagi.libs.ln_func_call import alcall
13
+
30
14
  from .abc import (
31
- Element,
32
- Record,
33
15
  Component,
34
- Ordering,
16
+ Element,
17
+ ItemNotFoundError,
35
18
  LionIDable,
36
- get_lion_id,
37
- LionValueError,
38
19
  LionTypeError,
39
- ItemNotFoundError,
20
+ LionValueError,
40
21
  ModelLimitExceededError,
22
+ Ordering,
23
+ Record,
24
+ get_lion_id,
41
25
  )
42
26
  from .model import iModel
43
- from .util import to_list_type, _validate_order
27
+ from .util import _validate_order, to_list_type
44
28
 
45
29
  T = TypeVar("T")
46
30
 
47
31
 
32
+ def async_synchronized(func: Callable):
33
+ @wraps(func)
34
+ async def wrapper(self, *args, **kwargs):
35
+ async with self.async_lock:
36
+ return await func(self, *args, **kwargs)
37
+
38
+ return wrapper
39
+
40
+
48
41
  class Pile(Element, Record, Generic[T]):
49
42
  """
50
43
  Collection class for managing Element objects.
@@ -70,6 +63,14 @@ class Pile(Element, Record, Generic[T]):
70
63
  query_response: list = []
71
64
  tools: dict = {}
72
65
 
66
+ def __pydantic_extra__(self) -> dict[str, Any]:
67
+ return {
68
+ "_async": Field(default_factory=asyncio.Lock),
69
+ }
70
+
71
+ def __pydantic_private__(self) -> dict[str, Any]:
72
+ return self.__pydantic_extra__()
73
+
73
74
  def __init__(
74
75
  self,
75
76
  items=None,
@@ -442,6 +443,103 @@ class Pile(Element, Record, Generic[T]):
442
443
  def __radd__(self, other: T) -> "Pile":
443
444
  return other + self
444
445
 
446
+ def __ior__(self, other: Any | Pile) -> Pile:
447
+ if not isinstance(other, Pile):
448
+ raise LionTypeError(
449
+ "Invalid type for Pile operation.",
450
+ expected_type=Pile,
451
+ actual_type=type(other),
452
+ )
453
+ other = self._validate_pile(list(other))
454
+ self.include(other)
455
+ return self
456
+
457
+ def __or__(self, other: Any | Pile) -> Pile:
458
+ if not isinstance(other, Pile):
459
+ raise LionTypeError(
460
+ "Invalid type for Pile operation.",
461
+ expected_type=Pile,
462
+ actual_type=type(other),
463
+ )
464
+
465
+ result = self.__class__(
466
+ items=self.values(),
467
+ item_type=self.item_type,
468
+ order=self.order,
469
+ )
470
+ result.include(list(other))
471
+ return result
472
+
473
+ def __ixor__(self, other: Any | Pile) -> Pile:
474
+ if not isinstance(other, Pile):
475
+ raise LionTypeError(
476
+ "Invalid type for Pile operation.",
477
+ expected_type=Pile,
478
+ actual_type=type(other),
479
+ )
480
+
481
+ to_exclude = []
482
+ for i in other:
483
+ if i in self:
484
+ to_exclude.append(i)
485
+
486
+ other = [i for i in other if i not in to_exclude]
487
+ self.exclude(to_exclude)
488
+ self.include(other)
489
+ return self
490
+
491
+ def __xor__(self, other: Any | Pile) -> Pile:
492
+ if not isinstance(other, Pile):
493
+ raise LionTypeError(
494
+ "Invalid type for Pile operation.",
495
+ expected_type=Pile,
496
+ actual_type=type(other),
497
+ )
498
+
499
+ to_exclude = []
500
+ for i in other:
501
+ if i in self:
502
+ to_exclude.append(i)
503
+
504
+ values = [i for i in self if i not in to_exclude] + [
505
+ i for i in other if i not in to_exclude
506
+ ]
507
+
508
+ result = self.__class__(
509
+ items=values,
510
+ item_type=self.item_type,
511
+ )
512
+ return result
513
+
514
+ def __iand__(self, other: Any) -> Pile:
515
+ if not isinstance(other, Pile):
516
+ raise LionTypeError(
517
+ "Invalid type for Pile operation.",
518
+ expected_type=Pile,
519
+ actual_type=type(other),
520
+ )
521
+
522
+ to_exclude = []
523
+ for i in self.values():
524
+ if i not in other:
525
+ to_exclude.append(i)
526
+ self.exclude(to_exclude)
527
+ return self
528
+
529
+ def __and__(self, other: Any | Pile) -> Pile:
530
+ if not isinstance(other, Pile):
531
+ raise LionTypeError(
532
+ "Invalid type for Pile operation.",
533
+ expected_type=Pile,
534
+ actual_type=type(other),
535
+ )
536
+
537
+ values = [i for i in self if i in other]
538
+ return self.__class__(
539
+ items=values,
540
+ item_type=self.item_type,
541
+ )
542
+
445
543
  def size(self) -> int:
446
544
  """Return the total size of the pile."""
447
545
  return sum([len(i) for i in self])
@@ -666,7 +764,7 @@ class Pile(Element, Record, Generic[T]):
666
764
  else:
667
765
  raise ValueError("Invalid index type")
668
766
 
669
- async def query_pile(self, query, engine_kwargs={}, **kwargs):
767
+ async def query_pile(self, query, engine_kwargs={}, return_dict=False, **kwargs):
670
768
  """
671
769
  Query the pile using the created query engine.
672
770
 
@@ -682,9 +780,11 @@ class Pile(Element, Record, Generic[T]):
682
780
  self.create_query_engine(**engine_kwargs)
683
781
  response = await self.engines["query"].aquery(query, **kwargs)
684
782
  self.query_response.append(response)
783
+ if return_dict:
784
+ return to_dict(response)
685
785
  return str(response)
686
786
 
687
- async def chat_pile(self, query, engine_kwargs={}, **kwargs):
787
+ async def chat_pile(self, query, engine_kwargs={}, return_dict=False, **kwargs):
688
788
  """
689
789
  Chat with the pile using the created chat engine.
690
790
 
@@ -700,6 +800,8 @@ class Pile(Element, Record, Generic[T]):
700
800
  self.create_chat_engine(**engine_kwargs)
701
801
  response = await self.engines["chat"].achat(query, **kwargs)
702
802
  self.query_response.append(response)
803
+ if return_dict:
804
+ return to_dict(response)
703
805
  return str(response)
704
806
 
705
807
  async def embed_pile(
@@ -792,6 +894,7 @@ class Pile(Element, Record, Generic[T]):
792
894
  name=None,
793
895
  guidance=None,
794
896
  query_description=None,
897
+ return_dict=False,
795
898
  **kwargs,
796
899
  ):
797
900
  """
@@ -830,10 +933,10 @@ class Pile(Element, Record, Generic[T]):
830
933
 
831
934
  async def query(query: str):
832
935
  if query_type == "query":
833
- return await self.query_pile(query, **kwargs)
936
+ return await self.query_pile(query, return_dict=return_dict, **kwargs)
834
937
 
835
938
  elif query_type == "chat":
836
- return await self.chat_pile(query, **kwargs)
939
+ return await self.chat_pile(query, return_dict=return_dict, **kwargs)
837
940
 
838
941
  name = name or "query"
839
942
  tool = func_to_tool(query)[0]
@@ -872,6 +975,185 @@ class Pile(Element, Record, Generic[T]):
872
975
  """
873
976
  return self.to_df().__repr__()
874
977
 
978
+ def __getstate__(self):
979
+ """Prepare the Pile instance for pickling."""
980
+ state = self.__dict__.copy()
981
+ state["_async_lock"] = None
982
+ return state
983
+
984
+ def __setstate__(self, state):
985
+ """Restore the Pile instance after unpickling."""
986
+ self.__dict__.update(state)
987
+ self._async_lock = asyncio.Lock()
988
+
989
+ @property
990
+ def async_lock(self):
991
+ """Ensure the async lock is always available, even during unpickling"""
992
+ if not hasattr(self, "_async_lock") or self._async_lock is None:
993
+ self._async_lock = asyncio.Lock()
994
+ return self._async_lock
995
+
996
+ # Async Interface methods
997
+ @async_synchronized
998
+ async def asetitem(
999
+ self,
1000
+ key: Any,
1001
+ item: T | Iterable[T],
1002
+ /,
1003
+ ) -> None:
1004
+ """Asynchronously set an item or items in the Pile.
1005
+
1006
+ Args:
1007
+ key: The key to set. Can be an integer index, a string ID, or a
1008
+ slice.
1009
+ item: The item or items to set. Must be of type T or an iterable
1010
+ of T for slices.
1011
+
1012
+ Raises:
1013
+ TypeError: If the item type is not allowed.
1014
+ KeyError: If the key is invalid.
1015
+ ValueError: If trying to set multiple items with a non-slice key.
1016
+ """
1017
+ self._setitem(key, item)
1018
+
1019
+ @async_synchronized
1020
+ async def apop(
1021
+ self,
1022
+ key: Any,
1023
+ default: Any = ...,
1024
+ /,
1025
+ ):
1026
+ """Asynchronously remove and return an item or items from the Pile.
1027
+
1028
+ Args:
1029
+ key: The key of the item(s) to remove. Can be an integer index,
1030
+ a string ID, or a slice.
1031
+ default: The value to return if the key is not found. Defaults to
1032
+ ....
1033
+
1034
+ Returns:
1035
+ The removed item(s), or the default value if not found.
1036
+
1037
+ Raises:
1038
+ KeyError: If the key is not found and no default is provided.
1039
+ """
1040
+ return self._pop(key, default)
1041
+
1042
+ @async_synchronized
1043
+ async def aremove(
1044
+ self,
1045
+ item: T,
1046
+ /,
1047
+ ) -> None:
1048
+ """Asynchronously remove a specific item from the Pile.
1049
+
1050
+ Args:
1051
+ item: The item to remove.
1052
+
1053
+ Raises:
1054
+ ValueError: If the item is not found in the Pile.
1055
+ """
1056
+ self._remove(item)
1057
+
1058
+ @async_synchronized
1059
+ async def ainclude(
1060
+ self,
1061
+ item: T | Iterable[T],
1062
+ /,
1063
+ ) -> None:
1064
+ """Asynchronously include item(s) in the Pile if not already present.
1065
+
1066
+ Args:
1067
+ item: Item or iterable of items to include.
1068
+
1069
+ Raises:
1070
+ TypeError: If the item(s) are not of allowed types.
1071
+ """
1072
+ self._include(item)
1073
+ if item not in self:
1074
+ raise LionTypeError(f"Item {item} is not of allowed types")
1075
+
1076
+ @async_synchronized
1077
+ async def aexclude(
1078
+ self,
1079
+ item: T | Iterable[T],
1080
+ /,
1081
+ ) -> None:
1082
+ """Asynchronously exclude item(s) from the Pile if present.
1083
+
1084
+ Args:
1085
+ item: Item or iterable of items to exclude.
1086
+
1087
+ Note:
1088
+ This method does not raise an error if an item is not found.
1089
+ """
1090
+ self._exclude(item)
1091
+
1092
+ @async_synchronized
1093
+ async def aclear(self) -> None:
1094
+ self._clear()
1095
+
1096
+ @async_synchronized
1097
+ async def aupdate(
1098
+ self,
1099
+ other: Any,
1100
+ /,
1101
+ ) -> None:
1102
+ self._update(other)
1103
+
1104
+ @async_synchronized
1105
+ async def aget(
1106
+ self,
1107
+ key: Any,
1108
+ default=...,
1109
+ /,
1110
+ ) -> list | Any | T:
1111
+ return self._get(key, default)
1112
+
1113
+ async def __aiter__(self) -> AsyncIterator[T]:
1114
+ """Return an asynchronous iterator over the items in the Pile.
1115
+
1116
+ This method creates a snapshot of the current order to prevent
1117
+ issues with concurrent modifications during iteration.
1118
+
1119
+ Yields:
1120
+ Items in the Pile in their current order.
1121
+
1122
+ Note:
1123
+ This method yields control to the event loop after each item,
1124
+ allowing other async operations to run between iterations.
1125
+ """
1126
+
1127
+ async with self.async_lock:
1128
+ current_order = list(self.order)
1129
+
1130
+ for key in current_order:
1131
+ yield self.pile_[key]
1132
+ await asyncio.sleep(0) # Yield control to the event loop
1133
+
1134
+ async def __anext__(self) -> T:
1135
+ """Asynchronously return the next item in the Pile."""
1136
+ try:
1137
+ return await anext(self.AsyncPileIterator(self))
1138
+ except StopAsyncIteration:
1139
+ raise StopAsyncIteration("End of pile")
1140
+
1141
+ class AsyncPileIterator:
1142
+ def __init__(self, pile: Pile):
1143
+ self.pile = pile
1144
+ self.index = 0
1145
+
1146
+ def __aiter__(self) -> AsyncIterator[T]:
1147
+ return self
1148
+
1149
+ async def __anext__(self) -> T:
1150
+ if self.index >= len(self.pile):
1151
+ raise StopAsyncIteration
1152
+ item = self.pile[self.pile.order[self.index]]
1153
+ self.index += 1
1154
+ await asyncio.sleep(0) # Yield control to the event loop
1155
+ return item
1156
+
875
1157
 
876
1158
  def pile(
877
1159
  items: Iterable[T] | None = None,
@@ -1,23 +1,10 @@
1
- """
2
- Copyright 2024 HaiyangLi
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
1
+ import contextlib
9
2
 
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- """
3
+ from pydantic import Field, field_validator
16
4
 
17
- import contextlib
18
5
  from lionagi.libs import SysUtil
19
- from pydantic import Field, field_validator
20
- from .abc import Ordering, get_lion_id, ItemNotFoundError, LionIDable, Element
6
+
7
+ from .abc import Element, ItemNotFoundError, LionIDable, Ordering, get_lion_id
21
8
  from .util import _validate_order
22
9
 
23
10
 
@@ -1,7 +1,7 @@
1
- from collections.abc import Mapping, Generator
2
1
  from collections import deque
2
+ from collections.abc import Generator, Mapping
3
3
 
4
- from .abc import LionTypeError, Record, Ordering, Component, get_lion_id, Element
4
+ from .abc import Component, Element, LionTypeError, Ordering, Record, get_lion_id
5
5
 
6
6
 
7
7
  def to_list_type(value):
@@ -0,0 +1 @@
1
+ director is an agent specialized in writing directives
@@ -1,21 +1,5 @@
1
- """
2
- Copyright 2024 HaiyangLi
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- """
16
-
17
- from lionagi.core.unit.unit import Unit
18
1
  from lionagi.core.session.branch import Branch
2
+ from lionagi.core.unit.unit import Unit
19
3
 
20
4
 
21
5
  async def chat(
@@ -1,13 +1,13 @@
1
1
  import contextlib
2
- from lionagi.libs import convert, AsyncUtil, ParseUtil
3
- from lionagi.core.generic.edge import Edge
2
+
4
3
  from lionagi.core.action import ActionNode
5
- from lionagi.core.mail.mail import Mail
6
- from lionagi.core.message import System, Instruction
7
4
  from lionagi.core.collections import Pile, Progression
8
-
9
- from lionagi.core.session.branch import Branch
10
5
  from lionagi.core.executor.base_executor import BaseExecutor
6
+ from lionagi.core.generic.edge import Edge
7
+ from lionagi.core.mail.mail import Mail
8
+ from lionagi.core.message import Instruction, System
9
+ from lionagi.core.session.branch import Branch
10
+ from lionagi.libs import AsyncUtil, ParseUtil, convert
11
11
 
12
12
 
13
13
  class BranchExecutor(Branch, BaseExecutor):
@@ -1,12 +1,12 @@
1
1
  import asyncio
2
+
2
3
  from pydantic import Field
3
4
 
5
+ from lionagi.core.collections import Exchange, Pile, pile, progression
6
+ from lionagi.core.engine.branch_engine import BranchExecutor
7
+ from lionagi.core.executor.base_executor import BaseExecutor
4
8
  from lionagi.core.mail.mail import Mail, Package
5
- from lionagi.core.collections import Exchange
6
9
  from lionagi.core.mail.mail_manager import MailManager
7
- from lionagi.core.executor.base_executor import BaseExecutor
8
- from lionagi.core.engine.branch_engine import BranchExecutor
9
- from lionagi.core.collections import progression, pile, Pile
10
10
 
11
11
 
12
12
  class InstructionMapEngine(BaseExecutor):
@@ -1,22 +1,8 @@
1
- """
2
- Copyright 2024 HaiyangLi
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- """
16
-
17
1
  import ast
18
2
  from functools import lru_cache
3
+
19
4
  from lionagi.libs import AsyncUtil
5
+
20
6
  from ..evaluator.base_evaluator import BaseEvaluator
21
7
  from .sandbox_ import SandboxTransformer
22
8
 
@@ -3,8 +3,8 @@ from typing import Any
3
3
 
4
4
  from pydantic import Field
5
5
 
6
- from lionagi.core.collections.abc import Element, Progressable, Executable
7
6
  from lionagi.core.collections import Exchange
7
+ from lionagi.core.collections.abc import Element, Executable, Progressable
8
8
  from lionagi.core.mail.mail import Mail, Package
9
9
 
10
10
 
@@ -1,16 +1,13 @@
1
1
  from collections import deque
2
2
 
3
- from lionagi.libs import AsyncUtil, convert
4
-
5
- from lionagi.core.generic.node import Node
6
- from lionagi.core.generic.edge import Edge
3
+ from lionagi.core.action import ActionNode, DirectiveSelection, Tool
4
+ from lionagi.core.collections.progression import progression
7
5
  from lionagi.core.executor.base_executor import BaseExecutor
8
-
9
- from lionagi.core.action import Tool, DirectiveSelection, ActionNode
10
-
11
- from lionagi.core.mail import Mail
6
+ from lionagi.core.generic.edge import Edge
12
7
  from lionagi.core.generic.graph import Graph
13
- from lionagi.core.collections.progression import progression
8
+ from lionagi.core.generic.node import Node
9
+ from lionagi.core.mail import Mail
10
+ from lionagi.libs import AsyncUtil, convert
14
11
 
15
12
 
16
13
  class GraphExecutor(BaseExecutor, Graph):
@@ -1,18 +1,16 @@
1
- from collections import deque
2
1
  import json
2
+ from collections import deque
3
3
  from typing import Callable
4
4
 
5
- from lionagi.core.executor.base_executor import BaseExecutor
6
- from lionagi.integrations.storage.neo4j import Neo4j
7
- from lionagi.integrations.storage.storage_util import ParseNode
5
+ from lionagi.core.action import ActionNode, DirectiveSelection, Tool
8
6
  from lionagi.core.agent.base_agent import BaseAgent
7
+ from lionagi.core.collections.progression import progression
9
8
  from lionagi.core.engine.instruction_map_engine import InstructionMapEngine
10
-
11
- from lionagi.core.mail import Mail
12
- from lionagi.core.action import Tool, DirectiveSelection, ActionNode
9
+ from lionagi.core.executor.base_executor import BaseExecutor
13
10
  from lionagi.core.generic.edge import Edge
14
- from lionagi.core.collections.progression import progression
15
-
11
+ from lionagi.core.mail import Mail
12
+ from lionagi.integrations.storage.neo4j import Neo4j
13
+ from lionagi.integrations.storage.storage_util import ParseNode
16
14
  from lionagi.libs import AsyncUtil
17
15
 
18
16
 
File without changes
@@ -1,7 +1,6 @@
1
1
  from .edge import Edge
2
- from .node import Node
3
2
  from .graph import Graph
3
+ from .node import Node
4
4
  from .tree import Tree
5
5
 
6
-
7
6
  __all__ = ["Edge", "Node", "Graph", "Tree"]
@@ -1,6 +1,8 @@
1
- from pydantic import Field, field_validator
2
1
  from typing import Any
3
- from lionagi.core.collections.abc import Component, get_lion_id, LionIDable, Condition
2
+
3
+ from pydantic import Field, field_validator
4
+
5
+ from lionagi.core.collections.abc import Component, Condition, LionIDable, get_lion_id
4
6
  from lionagi.core.generic.edge_condition import EdgeCondition
5
7
 
6
8
 
@@ -70,7 +72,8 @@ class Edge(Component):
70
72
  if self.condition is None:
71
73
  return
72
74
 
73
- import inspect, sys
75
+ import inspect
76
+ import sys
74
77
 
75
78
  def new_getfile(object, _old_getfile=inspect.getfile):
76
79
  if not inspect.isclass(object):
@@ -96,6 +99,7 @@ class Edge(Component):
96
99
  inspect.getfile = new_getfile
97
100
 
98
101
  import inspect
102
+
99
103
  from IPython.core.magics.code import extract_symbols
100
104
 
101
105
  obj = self.condition.__class__
@@ -1,7 +1,8 @@
1
1
  from typing import Any
2
- from pydantic import Field
2
+
3
+ from pydantic import BaseModel, Field
4
+
3
5
  from lionagi.core.collections.abc import Condition
4
- from pydantic import BaseModel
5
6
 
6
7
 
7
8
  class EdgeCondition(Condition, BaseModel):