cortexdbai 0.3.1__tar.gz → 0.4.0__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.3.1 → cortexdbai-0.4.0}/PKG-INFO +72 -1
- {cortexdbai-0.3.1 → cortexdbai-0.4.0}/cortexdb/__init__.py +1 -1
- cortexdbai-0.4.0/cortexdb/integrations/autogen/__init__.py +21 -0
- cortexdbai-0.4.0/cortexdb/integrations/autogen/agent.py +192 -0
- cortexdbai-0.4.0/cortexdb/integrations/autogen/tools.py +165 -0
- cortexdbai-0.4.0/cortexdb/integrations/beeai/__init__.py +15 -0
- cortexdbai-0.4.0/cortexdb/integrations/beeai/memory.py +142 -0
- cortexdbai-0.4.0/cortexdb/integrations/beeai/tools.py +189 -0
- cortexdbai-0.4.0/cortexdb/integrations/camel/__init__.py +21 -0
- cortexdbai-0.4.0/cortexdb/integrations/camel/memory.py +165 -0
- cortexdbai-0.4.0/cortexdb/integrations/camel/tools.py +160 -0
- cortexdbai-0.4.0/cortexdb/integrations/controlflow/__init__.py +12 -0
- cortexdbai-0.4.0/cortexdb/integrations/controlflow/memory.py +266 -0
- cortexdbai-0.4.0/cortexdb/integrations/crewai/__init__.py +14 -0
- cortexdbai-0.4.0/cortexdb/integrations/crewai/memory.py +83 -0
- cortexdbai-0.4.0/cortexdb/integrations/crewai/tools.py +80 -0
- cortexdbai-0.4.0/cortexdb/integrations/deepinfra/__init__.py +13 -0
- cortexdbai-0.4.0/cortexdb/integrations/deepinfra/config.py +73 -0
- cortexdbai-0.4.0/cortexdb/integrations/deepinfra/embeddings.py +36 -0
- cortexdbai-0.4.0/cortexdb/integrations/deepinfra/openai_compat.py +208 -0
- cortexdbai-0.4.0/cortexdb/integrations/dify/__init__.py +12 -0
- cortexdbai-0.4.0/cortexdb/integrations/dify/manifest.yaml +83 -0
- cortexdbai-0.4.0/cortexdb/integrations/dify/tool.py +229 -0
- cortexdbai-0.4.0/cortexdb/integrations/dspy/__init__.py +13 -0
- cortexdbai-0.4.0/cortexdb/integrations/dspy/memory.py +121 -0
- cortexdbai-0.4.0/cortexdb/integrations/dspy/retriever.py +111 -0
- cortexdbai-0.4.0/cortexdb/integrations/fireworks/__init__.py +13 -0
- cortexdbai-0.4.0/cortexdb/integrations/fireworks/config.py +68 -0
- cortexdbai-0.4.0/cortexdb/integrations/fireworks/embeddings.py +36 -0
- cortexdbai-0.4.0/cortexdb/integrations/fireworks/openai_compat.py +208 -0
- cortexdbai-0.4.0/cortexdb/integrations/flowise/__init__.py +12 -0
- cortexdbai-0.4.0/cortexdb/integrations/flowise/node.py +295 -0
- cortexdbai-0.4.0/cortexdb/integrations/google_adk/__init__.py +20 -0
- cortexdbai-0.4.0/cortexdb/integrations/google_adk/tools.py +198 -0
- cortexdbai-0.4.0/cortexdb/integrations/groq/__init__.py +13 -0
- cortexdbai-0.4.0/cortexdb/integrations/groq/config.py +63 -0
- cortexdbai-0.4.0/cortexdb/integrations/groq/embeddings.py +55 -0
- cortexdbai-0.4.0/cortexdb/integrations/groq/openai_compat.py +208 -0
- cortexdbai-0.4.0/cortexdb/integrations/hermes/__init__.py +5 -0
- cortexdbai-0.4.0/cortexdb/integrations/hermes/provider.py +272 -0
- cortexdbai-0.4.0/cortexdb/integrations/instructor/__init__.py +17 -0
- cortexdbai-0.4.0/cortexdb/integrations/instructor/memory.py +230 -0
- cortexdbai-0.4.0/cortexdb/integrations/langgraph/__init__.py +12 -0
- cortexdbai-0.4.0/cortexdb/integrations/langgraph/memory.py +333 -0
- cortexdbai-0.4.0/cortexdb/integrations/letta/__init__.py +12 -0
- cortexdbai-0.4.0/cortexdb/integrations/letta/storage.py +308 -0
- cortexdbai-0.4.0/cortexdb/integrations/llamaindex/__init__.py +12 -0
- cortexdbai-0.4.0/cortexdb/integrations/llamaindex/memory.py +260 -0
- cortexdbai-0.4.0/cortexdb/integrations/nemo_guardrails/__init__.py +24 -0
- cortexdbai-0.4.0/cortexdb/integrations/nemo_guardrails/actions.py +216 -0
- cortexdbai-0.4.0/cortexdb/integrations/nemo_guardrails/knowledge_base.py +132 -0
- cortexdbai-0.4.0/cortexdb/integrations/ollama/__init__.py +14 -0
- cortexdbai-0.4.0/cortexdb/integrations/ollama/config.py +44 -0
- cortexdbai-0.4.0/cortexdb/integrations/ollama/embeddings.py +126 -0
- cortexdbai-0.4.0/cortexdb/integrations/openai_agents/__init__.py +19 -0
- cortexdbai-0.4.0/cortexdb/integrations/openai_agents/tools.py +183 -0
- cortexdbai-0.4.0/cortexdb/integrations/prefect/__init__.py +21 -0
- cortexdbai-0.4.0/cortexdb/integrations/prefect/blocks.py +88 -0
- cortexdbai-0.4.0/cortexdb/integrations/prefect/tasks.py +143 -0
- cortexdbai-0.4.0/cortexdb/integrations/pydanticai/__init__.py +21 -0
- cortexdbai-0.4.0/cortexdb/integrations/pydanticai/dependencies.py +42 -0
- cortexdbai-0.4.0/cortexdb/integrations/pydanticai/tools.py +100 -0
- cortexdbai-0.4.0/cortexdb/integrations/smolagents/__init__.py +19 -0
- cortexdbai-0.4.0/cortexdb/integrations/smolagents/tools.py +249 -0
- cortexdbai-0.4.0/cortexdb/integrations/temporal/__init__.py +21 -0
- cortexdbai-0.4.0/cortexdb/integrations/temporal/activities.py +239 -0
- cortexdbai-0.4.0/cortexdb/integrations/temporal/interceptor.py +207 -0
- cortexdbai-0.4.0/cortexdb/integrations/together/__init__.py +13 -0
- cortexdbai-0.4.0/cortexdb/integrations/together/config.py +75 -0
- cortexdbai-0.4.0/cortexdb/integrations/together/embeddings.py +36 -0
- cortexdbai-0.4.0/cortexdb/integrations/together/openai_compat.py +208 -0
- cortexdbai-0.4.0/cortexdb/integrations/vllm/__init__.py +13 -0
- cortexdbai-0.4.0/cortexdb/integrations/vllm/config.py +59 -0
- cortexdbai-0.4.0/cortexdb/integrations/vllm/embeddings.py +37 -0
- cortexdbai-0.4.0/cortexdb/integrations/vllm/openai_compat.py +208 -0
- {cortexdbai-0.3.1 → cortexdbai-0.4.0}/cortexdb/v1/client.py +29 -3
- {cortexdbai-0.3.1 → cortexdbai-0.4.0}/pyproject.toml +104 -1
- cortexdbai-0.4.0/tests/__init__.py +0 -0
- {cortexdbai-0.3.1 → cortexdbai-0.4.0}/.gitignore +0 -0
- {cortexdbai-0.3.1 → cortexdbai-0.4.0}/README.md +0 -0
- {cortexdbai-0.3.1/tests → cortexdbai-0.4.0/cortexdb/integrations}/__init__.py +0 -0
- {cortexdbai-0.3.1 → cortexdbai-0.4.0}/cortexdb/py.typed +0 -0
- {cortexdbai-0.3.1 → cortexdbai-0.4.0}/cortexdb/v1/__init__.py +0 -0
- {cortexdbai-0.3.1 → cortexdbai-0.4.0}/cortexdb/v1/exceptions.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: cortexdbai
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: The Long-Term Memory Layer for AI Systems
|
|
5
5
|
Project-URL: Homepage, https://cortexdb.ai
|
|
6
6
|
Project-URL: Documentation, https://docs.cortexdb.ai
|
|
@@ -25,12 +25,83 @@ Requires-Dist: httpx>=0.24
|
|
|
25
25
|
Requires-Dist: pydantic>=2.0
|
|
26
26
|
Requires-Dist: requests>=2.31
|
|
27
27
|
Requires-Dist: tenacity>=8.0
|
|
28
|
+
Provides-Extra: all-integrations
|
|
29
|
+
Requires-Dist: ag2>=0.4; extra == 'all-integrations'
|
|
30
|
+
Requires-Dist: beeai-framework>=0.1; extra == 'all-integrations'
|
|
31
|
+
Requires-Dist: camel-ai>=0.2; extra == 'all-integrations'
|
|
32
|
+
Requires-Dist: controlflow>=0.11; extra == 'all-integrations'
|
|
33
|
+
Requires-Dist: crewai>=0.80; extra == 'all-integrations'
|
|
34
|
+
Requires-Dist: dspy>=2.0; extra == 'all-integrations'
|
|
35
|
+
Requires-Dist: google-adk>=0.1; extra == 'all-integrations'
|
|
36
|
+
Requires-Dist: httpx>=0.27; extra == 'all-integrations'
|
|
37
|
+
Requires-Dist: instructor>=1.0; extra == 'all-integrations'
|
|
38
|
+
Requires-Dist: langchain-core>=0.3; extra == 'all-integrations'
|
|
39
|
+
Requires-Dist: langgraph-checkpoint>=2.0; extra == 'all-integrations'
|
|
40
|
+
Requires-Dist: letta>=0.1; extra == 'all-integrations'
|
|
41
|
+
Requires-Dist: llama-index-core>=0.11; extra == 'all-integrations'
|
|
42
|
+
Requires-Dist: nemoguardrails>=0.9; extra == 'all-integrations'
|
|
43
|
+
Requires-Dist: openai-agents>=0.1; extra == 'all-integrations'
|
|
44
|
+
Requires-Dist: prefect>=3.0; extra == 'all-integrations'
|
|
45
|
+
Requires-Dist: pydantic-ai>=0.1; extra == 'all-integrations'
|
|
46
|
+
Requires-Dist: smolagents>=1.0; extra == 'all-integrations'
|
|
47
|
+
Requires-Dist: temporalio>=1.7; extra == 'all-integrations'
|
|
28
48
|
Provides-Extra: async
|
|
29
49
|
Requires-Dist: httpx[http2]; extra == 'async'
|
|
50
|
+
Provides-Extra: autogen
|
|
51
|
+
Requires-Dist: ag2>=0.4; extra == 'autogen'
|
|
52
|
+
Provides-Extra: beeai
|
|
53
|
+
Requires-Dist: beeai-framework>=0.1; extra == 'beeai'
|
|
54
|
+
Provides-Extra: camel
|
|
55
|
+
Requires-Dist: camel-ai>=0.2; extra == 'camel'
|
|
56
|
+
Provides-Extra: controlflow
|
|
57
|
+
Requires-Dist: controlflow>=0.11; extra == 'controlflow'
|
|
58
|
+
Provides-Extra: crewai
|
|
59
|
+
Requires-Dist: crewai>=0.80; extra == 'crewai'
|
|
60
|
+
Provides-Extra: deepinfra
|
|
61
|
+
Requires-Dist: httpx>=0.27; extra == 'deepinfra'
|
|
30
62
|
Provides-Extra: dev
|
|
31
63
|
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
|
|
32
64
|
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
33
65
|
Requires-Dist: ruff>=0.4; extra == 'dev'
|
|
66
|
+
Provides-Extra: dify
|
|
67
|
+
Provides-Extra: dspy
|
|
68
|
+
Requires-Dist: dspy>=2.0; extra == 'dspy'
|
|
69
|
+
Provides-Extra: fireworks
|
|
70
|
+
Requires-Dist: httpx>=0.27; extra == 'fireworks'
|
|
71
|
+
Provides-Extra: flowise
|
|
72
|
+
Provides-Extra: google-adk
|
|
73
|
+
Requires-Dist: google-adk>=0.1; extra == 'google-adk'
|
|
74
|
+
Provides-Extra: groq
|
|
75
|
+
Requires-Dist: httpx>=0.27; extra == 'groq'
|
|
76
|
+
Provides-Extra: hermes
|
|
77
|
+
Provides-Extra: instructor
|
|
78
|
+
Requires-Dist: instructor>=1.0; extra == 'instructor'
|
|
79
|
+
Requires-Dist: pydantic>=2.0; extra == 'instructor'
|
|
80
|
+
Provides-Extra: langgraph
|
|
81
|
+
Requires-Dist: langchain-core>=0.3; extra == 'langgraph'
|
|
82
|
+
Requires-Dist: langgraph-checkpoint>=2.0; extra == 'langgraph'
|
|
83
|
+
Provides-Extra: letta
|
|
84
|
+
Requires-Dist: letta>=0.1; extra == 'letta'
|
|
85
|
+
Provides-Extra: llamaindex
|
|
86
|
+
Requires-Dist: llama-index-core>=0.11; extra == 'llamaindex'
|
|
87
|
+
Provides-Extra: nemo-guardrails
|
|
88
|
+
Requires-Dist: nemoguardrails>=0.9; extra == 'nemo-guardrails'
|
|
89
|
+
Provides-Extra: ollama
|
|
90
|
+
Requires-Dist: httpx>=0.27; extra == 'ollama'
|
|
91
|
+
Provides-Extra: openai-agents
|
|
92
|
+
Requires-Dist: openai-agents>=0.1; extra == 'openai-agents'
|
|
93
|
+
Provides-Extra: prefect
|
|
94
|
+
Requires-Dist: prefect>=3.0; extra == 'prefect'
|
|
95
|
+
Provides-Extra: pydanticai
|
|
96
|
+
Requires-Dist: pydantic-ai>=0.1; extra == 'pydanticai'
|
|
97
|
+
Provides-Extra: smolagents
|
|
98
|
+
Requires-Dist: smolagents>=1.0; extra == 'smolagents'
|
|
99
|
+
Provides-Extra: temporal
|
|
100
|
+
Requires-Dist: temporalio>=1.7; extra == 'temporal'
|
|
101
|
+
Provides-Extra: together
|
|
102
|
+
Requires-Dist: httpx>=0.27; extra == 'together'
|
|
103
|
+
Provides-Extra: vllm
|
|
104
|
+
Requires-Dist: httpx>=0.27; extra == 'vllm'
|
|
34
105
|
Description-Content-Type: text/markdown
|
|
35
106
|
|
|
36
107
|
# CortexDB Python SDK
|
|
@@ -0,0 +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
|
+
]
|
|
@@ -0,0 +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
|
+
)
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"""AutoGen callable tools for interacting with CortexDB.
|
|
2
|
+
|
|
3
|
+
Provides factory functions that create CortexDB-backed callables
|
|
4
|
+
suitable for registration with AutoGen's ConversableAgent tool system.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from typing import Annotated, Any, Callable
|
|
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 AutoGen tool.
|
|
19
|
+
|
|
20
|
+
Returns a callable that performs semantic recall over CortexDB's
|
|
21
|
+
memory store, suitable for registration with an AutoGen 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.integrations.autogen 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 AutoGen tool.
|
|
65
|
+
|
|
66
|
+
Returns a callable that persists information into CortexDB's
|
|
67
|
+
long-term memory, suitable for registration with an AutoGen 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 AutoGen tool.
|
|
95
|
+
|
|
96
|
+
Returns a callable that removes memories from CortexDB, suitable
|
|
97
|
+
for registration with an AutoGen 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 AutoGen 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 AutoGen ConversableAgent that will call the tools.
|
|
138
|
+
executor: The AutoGen 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 autogen import ConversableAgent, UserProxyAgent
|
|
146
|
+
from cortexdb import Cortex
|
|
147
|
+
from cortexdb.integrations.autogen 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,15 @@
|
|
|
1
|
+
"""CortexDB integration for BeeAI (IBM / Linux Foundation).
|
|
2
|
+
|
|
3
|
+
Provides a memory backend and tool implementations that connect
|
|
4
|
+
BeeAI's agent framework to CortexDB's long-term memory system.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from cortexdb.integrations.beeai.memory import CortexDBMemory
|
|
8
|
+
from cortexdb.integrations.beeai.tools import CortexDBForgetTool, CortexDBSearchTool, CortexDBStoreTool
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
"CortexDBMemory",
|
|
12
|
+
"CortexDBSearchTool",
|
|
13
|
+
"CortexDBStoreTool",
|
|
14
|
+
"CortexDBForgetTool",
|
|
15
|
+
]
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"""BeeAI memory backend powered by CortexDB.
|
|
2
|
+
|
|
3
|
+
Provides a BaseMemory-compatible implementation that allows BeeAI agents
|
|
4
|
+
to persist and retrieve long-term memories through CortexDB's distributed
|
|
5
|
+
memory system.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from typing import Any, Optional
|
|
11
|
+
|
|
12
|
+
from cortexdb import Cortex
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class CortexDBMemory:
|
|
16
|
+
"""BeeAI memory backend that stores and retrieves context via CortexDB.
|
|
17
|
+
|
|
18
|
+
Implements a memory interface compatible with BeeAI's agent memory
|
|
19
|
+
system, backed by CortexDB's semantic storage. This enables BeeAI
|
|
20
|
+
agents to maintain persistent long-term memory across sessions,
|
|
21
|
+
with semantic retrieval of relevant past context.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
client: An initialized CortexDB client instance.
|
|
25
|
+
scope: The hierarchical scope path for memory isolation.
|
|
26
|
+
|
|
27
|
+
Example::
|
|
28
|
+
|
|
29
|
+
from cortexdb import Cortex
|
|
30
|
+
from cortexdb.integrations.beeai import CortexDBMemory
|
|
31
|
+
|
|
32
|
+
client = Cortex("http://localhost:3141")
|
|
33
|
+
memory = CortexDBMemory(
|
|
34
|
+
client=client,
|
|
35
|
+
scope="user:default",
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
# Add a memory
|
|
39
|
+
memory.add("The deployment cadence is every two weeks.")
|
|
40
|
+
|
|
41
|
+
# Retrieve relevant memories
|
|
42
|
+
context = memory.recall("deployment schedule")
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
def __init__(
|
|
46
|
+
self,
|
|
47
|
+
client: Cortex,
|
|
48
|
+
scope: str = "user:default",
|
|
49
|
+
) -> None:
|
|
50
|
+
self._client = client
|
|
51
|
+
self._scope = scope
|
|
52
|
+
|
|
53
|
+
def add(self, content: str, metadata: Optional[dict[str, Any]] = None) -> dict[str, Any]:
|
|
54
|
+
"""Store a memory in CortexDB.
|
|
55
|
+
|
|
56
|
+
Args:
|
|
57
|
+
content: The text content to store.
|
|
58
|
+
metadata: Optional metadata to associate with the memory.
|
|
59
|
+
|
|
60
|
+
Returns:
|
|
61
|
+
The experience response dict from CortexDB.
|
|
62
|
+
"""
|
|
63
|
+
return self._client.experience(self._scope, text=content)
|
|
64
|
+
|
|
65
|
+
def search(self, query: str) -> list[dict[str, Any]]:
|
|
66
|
+
"""Search for relevant memories in CortexDB.
|
|
67
|
+
|
|
68
|
+
Performs a semantic search over stored memories, returning
|
|
69
|
+
results ranked by relevance to the query.
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
query: The search query string.
|
|
73
|
+
|
|
74
|
+
Returns:
|
|
75
|
+
A list containing a single result dictionary with a
|
|
76
|
+
``"content"`` key, or an empty list if no context was found.
|
|
77
|
+
"""
|
|
78
|
+
result = self._client.recall(self._scope, query=query)
|
|
79
|
+
|
|
80
|
+
context = result.get("context_block", "") if result else ""
|
|
81
|
+
if context:
|
|
82
|
+
return [{"content": context}]
|
|
83
|
+
return []
|
|
84
|
+
|
|
85
|
+
def recall(self, query: str) -> str:
|
|
86
|
+
"""Retrieve relevant context as a formatted string.
|
|
87
|
+
|
|
88
|
+
Convenience method that performs a recall and returns the
|
|
89
|
+
context string suitable for injection into an agent's prompt.
|
|
90
|
+
|
|
91
|
+
Args:
|
|
92
|
+
query: The query to search for relevant memories.
|
|
93
|
+
|
|
94
|
+
Returns:
|
|
95
|
+
A string containing relevant memories, or an empty
|
|
96
|
+
string if no relevant memories are found.
|
|
97
|
+
"""
|
|
98
|
+
result = self._client.recall(self._scope, query=query)
|
|
99
|
+
return result.get("context_block", "") if result else ""
|
|
100
|
+
|
|
101
|
+
def load(self, query: str) -> dict[str, Any]:
|
|
102
|
+
"""Load memory variables for the current query.
|
|
103
|
+
|
|
104
|
+
Returns a dictionary of memory variables that can be injected
|
|
105
|
+
into an agent's context. Compatible with BeeAI's memory loading
|
|
106
|
+
pattern.
|
|
107
|
+
|
|
108
|
+
Args:
|
|
109
|
+
query: The current query or input text.
|
|
110
|
+
|
|
111
|
+
Returns:
|
|
112
|
+
A dictionary with a ``"context"`` key containing the
|
|
113
|
+
retrieved memory text.
|
|
114
|
+
"""
|
|
115
|
+
context = self.recall(query)
|
|
116
|
+
return {"context": context}
|
|
117
|
+
|
|
118
|
+
def save(self, input_text: str, output_text: str) -> None:
|
|
119
|
+
"""Save a conversation turn to memory.
|
|
120
|
+
|
|
121
|
+
Stores both the input and output as a single memory entry
|
|
122
|
+
for future retrieval.
|
|
123
|
+
|
|
124
|
+
Args:
|
|
125
|
+
input_text: The user's input or query.
|
|
126
|
+
output_text: The agent's response.
|
|
127
|
+
"""
|
|
128
|
+
content = f"User: {input_text}\nAssistant: {output_text}"
|
|
129
|
+
self.add(content)
|
|
130
|
+
|
|
131
|
+
def clear(self) -> dict[str, Any]:
|
|
132
|
+
"""Clear all memories for the current scope.
|
|
133
|
+
|
|
134
|
+
Removes all stored memories from CortexDB for the configured
|
|
135
|
+
scope. Returns the forget response dict.
|
|
136
|
+
"""
|
|
137
|
+
return self._client.forget(
|
|
138
|
+
self._scope,
|
|
139
|
+
confirm_all=True,
|
|
140
|
+
cascade="redact_events",
|
|
141
|
+
reason="BeeAI memory clear requested",
|
|
142
|
+
)
|