๐งโโ๏ธ Developer Guideยถ
Welcome to the CellMage developer guide! This section is dedicated to those who want to understand CellMageโs inner workings, contribute to the project, or extend its functionality.
๐๏ธ Architecture Overviewยถ
CellMage is built with a modular architecture designed for flexibility and extensibility:
+------------------+ +-------------------+ +---------------+
| | | | | |
| Magic Commands +----->+ Chat Manager +----->+ LLM Adapters |
| | | | | |
+------------------+ +-------------------+ +---------------+
^ | |
| v v
+------------------+ +-------------------+ +---------------+
| | | | | |
| Integrations | | Conversation | | API Services |
| | | Manager | | |
+------------------+ +-------------------+ +---------------+
^ |
| v
+------------------+ +-------------------+
| | | |
| Resources | | Storage |
| (Personas, | | (SQLite, |
| Snippets) | | Markdown) |
+------------------+ +-------------------+
Key components:
Magic Commands: Entry point for user interactions via IPython
Chat Manager: Orchestrates conversations with LLMs
Adapters: Interface with different LLM providers
Conversation Manager: Tracks and manages conversation history
Storage: Persists conversations to disk
Integrations: Connects with external services
Resources: Manages personas and snippets
๐งฑ Core Classesยถ
Class |
Purpose |
|---|---|
|
Central orchestrator for LLM interactions |
|
Manages conversation history and state |
|
Base class for all magic commands |
|
Adapter for direct API calls |
|
Adapter for Langchain integration |
|
Storage implementation using SQLite |
|
Storage implementation using Markdown files |
Note:
HistoryManageris deprecated. UseConversationManagerfor all conversation and history management.
๐ ๏ธ Development Setupยถ
Prerequisitesยถ
Python 3.9+
Poetry (recommended for dependency management)
Access to an OpenAI API key or compatible service
Setting Up Your Environmentยถ
# Clone the repository
git clone https://github.com/madpin/cellmage.git
cd cellmage
# Install dependencies
pip install -e .
pip install -r requirements-docs.txt # For documentation development
Running Testsยถ
# Run all tests
pytest
# Run specific test categories
pytest tests/unit
pytest tests/integration
# Run magic commands tests specifically
python tests/run_magic_tests.py
For more details on testing magic commands, see Magic Commands Testing.
Building Documentationยถ
cd docs
make html
The documentation will be built in docs/build/html.
๐ Contribution Workflowยถ
Setup: Fork the repository and create a branch
Develop: Make your changes, adding tests as needed
Test: Ensure all tests pass
Document: Update or add documentation as needed
Submit: Create a pull request with a clear description
Review: Address any feedback from maintainers
See our Contributing Guide for more details.
๐ Creating Custom Extensionsยถ
Custom Magic Commandsยถ
To create a new magic command:
Subclass
BaseMagicfromcellmage.integrations.base_magicImplement the required methods:
class CustomMagic(BaseMagic): name = "custom" def line_magic(self, line): # Implementation for %custom pass def cell_magic(self, line, cell): # Implementation for %%custom passRegister your magic with IPython:
def load_ipython_extension(ipython): custom_magic = CustomMagic(ipython) ipython.register_magics(custom_magic)
Custom Adaptersยถ
To add support for a new LLM provider:
Subclass
interfaces.LLMAdapterImplement the required methods:
class CustomAdapter(LLMAdapter): def send_message(self, messages, **kwargs): # Implementation to send messages to the custom LLM pass def get_completion(self, prompt, **kwargs): # Implementation for simple completions pass
Custom Storage Backendsยถ
To create a new storage backend:
Subclass
interfaces.ConversationStoreImplement the required methods for saving and loading conversations
๐ Performance Optimizationยถ
When working with CellMage, keep these performance considerations in mind:
Token Usage: Monitor and optimize token usage in prompts
Streaming: Use streaming for better user experience with larger responses
Storage: SQLite is more efficient than Markdown for frequent operations
Memory Management: Release resources when no longer needed
๐ Debuggingยถ
Enable debug mode to see detailed logs:
from cellmage.utils.logging import setup_logging
import logging
setup_logging(level=logging.DEBUG)
Logs will be written to cellmage.log in your working directory.
๐ Release Processยถ
Our release process follows these steps:
Update version in
cellmage/version.pyUpdate changelog
Create a release branch
Run tests and build documentation
Tag the release
Publish to PyPI
For detailed instructions, see Release Process.