dm-aioaiagent 0.5.1__tar.gz → 0.5.3__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.
- {dm_aioaiagent-0.5.1 → dm_aioaiagent-0.5.3}/PKG-INFO +1 -1
- {dm_aioaiagent-0.5.1 → dm_aioaiagent-0.5.3}/dm_aioaiagent/ai_agent.py +17 -1
- {dm_aioaiagent-0.5.1 → dm_aioaiagent-0.5.3}/dm_aioaiagent/async_ai_agent.py +26 -3
- {dm_aioaiagent-0.5.1 → dm_aioaiagent-0.5.3}/dm_aioaiagent/types.py +5 -1
- {dm_aioaiagent-0.5.1 → dm_aioaiagent-0.5.3}/dm_aioaiagent.egg-info/PKG-INFO +1 -1
- {dm_aioaiagent-0.5.1 → dm_aioaiagent-0.5.3}/setup.py +1 -1
- {dm_aioaiagent-0.5.1 → dm_aioaiagent-0.5.3}/README.md +0 -0
- {dm_aioaiagent-0.5.1 → dm_aioaiagent-0.5.3}/dm_aioaiagent/__init__.py +0 -0
- {dm_aioaiagent-0.5.1 → dm_aioaiagent-0.5.3}/dm_aioaiagent/openai_image_message_content.py +0 -0
- {dm_aioaiagent-0.5.1 → dm_aioaiagent-0.5.3}/dm_aioaiagent.egg-info/SOURCES.txt +0 -0
- {dm_aioaiagent-0.5.1 → dm_aioaiagent-0.5.3}/dm_aioaiagent.egg-info/dependency_links.txt +0 -0
- {dm_aioaiagent-0.5.1 → dm_aioaiagent-0.5.3}/dm_aioaiagent.egg-info/requires.txt +0 -0
- {dm_aioaiagent-0.5.1 → dm_aioaiagent-0.5.3}/dm_aioaiagent.egg-info/top_level.txt +0 -0
- {dm_aioaiagent-0.5.1 → dm_aioaiagent-0.5.3}/setup.cfg +0 -0
|
@@ -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(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
188
|
self._logger.error(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(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__(
|
|
12
|
-
|
|
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(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
104
|
self._logger.error(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(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,9 +1,13 @@
|
|
|
1
|
-
from typing import Literal, Union, Type, TypedDict
|
|
1
|
+
from typing import Literal, Union, Type, TypedDict, Callable, Coroutine, Any
|
|
2
2
|
from pydantic import BaseModel
|
|
3
3
|
from langchain_core.messages import BaseMessage
|
|
4
4
|
|
|
5
5
|
OutputSchemaType = Union[Type[TypedDict], Type[BaseModel], None]
|
|
6
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]]
|
|
7
11
|
|
|
8
12
|
class ImageMessageTextItem(TypedDict):
|
|
9
13
|
type: Literal['text']
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|