grasp_agents 0.5.3__py3-none-any.whl → 0.5.5__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.
- grasp_agents/__init__.py +4 -6
- grasp_agents/errors.py +80 -18
- grasp_agents/llm_agent.py +106 -146
- grasp_agents/llm_agent_memory.py +1 -1
- grasp_agents/llm_policy_executor.py +17 -15
- grasp_agents/packet.py +23 -4
- grasp_agents/packet_pool.py +117 -50
- grasp_agents/printer.py +9 -5
- grasp_agents/processor.py +217 -166
- grasp_agents/prompt_builder.py +75 -138
- grasp_agents/run_context.py +3 -16
- grasp_agents/runner.py +110 -21
- grasp_agents/typing/events.py +8 -4
- grasp_agents/typing/io.py +1 -8
- grasp_agents/workflow/looped_workflow.py +13 -19
- grasp_agents/workflow/sequential_workflow.py +6 -10
- grasp_agents/workflow/workflow_processor.py +23 -16
- {grasp_agents-0.5.3.dist-info → grasp_agents-0.5.5.dist-info}/METADATA +1 -1
- {grasp_agents-0.5.3.dist-info → grasp_agents-0.5.5.dist-info}/RECORD +21 -22
- grasp_agents/comm_processor.py +0 -214
- {grasp_agents-0.5.3.dist-info → grasp_agents-0.5.5.dist-info}/WHEEL +0 -0
- {grasp_agents-0.5.3.dist-info → grasp_agents-0.5.5.dist-info}/licenses/LICENSE.md +0 -0
grasp_agents/__init__.py
CHANGED
@@ -1,17 +1,16 @@
|
|
1
1
|
# pyright: reportUnusedImport=false
|
2
2
|
|
3
3
|
|
4
|
-
from .comm_processor import CommProcessor
|
5
4
|
from .llm import LLM, LLMSettings
|
6
5
|
from .llm_agent import LLMAgent
|
7
6
|
from .llm_agent_memory import LLMAgentMemory
|
8
7
|
from .memory import Memory
|
9
8
|
from .packet import Packet
|
10
9
|
from .processor import Processor
|
11
|
-
from .run_context import
|
10
|
+
from .run_context import RunContext
|
12
11
|
from .typing.completion import Completion
|
13
12
|
from .typing.content import Content, ImageData
|
14
|
-
from .typing.io import LLMPrompt,
|
13
|
+
from .typing.io import LLMPrompt, ProcName
|
15
14
|
from .typing.message import AssistantMessage, Messages, SystemMessage, UserMessage
|
16
15
|
from .typing.tool import BaseTool
|
17
16
|
|
@@ -19,20 +18,19 @@ __all__ = [
|
|
19
18
|
"LLM",
|
20
19
|
"AssistantMessage",
|
21
20
|
"BaseTool",
|
22
|
-
"CommProcessor",
|
23
21
|
"Completion",
|
24
22
|
"Content",
|
25
23
|
"ImageData",
|
26
24
|
"LLMAgent",
|
25
|
+
"LLMAgentMemory",
|
27
26
|
"LLMPrompt",
|
28
|
-
"LLMPromptArgs",
|
29
27
|
"LLMSettings",
|
28
|
+
"Memory",
|
30
29
|
"Messages",
|
31
30
|
"Packet",
|
32
31
|
"Packet",
|
33
32
|
"ProcName",
|
34
33
|
"Processor",
|
35
|
-
"RunArgs",
|
36
34
|
"RunContext",
|
37
35
|
"SystemMessage",
|
38
36
|
"UserMessage",
|
grasp_agents/errors.py
CHANGED
@@ -1,24 +1,48 @@
|
|
1
1
|
# from openai import APIResponseValidationError
|
2
|
-
class CompletionError(Exception):
|
3
|
-
pass
|
4
2
|
|
5
3
|
|
6
|
-
class
|
7
|
-
|
4
|
+
class ProcRunError(Exception):
|
5
|
+
def __init__(
|
6
|
+
self, proc_name: str, call_id: str, message: str | None = None
|
7
|
+
) -> None:
|
8
|
+
super().__init__(
|
9
|
+
message
|
10
|
+
or f"Processor run failed [proc_name: {proc_name}; call_id: {call_id}]."
|
11
|
+
)
|
12
|
+
self.proc_name = proc_name
|
13
|
+
self.call_id = call_id
|
8
14
|
|
9
15
|
|
10
|
-
class ProcInputValidationError(
|
16
|
+
class ProcInputValidationError(ProcRunError):
|
11
17
|
pass
|
12
18
|
|
13
19
|
|
14
|
-
class ProcOutputValidationError(
|
15
|
-
|
20
|
+
class ProcOutputValidationError(ProcRunError):
|
21
|
+
def __init__(
|
22
|
+
self, schema: object, proc_name: str, call_id: str, message: str | None = None
|
23
|
+
):
|
24
|
+
super().__init__(
|
25
|
+
proc_name=proc_name,
|
26
|
+
call_id=call_id,
|
27
|
+
message=message
|
28
|
+
or (
|
29
|
+
"Processor output validation failed "
|
30
|
+
f"[proc_name: {proc_name}; call_id: {call_id}]. "
|
31
|
+
f"Expected type:\n{schema}"
|
32
|
+
),
|
33
|
+
)
|
16
34
|
|
17
35
|
|
18
|
-
class AgentFinalAnswerError(
|
19
|
-
def __init__(
|
36
|
+
class AgentFinalAnswerError(ProcRunError):
|
37
|
+
def __init__(
|
38
|
+
self, proc_name: str, call_id: str, message: str | None = None
|
39
|
+
) -> None:
|
20
40
|
super().__init__(
|
21
|
-
|
41
|
+
proc_name=proc_name,
|
42
|
+
call_id=call_id,
|
43
|
+
message=message
|
44
|
+
or "Final answer tool call did not return a final answer message "
|
45
|
+
f"[proc_name={proc_name}; call_id={call_id}]",
|
22
46
|
)
|
23
47
|
self.message = message
|
24
48
|
|
@@ -27,28 +51,58 @@ class WorkflowConstructionError(Exception):
|
|
27
51
|
pass
|
28
52
|
|
29
53
|
|
30
|
-
class PacketRoutingError(
|
54
|
+
class PacketRoutingError(ProcRunError):
|
31
55
|
def __init__(
|
32
56
|
self,
|
33
|
-
|
34
|
-
|
57
|
+
proc_name: str,
|
58
|
+
call_id: str,
|
59
|
+
selected_recipient: str | None = None,
|
60
|
+
allowed_recipients: list[str] | None = None,
|
35
61
|
message: str | None = None,
|
36
62
|
) -> None:
|
37
63
|
default_message = (
|
38
64
|
f"Selected recipient '{selected_recipient}' is not in the allowed "
|
39
|
-
f"recipients: {allowed_recipients}"
|
65
|
+
f"recipients: {allowed_recipients} "
|
66
|
+
f"[proc_name={proc_name}; call_id={call_id}]"
|
67
|
+
)
|
68
|
+
super().__init__(
|
69
|
+
proc_name=proc_name, call_id=call_id, message=message or default_message
|
40
70
|
)
|
41
|
-
super().__init__(message or default_message)
|
42
71
|
self.selected_recipient = selected_recipient
|
43
72
|
self.allowed_recipients = allowed_recipients
|
44
73
|
|
45
74
|
|
46
|
-
class
|
75
|
+
class RunnerError(Exception):
|
47
76
|
pass
|
48
77
|
|
49
78
|
|
50
|
-
class
|
51
|
-
|
79
|
+
class PromptBuilderError(Exception):
|
80
|
+
def __init__(self, proc_name: str, message: str | None = None) -> None:
|
81
|
+
super().__init__(message or f"Prompt builder failed [proc_name={proc_name}]")
|
82
|
+
self.proc_name = proc_name
|
83
|
+
self.message = message
|
84
|
+
|
85
|
+
|
86
|
+
class SystemPromptBuilderError(PromptBuilderError):
|
87
|
+
def __init__(self, proc_name: str, message: str | None = None) -> None:
|
88
|
+
super().__init__(
|
89
|
+
proc_name=proc_name,
|
90
|
+
message=message
|
91
|
+
or "System prompt builder failed to make system prompt "
|
92
|
+
f"[proc_name={proc_name}]",
|
93
|
+
)
|
94
|
+
self.message = message
|
95
|
+
|
96
|
+
|
97
|
+
class InputPromptBuilderError(PromptBuilderError):
|
98
|
+
def __init__(self, proc_name: str, message: str | None = None) -> None:
|
99
|
+
super().__init__(
|
100
|
+
proc_name=proc_name,
|
101
|
+
message=message
|
102
|
+
or "Input prompt builder failed to make input content "
|
103
|
+
f"[proc_name={proc_name}]",
|
104
|
+
)
|
105
|
+
self.message = message
|
52
106
|
|
53
107
|
|
54
108
|
class PyJSONStringParsingError(Exception):
|
@@ -71,6 +125,14 @@ class JSONSchemaValidationError(Exception):
|
|
71
125
|
self.schema = schema
|
72
126
|
|
73
127
|
|
128
|
+
class CompletionError(Exception):
|
129
|
+
pass
|
130
|
+
|
131
|
+
|
132
|
+
class CombineCompletionChunksError(Exception):
|
133
|
+
pass
|
134
|
+
|
135
|
+
|
74
136
|
class LLMToolCallValidationError(Exception):
|
75
137
|
def __init__(
|
76
138
|
self, tool_name: str, tool_args: str, message: str | None = None
|
grasp_agents/llm_agent.py
CHANGED
@@ -1,22 +1,21 @@
|
|
1
1
|
from collections.abc import AsyncIterator, Sequence
|
2
2
|
from pathlib import Path
|
3
|
-
from typing import Any, ClassVar, Generic, Protocol, TypeVar, cast
|
3
|
+
from typing import Any, ClassVar, Generic, Protocol, TypeVar, cast, final
|
4
4
|
|
5
5
|
from pydantic import BaseModel
|
6
6
|
|
7
|
-
from .comm_processor import CommProcessor
|
8
7
|
from .llm import LLM, LLMSettings
|
9
|
-
from .llm_agent_memory import LLMAgentMemory,
|
8
|
+
from .llm_agent_memory import LLMAgentMemory, MemoryPreparator
|
10
9
|
from .llm_policy_executor import (
|
11
|
-
ExitToolCallLoopHandler,
|
12
10
|
LLMPolicyExecutor,
|
13
|
-
|
11
|
+
MemoryManager,
|
12
|
+
ToolCallLoopTerminator,
|
14
13
|
)
|
15
|
-
from .
|
14
|
+
from .processor import Processor
|
16
15
|
from .prompt_builder import (
|
17
|
-
|
18
|
-
MakeSystemPromptHandler,
|
16
|
+
InputContentBuilder,
|
19
17
|
PromptBuilder,
|
18
|
+
SystemPromptBuilder,
|
20
19
|
)
|
21
20
|
from .run_context import CtxT, RunContext
|
22
21
|
from .typing.content import Content, ImageData
|
@@ -27,7 +26,7 @@ from .typing.events import (
|
|
27
26
|
SystemMessageEvent,
|
28
27
|
UserMessageEvent,
|
29
28
|
)
|
30
|
-
from .typing.io import InT, LLMPrompt,
|
29
|
+
from .typing.io import InT, LLMPrompt, OutT, ProcName
|
31
30
|
from .typing.message import Message, Messages, SystemMessage, UserMessage
|
32
31
|
from .typing.tool import BaseTool
|
33
32
|
from .utils import get_prompt, validate_obj_from_json_or_py_string
|
@@ -36,7 +35,7 @@ _InT_contra = TypeVar("_InT_contra", contravariant=True)
|
|
36
35
|
_OutT_co = TypeVar("_OutT_co", covariant=True)
|
37
36
|
|
38
37
|
|
39
|
-
class
|
38
|
+
class OutputParser(Protocol[_InT_contra, _OutT_co, CtxT]):
|
40
39
|
def __call__(
|
41
40
|
self,
|
42
41
|
conversation: Messages,
|
@@ -47,8 +46,8 @@ class ParseOutputHandler(Protocol[_InT_contra, _OutT_co, CtxT]):
|
|
47
46
|
|
48
47
|
|
49
48
|
class LLMAgent(
|
50
|
-
|
51
|
-
Generic[InT,
|
49
|
+
Processor[InT, OutT, LLMAgentMemory, CtxT],
|
50
|
+
Generic[InT, OutT, CtxT],
|
52
51
|
):
|
53
52
|
_generic_arg_to_instance_attr_map: ClassVar[dict[int, str]] = {
|
54
53
|
0: "_in_type",
|
@@ -69,10 +68,6 @@ class LLMAgent(
|
|
69
68
|
# System prompt template
|
70
69
|
sys_prompt: LLMPrompt | None = None,
|
71
70
|
sys_prompt_path: str | Path | None = None,
|
72
|
-
# System args (static args provided via RunContext)
|
73
|
-
sys_args_schema: type[LLMPromptArgs] | None = None,
|
74
|
-
# User args (static args provided via RunContext)
|
75
|
-
usr_args_schema: type[LLMPromptArgs] | None = None,
|
76
71
|
# Agent loop settings
|
77
72
|
max_turns: int = 100,
|
78
73
|
react_mode: bool = False,
|
@@ -82,25 +77,31 @@ class LLMAgent(
|
|
82
77
|
# Retries
|
83
78
|
max_retries: int = 0,
|
84
79
|
# Multi-agent routing
|
85
|
-
packet_pool: PacketPool[CtxT] | None = None,
|
86
80
|
recipients: list[ProcName] | None = None,
|
87
81
|
) -> None:
|
88
|
-
super().__init__(
|
89
|
-
name=name,
|
90
|
-
packet_pool=packet_pool,
|
91
|
-
recipients=recipients,
|
92
|
-
max_retries=max_retries,
|
93
|
-
)
|
82
|
+
super().__init__(name=name, recipients=recipients, max_retries=max_retries)
|
94
83
|
|
95
84
|
# Agent memory
|
96
85
|
|
97
86
|
self._memory: LLMAgentMemory = LLMAgentMemory()
|
98
87
|
self._reset_memory_on_run = reset_memory_on_run
|
99
88
|
|
89
|
+
self.memory_preparator: MemoryPreparator | None
|
90
|
+
if not hasattr(type(self), "memory_preparator"):
|
91
|
+
self.memory_preparator = None
|
92
|
+
|
93
|
+
self.output_parser: OutputParser[InT, OutT, CtxT] | None
|
94
|
+
if not hasattr(type(self), "output_parser"):
|
95
|
+
self.output_parser = None
|
96
|
+
|
100
97
|
# LLM policy executor
|
101
98
|
|
102
99
|
self._used_default_llm_response_schema: bool = False
|
103
|
-
if
|
100
|
+
if (
|
101
|
+
llm.response_schema is None
|
102
|
+
and tools is None
|
103
|
+
and not hasattr(type(self), "output_parser")
|
104
|
+
):
|
104
105
|
llm.response_schema = self.out_type
|
105
106
|
self._used_default_llm_response_schema = True
|
106
107
|
|
@@ -128,18 +129,11 @@ class LLMAgent(
|
|
128
129
|
|
129
130
|
sys_prompt = get_prompt(prompt_text=sys_prompt, prompt_path=sys_prompt_path)
|
130
131
|
in_prompt = get_prompt(prompt_text=in_prompt, prompt_path=in_prompt_path)
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
agent_name=self._name,
|
135
|
-
sys_prompt_template=sys_prompt,
|
136
|
-
in_prompt_template=in_prompt,
|
137
|
-
sys_args_schema=sys_args_schema,
|
138
|
-
usr_args_schema=usr_args_schema,
|
132
|
+
|
133
|
+
self._prompt_builder = PromptBuilder[self.in_type, CtxT](
|
134
|
+
agent_name=self._name, sys_prompt=sys_prompt, in_prompt=in_prompt
|
139
135
|
)
|
140
136
|
|
141
|
-
self._prepare_memory_impl: PrepareMemoryHandler | None = None
|
142
|
-
self._parse_output_impl: ParseOutputHandler[InT, OutT_co, CtxT] | None = None
|
143
137
|
self._register_overridden_handlers()
|
144
138
|
|
145
139
|
@property
|
@@ -154,22 +148,15 @@ class LLMAgent(
|
|
154
148
|
def max_turns(self) -> int:
|
155
149
|
return self._policy_executor.max_turns
|
156
150
|
|
157
|
-
@property
|
158
|
-
def sys_args_schema(self) -> type[LLMPromptArgs] | None:
|
159
|
-
return self._prompt_builder.sys_args_schema
|
160
|
-
|
161
|
-
@property
|
162
|
-
def usr_args_schema(self) -> type[LLMPromptArgs] | None:
|
163
|
-
return self._prompt_builder.usr_args_schema
|
164
|
-
|
165
151
|
@property
|
166
152
|
def sys_prompt(self) -> LLMPrompt | None:
|
167
|
-
return self._prompt_builder.
|
153
|
+
return self._prompt_builder.sys_prompt
|
168
154
|
|
169
155
|
@property
|
170
156
|
def in_prompt(self) -> LLMPrompt | None:
|
171
|
-
return self._prompt_builder.
|
157
|
+
return self._prompt_builder.in_prompt
|
172
158
|
|
159
|
+
@final
|
173
160
|
def _prepare_memory(
|
174
161
|
self,
|
175
162
|
memory: LLMAgentMemory,
|
@@ -177,35 +164,19 @@ class LLMAgent(
|
|
177
164
|
sys_prompt: LLMPrompt | None = None,
|
178
165
|
ctx: RunContext[Any] | None = None,
|
179
166
|
) -> None:
|
180
|
-
if self.
|
181
|
-
return self.
|
167
|
+
if self.memory_preparator:
|
168
|
+
return self.memory_preparator(
|
182
169
|
memory=memory, in_args=in_args, sys_prompt=sys_prompt, ctx=ctx
|
183
170
|
)
|
184
171
|
|
185
172
|
def _memorize_inputs(
|
186
173
|
self,
|
174
|
+
memory: LLMAgentMemory,
|
187
175
|
chat_inputs: LLMPrompt | Sequence[str | ImageData] | None = None,
|
188
|
-
*,
|
189
176
|
in_args: InT | None = None,
|
190
|
-
memory: LLMAgentMemory,
|
191
177
|
ctx: RunContext[CtxT] | None = None,
|
192
178
|
) -> tuple[SystemMessage | None, UserMessage | None]:
|
193
|
-
|
194
|
-
sys_args: LLMPromptArgs | None = None
|
195
|
-
usr_args: LLMPromptArgs | None = None
|
196
|
-
if ctx is not None:
|
197
|
-
run_args = ctx.run_args.get(self.name)
|
198
|
-
if run_args is not None:
|
199
|
-
sys_args = run_args.sys
|
200
|
-
usr_args = run_args.usr
|
201
|
-
|
202
|
-
# 2. Make system prompt (can be None)
|
203
|
-
|
204
|
-
formatted_sys_prompt = self._prompt_builder.make_system_prompt(
|
205
|
-
sys_args=sys_args, ctx=ctx
|
206
|
-
)
|
207
|
-
|
208
|
-
# 3. Set agent memory
|
179
|
+
formatted_sys_prompt = self._prompt_builder.build_system_prompt(ctx=ctx)
|
209
180
|
|
210
181
|
system_message: SystemMessage | None = None
|
211
182
|
if self._reset_memory_on_run or memory.is_empty:
|
@@ -214,16 +185,11 @@ class LLMAgent(
|
|
214
185
|
system_message = cast("SystemMessage", memory.message_history[0])
|
215
186
|
else:
|
216
187
|
self._prepare_memory(
|
217
|
-
memory=memory,
|
218
|
-
in_args=in_args,
|
219
|
-
sys_prompt=formatted_sys_prompt,
|
220
|
-
ctx=ctx,
|
188
|
+
memory=memory, in_args=in_args, sys_prompt=formatted_sys_prompt, ctx=ctx
|
221
189
|
)
|
222
190
|
|
223
|
-
|
224
|
-
|
225
|
-
input_message = self._prompt_builder.make_input_message(
|
226
|
-
chat_inputs=chat_inputs, in_args=in_args, usr_args=usr_args, ctx=ctx
|
191
|
+
input_message = self._prompt_builder.build_input_message(
|
192
|
+
chat_inputs=chat_inputs, in_args=in_args, ctx=ctx
|
227
193
|
)
|
228
194
|
if input_message:
|
229
195
|
memory.update([input_message])
|
@@ -236,9 +202,9 @@ class LLMAgent(
|
|
236
202
|
*,
|
237
203
|
in_args: InT | None = None,
|
238
204
|
ctx: RunContext[CtxT] | None = None,
|
239
|
-
) ->
|
240
|
-
if self.
|
241
|
-
return self.
|
205
|
+
) -> OutT:
|
206
|
+
if self.output_parser:
|
207
|
+
return self.output_parser(
|
242
208
|
conversation=conversation, in_args=in_args, ctx=ctx
|
243
209
|
)
|
244
210
|
|
@@ -257,9 +223,12 @@ class LLMAgent(
|
|
257
223
|
memory: LLMAgentMemory,
|
258
224
|
call_id: str,
|
259
225
|
ctx: RunContext[CtxT] | None = None,
|
260
|
-
) ->
|
226
|
+
) -> OutT:
|
261
227
|
system_message, input_message = self._memorize_inputs(
|
262
|
-
|
228
|
+
memory=memory,
|
229
|
+
chat_inputs=chat_inputs,
|
230
|
+
in_args=in_args,
|
231
|
+
ctx=ctx,
|
263
232
|
)
|
264
233
|
if system_message:
|
265
234
|
self._print_messages([system_message], call_id=call_id, ctx=ctx)
|
@@ -268,11 +237,9 @@ class LLMAgent(
|
|
268
237
|
|
269
238
|
await self._policy_executor.execute(memory, call_id=call_id, ctx=ctx)
|
270
239
|
|
271
|
-
return
|
272
|
-
|
273
|
-
|
274
|
-
)
|
275
|
-
]
|
240
|
+
return self._parse_output(
|
241
|
+
conversation=memory.message_history, in_args=in_args, ctx=ctx
|
242
|
+
)
|
276
243
|
|
277
244
|
async def _process_stream(
|
278
245
|
self,
|
@@ -284,7 +251,10 @@ class LLMAgent(
|
|
284
251
|
ctx: RunContext[CtxT] | None = None,
|
285
252
|
) -> AsyncIterator[Event[Any]]:
|
286
253
|
system_message, input_message = self._memorize_inputs(
|
287
|
-
|
254
|
+
memory=memory,
|
255
|
+
chat_inputs=chat_inputs,
|
256
|
+
in_args=in_args,
|
257
|
+
ctx=ctx,
|
288
258
|
)
|
289
259
|
if system_message:
|
290
260
|
self._print_messages([system_message], call_id=call_id, ctx=ctx)
|
@@ -324,108 +294,98 @@ class LLMAgent(
|
|
324
294
|
|
325
295
|
# Prompt builder
|
326
296
|
|
327
|
-
if cur_cls.
|
328
|
-
self._prompt_builder.
|
297
|
+
if cur_cls.system_prompt_builder is not base_cls.system_prompt_builder:
|
298
|
+
self._prompt_builder.system_prompt_builder = self.system_prompt_builder
|
329
299
|
|
330
|
-
if cur_cls.
|
331
|
-
self._prompt_builder.
|
300
|
+
if cur_cls.input_content_builder is not base_cls.input_content_builder:
|
301
|
+
self._prompt_builder.input_content_builder = self.input_content_builder
|
332
302
|
|
333
303
|
# Policy executor
|
334
304
|
|
335
|
-
if
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
if cur_cls._manage_memory is not base_cls._manage_memory: # noqa: SLF001
|
341
|
-
self._policy_executor.manage_memory_impl = self._manage_memory
|
305
|
+
if cur_cls.tool_call_loop_terminator is not base_cls.tool_call_loop_terminator:
|
306
|
+
self._policy_executor.tool_call_loop_terminator = (
|
307
|
+
self.tool_call_loop_terminator
|
308
|
+
)
|
342
309
|
|
343
|
-
|
344
|
-
|
345
|
-
if (
|
346
|
-
cur_cls._parse_output is not base_cls._parse_output # noqa: SLF001
|
347
|
-
and self._used_default_llm_response_schema
|
348
|
-
):
|
349
|
-
self._policy_executor.llm.response_schema = None
|
310
|
+
if cur_cls.memory_manager is not base_cls.memory_manager:
|
311
|
+
self._policy_executor.memory_manager = self.memory_manager
|
350
312
|
|
351
|
-
def
|
352
|
-
self
|
353
|
-
|
354
|
-
|
313
|
+
def system_prompt_builder(self, ctx: RunContext[CtxT] | None = None) -> str | None:
|
314
|
+
if self._prompt_builder.system_prompt_builder is not None:
|
315
|
+
return self._prompt_builder.system_prompt_builder(ctx=ctx)
|
316
|
+
raise NotImplementedError("System prompt builder is not implemented.")
|
355
317
|
|
356
|
-
def
|
357
|
-
self,
|
358
|
-
*,
|
359
|
-
in_args: InT | None = None,
|
360
|
-
usr_args: LLMPromptArgs | None = None,
|
361
|
-
ctx: RunContext[CtxT] | None = None,
|
318
|
+
def input_content_builder(
|
319
|
+
self, in_args: InT | None = None, *, ctx: RunContext[CtxT] | None = None
|
362
320
|
) -> Content:
|
363
|
-
|
364
|
-
in_args=in_args,
|
365
|
-
)
|
321
|
+
if self._prompt_builder.input_content_builder is not None:
|
322
|
+
return self._prompt_builder.input_content_builder(in_args=in_args, ctx=ctx)
|
323
|
+
raise NotImplementedError("Input content builder is not implemented.")
|
366
324
|
|
367
|
-
def
|
325
|
+
def tool_call_loop_terminator(
|
368
326
|
self,
|
369
327
|
conversation: Messages,
|
370
328
|
*,
|
371
329
|
ctx: RunContext[CtxT] | None = None,
|
372
330
|
**kwargs: Any,
|
373
331
|
) -> bool:
|
374
|
-
|
375
|
-
|
376
|
-
|
332
|
+
if self._policy_executor.tool_call_loop_terminator is not None:
|
333
|
+
return self._policy_executor.tool_call_loop_terminator(
|
334
|
+
conversation=conversation, ctx=ctx, **kwargs
|
335
|
+
)
|
336
|
+
raise NotImplementedError("Tool call loop terminator is not implemented.")
|
377
337
|
|
378
|
-
def
|
338
|
+
def memory_manager(
|
379
339
|
self,
|
380
340
|
memory: LLMAgentMemory,
|
381
341
|
*,
|
382
342
|
ctx: RunContext[CtxT] | None = None,
|
383
343
|
**kwargs: Any,
|
384
344
|
) -> None:
|
385
|
-
|
386
|
-
|
387
|
-
|
345
|
+
if self._policy_executor.memory_manager is not None:
|
346
|
+
return self._policy_executor.memory_manager(
|
347
|
+
memory=memory, ctx=ctx, **kwargs
|
348
|
+
)
|
349
|
+
raise NotImplementedError("Memory manager is not implemented.")
|
388
350
|
|
389
351
|
# Decorators for custom implementations as an alternative to overriding methods
|
390
352
|
|
391
|
-
def
|
392
|
-
self, func:
|
393
|
-
) ->
|
394
|
-
self._prompt_builder.
|
353
|
+
def add_system_prompt_builder(
|
354
|
+
self, func: SystemPromptBuilder[CtxT]
|
355
|
+
) -> SystemPromptBuilder[CtxT]:
|
356
|
+
self._prompt_builder.system_prompt_builder = func
|
395
357
|
|
396
358
|
return func
|
397
359
|
|
398
|
-
def
|
399
|
-
self, func:
|
400
|
-
) ->
|
401
|
-
self._prompt_builder.
|
360
|
+
def add_input_content_builder(
|
361
|
+
self, func: InputContentBuilder[InT, CtxT]
|
362
|
+
) -> InputContentBuilder[InT, CtxT]:
|
363
|
+
self._prompt_builder.input_content_builder = func
|
402
364
|
|
403
365
|
return func
|
404
366
|
|
405
|
-
def
|
406
|
-
self
|
407
|
-
) -> ParseOutputHandler[InT, OutT_co, CtxT]:
|
408
|
-
if self._used_default_llm_response_schema:
|
409
|
-
self._policy_executor.llm.response_schema = None
|
410
|
-
self._parse_output_impl = func
|
367
|
+
def add_memory_manager(self, func: MemoryManager[CtxT]) -> MemoryManager[CtxT]:
|
368
|
+
self._policy_executor.memory_manager = func
|
411
369
|
|
412
370
|
return func
|
413
371
|
|
414
|
-
def
|
415
|
-
self
|
372
|
+
def add_tool_call_loop_terminator(
|
373
|
+
self, func: ToolCallLoopTerminator[CtxT]
|
374
|
+
) -> ToolCallLoopTerminator[CtxT]:
|
375
|
+
self._policy_executor.tool_call_loop_terminator = func
|
416
376
|
|
417
377
|
return func
|
418
378
|
|
419
|
-
def
|
420
|
-
self, func:
|
421
|
-
) ->
|
422
|
-
self.
|
379
|
+
def add_output_parser(
|
380
|
+
self, func: OutputParser[InT, OutT, CtxT]
|
381
|
+
) -> OutputParser[InT, OutT, CtxT]:
|
382
|
+
if self._used_default_llm_response_schema:
|
383
|
+
self._policy_executor.llm.response_schema = None
|
384
|
+
self.output_parser = func
|
423
385
|
|
424
386
|
return func
|
425
387
|
|
426
|
-
def
|
427
|
-
self
|
428
|
-
) -> ExitToolCallLoopHandler[CtxT]:
|
429
|
-
self._policy_executor.exit_tool_call_loop_impl = func
|
388
|
+
def add_memory_preparator(self, func: MemoryPreparator) -> MemoryPreparator:
|
389
|
+
self.memory_preparator = func
|
430
390
|
|
431
391
|
return func
|
grasp_agents/llm_agent_memory.py
CHANGED