dm-aioaiagent 0.3.3__tar.gz → 0.3.4__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.3.3 → dm_aioaiagent-0.3.4}/PKG-INFO +1 -1
- {dm_aioaiagent-0.3.3 → dm_aioaiagent-0.3.4}/dm_aioaiagent/ai_agent.py +18 -5
- {dm_aioaiagent-0.3.3 → dm_aioaiagent-0.3.4}/dm_aioaiagent.egg-info/PKG-INFO +1 -1
- {dm_aioaiagent-0.3.3 → dm_aioaiagent-0.3.4}/setup.py +1 -1
- {dm_aioaiagent-0.3.3 → dm_aioaiagent-0.3.4}/README.md +0 -0
- {dm_aioaiagent-0.3.3 → dm_aioaiagent-0.3.4}/dm_aioaiagent/__init__.py +0 -0
- {dm_aioaiagent-0.3.3 → dm_aioaiagent-0.3.4}/dm_aioaiagent/async_ai_agent.py +0 -0
- {dm_aioaiagent-0.3.3 → dm_aioaiagent-0.3.4}/dm_aioaiagent/image_message_content_builder.py +0 -0
- {dm_aioaiagent-0.3.3 → dm_aioaiagent-0.3.4}/dm_aioaiagent/types.py +0 -0
- {dm_aioaiagent-0.3.3 → dm_aioaiagent-0.3.4}/dm_aioaiagent.egg-info/SOURCES.txt +0 -0
- {dm_aioaiagent-0.3.3 → dm_aioaiagent-0.3.4}/dm_aioaiagent.egg-info/dependency_links.txt +0 -0
- {dm_aioaiagent-0.3.3 → dm_aioaiagent-0.3.4}/dm_aioaiagent.egg-info/requires.txt +0 -0
- {dm_aioaiagent-0.3.3 → dm_aioaiagent-0.3.4}/dm_aioaiagent.egg-info/top_level.txt +0 -0
- {dm_aioaiagent-0.3.3 → dm_aioaiagent-0.3.4}/setup.cfg +0 -0
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import os
|
|
2
|
+
from pydantic import SecretStr
|
|
2
3
|
from itertools import dropwhile
|
|
3
4
|
from threading import Thread
|
|
4
5
|
from langchain_openai import ChatOpenAI
|
|
@@ -30,24 +31,29 @@ class DMAIAgent:
|
|
|
30
31
|
agent_name: str = None,
|
|
31
32
|
input_output_logging: bool = True,
|
|
32
33
|
is_memory_enabled: bool = True,
|
|
34
|
+
save_tools_responses_in_memory: bool = True,
|
|
33
35
|
max_memory_messages: int = None,
|
|
34
36
|
response_if_request_fail: str = None,
|
|
35
|
-
response_if_invalid_image: str = None
|
|
37
|
+
response_if_invalid_image: str = None,
|
|
38
|
+
openai_api_key: str = None
|
|
36
39
|
):
|
|
37
|
-
if not os.getenv("OPENAI_API_KEY"):
|
|
40
|
+
if openai_api_key is None and not os.getenv("OPENAI_API_KEY"):
|
|
38
41
|
raise EnvironmentError("'OPENAI_API_KEY' environment variable is not set!")
|
|
39
42
|
|
|
40
43
|
self._logger = DMLogger(agent_name or self.agent_name)
|
|
41
44
|
self._is_tools_exists = bool(tools)
|
|
42
45
|
self._input_output_logging = bool(input_output_logging)
|
|
43
46
|
self._is_memory_enabled = bool(is_memory_enabled)
|
|
47
|
+
self._save_tools_responses_in_memory = bool(save_tools_responses_in_memory)
|
|
44
48
|
self._max_memory_messages = self._validate_max_memory_messages(max_memory_messages)
|
|
45
49
|
self._response_if_request_fail = str(response_if_request_fail or self._response_if_request_fail)
|
|
46
50
|
self._response_if_invalid_image = str(response_if_invalid_image or self._response_if_invalid_image)
|
|
47
51
|
|
|
48
52
|
prompt = ChatPromptTemplate.from_messages([SystemMessage(content=system_message),
|
|
49
53
|
MessagesPlaceholder(variable_name="messages")])
|
|
50
|
-
|
|
54
|
+
if openai_api_key:
|
|
55
|
+
openai_api_key = SecretStr(openai_api_key)
|
|
56
|
+
llm = ChatOpenAI(model_name=str(model), temperature=int(temperature), openai_api_key=openai_api_key)
|
|
51
57
|
if self._is_tools_exists:
|
|
52
58
|
self._tool_map = {t.name: t for t in tools}
|
|
53
59
|
llm = llm.bind_tools(tools)
|
|
@@ -168,8 +174,15 @@ class DMAIAgent:
|
|
|
168
174
|
if self._is_memory_enabled:
|
|
169
175
|
memory_id = self._validate_memory_id(state.memory_id)
|
|
170
176
|
messages_to_memory = state.messages[-self._max_memory_messages:]
|
|
171
|
-
|
|
172
|
-
|
|
177
|
+
if self._save_tools_responses_in_memory:
|
|
178
|
+
# drop ToolsMessages from start of list
|
|
179
|
+
self._memory[memory_id] = list(dropwhile(lambda x: isinstance(x, ToolMessage), messages_to_memory))
|
|
180
|
+
else:
|
|
181
|
+
self._memory[memory_id] = []
|
|
182
|
+
for mes in messages_to_memory:
|
|
183
|
+
if isinstance(mes, ToolMessage) or (isinstance(mes, AIMessage) and mes.tool_calls):
|
|
184
|
+
continue
|
|
185
|
+
self._memory[memory_id].append(mes)
|
|
173
186
|
state.response = answer
|
|
174
187
|
else:
|
|
175
188
|
state.response = state.messages[len(state.input_messages):]
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|