dm-aioaiagent 0.5.0__tar.gz → 0.5.2__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dm-aioaiagent
3
- Version: 0.5.0
3
+ Version: 0.5.2
4
4
  Summary: This is my custom aioaiagent client
5
5
  Home-page: https://pypi.org/project/dm-aioaiagent
6
6
  Author: dimka4621
@@ -28,6 +28,8 @@ class DMAIAgent:
28
28
  # tools
29
29
  tools: list[BaseTool] = None,
30
30
  parallel_tool_calls: bool = None,
31
+ before_tool_call_callback: BeforeToolCallCallback = None,
32
+ after_tool_call_callback: AfterToolCallCallback = None,
31
33
  # memory
32
34
  is_memory_enabled: bool = True,
33
35
  save_tools_responses_in_memory: bool = True,
@@ -55,6 +57,8 @@ class DMAIAgent:
55
57
  self._tools = tools or []
56
58
  self._is_tools_exists = bool(tools)
57
59
  self._parallel_tool_calls = parallel_tool_calls
60
+ self._before_tool_call_callback = before_tool_call_callback
61
+ self._after_tool_call_callback = after_tool_call_callback
58
62
  # memory
59
63
  self._memory_messages = []
60
64
  self._is_memory_enabled = bool(is_memory_enabled)
@@ -170,13 +174,25 @@ class DMAIAgent:
170
174
  tool_args = tool_call["args"]
171
175
 
172
176
  def tool_callback(tool_id=tool_id, tool_name=tool_name, tool_args=tool_args) -> None:
173
- self._logger.debug("Invoke tool", tool_id=tool_id, tool_name=tool_name, tool_args=tool_args)
174
177
  if tool_name in self._tool_map:
175
178
  try:
179
+ if self._before_tool_call_callback:
180
+ self._before_tool_call_callback(tool_name, tool_args)
181
+ except Exception as e:
182
+ self._logger.error(str(e), callback_type="before_tool_call")
183
+
184
+ try:
185
+ self._logger.debug("Invoke tool", tool_id=tool_id, tool_name=tool_name, tool_args=tool_args)
176
186
  tool_response = self._tool_map[tool_name].run(tool_args)
177
187
  except Exception as e:
178
- self._logger.error(e, tool_id=tool_id)
188
+ self._logger.error(str(e), tool_id=tool_id)
179
189
  tool_response = "Tool executed with an error!"
190
+
191
+ try:
192
+ if self._after_tool_call_callback:
193
+ self._after_tool_call_callback(tool_name, tool_args, tool_response)
194
+ except Exception as e:
195
+ self._logger.error(str(e), callback_type="after_tool_call")
180
196
  else:
181
197
  tool_response = f"Tool not found!"
182
198
  self._logger.debug(f"Tool response:\n{tool_response}", tool_id=tool_id)
@@ -8,8 +8,19 @@ from .types import *
8
8
 
9
9
 
10
10
  class DMAioAIAgent(DMAIAgent):
11
- def __init__(self, *args, **kwargs):
12
- super().__init__(*args, **kwargs)
11
+ def __init__(
12
+ self,
13
+ *args,
14
+ before_tool_call_callback: AsyncBeforeToolCallCallback = None,
15
+ after_tool_call_callback: AsyncAfterToolCallCallback = None,
16
+ **kwargs
17
+ ):
18
+ super().__init__(
19
+ *args,
20
+ before_tool_call_callback=before_tool_call_callback,
21
+ after_tool_call_callback=after_tool_call_callback,
22
+ **kwargs
23
+ )
13
24
 
14
25
  async def run(self, query: str, *args, **kwargs) -> Union[str, TypedDict, BaseModel]:
15
26
  new_messages = await self.run_messages(messages=[TextMessage(role="user", content=query)], *args, **kwargs)
@@ -79,13 +90,25 @@ class DMAioAIAgent(DMAIAgent):
79
90
  tool_args = tool_call["args"]
80
91
 
81
92
  async def tool_callback(tool_id=tool_id, tool_name=tool_name, tool_args=tool_args) -> None:
82
- self._logger.debug("Invoke tool", tool_id=tool_id, tool_name=tool_name, tool_args=tool_args)
83
93
  if tool_name in self._tool_map:
84
94
  try:
95
+ if self._before_tool_call_callback:
96
+ await self._before_tool_call_callback(tool_name, tool_args)
97
+ except Exception as e:
98
+ self._logger.error(str(e), callback_type="before_tool_call")
99
+
100
+ try:
101
+ self._logger.debug("Invoke tool", tool_id=tool_id, tool_name=tool_name, tool_args=tool_args)
85
102
  tool_response = await self._tool_map[tool_name].arun(tool_args)
86
103
  except Exception as e:
87
- self._logger.error(e, tool_id=tool_id)
104
+ self._logger.error(str(e), tool_id=tool_id)
88
105
  tool_response = "Tool executed with an error!"
106
+
107
+ try:
108
+ if self._after_tool_call_callback:
109
+ await self._after_tool_call_callback(tool_name, tool_args, tool_response)
110
+ except Exception as e:
111
+ self._logger.error(str(e), callback_type="after_tool_call")
89
112
  else:
90
113
  tool_response = f"Tool '{tool_name}' not found!"
91
114
  self._logger.debug(f"Tool response:\n{tool_response}", tool_id=tool_id)
@@ -1,10 +1,13 @@
1
- from typing import Literal, Union, Type
2
- from typing_extensions import TypedDict
1
+ from typing import Literal, Union, Type, TypedDict, Callable, Coroutine, Any
3
2
  from pydantic import BaseModel
4
3
  from langchain_core.messages import BaseMessage
5
4
 
6
5
  OutputSchemaType = Union[Type[TypedDict], Type[BaseModel], None]
7
6
 
7
+ BeforeToolCallCallback = Callable[[str, dict], None]
8
+ AfterToolCallCallback = Callable[[str, dict, str], None]
9
+ AsyncBeforeToolCallCallback = Callable[[str, dict], Coroutine[Any, Any, None]]
10
+ AsyncAfterToolCallCallback = Callable[[str, dict, str], Coroutine[Any, Any, None]]
8
11
 
9
12
  class ImageMessageTextItem(TypedDict):
10
13
  type: Literal['text']
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dm-aioaiagent
3
- Version: 0.5.0
3
+ Version: 0.5.2
4
4
  Summary: This is my custom aioaiagent client
5
5
  Home-page: https://pypi.org/project/dm-aioaiagent
6
6
  Author: dimka4621
@@ -8,7 +8,7 @@ def readme():
8
8
 
9
9
  setup(
10
10
  name='dm-aioaiagent',
11
- version='v0.5.0',
11
+ version='v0.5.2',
12
12
  author='dimka4621',
13
13
  author_email='mismartconfig@gmail.com',
14
14
  description='This is my custom aioaiagent client',
File without changes
File without changes