cortexdb-ag2 0.1.0__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.
@@ -0,0 +1,21 @@
1
+ """CortexDB integration for AG2 (successor to AutoGen).
2
+
3
+ Provides a memory-augmented agent mixin and callable tools that connect
4
+ AG2's multi-agent framework to CortexDB's long-term memory system.
5
+ """
6
+
7
+ from cortexdb_ag2.agent import CortexDBAgent
8
+ from cortexdb_ag2.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
+ ]
cortexdb_ag2/agent.py ADDED
@@ -0,0 +1,183 @@
1
+ """AG2 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. Compatible with AG2 (the successor
6
+ to Microsoft AutoGen).
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ from typing import Any, Optional, Union
12
+
13
+ from autogen import ConversableAgent
14
+
15
+ from cortexdb import Cortex
16
+
17
+
18
+ class CortexDBAgent(ConversableAgent):
19
+ """AG2 ConversableAgent with automatic CortexDB memory integration.
20
+
21
+ Extends AG2's ConversableAgent to automatically store conversation
22
+ turns in CortexDB and retrieve relevant past context before generating
23
+ responses. This enables the agent to maintain long-term memory across
24
+ conversations and sessions.
25
+
26
+ The agent injects retrieved context as a system-level prefix to the
27
+ conversation, giving the LLM access to relevant memories without
28
+ modifying the visible chat history.
29
+
30
+ Args:
31
+ name: The agent's name.
32
+ cortex_client: An initialized CortexDB client instance.
33
+ scope: The hierarchical scope path for memory isolation.
34
+ auto_store: Whether to automatically store conversation turns.
35
+ Defaults to ``True``.
36
+ auto_recall: Whether to automatically retrieve context before
37
+ responding. Defaults to ``True``.
38
+ **kwargs: Additional keyword arguments passed to ConversableAgent.
39
+
40
+ Example::
41
+
42
+ from cortexdb import Cortex
43
+ from cortexdb_ag2 import CortexDBAgent
44
+
45
+ client = Cortex("http://localhost:3141")
46
+ agent = CortexDBAgent(
47
+ name="memory_assistant",
48
+ cortex_client=client,
49
+ scope="user:default",
50
+ llm_config=llm_config,
51
+ system_message="You are a helpful assistant with long-term memory.",
52
+ )
53
+ """
54
+
55
+ def __init__(
56
+ self,
57
+ name: str,
58
+ cortex_client: Cortex,
59
+ scope: str = "user:default",
60
+ auto_store: bool = True,
61
+ auto_recall: bool = True,
62
+ **kwargs: Any,
63
+ ) -> None:
64
+ super().__init__(name=name, **kwargs)
65
+ self._cortex_client = cortex_client
66
+ self._scope = scope
67
+ self._auto_store = auto_store
68
+ self._auto_recall = auto_recall
69
+
70
+ def generate_reply(
71
+ self,
72
+ messages: Optional[list[dict[str, Any]]] = None,
73
+ sender: Optional[Any] = None,
74
+ **kwargs: Any,
75
+ ) -> Union[str, dict[str, Any], None]:
76
+ """Generate a reply with CortexDB memory augmentation.
77
+
78
+ Before generating a reply, retrieves relevant context from CortexDB
79
+ based on the latest message. After generating, stores the conversation
80
+ turn for future recall.
81
+
82
+ Args:
83
+ messages: The conversation messages to respond to.
84
+ sender: The agent that sent the message.
85
+ **kwargs: Additional keyword arguments.
86
+
87
+ Returns:
88
+ The generated reply string, a structured reply dict, or None.
89
+ """
90
+ if messages and self._auto_recall:
91
+ latest_message = messages[-1]
92
+ content = latest_message.get("content", "")
93
+ if content:
94
+ context = self._recall_context(str(content))
95
+ if context:
96
+ messages = self._inject_memory_context(messages, context)
97
+
98
+ reply = super().generate_reply(messages=messages, sender=sender, **kwargs)
99
+
100
+ if reply and self._auto_store and messages:
101
+ latest_content = messages[-1].get("content", "") if messages else ""
102
+ reply_text = reply if isinstance(reply, str) else reply.get("content", "")
103
+ if latest_content and reply_text:
104
+ self._store_turn(str(latest_content), str(reply_text))
105
+
106
+ return reply
107
+
108
+ def _recall_context(self, query: str) -> str:
109
+ """Retrieve relevant context from CortexDB.
110
+
111
+ Args:
112
+ query: The query to search for relevant memories.
113
+
114
+ Returns:
115
+ The context string from CortexDB, or empty string on failure.
116
+ """
117
+ try:
118
+ result = self._cortex_client.recall(
119
+ self._scope,
120
+ query=query,
121
+ )
122
+ except Exception:
123
+ return ""
124
+
125
+ return result.get("context_block", "") if result else ""
126
+
127
+ def _store_turn(self, user_message: str, assistant_reply: str) -> None:
128
+ """Store a conversation turn in CortexDB.
129
+
130
+ Args:
131
+ user_message: The user's message.
132
+ assistant_reply: The agent's reply.
133
+ """
134
+ content = f"User: {user_message}\nAssistant: {assistant_reply}"
135
+ try:
136
+ self._cortex_client.experience(self._scope, text=content)
137
+ except Exception:
138
+ pass
139
+
140
+ def _inject_memory_context(
141
+ self,
142
+ messages: list[dict[str, Any]],
143
+ context: str,
144
+ ) -> list[dict[str, Any]]:
145
+ """Inject retrieved memory context into the message list.
146
+
147
+ Prepends a system message containing the retrieved context
148
+ so the LLM can reference it when generating a response.
149
+
150
+ Args:
151
+ messages: The original conversation messages.
152
+ context: The retrieved context string from CortexDB.
153
+
154
+ Returns:
155
+ A new message list with the memory context injected.
156
+ """
157
+ context_message = {
158
+ "role": "system",
159
+ "content": (
160
+ f"Relevant context from long-term memory:\n\n{context}\n\n"
161
+ "Use this context to inform your response if relevant."
162
+ ),
163
+ }
164
+
165
+ augmented = list(messages)
166
+ augmented.insert(0, context_message)
167
+ return augmented
168
+
169
+ def clear_memory(self) -> dict[str, Any]:
170
+ """Clear all memories for this agent's scope.
171
+
172
+ Removes all stored memories from CortexDB for the configured
173
+ scope path.
174
+
175
+ Returns:
176
+ The forget response dict from CortexDB.
177
+ """
178
+ return self._cortex_client.forget(
179
+ self._scope,
180
+ confirm_all=True,
181
+ cascade="redact_events",
182
+ reason="AG2 agent memory clear requested",
183
+ )
cortexdb_ag2/tools.py ADDED
@@ -0,0 +1,165 @@
1
+ """AG2 callable tools for interacting with CortexDB.
2
+
3
+ Provides factory functions that create CortexDB-backed callables
4
+ suitable for registration with AG2's ConversableAgent tool system.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ from typing import Annotated, Any, Callable, Optional
10
+
11
+ from cortexdb import Cortex
12
+
13
+
14
+ def cortexdb_search_fn(
15
+ client: Cortex,
16
+ scope: str = "user:default",
17
+ ) -> Callable[..., str]:
18
+ """Create a search function for use as an AG2 tool.
19
+
20
+ Returns a callable that performs semantic recall over CortexDB's
21
+ memory store, suitable for registration with an AG2 agent.
22
+
23
+ Args:
24
+ client: An initialized CortexDB client instance.
25
+ scope: The hierarchical scope path for memory isolation.
26
+
27
+ Returns:
28
+ A callable function that accepts a ``query`` parameter and
29
+ returns the recalled context block.
30
+
31
+ Example::
32
+
33
+ from cortexdb import Cortex
34
+ from cortexdb_ag2 import cortexdb_search_fn
35
+
36
+ client = Cortex("http://localhost:3141")
37
+ search = cortexdb_search_fn(client, scope="user:default")
38
+
39
+ agent.register_for_llm(description="Search memories")(search)
40
+ user_proxy.register_for_execution()(search)
41
+ """
42
+
43
+ def search(
44
+ query: Annotated[str, "The search query to find relevant memories."],
45
+ ) -> str:
46
+ """Search CortexDB for relevant memories and past context."""
47
+ result = client.recall(scope, query=query)
48
+
49
+ context = result.get("context_block", "") if result else ""
50
+ if not context:
51
+ return "No relevant memories found."
52
+
53
+ return context
54
+
55
+ search.__name__ = "cortexdb_search"
56
+ search.__doc__ = "Search CortexDB for relevant memories and past context."
57
+ return search
58
+
59
+
60
+ def cortexdb_store_fn(
61
+ client: Cortex,
62
+ scope: str = "user:default",
63
+ ) -> Callable[..., str]:
64
+ """Create a store function for use as an AG2 tool.
65
+
66
+ Returns a callable that persists information into CortexDB's
67
+ long-term memory, suitable for registration with an AG2 agent.
68
+
69
+ Args:
70
+ client: An initialized CortexDB client instance.
71
+ scope: The hierarchical scope path for memory isolation.
72
+
73
+ Returns:
74
+ A callable function that accepts a ``content`` parameter and
75
+ returns a confirmation message.
76
+ """
77
+
78
+ def store(
79
+ content: Annotated[str, "The content to store as a memory."],
80
+ ) -> str:
81
+ """Store information in CortexDB's long-term memory."""
82
+ client.experience(scope, text=content)
83
+ return "Memory stored successfully in CortexDB."
84
+
85
+ store.__name__ = "cortexdb_store"
86
+ store.__doc__ = "Store information in CortexDB's long-term memory."
87
+ return store
88
+
89
+
90
+ def cortexdb_forget_fn(
91
+ client: Cortex,
92
+ scope: str = "user:default",
93
+ ) -> Callable[..., str]:
94
+ """Create a forget function for use as an AG2 tool.
95
+
96
+ Returns a callable that removes memories from CortexDB, suitable
97
+ for registration with an AG2 agent.
98
+
99
+ Args:
100
+ client: An initialized CortexDB client instance.
101
+ scope: The hierarchical scope path for memory isolation.
102
+
103
+ Returns:
104
+ A callable function that accepts a ``reason`` parameter
105
+ and returns a confirmation message.
106
+ """
107
+
108
+ def forget(
109
+ reason: Annotated[str, "The reason for forgetting these memories."],
110
+ ) -> str:
111
+ """Forget or remove memories from CortexDB."""
112
+ client.forget(
113
+ scope,
114
+ confirm_all=True,
115
+ cascade="redact_events",
116
+ reason=reason,
117
+ )
118
+ return f"Memories in scope '{scope}' have been forgotten."
119
+
120
+ forget.__name__ = "cortexdb_forget"
121
+ forget.__doc__ = "Forget or remove memories from CortexDB."
122
+ return forget
123
+
124
+
125
+ def register_cortexdb_tools(
126
+ agent: Any,
127
+ executor: Any,
128
+ client: Cortex,
129
+ scope: str = "user:default",
130
+ ) -> None:
131
+ """Register all CortexDB tools with an AG2 agent and executor.
132
+
133
+ Convenience function that creates and registers search, store, and
134
+ forget tools on both the LLM-facing agent and the execution proxy.
135
+
136
+ Args:
137
+ agent: The AG2 ConversableAgent that will call the tools.
138
+ executor: The AG2 agent (typically UserProxyAgent) that
139
+ executes tool calls.
140
+ client: An initialized CortexDB client instance.
141
+ scope: The hierarchical scope path for memory isolation.
142
+
143
+ Example::
144
+
145
+ from ag2 import ConversableAgent, UserProxyAgent
146
+ from cortexdb import Cortex
147
+ from cortexdb_ag2 import register_cortexdb_tools
148
+
149
+ client = Cortex("http://localhost:3141")
150
+ assistant = ConversableAgent("assistant", llm_config=llm_config)
151
+ user_proxy = UserProxyAgent("user_proxy")
152
+
153
+ register_cortexdb_tools(assistant, user_proxy, client, scope="user:default")
154
+ """
155
+ search = cortexdb_search_fn(client, scope)
156
+ store = cortexdb_store_fn(client, scope)
157
+ forget = cortexdb_forget_fn(client, scope)
158
+
159
+ for fn, desc in [
160
+ (search, "Search CortexDB for relevant memories and past context."),
161
+ (store, "Store information in CortexDB's long-term memory."),
162
+ (forget, "Forget or remove memories from CortexDB."),
163
+ ]:
164
+ agent.register_for_llm(description=desc)(fn)
165
+ executor.register_for_execution()(fn)
@@ -0,0 +1,31 @@
1
+ Metadata-Version: 2.4
2
+ Name: cortexdb-ag2
3
+ Version: 0.1.0
4
+ Summary: AG2 integration for CortexDB — long-term memory for AI agent conversations
5
+ License-Expression: Apache-2.0
6
+ Requires-Python: >=3.10
7
+ Requires-Dist: ag2>=0.4
8
+ Requires-Dist: cortexdbai>=0.1.0
9
+ Provides-Extra: dev
10
+ Requires-Dist: pytest>=7.0; extra == 'dev'
11
+ Description-Content-Type: text/markdown
12
+
13
+ # cortexdb-ag2
14
+
15
+ AG2 (formerly AutoGen) integration for CortexDB long-term memory.
16
+
17
+ > **LLM provider note (audit BLK-2):** the canonical example shows GPT-4o
18
+ > in the AG2 LLM config. CortexDB itself is LLM-agnostic. Swap providers
19
+ > through AG2's standard `llm_config` — Anthropic, Gemini, Mistral, and
20
+ > Together AI all work. Nothing in `cortexdb-ag2` reads `OPENAI_API_KEY`.
21
+
22
+ ## Install
23
+
24
+ ```bash
25
+ pip install cortexdb-ag2
26
+ ```
27
+
28
+ ## API base URL
29
+
30
+ Defaults to `https://api-v1.cortexdb.ai` (audit FRI-8). Override with
31
+ `CORTEXDB_API_URL`.
@@ -0,0 +1,6 @@
1
+ cortexdb_ag2/__init__.py,sha256=MZgDfn2RcGWU-3XsHDy3_ZuwCUhQa-JLlu2AcxUkrL8,546
2
+ cortexdb_ag2/agent.py,sha256=eWikM55fMJAOUcHq3KMDz7_wQvf7aVIZEA9F9BEAAGo,6344
3
+ cortexdb_ag2/tools.py,sha256=6dfZZkWs1YG3ujJNKgRudMBOSTwBzB4btADzTN4Q-_E,5440
4
+ cortexdb_ag2-0.1.0.dist-info/METADATA,sha256=Gln6iOUf5QyCgb8FLv9V9ySZ94U3GHANmKqA6DXX6EA,894
5
+ cortexdb_ag2-0.1.0.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
6
+ cortexdb_ag2-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.30.1
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any