grasp_agents 0.3.2__py3-none-any.whl → 0.3.3__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/llm_agent.py CHANGED
@@ -192,7 +192,7 @@ class LLMAgent(
192
192
 
193
193
  # 1. Make system prompt (can be None)
194
194
 
195
- formatted_sys_prompt = self._prompt_builder.make_sys_prompt(
195
+ formatted_sys_prompt = self._prompt_builder.make_system_prompt(
196
196
  sys_args=sys_args, ctx=ctx
197
197
  )
198
198
 
@@ -313,17 +313,17 @@ class LLMAgent(
313
313
 
314
314
  # -- Decorators for custom implementations --
315
315
 
316
- def make_sys_prompt(
316
+ def make_system_prompt(
317
317
  self, func: MakeSystemPromptHandler[CtxT]
318
318
  ) -> MakeSystemPromptHandler[CtxT]:
319
- self._prompt_builder.make_sys_prompt_impl = func
319
+ self._prompt_builder.make_system_prompt_impl = func
320
320
 
321
321
  return func
322
322
 
323
- def make_in_content(
323
+ def make_input_content(
324
324
  self, func: MakeInputContentHandler[InT_contra, CtxT]
325
325
  ) -> MakeInputContentHandler[InT_contra, CtxT]:
326
- self._prompt_builder.make_in_content_impl = func
326
+ self._prompt_builder.make_input_content_impl = func
327
327
 
328
328
  return func
329
329
 
@@ -359,30 +359,28 @@ class LLMAgent(
359
359
  cur_cls = type(self)
360
360
  base_cls = LLMAgent[Any, Any, Any]
361
361
 
362
- if cur_cls._make_sys_prompt_fn is not base_cls._make_sys_prompt_fn: # noqa: SLF001
363
- self._prompt_builder.make_sys_prompt_impl = self._make_sys_prompt_fn
362
+ if cur_cls._make_system_prompt is not base_cls._make_system_prompt: # noqa: SLF001
363
+ self._prompt_builder.make_system_prompt_impl = self._make_system_prompt
364
364
 
365
- if cur_cls._make_in_content_fn is not base_cls._make_in_content_fn: # noqa: SLF001
366
- self._prompt_builder.make_in_content_impl = self._make_in_content_fn
365
+ if cur_cls._make_input_content is not base_cls._make_input_content: # noqa: SLF001
366
+ self._prompt_builder.make_input_content_impl = self._make_input_content
367
367
 
368
- if cur_cls._set_memory_fn is not base_cls._set_memory_fn: # noqa: SLF001
369
- self._set_memory_impl = self._set_memory_fn
368
+ if cur_cls._set_memory is not base_cls._set_memory: # noqa: SLF001
369
+ self._set_memory_impl = self._set_memory
370
370
 
371
- if cur_cls._manage_memory_fn is not base_cls._manage_memory_fn: # noqa: SLF001
372
- self._policy_executor.manage_memory_impl = self._manage_memory_fn
371
+ if cur_cls._manage_memory is not base_cls._manage_memory: # noqa: SLF001
372
+ self._policy_executor.manage_memory_impl = self._manage_memory
373
373
 
374
374
  if (
375
- cur_cls._exit_tool_call_loop_fn is not base_cls._exit_tool_call_loop_fn # noqa: SLF001
375
+ cur_cls._exit_tool_call_loop is not base_cls._exit_tool_call_loop # noqa: SLF001
376
376
  ):
377
- self._policy_executor.exit_tool_call_loop_impl = (
378
- self._exit_tool_call_loop_fn
379
- )
377
+ self._policy_executor.exit_tool_call_loop_impl = self._exit_tool_call_loop
380
378
 
381
379
  self._parse_output_impl: (
382
380
  ParseOutputHandler[InT_contra, OutT_co, CtxT] | None
383
381
  ) = None
384
382
 
385
- def _make_sys_prompt_fn(
383
+ def _make_system_prompt(
386
384
  self, sys_args: LLMPromptArgs | None, *, ctx: RunContext[CtxT] | None = None
387
385
  ) -> str:
388
386
  raise NotImplementedError(
@@ -390,7 +388,7 @@ class LLMAgent(
390
388
  "if it's intended to be used as the system arguments formatter."
391
389
  )
392
390
 
393
- def _make_in_content_fn(
391
+ def _make_input_content(
394
392
  self,
395
393
  *,
396
394
  in_args: InT_contra | None = None,
@@ -402,10 +400,10 @@ class LLMAgent(
402
400
  "LLMAgent._format_in_args must be overridden by a subclass"
403
401
  )
404
402
 
405
- def _set_memory_fn(
403
+ def _set_memory(
406
404
  self,
407
405
  prev_memory: LLMAgentMemory,
408
- in_args: InT_contra | Sequence[InT_contra] | None = None,
406
+ in_args: Sequence[InT_contra] | None = None,
409
407
  sys_prompt: LLMPrompt | None = None,
410
408
  ctx: RunContext[Any] | None = None,
411
409
  ) -> LLMAgentMemory:
@@ -413,7 +411,7 @@ class LLMAgent(
413
411
  "LLMAgent._set_memory must be overridden by a subclass"
414
412
  )
415
413
 
416
- def _exit_tool_call_loop_fn(
414
+ def _exit_tool_call_loop(
417
415
  self,
418
416
  conversation: Messages,
419
417
  *,
@@ -424,7 +422,7 @@ class LLMAgent(
424
422
  "LLMAgent._exit_tool_call_loop must be overridden by a subclass"
425
423
  )
426
424
 
427
- def _manage_memory_fn(
425
+ def _manage_memory(
428
426
  self,
429
427
  memory: LLMAgentMemory,
430
428
  *,
@@ -28,6 +28,9 @@ from .typing.tool import BaseTool, NamedToolChoice, ToolCall, ToolChoice
28
28
  logger = getLogger(__name__)
29
29
 
30
30
 
31
+ FINAL_ANSWER_TOOL_NAME = "final_answer"
32
+
33
+
31
34
  _FinalAnswerT = TypeVar("_FinalAnswerT")
32
35
 
33
36
 
@@ -102,7 +105,7 @@ class LLMPolicyExecutor(AutoInstanceAttributesMixin, Generic[_FinalAnswerT, CtxT
102
105
  def max_turns(self) -> int:
103
106
  return self._max_turns
104
107
 
105
- def _exit_tool_call_loop_fn(
108
+ def _exit_tool_call_loop(
106
109
  self,
107
110
  conversation: Messages,
108
111
  *,
@@ -119,7 +122,7 @@ class LLMPolicyExecutor(AutoInstanceAttributesMixin, Generic[_FinalAnswerT, CtxT
119
122
 
120
123
  return not bool(conversation[-1].tool_calls)
121
124
 
122
- def _manage_memory_fn(
125
+ def _manage_memory(
123
126
  self,
124
127
  memory: LLMAgentMemory,
125
128
  *,
@@ -318,7 +321,7 @@ class LLMPolicyExecutor(AutoInstanceAttributesMixin, Generic[_FinalAnswerT, CtxT
318
321
 
319
322
  # When final_answer_tool_name is None, we use exit_tool_call_loop_impl
320
323
  # to determine whether to exit the loop.
321
- if self._final_answer_tool_name is None and self._exit_tool_call_loop_fn(
324
+ if self._final_answer_tool_name is None and self._exit_tool_call_loop(
322
325
  conversation, ctx=ctx, num_turns=turns
323
326
  ):
324
327
  return gen_message
@@ -353,7 +356,7 @@ class LLMPolicyExecutor(AutoInstanceAttributesMixin, Generic[_FinalAnswerT, CtxT
353
356
  await self.call_tools(gen_message.tool_calls, memory=memory, ctx=ctx)
354
357
 
355
358
  # Apply the memory management function if provided.
356
- self._manage_memory_fn(memory, ctx=ctx, num_turns=turns)
359
+ self._manage_memory(memory, ctx=ctx, num_turns=turns)
357
360
 
358
361
  # 4. Generate the next message based on the updated memory.
359
362
  # In ReAct mode, we set tool_choice to "none" if we just called tools,
@@ -394,7 +397,7 @@ class LLMPolicyExecutor(AutoInstanceAttributesMixin, Generic[_FinalAnswerT, CtxT
394
397
  while True:
395
398
  conversation = memory.message_history.conversations[0]
396
399
 
397
- if self._final_answer_tool_name is None and self._exit_tool_call_loop_fn(
400
+ if self._final_answer_tool_name is None and self._exit_tool_call_loop(
398
401
  conversation, ctx=ctx, num_turns=turns
399
402
  ):
400
403
  return
@@ -429,7 +432,7 @@ class LLMPolicyExecutor(AutoInstanceAttributesMixin, Generic[_FinalAnswerT, CtxT
429
432
  ):
430
433
  yield tool_message_event
431
434
 
432
- self._manage_memory_fn(memory, ctx=ctx, num_turns=turns)
435
+ self._manage_memory(memory, ctx=ctx, num_turns=turns)
433
436
 
434
437
  tool_choice = (
435
438
  "none" if (self._react_mode and gen_message.tool_calls) else "required"
@@ -458,11 +461,10 @@ class LLMPolicyExecutor(AutoInstanceAttributesMixin, Generic[_FinalAnswerT, CtxT
458
461
  )
459
462
 
460
463
  class FinalAnswerTool(BaseTool[self._final_answer_type, None, Any]):
461
- name: str = "final_answer"
464
+ name: str = FINAL_ANSWER_TOOL_NAME
462
465
  description: str = (
463
- "You must use this tool to provide the final answer. "
464
- "Do not provide the final answer anywhere else. "
465
- "Input arguments correspond to the final answer."
466
+ "You must call this tool to provide the final answer. "
467
+ "DO NOT output your answer before calling the tool. "
466
468
  )
467
469
 
468
470
  async def run(
@@ -53,14 +53,14 @@ class PromptBuilder(AutoInstanceAttributesMixin, Generic[InT_contra, CtxT]):
53
53
  self.in_prompt_template = in_prompt_template
54
54
  self.sys_args_schema = sys_args_schema
55
55
  self.usr_args_schema = usr_args_schema
56
- self.make_sys_prompt_impl: MakeSystemPromptHandler[CtxT] | None = None
57
- self.make_in_content_impl: MakeInputContentHandler[InT_contra, CtxT] | None = (
58
- None
59
- )
56
+ self.make_system_prompt_impl: MakeSystemPromptHandler[CtxT] | None = None
57
+ self.make_input_content_impl: (
58
+ MakeInputContentHandler[InT_contra, CtxT] | None
59
+ ) = None
60
60
 
61
61
  self._in_args_type_adapter: TypeAdapter[InT_contra] = TypeAdapter(self._in_type)
62
62
 
63
- def make_sys_prompt(
63
+ def make_system_prompt(
64
64
  self, sys_args: LLMPromptArgs | None = None, ctx: RunContext[CtxT] | None = None
65
65
  ) -> str | None:
66
66
  if self.sys_prompt_template is None:
@@ -76,8 +76,8 @@ class PromptBuilder(AutoInstanceAttributesMixin, Generic[InT_contra, CtxT]):
76
76
  "provided."
77
77
  )
78
78
 
79
- if self.make_sys_prompt_impl:
80
- return self.make_sys_prompt_impl(sys_args=val_sys_args, ctx=ctx)
79
+ if self.make_system_prompt_impl:
80
+ return self.make_system_prompt_impl(sys_args=val_sys_args, ctx=ctx)
81
81
 
82
82
  sys_args_dict = (
83
83
  val_sys_args.model_dump(exclude_unset=True) if val_sys_args else {}
@@ -85,7 +85,7 @@ class PromptBuilder(AutoInstanceAttributesMixin, Generic[InT_contra, CtxT]):
85
85
 
86
86
  return self.sys_prompt_template.format(**sys_args_dict)
87
87
 
88
- def make_in_content(
88
+ def make_input_content(
89
89
  self,
90
90
  *,
91
91
  in_args: InT_contra | None,
@@ -97,8 +97,8 @@ class PromptBuilder(AutoInstanceAttributesMixin, Generic[InT_contra, CtxT]):
97
97
  in_args=in_args, usr_args=usr_args
98
98
  )
99
99
 
100
- if self.make_in_content_impl:
101
- return self.make_in_content_impl(
100
+ if self.make_input_content_impl:
101
+ return self.make_input_content_impl(
102
102
  in_args=val_in_args, usr_args=val_usr_args, batch_idx=batch_idx, ctx=ctx
103
103
  )
104
104
 
@@ -126,7 +126,7 @@ class PromptBuilder(AutoInstanceAttributesMixin, Generic[InT_contra, CtxT]):
126
126
  return self._usr_messages_from_content_parts(chat_inputs)
127
127
 
128
128
  in_content_batch = [
129
- self.make_in_content(
129
+ self.make_input_content(
130
130
  in_args=in_args, usr_args=usr_args, batch_idx=i, ctx=ctx
131
131
  )
132
132
  for i, in_args in enumerate(in_args_batch or [None])
grasp_agents/utils.py CHANGED
@@ -88,6 +88,9 @@ def validate_obj_from_json_or_py_string(
88
88
  else:
89
89
  _selected_adapter = adapter
90
90
 
91
+ if _selected_adapter._type is str: # type: ignore[arg-type]
92
+ return s
93
+
91
94
  try:
92
95
  if from_substring:
93
96
  parsed = parse_json_or_py_substring(s, return_none_on_failure=True)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: grasp_agents
3
- Version: 0.3.2
3
+ Version: 0.3.3
4
4
  Summary: Grasp Agents Library
5
5
  License-File: LICENSE.md
6
6
  Requires-Python: <4,>=3.11.4
@@ -6,19 +6,19 @@ grasp_agents/generics_utils.py,sha256=5Pw3I9dlnKC2VGqYKC4ZZUO3Z_vTNT-NPFovNfPkl6
6
6
  grasp_agents/grasp_logging.py,sha256=H1GYhXdQvVkmauFDZ-KDwvVmPQHZUUm9sRqX_ObK2xI,1111
7
7
  grasp_agents/http_client.py,sha256=KZva2MjJjuI5ohUeU8RdTAImUnQYaqBrV2jDH8smbJw,738
8
8
  grasp_agents/llm.py,sha256=5Yn4jkBPSbGVMf1sEuYXX6YrJlYZiH7oqKLY0yQIn-U,5192
9
- grasp_agents/llm_agent.py,sha256=Ez6_SQ3yq5X0hs7-zjqgYB0RvxoIJsaf4zzS8awrDZI,14851
9
+ grasp_agents/llm_agent.py,sha256=a9TCUpQJwpC5Y0dnTqh5bhRLNsQt8pQOjp9d_7QGRVY,14791
10
10
  grasp_agents/llm_agent_memory.py,sha256=kD_UIF8xVgbSgW6xN87TzkdQcbTWLB-C5ZQu1_2HLx8,1770
11
- grasp_agents/llm_policy_executor.py,sha256=ODyxTgqtbBnTRbCanMKjcQ7ET1fYy28_f2Jh_xXxY0Y,17876
11
+ grasp_agents/llm_policy_executor.py,sha256=PVNuAejt3S-aSGsT7HvgYHyX_cZQSvrt1aUGPdkXtE4,17847
12
12
  grasp_agents/memory.py,sha256=gPkVIIF6dI_xXzarIAw9kSEnSJcfW_teUsWA2JAih94,671
13
13
  grasp_agents/message_history.py,sha256=-ZNy3C1z0yQeahjqR0oIoWDMySJ7vPS19jdutibW7OE,5408
14
14
  grasp_agents/packet.py,sha256=PZ1EpclniAoLk7z4ieZbWzgYH3JSRgnlTe_WfbJYG_4,707
15
15
  grasp_agents/packet_pool.py,sha256=9umHbi5FwuUYYhhodSR-Z-fRR6OYiZyYEzq5d4nZFK4,3036
16
16
  grasp_agents/printer.py,sha256=eVpSZMVk4ZLkV78Sgfg1euzkaS3XBCb30yJcwLMqI0w,5464
17
17
  grasp_agents/processor.py,sha256=Atd_iTQNd3Nudb4mHqt3a5AQ31QUgpbrt1Fhn1sgpSk,6708
18
- grasp_agents/prompt_builder.py,sha256=eW_TOVarvh5niZcNg6EpiUrR_xv_xKZ0QT04RnzCLxc,8454
18
+ grasp_agents/prompt_builder.py,sha256=CGs7ADlGGmRyDqgFte36cYXKsB3C7SjzqSbkRaWlLN8,8481
19
19
  grasp_agents/run_context.py,sha256=0y1JDbz3hJbGiqd3EjUYagkMbEK8YlCzdErp3R2MCr0,1647
20
20
  grasp_agents/usage_tracker.py,sha256=SPwv6RpdoHRuMIKE2hCAWAvDbtR3uXuhr2jpHQuKWhI,3438
21
- grasp_agents/utils.py,sha256=uOUt4LDnErp3WQ2aQesH6mkMzon1-NIsft-o6aVznJE,4522
21
+ grasp_agents/utils.py,sha256=GyJNHkMHnQM3WOUGWD05GpY-iWSd9TtZxfUnFUw9Im0,4605
22
22
  grasp_agents/openai/__init__.py,sha256=wpTeew6EjhM6esHCKrEKUpwq0kygMN2QQDxYtmbRG8Y,4201
23
23
  grasp_agents/openai/completion_chunk_converters.py,sha256=i-1SvIWhRKtL0K8p5pb3jjACSnyHuJHTCoZovEERpxs,2628
24
24
  grasp_agents/openai/completion_converters.py,sha256=vzPEkUOX4l2hobKxZjEk_dyWfzeYesO0DlvWvNVb-Sg,2656
@@ -45,7 +45,7 @@ grasp_agents/workflow/looped_workflow.py,sha256=QqXclXYxsW6C8Rxkf3dRaMHi-DfCvCbj
45
45
  grasp_agents/workflow/parallel_processor.py,sha256=Xyzs2UR_mRe2GFgzzadHOhqgMu3rFjd3GUjvmZimt_k,3505
46
46
  grasp_agents/workflow/sequential_workflow.py,sha256=Pl7jl9ZVDu-rC5UMfympEaQN8iG3kZurVF5eIPG62XA,2130
47
47
  grasp_agents/workflow/workflow_processor.py,sha256=2-iaDIlgNXgj-ClGbiE3fYfSv-N_qRC49Gf_dF6M_40,2640
48
- grasp_agents-0.3.2.dist-info/METADATA,sha256=y7JtyI-29EtIUDTcqDhPWGyTJJd5w2QP6EKokGQqRbY,6806
49
- grasp_agents-0.3.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
50
- grasp_agents-0.3.2.dist-info/licenses/LICENSE.md,sha256=-nNNdWqGB8gJ2O-peFQ2Irshv5tW5pHKyTcYkwvH7CE,1201
51
- grasp_agents-0.3.2.dist-info/RECORD,,
48
+ grasp_agents-0.3.3.dist-info/METADATA,sha256=bV-dofgYSNMv1JHo7kxOZKD6DFDKAb4pHL5yeApC2aA,6806
49
+ grasp_agents-0.3.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
50
+ grasp_agents-0.3.3.dist-info/licenses/LICENSE.md,sha256=-nNNdWqGB8gJ2O-peFQ2Irshv5tW5pHKyTcYkwvH7CE,1201
51
+ grasp_agents-0.3.3.dist-info/RECORD,,