cortexdbai 0.4.0__tar.gz → 0.4.1__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.
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/PKG-INFO +1 -1
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/__init__.py +1 -1
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/autogen/__init__.py +21 -21
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/autogen/agent.py +192 -192
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/autogen/tools.py +165 -165
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/beeai/__init__.py +15 -15
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/beeai/memory.py +142 -142
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/beeai/tools.py +189 -189
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/camel/__init__.py +21 -21
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/camel/memory.py +165 -165
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/camel/tools.py +160 -160
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/controlflow/__init__.py +12 -12
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/controlflow/memory.py +266 -266
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/crewai/__init__.py +14 -14
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/crewai/memory.py +83 -83
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/crewai/tools.py +80 -80
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/deepinfra/__init__.py +13 -13
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/deepinfra/config.py +73 -73
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/deepinfra/embeddings.py +36 -36
- {cortexdbai-0.4.0/cortexdb/integrations/together → cortexdbai-0.4.1/cortexdb/integrations/deepinfra}/openai_compat.py +208 -208
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/dify/__init__.py +12 -12
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/dify/tool.py +229 -229
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/dspy/__init__.py +13 -13
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/dspy/memory.py +121 -121
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/dspy/retriever.py +111 -111
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/fireworks/__init__.py +13 -13
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/fireworks/config.py +68 -68
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/fireworks/embeddings.py +36 -36
- {cortexdbai-0.4.0/cortexdb/integrations/deepinfra → cortexdbai-0.4.1/cortexdb/integrations/fireworks}/openai_compat.py +208 -208
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/flowise/__init__.py +12 -12
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/flowise/node.py +295 -295
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/google_adk/__init__.py +20 -20
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/google_adk/tools.py +198 -198
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/groq/__init__.py +13 -13
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/groq/config.py +63 -63
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/groq/embeddings.py +55 -55
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/groq/openai_compat.py +208 -208
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/hermes/__init__.py +5 -5
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/hermes/provider.py +272 -272
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/instructor/__init__.py +17 -17
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/instructor/memory.py +230 -230
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/langgraph/__init__.py +12 -12
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/langgraph/memory.py +333 -333
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/letta/__init__.py +12 -12
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/letta/storage.py +308 -308
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/llamaindex/__init__.py +12 -12
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/llamaindex/memory.py +260 -260
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/nemo_guardrails/__init__.py +24 -24
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/nemo_guardrails/actions.py +216 -216
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/nemo_guardrails/knowledge_base.py +132 -132
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/ollama/__init__.py +14 -14
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/ollama/config.py +44 -44
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/ollama/embeddings.py +126 -126
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/openai_agents/__init__.py +19 -19
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/openai_agents/tools.py +183 -183
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/prefect/__init__.py +21 -21
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/prefect/blocks.py +88 -88
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/prefect/tasks.py +143 -143
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/pydanticai/__init__.py +21 -21
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/pydanticai/dependencies.py +42 -42
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/pydanticai/tools.py +100 -100
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/smolagents/__init__.py +19 -19
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/smolagents/tools.py +249 -249
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/temporal/__init__.py +21 -21
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/temporal/activities.py +239 -239
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/temporal/interceptor.py +207 -207
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/together/__init__.py +13 -13
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/together/config.py +75 -75
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/together/embeddings.py +36 -36
- {cortexdbai-0.4.0/cortexdb/integrations/fireworks → cortexdbai-0.4.1/cortexdb/integrations/together}/openai_compat.py +208 -208
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/vllm/__init__.py +13 -13
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/vllm/config.py +59 -59
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/vllm/embeddings.py +37 -37
- cortexdbai-0.4.1/cortexdb/integrations/vllm/openai_compat.py +208 -0
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/pyproject.toml +175 -175
- cortexdbai-0.4.0/cortexdb/integrations/vllm/openai_compat.py +0 -208
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/.gitignore +0 -0
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/README.md +0 -0
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/__init__.py +0 -0
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/integrations/dify/manifest.yaml +0 -0
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/py.typed +0 -0
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/v1/__init__.py +0 -0
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/v1/client.py +0 -0
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/cortexdb/v1/exceptions.py +0 -0
- {cortexdbai-0.4.0 → cortexdbai-0.4.1}/tests/__init__.py +0 -0
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
"""CortexDB integration for AutoGen.
|
|
2
|
-
|
|
3
|
-
Provides callable tools and a memory-augmented agent that connect
|
|
4
|
-
AutoGen's multi-agent framework to CortexDB's long-term memory system.
|
|
5
|
-
"""
|
|
6
|
-
|
|
7
|
-
from cortexdb.integrations.autogen.agent import CortexDBAgent
|
|
8
|
-
from cortexdb.integrations.autogen.tools import (
|
|
9
|
-
cortexdb_forget_fn,
|
|
10
|
-
cortexdb_search_fn,
|
|
11
|
-
cortexdb_store_fn,
|
|
12
|
-
register_cortexdb_tools,
|
|
13
|
-
)
|
|
14
|
-
|
|
15
|
-
__all__ = [
|
|
16
|
-
"CortexDBAgent",
|
|
17
|
-
"cortexdb_search_fn",
|
|
18
|
-
"cortexdb_store_fn",
|
|
19
|
-
"cortexdb_forget_fn",
|
|
20
|
-
"register_cortexdb_tools",
|
|
21
|
-
]
|
|
1
|
+
"""CortexDB integration for AutoGen.
|
|
2
|
+
|
|
3
|
+
Provides callable tools and a memory-augmented agent that connect
|
|
4
|
+
AutoGen's multi-agent framework to CortexDB's long-term memory system.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from cortexdb.integrations.autogen.agent import CortexDBAgent
|
|
8
|
+
from cortexdb.integrations.autogen.tools import (
|
|
9
|
+
cortexdb_forget_fn,
|
|
10
|
+
cortexdb_search_fn,
|
|
11
|
+
cortexdb_store_fn,
|
|
12
|
+
register_cortexdb_tools,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"CortexDBAgent",
|
|
17
|
+
"cortexdb_search_fn",
|
|
18
|
+
"cortexdb_store_fn",
|
|
19
|
+
"cortexdb_forget_fn",
|
|
20
|
+
"register_cortexdb_tools",
|
|
21
|
+
]
|
|
@@ -1,192 +1,192 @@
|
|
|
1
|
-
"""AutoGen agent with built-in CortexDB memory.
|
|
2
|
-
|
|
3
|
-
Provides a ConversableAgent subclass that automatically stores
|
|
4
|
-
conversation turns and retrieves relevant context from CortexDB
|
|
5
|
-
before generating responses.
|
|
6
|
-
"""
|
|
7
|
-
|
|
8
|
-
from __future__ import annotations
|
|
9
|
-
|
|
10
|
-
from typing import Any, Optional, Union
|
|
11
|
-
|
|
12
|
-
from autogen import ConversableAgent
|
|
13
|
-
|
|
14
|
-
from cortexdb import Cortex
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
class CortexDBAgent(ConversableAgent):
|
|
18
|
-
"""AutoGen ConversableAgent with automatic CortexDB memory integration.
|
|
19
|
-
|
|
20
|
-
Extends AutoGen's ConversableAgent to automatically store conversation
|
|
21
|
-
turns in CortexDB and retrieve relevant past context before generating
|
|
22
|
-
responses. This enables the agent to maintain long-term memory across
|
|
23
|
-
conversations and sessions.
|
|
24
|
-
|
|
25
|
-
The agent injects retrieved context as a system-level prefix to the
|
|
26
|
-
conversation, giving the LLM access to relevant memories without
|
|
27
|
-
modifying the visible chat history.
|
|
28
|
-
|
|
29
|
-
Args:
|
|
30
|
-
name: The agent's name.
|
|
31
|
-
cortex_client: An initialized CortexDB client instance.
|
|
32
|
-
scope: The hierarchical scope path for memory isolation.
|
|
33
|
-
auto_store: Whether to automatically store conversation turns.
|
|
34
|
-
Defaults to ``True``.
|
|
35
|
-
auto_recall: Whether to automatically retrieve context before
|
|
36
|
-
responding. Defaults to ``True``.
|
|
37
|
-
**kwargs: Additional keyword arguments passed to ConversableAgent.
|
|
38
|
-
|
|
39
|
-
Example::
|
|
40
|
-
|
|
41
|
-
from cortexdb import Cortex
|
|
42
|
-
from cortexdb.integrations.autogen import CortexDBAgent
|
|
43
|
-
|
|
44
|
-
client = Cortex("http://localhost:3141")
|
|
45
|
-
agent = CortexDBAgent(
|
|
46
|
-
name="memory_assistant",
|
|
47
|
-
cortex_client=client,
|
|
48
|
-
scope="user:default",
|
|
49
|
-
llm_config=llm_config,
|
|
50
|
-
system_message="You are a helpful assistant with long-term memory.",
|
|
51
|
-
)
|
|
52
|
-
"""
|
|
53
|
-
|
|
54
|
-
def __init__(
|
|
55
|
-
self,
|
|
56
|
-
name: str,
|
|
57
|
-
cortex_client: Cortex,
|
|
58
|
-
scope: str = "user:default",
|
|
59
|
-
auto_store: bool = True,
|
|
60
|
-
auto_recall: bool = True,
|
|
61
|
-
**kwargs: Any,
|
|
62
|
-
) -> None:
|
|
63
|
-
"""Initialize CortexDBAgent with memory capabilities.
|
|
64
|
-
|
|
65
|
-
Args:
|
|
66
|
-
name: The agent's name.
|
|
67
|
-
cortex_client: An initialized CortexDB client instance.
|
|
68
|
-
scope: The hierarchical scope path for memory isolation.
|
|
69
|
-
auto_store: Whether to automatically store conversation turns.
|
|
70
|
-
auto_recall: Whether to automatically retrieve context.
|
|
71
|
-
**kwargs: Additional keyword arguments passed to ConversableAgent.
|
|
72
|
-
"""
|
|
73
|
-
super().__init__(name=name, **kwargs)
|
|
74
|
-
self._cortex_client = cortex_client
|
|
75
|
-
self._scope = scope
|
|
76
|
-
self._auto_store = auto_store
|
|
77
|
-
self._auto_recall = auto_recall
|
|
78
|
-
|
|
79
|
-
def generate_reply(
|
|
80
|
-
self,
|
|
81
|
-
messages: Optional[list[dict[str, Any]]] = None,
|
|
82
|
-
sender: Optional[Any] = None,
|
|
83
|
-
**kwargs: Any,
|
|
84
|
-
) -> Union[str, dict[str, Any], None]:
|
|
85
|
-
"""Generate a reply with CortexDB memory augmentation.
|
|
86
|
-
|
|
87
|
-
Before generating a reply, retrieves relevant context from CortexDB
|
|
88
|
-
based on the latest message. After generating, stores the conversation
|
|
89
|
-
turn for future recall.
|
|
90
|
-
|
|
91
|
-
Args:
|
|
92
|
-
messages: The conversation messages to respond to.
|
|
93
|
-
sender: The agent that sent the message.
|
|
94
|
-
**kwargs: Additional keyword arguments.
|
|
95
|
-
|
|
96
|
-
Returns:
|
|
97
|
-
The generated reply string, a structured reply dict, or None.
|
|
98
|
-
"""
|
|
99
|
-
if messages and self._auto_recall:
|
|
100
|
-
latest_message = messages[-1]
|
|
101
|
-
content = latest_message.get("content", "")
|
|
102
|
-
if content:
|
|
103
|
-
context = self._recall_context(str(content))
|
|
104
|
-
if context:
|
|
105
|
-
messages = self._inject_memory_context(messages, context)
|
|
106
|
-
|
|
107
|
-
reply = super().generate_reply(messages=messages, sender=sender, **kwargs)
|
|
108
|
-
|
|
109
|
-
if reply and self._auto_store and messages:
|
|
110
|
-
latest_content = messages[-1].get("content", "") if messages else ""
|
|
111
|
-
reply_text = reply if isinstance(reply, str) else reply.get("content", "")
|
|
112
|
-
if latest_content and reply_text:
|
|
113
|
-
self._store_turn(str(latest_content), str(reply_text))
|
|
114
|
-
|
|
115
|
-
return reply
|
|
116
|
-
|
|
117
|
-
def _recall_context(self, query: str) -> str:
|
|
118
|
-
"""Retrieve relevant context from CortexDB.
|
|
119
|
-
|
|
120
|
-
Args:
|
|
121
|
-
query: The query to search for relevant memories.
|
|
122
|
-
|
|
123
|
-
Returns:
|
|
124
|
-
The context string from CortexDB, or empty string on failure.
|
|
125
|
-
"""
|
|
126
|
-
try:
|
|
127
|
-
result = self._cortex_client.recall(
|
|
128
|
-
self._scope,
|
|
129
|
-
query=query,
|
|
130
|
-
)
|
|
131
|
-
except Exception:
|
|
132
|
-
return ""
|
|
133
|
-
|
|
134
|
-
return result.get("context_block", "") if result else ""
|
|
135
|
-
|
|
136
|
-
def _store_turn(self, user_message: str, assistant_reply: str) -> None:
|
|
137
|
-
"""Store a conversation turn in CortexDB.
|
|
138
|
-
|
|
139
|
-
Args:
|
|
140
|
-
user_message: The user's message.
|
|
141
|
-
assistant_reply: The agent's reply.
|
|
142
|
-
"""
|
|
143
|
-
content = f"User: {user_message}\nAssistant: {assistant_reply}"
|
|
144
|
-
try:
|
|
145
|
-
self._cortex_client.experience(self._scope, text=content)
|
|
146
|
-
except Exception:
|
|
147
|
-
pass
|
|
148
|
-
|
|
149
|
-
def _inject_memory_context(
|
|
150
|
-
self,
|
|
151
|
-
messages: list[dict[str, Any]],
|
|
152
|
-
context: str,
|
|
153
|
-
) -> list[dict[str, Any]]:
|
|
154
|
-
"""Inject retrieved memory context into the message list.
|
|
155
|
-
|
|
156
|
-
Prepends a system message containing the retrieved context
|
|
157
|
-
so the LLM can reference it when generating a response.
|
|
158
|
-
|
|
159
|
-
Args:
|
|
160
|
-
messages: The original conversation messages.
|
|
161
|
-
context: The retrieved context string from CortexDB.
|
|
162
|
-
|
|
163
|
-
Returns:
|
|
164
|
-
A new message list with the memory context injected.
|
|
165
|
-
"""
|
|
166
|
-
context_message = {
|
|
167
|
-
"role": "system",
|
|
168
|
-
"content": (
|
|
169
|
-
f"Relevant context from long-term memory:\n\n{context}\n\n"
|
|
170
|
-
"Use this context to inform your response if relevant."
|
|
171
|
-
),
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
augmented = list(messages)
|
|
175
|
-
augmented.insert(0, context_message)
|
|
176
|
-
return augmented
|
|
177
|
-
|
|
178
|
-
def clear_memory(self) -> dict[str, Any]:
|
|
179
|
-
"""Clear all memories for this agent's scope.
|
|
180
|
-
|
|
181
|
-
Removes all stored memories from CortexDB for the configured
|
|
182
|
-
scope path.
|
|
183
|
-
|
|
184
|
-
Returns:
|
|
185
|
-
The forget response dict from CortexDB.
|
|
186
|
-
"""
|
|
187
|
-
return self._cortex_client.forget(
|
|
188
|
-
self._scope,
|
|
189
|
-
confirm_all=True,
|
|
190
|
-
cascade="redact_events",
|
|
191
|
-
reason="AutoGen agent memory clear requested",
|
|
192
|
-
)
|
|
1
|
+
"""AutoGen agent with built-in CortexDB memory.
|
|
2
|
+
|
|
3
|
+
Provides a ConversableAgent subclass that automatically stores
|
|
4
|
+
conversation turns and retrieves relevant context from CortexDB
|
|
5
|
+
before generating responses.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from typing import Any, Optional, Union
|
|
11
|
+
|
|
12
|
+
from autogen import ConversableAgent
|
|
13
|
+
|
|
14
|
+
from cortexdb import Cortex
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class CortexDBAgent(ConversableAgent):
|
|
18
|
+
"""AutoGen ConversableAgent with automatic CortexDB memory integration.
|
|
19
|
+
|
|
20
|
+
Extends AutoGen's ConversableAgent to automatically store conversation
|
|
21
|
+
turns in CortexDB and retrieve relevant past context before generating
|
|
22
|
+
responses. This enables the agent to maintain long-term memory across
|
|
23
|
+
conversations and sessions.
|
|
24
|
+
|
|
25
|
+
The agent injects retrieved context as a system-level prefix to the
|
|
26
|
+
conversation, giving the LLM access to relevant memories without
|
|
27
|
+
modifying the visible chat history.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
name: The agent's name.
|
|
31
|
+
cortex_client: An initialized CortexDB client instance.
|
|
32
|
+
scope: The hierarchical scope path for memory isolation.
|
|
33
|
+
auto_store: Whether to automatically store conversation turns.
|
|
34
|
+
Defaults to ``True``.
|
|
35
|
+
auto_recall: Whether to automatically retrieve context before
|
|
36
|
+
responding. Defaults to ``True``.
|
|
37
|
+
**kwargs: Additional keyword arguments passed to ConversableAgent.
|
|
38
|
+
|
|
39
|
+
Example::
|
|
40
|
+
|
|
41
|
+
from cortexdb import Cortex
|
|
42
|
+
from cortexdb.integrations.autogen import CortexDBAgent
|
|
43
|
+
|
|
44
|
+
client = Cortex("http://localhost:3141")
|
|
45
|
+
agent = CortexDBAgent(
|
|
46
|
+
name="memory_assistant",
|
|
47
|
+
cortex_client=client,
|
|
48
|
+
scope="user:default",
|
|
49
|
+
llm_config=llm_config,
|
|
50
|
+
system_message="You are a helpful assistant with long-term memory.",
|
|
51
|
+
)
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
def __init__(
|
|
55
|
+
self,
|
|
56
|
+
name: str,
|
|
57
|
+
cortex_client: Cortex,
|
|
58
|
+
scope: str = "user:default",
|
|
59
|
+
auto_store: bool = True,
|
|
60
|
+
auto_recall: bool = True,
|
|
61
|
+
**kwargs: Any,
|
|
62
|
+
) -> None:
|
|
63
|
+
"""Initialize CortexDBAgent with memory capabilities.
|
|
64
|
+
|
|
65
|
+
Args:
|
|
66
|
+
name: The agent's name.
|
|
67
|
+
cortex_client: An initialized CortexDB client instance.
|
|
68
|
+
scope: The hierarchical scope path for memory isolation.
|
|
69
|
+
auto_store: Whether to automatically store conversation turns.
|
|
70
|
+
auto_recall: Whether to automatically retrieve context.
|
|
71
|
+
**kwargs: Additional keyword arguments passed to ConversableAgent.
|
|
72
|
+
"""
|
|
73
|
+
super().__init__(name=name, **kwargs)
|
|
74
|
+
self._cortex_client = cortex_client
|
|
75
|
+
self._scope = scope
|
|
76
|
+
self._auto_store = auto_store
|
|
77
|
+
self._auto_recall = auto_recall
|
|
78
|
+
|
|
79
|
+
def generate_reply(
|
|
80
|
+
self,
|
|
81
|
+
messages: Optional[list[dict[str, Any]]] = None,
|
|
82
|
+
sender: Optional[Any] = None,
|
|
83
|
+
**kwargs: Any,
|
|
84
|
+
) -> Union[str, dict[str, Any], None]:
|
|
85
|
+
"""Generate a reply with CortexDB memory augmentation.
|
|
86
|
+
|
|
87
|
+
Before generating a reply, retrieves relevant context from CortexDB
|
|
88
|
+
based on the latest message. After generating, stores the conversation
|
|
89
|
+
turn for future recall.
|
|
90
|
+
|
|
91
|
+
Args:
|
|
92
|
+
messages: The conversation messages to respond to.
|
|
93
|
+
sender: The agent that sent the message.
|
|
94
|
+
**kwargs: Additional keyword arguments.
|
|
95
|
+
|
|
96
|
+
Returns:
|
|
97
|
+
The generated reply string, a structured reply dict, or None.
|
|
98
|
+
"""
|
|
99
|
+
if messages and self._auto_recall:
|
|
100
|
+
latest_message = messages[-1]
|
|
101
|
+
content = latest_message.get("content", "")
|
|
102
|
+
if content:
|
|
103
|
+
context = self._recall_context(str(content))
|
|
104
|
+
if context:
|
|
105
|
+
messages = self._inject_memory_context(messages, context)
|
|
106
|
+
|
|
107
|
+
reply = super().generate_reply(messages=messages, sender=sender, **kwargs)
|
|
108
|
+
|
|
109
|
+
if reply and self._auto_store and messages:
|
|
110
|
+
latest_content = messages[-1].get("content", "") if messages else ""
|
|
111
|
+
reply_text = reply if isinstance(reply, str) else reply.get("content", "")
|
|
112
|
+
if latest_content and reply_text:
|
|
113
|
+
self._store_turn(str(latest_content), str(reply_text))
|
|
114
|
+
|
|
115
|
+
return reply
|
|
116
|
+
|
|
117
|
+
def _recall_context(self, query: str) -> str:
|
|
118
|
+
"""Retrieve relevant context from CortexDB.
|
|
119
|
+
|
|
120
|
+
Args:
|
|
121
|
+
query: The query to search for relevant memories.
|
|
122
|
+
|
|
123
|
+
Returns:
|
|
124
|
+
The context string from CortexDB, or empty string on failure.
|
|
125
|
+
"""
|
|
126
|
+
try:
|
|
127
|
+
result = self._cortex_client.recall(
|
|
128
|
+
self._scope,
|
|
129
|
+
query=query,
|
|
130
|
+
)
|
|
131
|
+
except Exception:
|
|
132
|
+
return ""
|
|
133
|
+
|
|
134
|
+
return result.get("context_block", "") if result else ""
|
|
135
|
+
|
|
136
|
+
def _store_turn(self, user_message: str, assistant_reply: str) -> None:
|
|
137
|
+
"""Store a conversation turn in CortexDB.
|
|
138
|
+
|
|
139
|
+
Args:
|
|
140
|
+
user_message: The user's message.
|
|
141
|
+
assistant_reply: The agent's reply.
|
|
142
|
+
"""
|
|
143
|
+
content = f"User: {user_message}\nAssistant: {assistant_reply}"
|
|
144
|
+
try:
|
|
145
|
+
self._cortex_client.experience(self._scope, text=content)
|
|
146
|
+
except Exception:
|
|
147
|
+
pass
|
|
148
|
+
|
|
149
|
+
def _inject_memory_context(
|
|
150
|
+
self,
|
|
151
|
+
messages: list[dict[str, Any]],
|
|
152
|
+
context: str,
|
|
153
|
+
) -> list[dict[str, Any]]:
|
|
154
|
+
"""Inject retrieved memory context into the message list.
|
|
155
|
+
|
|
156
|
+
Prepends a system message containing the retrieved context
|
|
157
|
+
so the LLM can reference it when generating a response.
|
|
158
|
+
|
|
159
|
+
Args:
|
|
160
|
+
messages: The original conversation messages.
|
|
161
|
+
context: The retrieved context string from CortexDB.
|
|
162
|
+
|
|
163
|
+
Returns:
|
|
164
|
+
A new message list with the memory context injected.
|
|
165
|
+
"""
|
|
166
|
+
context_message = {
|
|
167
|
+
"role": "system",
|
|
168
|
+
"content": (
|
|
169
|
+
f"Relevant context from long-term memory:\n\n{context}\n\n"
|
|
170
|
+
"Use this context to inform your response if relevant."
|
|
171
|
+
),
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
augmented = list(messages)
|
|
175
|
+
augmented.insert(0, context_message)
|
|
176
|
+
return augmented
|
|
177
|
+
|
|
178
|
+
def clear_memory(self) -> dict[str, Any]:
|
|
179
|
+
"""Clear all memories for this agent's scope.
|
|
180
|
+
|
|
181
|
+
Removes all stored memories from CortexDB for the configured
|
|
182
|
+
scope path.
|
|
183
|
+
|
|
184
|
+
Returns:
|
|
185
|
+
The forget response dict from CortexDB.
|
|
186
|
+
"""
|
|
187
|
+
return self._cortex_client.forget(
|
|
188
|
+
self._scope,
|
|
189
|
+
confirm_all=True,
|
|
190
|
+
cascade="redact_events",
|
|
191
|
+
reason="AutoGen agent memory clear requested",
|
|
192
|
+
)
|