grasp_agents 0.2.11__py3-none-any.whl → 0.3.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 (51) hide show
  1. grasp_agents/__init__.py +15 -14
  2. grasp_agents/cloud_llm.py +118 -131
  3. grasp_agents/comm_processor.py +201 -0
  4. grasp_agents/generics_utils.py +15 -7
  5. grasp_agents/llm.py +60 -31
  6. grasp_agents/llm_agent.py +229 -273
  7. grasp_agents/llm_agent_memory.py +58 -0
  8. grasp_agents/llm_policy_executor.py +482 -0
  9. grasp_agents/memory.py +20 -134
  10. grasp_agents/message_history.py +140 -0
  11. grasp_agents/openai/__init__.py +54 -36
  12. grasp_agents/openai/completion_chunk_converters.py +78 -0
  13. grasp_agents/openai/completion_converters.py +53 -30
  14. grasp_agents/openai/content_converters.py +13 -14
  15. grasp_agents/openai/converters.py +44 -68
  16. grasp_agents/openai/message_converters.py +58 -72
  17. grasp_agents/openai/openai_llm.py +101 -42
  18. grasp_agents/openai/tool_converters.py +24 -19
  19. grasp_agents/packet.py +24 -0
  20. grasp_agents/packet_pool.py +91 -0
  21. grasp_agents/printer.py +29 -15
  22. grasp_agents/processor.py +194 -0
  23. grasp_agents/prompt_builder.py +175 -192
  24. grasp_agents/run_context.py +20 -37
  25. grasp_agents/typing/completion.py +58 -12
  26. grasp_agents/typing/completion_chunk.py +173 -0
  27. grasp_agents/typing/converters.py +8 -12
  28. grasp_agents/typing/events.py +86 -0
  29. grasp_agents/typing/io.py +4 -13
  30. grasp_agents/typing/message.py +12 -50
  31. grasp_agents/typing/tool.py +52 -26
  32. grasp_agents/usage_tracker.py +6 -6
  33. grasp_agents/utils.py +3 -3
  34. grasp_agents/workflow/looped_workflow.py +132 -0
  35. grasp_agents/workflow/parallel_processor.py +95 -0
  36. grasp_agents/workflow/sequential_workflow.py +66 -0
  37. grasp_agents/workflow/workflow_processor.py +78 -0
  38. {grasp_agents-0.2.11.dist-info → grasp_agents-0.3.1.dist-info}/METADATA +41 -50
  39. grasp_agents-0.3.1.dist-info/RECORD +51 -0
  40. grasp_agents/agent_message.py +0 -27
  41. grasp_agents/agent_message_pool.py +0 -92
  42. grasp_agents/base_agent.py +0 -51
  43. grasp_agents/comm_agent.py +0 -217
  44. grasp_agents/llm_agent_state.py +0 -79
  45. grasp_agents/tool_orchestrator.py +0 -203
  46. grasp_agents/workflow/looped_agent.py +0 -134
  47. grasp_agents/workflow/sequential_agent.py +0 -72
  48. grasp_agents/workflow/workflow_agent.py +0 -88
  49. grasp_agents-0.2.11.dist-info/RECORD +0 -46
  50. {grasp_agents-0.2.11.dist-info → grasp_agents-0.3.1.dist-info}/WHEEL +0 -0
  51. {grasp_agents-0.2.11.dist-info → grasp_agents-0.3.1.dist-info}/licenses/LICENSE.md +0 -0
