VertexAI Memory integration for Autogen agents
Project description
autogen-vertexai-memory
VertexAI Memory integration for Autogen agents. This package provides a Memory implementation that enables Autogen agents to store and retrieve memories using Google Cloud's VertexAI Memory service.
Features
- 🧠 Persistent Memory: Store agent memories in VertexAI for long-term retention
- 🔍 Semantic Search: Retrieve relevant memories using similarity search
- 🔄 Context Management: Automatically update chat context with relevant memories
- ⚡ Async Support: Built with async/await for efficient operations
- 🎯 User Scoping: Isolate memories per user for multi-tenant applications
Installation
pip install autogen-vertexai-memory
Or with poetry:
poetry add autogen-vertexai-memory
Prerequisites
- Google Cloud Project: You need a GCP project with VertexAI API enabled
- Authentication: Set up authentication using one of these methods:
- Application Default Credentials (recommended for local development)
- Service Account Key
- Workload Identity (for GKE)
# Set up Application Default Credentials
gcloud auth application-default login
- VertexAI Memory Resource: Create a memory resource in your GCP project
Quick Start
from autogen_vertexai_memory import VertexaiMemory
from autogen_core.memory import MemoryContent, MemoryMimeType
# Initialize the memory
memory = VertexaiMemory(
api_resource_name="projects/YOUR_PROJECT/locations/us-central1/memories/YOUR_MEMORY",
project_id="YOUR_PROJECT_ID",
location="us-central1"
)
# Add a memory
await memory.add(
content=MemoryContent(
content="The user prefers Python over JavaScript",
mime_type=MemoryMimeType.TEXT
),
user_id="user123"
)
# Query memories
results = await memory.query(
query="What programming language does the user prefer?",
user_id="user123"
)
for result in results.results:
print(result.content)
API Reference
VertexaiMemory
Constructor
VertexaiMemory(
api_resource_name: str,
project_id: str,
location: str,
client: Client | None = None
)
Parameters:
api_resource_name: Full resource name of the VertexAI memoryproject_id: Google Cloud project IDlocation: GCP region (e.g., "us-central1")client: Optional pre-configured VertexAI client
Methods
add(content, user_id, cancellation_token=None)
Add a new memory.
await memory.add(
content=MemoryContent(content="Important fact", mime_type=MemoryMimeType.TEXT),
user_id="user123"
)
query(query="", user_id="", cancellation_token=None, **kwargs)
Query memories. If query is empty, returns all memories for the user.
# Search with query
results = await memory.query(query="user preferences", user_id="user123")
# Get all memories
all_memories = await memory.query(user_id="user123")
update_context(model_context)
Update chat context with relevant memories.
from autogen_core.model_context import ChatCompletionContext
context = ChatCompletionContext()
await memory.update_context(context)
clear()
Clear all memories (deletes the resource).
await memory.clear()
close()
Cleanup resources.
await memory.close()
Advanced Usage
Custom Client Configuration
from vertexai import Client
# Create custom client with specific settings
client = Client(
project="my-project",
location="us-central1",
# Additional client configurations...
)
# Use with VertexaiMemory
memory = VertexaiMemory(
api_resource_name="...",
project_id="my-project",
location="us-central1",
client=client
)
Batch Operations
# Add multiple memories
memories = [
("User likes cats", "user123"),
("User prefers dark mode", "user123"),
("User is from New York", "user123")
]
for content, user_id in memories:
await memory.add(
content=MemoryContent(content=content, mime_type=MemoryMimeType.TEXT),
user_id=user_id
)
Error Handling
from google.api_core.exceptions import GoogleAPIError
try:
await memory.add(content=..., user_id="user123")
except GoogleAPIError as e:
print(f"Error adding memory: {e}")
Development
Setup Development Environment
# Clone the repository
git clone https://github.com/yourusername/autogen-vertexai-memory.git
cd autogen-vertexai-memory
# Install dependencies
poetry install
# Run tests
poetry run pytest
Running Tests
# Run all tests
poetry run pytest tests/ -v
# Run with coverage
poetry run pytest tests/ --cov=autogen_vertexai_memory --cov-report=html
# Run specific test
poetry run pytest tests/test_vertexai_memory.py::TestAdd -v
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Built for use with Autogen
- Powered by Google Cloud VertexAI
Support
- 📫 Report issues on GitHub Issues
- 💬 Discuss on GitHub Discussions
- 📖 Read the documentation
Changelog
See CHANGELOG.md for a list of changes.
Made with ❤️ for the Autogen community
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file autogen_vertexai_memory-0.1.2.tar.gz.
File metadata
- Download URL: autogen_vertexai_memory-0.1.2.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.3 Linux/6.6.87.2-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a00bfc9d8671c52409eb03b1bf0d260647ddc2893cda3a612b4797b44f520d1f
|
|
| MD5 |
98827ec0f1e4d1d93f398d3a4cd91316
|
|
| BLAKE2b-256 |
ac8804e0be10f07ac5a717d57aed7046956c0cee3bd04b89f3739cee9eb4c6f8
|
File details
Details for the file autogen_vertexai_memory-0.1.2-py3-none-any.whl.
File metadata
- Download URL: autogen_vertexai_memory-0.1.2-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.3 Linux/6.6.87.2-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
73c7132cbd98306f8c091c609e9a6ee5a0b7f871a3361f081dcf7abd615618b8
|
|
| MD5 |
f50364434bcbda53e040eb040d398f73
|
|
| BLAKE2b-256 |
be741e853b7d685b8b6d6333d4a6b55d6d3f9fb6508c0367f4907817e556421c
|