@@ -1,134 +0,0 @@
1
- from collections.abc import Sequence
2
- from itertools import pairwise
3
- from logging import getLogger
4
- from typing import Any, ClassVar, Generic, Protocol, TypeVar, cast, final
5
-
6
- from ..agent_message_pool import AgentMessage, AgentMessagePool
7
- from ..comm_agent import CommunicatingAgent
8
- from ..run_context import CtxT, RunContextWrapper
9
- from ..typing.io import AgentID, AgentState, InT, OutT
10
- from .workflow_agent import WorkflowAgent
11
-
12
- logger = getLogger(__name__)
13
-
14
- _EH_OutT = TypeVar("_EH_OutT", contravariant=True) # noqa: PLC0105
15
-
16
-
17
- class ExitWorkflowLoopHandler(Protocol[_EH_OutT, CtxT]):
18
- def __call__(
19
- self,
20
- output_message: AgentMessage[_EH_OutT, Any],
21
- ctx: RunContextWrapper[CtxT] | None,
22
- **kwargs: Any,
23
- ) -> bool: ...
24
-
25
-
26
- class LoopedWorkflowAgent(WorkflowAgent[InT, OutT, CtxT], Generic[InT, OutT, CtxT]):
27
- _generic_arg_to_instance_attr_map: ClassVar[dict[int, str]] = {
28
- 0: "_in_type",
29
- 1: "_out_type",
30
- }
31
-
32
- def __init__(
33
- self,
34
- agent_id: AgentID,
35
- subagents: Sequence[CommunicatingAgent[Any, Any, Any, CtxT]],
36
- exit_agent: CommunicatingAgent[Any, OutT, Any, CtxT],
37
- message_pool: AgentMessagePool[CtxT] | None = None,
38
- recipient_ids: list[AgentID] | None = None,
39
- dynamic_routing: bool = False,
40
- max_iterations: int = 10,
41
- **kwargs: Any, # noqa: ARG002
42
- ) -> None:
43
- super().__init__(
44
- subagents=subagents,
45
- agent_id=agent_id,
46
- start_agent=subagents[0],
47
- end_agent=exit_agent,
48
- message_pool=message_pool,
49
- recipient_ids=recipient_ids,
50
- dynamic_routing=dynamic_routing,
51
- )
52
-
53
- for prev_agent, agent in pairwise(subagents):
54
- if prev_agent.out_type != agent.in_type:
55
- raise ValueError(
56
- f"Output type {prev_agent.out_type} of agent {prev_agent.agent_id} "
57
- f"does not match input type {agent.in_type} of agent "
58
- f"{agent.agent_id}"
59
- )
60
- if subagents[-1].out_type != subagents[0].in_type:
61
- raise ValueError(
62
- f"Looped workflow's last agent output type {subagents[-1].out_type} "
63
- f"does not match first agent input type {subagents[0].in_type}"
64
- )
65
-
66
- self._max_iterations = max_iterations
67
-
68
- self._exit_workflow_loop_impl: ExitWorkflowLoopHandler[OutT, CtxT] | None = None
69
-
70
- @property
71
- def max_iterations(self) -> int:
72
- return self._max_iterations
73
-
74
- def exit_workflow_loop_handler(
75
- self, func: ExitWorkflowLoopHandler[OutT, CtxT]
76
- ) -> ExitWorkflowLoopHandler[OutT, CtxT]:
77
- self._exit_workflow_loop_impl = func
78
-
79
- return func
80
-
81
- def _exit_workflow_loop(
82
- self,
83
- output_message: AgentMessage[OutT, Any],
84
- *,
85
- ctx: RunContextWrapper[CtxT] | None = None,
86
- **kwargs: Any,
87
- ) -> bool:
88
- if self._exit_workflow_loop_impl:
89
- return self._exit_workflow_loop_impl(output_message, ctx=ctx, **kwargs)
90
-
91
- return False
92
-
93
- @final
94
- async def run(
95
- self,
96
- chat_inputs: Any | None = None,
97
- *,
98
- in_args: InT | Sequence[InT] | None = None,
99
- in_message: AgentMessage[InT, Any] | None = None,
100
- ctx: RunContextWrapper[CtxT] | None = None,
101
- entry_point: bool = False,
102
- forbid_state_change: bool = False,
103
- **kwargs: Any,
104
- ) -> AgentMessage[OutT, AgentState]:
105
- agent_message = in_message
106
- num_iterations = 0
107
- exit_message: AgentMessage[OutT, Any] | None = None
108
-
109
- while True:
110
- for subagent in self.subagents:
111
- agent_message = await subagent.run(
112
- chat_inputs=chat_inputs,
113
- in_args=in_args,
114
- in_message=agent_message,
115
- entry_point=entry_point,
116
- forbid_state_change=forbid_state_change,
117
- ctx=ctx,
118
- **kwargs,
119
- )
120
-
121
- if subagent is self._end_agent:
122
- num_iterations += 1
123
- exit_message = cast("AgentMessage[OutT, AgentState]", agent_message)
124
- if self._exit_workflow_loop(exit_message, ctx=ctx):
125
- return exit_message
126
- if num_iterations >= self._max_iterations:
127
- logger.info(
128
- f"Max iterations reached ({self._max_iterations}). Exiting loop."
129
- )
130
- return exit_message
131
-
132
- chat_inputs = None
133
- in_args = None
134
- entry_point = False
@@ -1,72 +0,0 @@
1
- from collections.abc import Sequence
2
- from itertools import pairwise
3
- from typing import Any, ClassVar, Generic, cast, final
4
-
5
- from ..agent_message_pool import AgentMessage, AgentMessagePool
6
- from ..comm_agent import CommunicatingAgent
7
- from ..run_context import CtxT, RunContextWrapper
8
- from ..typing.io import AgentID, InT, OutT
9
- from .workflow_agent import WorkflowAgent
10
-
11
-
12
- class SequentialWorkflowAgent(WorkflowAgent[InT, OutT, CtxT], Generic[InT, OutT, CtxT]):
13
- _generic_arg_to_instance_attr_map: ClassVar[dict[int, str]] = {
14
- 0: "_in_type",
15
- 1: "_out_type",
16
- }
17
-
18
- def __init__(
19
- self,
20
- agent_id: AgentID,
21
- subagents: Sequence[CommunicatingAgent[Any, Any, Any, CtxT]],
22
- message_pool: AgentMessagePool[CtxT] | None = None,
23
- recipient_ids: list[AgentID] | None = None,
24
- dynamic_routing: bool = False,
25
- **kwargs: Any, # noqa: ARG002
26
- ) -> None:
27
- super().__init__(
28
- subagents=subagents,
29
- start_agent=subagents[0],
30
- end_agent=subagents[-1],
31
- agent_id=agent_id,
32
- message_pool=message_pool,
33
- recipient_ids=recipient_ids,
34
- dynamic_routing=dynamic_routing,
35
- )
36
-
37
- for prev_agent, agent in pairwise(subagents):
38
- if prev_agent.out_type != agent.in_type:
39
- raise ValueError(
40
- f"Output type {prev_agent.out_type} of agent {prev_agent.agent_id} "
41
- f"does not match input type {agent.in_type} of agent "
42
- f"{agent.agent_id}"
43
- )
44
-
45
- @final
46
- async def run(
47
- self,
48
- chat_inputs: Any | None = None,
49
- *,
50
- in_args: InT | Sequence[InT] | None = None,
51
- in_message: AgentMessage[InT, Any] | None = None,
52
- ctx: RunContextWrapper[CtxT] | None = None,
53
- entry_point: bool = False,
54
- forbid_state_change: bool = False,
55
- **kwargs: Any,
56
- ) -> AgentMessage[OutT, Any]:
57
- agent_message = in_message
58
- for subagent in self.subagents:
59
- agent_message = await subagent.run(
60
- chat_inputs=chat_inputs,
61
- in_args=in_args,
62
- in_message=agent_message,
63
- entry_point=entry_point,
64
- forbid_state_change=forbid_state_change,
65
- ctx=ctx,
66
- **kwargs,
67
- )
68
- chat_inputs = None
69
- in_args = None
70
- entry_point = False
71
-
72
- return cast("AgentMessage[OutT, Any]", agent_message)
@@ -1,88 +0,0 @@
1
- from abc import ABC, abstractmethod
2
- from collections.abc import Sequence
3
- from typing import Any, ClassVar, Generic
4
-
5
- from ..agent_message_pool import AgentMessage, AgentMessagePool
6
- from ..comm_agent import CommunicatingAgent
7
- from ..run_context import CtxT, RunContextWrapper
8
- from ..typing.io import AgentID, InT, OutT
9
-
10
-
11
- class WorkflowAgent(
12
- CommunicatingAgent[InT, OutT, Any, CtxT], ABC, Generic[InT, OutT, CtxT]
13
- ):
14
- _generic_arg_to_instance_attr_map: ClassVar[dict[int, str]] = {
15
- 0: "_in_type",
16
- 1: "_out_type",
17
- }
18
-
19
- def __init__(
20
- self,
21
- agent_id: AgentID,
22
- subagents: Sequence[CommunicatingAgent[Any, Any, Any, CtxT]],
23
- start_agent: CommunicatingAgent[InT, Any, Any, CtxT],
24
- end_agent: CommunicatingAgent[Any, OutT, Any, CtxT],
25
- message_pool: AgentMessagePool[CtxT] | None = None,
26
- recipient_ids: list[AgentID] | None = None,
27
- dynamic_routing: bool = False,
28
- **kwargs: Any, # noqa: ARG002
29
- ) -> None:
30
- super().__init__(
31
- agent_id=agent_id,
32
- message_pool=message_pool,
33
- recipient_ids=recipient_ids,
34
- dynamic_routing=dynamic_routing,
35
- )
36
-
37
- if len(subagents) < 2:
38
- raise ValueError("At least two steps are required")
39
- if start_agent not in subagents:
40
- raise ValueError("Start agent must be in the subagents list")
41
- if end_agent not in subagents:
42
- raise ValueError("End agent must be in the subagents list")
43
-
44
- if start_agent.in_type != self.in_type:
45
- raise ValueError(
46
- f"Start agent's input type {start_agent.in_type} does not "
47
- f"match workflow's input type {self._in_type}"
48
- )
49
- if end_agent.out_type != self.out_type:
50
- raise ValueError(
51
- f"End agent's output type {end_agent.out_type} does not "
52
- f"match workflow's output type {self._out_type}"
53
- )
54
-
55
- self._subagents = subagents
56
- self._start_agent = start_agent
57
- self._end_agent = end_agent
58
-
59
- for subagent in subagents:
60
- assert not subagent.recipient_ids, (
61
- "Subagents must not have recipient_ids set."
62
- )
63
-
64
- @property
65
- def subagents(self) -> Sequence[CommunicatingAgent[Any, Any, Any, CtxT]]:
66
- return self._subagents
67
-
68
- @property
69
- def start_agent(self) -> CommunicatingAgent[InT, Any, Any, CtxT]:
70
- return self._start_agent
71
-
72
- @property
73
- def end_agent(self) -> CommunicatingAgent[Any, OutT, Any, CtxT]:
74
- return self._end_agent
75
-
76
- @abstractmethod
77
- async def run(
78
- self,
79
- chat_inputs: Any | None = None,
80
- *,
81
- in_args: InT | Sequence[InT] | None = None,
82
- in_message: AgentMessage[InT, Any] | None = None,
83
- ctx: RunContextWrapper[CtxT] | None = None,
84
- entry_point: bool = False,
85
- forbid_state_change: bool = False,
86
- **generation_kwargs: Any,
87
- ) -> AgentMessage[OutT, Any]:
88
- pass
@@ -1,46 +0,0 @@
1
- grasp_agents/__init__.py,sha256=WPNUUFbucwli6oWIwxbckz0zpY4W2LnND0o7melYZOw,979
2
- grasp_agents/agent_message.py,sha256=eJV5n44t8EIE6M3jl48Ld7pmaW9dDhBX_FWm_u9yGWE,877
3
- grasp_agents/agent_message_pool.py,sha256=OKTXNEo9LAJTQJkzxmJ3TQgWw7WJKOzrKCJjeHpln6o,3158
4
- grasp_agents/base_agent.py,sha256=6WHBS17KqbiCVSREOiIJXlHfNrYb0AKvs0dEShogQGE,1363
5
- grasp_agents/cloud_llm.py,sha256=QqfnqLtInbekeT8R-sWMTrPiMwazyPaGoQ9K2wuh6gE,13336
6
- grasp_agents/comm_agent.py,sha256=U2R942X5yjPr27j9soUdueXtAzvduI7iON9QgoSDBAo,7271
7
- grasp_agents/costs_dict.yaml,sha256=2MFNWtkv5W5WSCcv1Cj13B1iQLVv5Ot9pS_KW2Gu2DA,2510
8
- grasp_agents/generics_utils.py,sha256=kw4Odte6Nvl4c9U7-mKPgXCavWZXo009zYDHAA0BR3g,6234
9
- grasp_agents/grasp_logging.py,sha256=H1GYhXdQvVkmauFDZ-KDwvVmPQHZUUm9sRqX_ObK2xI,1111
10
- grasp_agents/http_client.py,sha256=KZva2MjJjuI5ohUeU8RdTAImUnQYaqBrV2jDH8smbJw,738
11
- grasp_agents/llm.py,sha256=YClNxN9GUGaFHXhTU72z1AqW_Y726OC7kyVRYCnfhZ8,3682
12
- grasp_agents/llm_agent.py,sha256=UvmcvuLl8EWe51isSw9Oa9lxA9gQBJngRXj8LUMAd4w,16097
13
- grasp_agents/llm_agent_state.py,sha256=L54zUW5nAT-ubvEB7XNAQ84ExOgRlUFzc-Q49mUXUT0,2390
14
- grasp_agents/memory.py,sha256=X1YtVX8XxP5KnGPMW8BqjID8QK4hTG2obxoyhnnZ4pU,5575
15
- grasp_agents/printer.py,sha256=ZENcTITCcgSizvcUXnIQiFl_NcVHg-801Z-sbT0D8rg,5030
16
- grasp_agents/prompt_builder.py,sha256=A4z8B5nIfgn_cHnZCNyYjwA904UDiOaCyBc5VPu8168,8867
17
- grasp_agents/run_context.py,sha256=OpTdo32-WPD7XwE16LQphh2C7yrrkZ3C4Ia80pWeBDQ,2192
18
- grasp_agents/tool_orchestrator.py,sha256=XgKCOytPs4y4SBpS5i4eogCB428XaDnNIB3VxzJC_k0,6095
19
- grasp_agents/usage_tracker.py,sha256=4gy0XtfIBAjQHblEFpQuPelmxMGDvE7vw4c8Ccr1msk,3471
20
- grasp_agents/utils.py,sha256=KzoInW0sq-pwwUtgjtYMW8b0ivBH6MR0Zxv6Kqq3k3M,4510
21
- grasp_agents/openai/__init__.py,sha256=P7oa4tdJ2JKJv16frRi1C09FnrMayuOYomu5l95uayE,3196
22
- grasp_agents/openai/completion_converters.py,sha256=gt06DO2jrMsgJGHPIhoTHeM3R0yHqUjS0_VfaC6Xu4I,2146
23
- grasp_agents/openai/content_converters.py,sha256=6GI0D7xJalzsiawAJOyCUzTJTo0NQdpv87YKmfN0LYQ,2631
24
- grasp_agents/openai/converters.py,sha256=DBXBxow9oRG6pc8inpZBLiuUqHzVfpscmHFpN9bAdvc,5276
25
- grasp_agents/openai/message_converters.py,sha256=Gol5lFDu3eSagDsvWi_ql0QYcAT3-_5YCbU75xwuBK0,4823
26
- grasp_agents/openai/openai_llm.py,sha256=JZdM0YerpDJW1IX7BW8lawgGdUHUW1ka4JSEqKxXuUQ,6075
27
- grasp_agents/openai/tool_converters.py,sha256=72DmyNW9WylesRH-m5fi7H2Wu1U9gUXqMQ-UdX_M7mU,1068
28
- grasp_agents/rate_limiting/__init__.py,sha256=KRgtF_E7R3YfA2cpYcFcZ7wycV0pWVJ0xRQC7YhiIEQ,158
29
- grasp_agents/rate_limiting/rate_limiter_chunked.py,sha256=BPgkUXvhmZhTpZs2T6uujNFuxH_kYHiISuf6_-eNhUc,5544
30
- grasp_agents/rate_limiting/types.py,sha256=PbnNhEAcYedQdIpPJWud8HUVcxa_xZS2RDZu4c5jr40,1003
31
- grasp_agents/rate_limiting/utils.py,sha256=oEDWDNHYMUdxOOG49PlAJochkZq8nnVBCo6JxPc1iSo,2007
32
- grasp_agents/typing/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
33
- grasp_agents/typing/completion.py,sha256=_KDLx3Gtz7o-pEZrvAFgCZwDmkr2oQkxrL-2LSXHHsw,657
34
- grasp_agents/typing/content.py,sha256=VdnHdW8PHDCtX_ffvcwQz-7ypPUQNSGqHa3txFE_72Y,3676
35
- grasp_agents/typing/converters.py,sha256=yORIljRsVoKz7oj38pHLD6luIelM1RcYL_PqG_D4nWM,3086
36
- grasp_agents/typing/io.py,sha256=uxSvbD05UK5nIhPfDvXIoGuU6xRMW4USZq_4IgBeGCY,609
37
- grasp_agents/typing/message.py,sha256=DC24XMcUPG1MHDNZKT67yKWyaMTQNF5B0yDiUg8b54Q,3833
38
- grasp_agents/typing/tool.py,sha256=e0pTMnRcpMpGNVQ8muE9wnh7LdIgh92AqXDo9hMDxf0,1960
39
- grasp_agents/workflow/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
40
- grasp_agents/workflow/looped_agent.py,sha256=f7R-yVq4cP8zMTlvAF0GCpD0Izkyz5jUvUrxaXycusw,4710
41
- grasp_agents/workflow/sequential_agent.py,sha256=yU3X28rqZo-xVdkbEETb9pBCBG7v1GK7oxtbl5xLdCo,2526
42
- grasp_agents/workflow/workflow_agent.py,sha256=T1DpSipIkcrC_WJKR6Ho_cSHmVltCcF6Ve1F7isPRp0,3031
43
- grasp_agents-0.2.11.dist-info/METADATA,sha256=xXvakAAM5nycFuJf70buNFqQ5BMNyMbI7rdFZHHwphc,7188
44
- grasp_agents-0.2.11.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
45
- grasp_agents-0.2.11.dist-info/licenses/LICENSE.md,sha256=-nNNdWqGB8gJ2O-peFQ2Irshv5tW5pHKyTcYkwvH7CE,1201
46
- grasp_agents-0.2.11.dist-info/RECORD,,