I know everyone using Macaulay2 is smarter than enough to set these things up. But it would be nice if this can save you a couple minutes from typing them yourself.
- Natural language interface for Macaulay2
- Powered by Claude Sonnet 4.5 (or your favorite LLM)
- Gröbner bases, Hilbert series, resolutions
- LangChain-based agent architecture
- macOS 10.15+
- Python 3.8+
- Homebrew
- Anthropic API key with billing enabled
# Install Macaulay2
brew tap macaulay2/tap
brew install macaulay2
# Clone and setup
git clone https://github.com/Retieun/macaulay2-AI-agent-tutorial.git
cd macaulay2-agent
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
# Configure API key
echo 'ANTHROPIC_API_KEY=sk-ant-api03-your-key-here' > .env
# Run
python3 agent.py/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"brew tap macaulay2/tap
brew install macaulay2
M2 --version # Verify installationmkdir macaulay2-agent && cd macaulay2-agent
python3 -m venv venv
source venv/bin/activate
pip install anthropic langchain langchain-anthropic python-dotenv- Sign up at https://console.anthropic.com/
- Add billing (minimum $5)
- Create API key
- Add to
.envfile:
echo 'ANTHROPIC_API_KEY=sk-ant-api03-your-key-here' > .env
chmod 600 .envsource venv/bin/activate
python3 agent.pyCompute Gröbner basis of ideal(x^2-y, xy-1) in Q[x,y]Dimension of variety x^2+y^2+z^2-1?Hilbert series of Q[x,y,z]/(x^2, y^2, z^2)Minimal free resolution of ideal(x^2, xy, y^2)
Type quit to exit.
macaulay2-agent/
├── agent.py # Main agent
├── macaulay2_executor.py # M2 code executor
├── m2_tool.py # LangChain tool wrapper
├── requirements.txt # Dependencies
├── .env # API key (gitignored)
└── README.md # This file
User Query → Claude Agent → Macaulay2Tool → Executor → M2 → Results → Claude → User
- ~$0.05-$0.20 per query
- $5 minimum credit
- Monitor: https://console.anthropic.com/settings/usage
The agent is designed to work with Claude but can be adapted for other LLMs:
- Install:
pip install langchain-openai openai - Update
.env:
OPENAI_API_KEY=sk-proj-your-key-here
- Modify
agent.py:
from langchain_openai import ChatOpenAI # Replace import
# In __init__, replace:
self.llm = ChatOpenAI(model="gpt-4", temperature=temperature, max_tokens=4096)- Install:
pip install langchain-google-genai google-generativeai - Update
.env:
GOOGLE_API_KEY=your-key-here
- Modify
agent.py:
from langchain_google_genai import ChatGoogleGenerativeAI
# In __init__, replace:
self.llm = ChatGoogleGenerativeAI(model="gemini-pro", temperature=temperature)- Install Ollama: https://ollama.ai
- Pull a model:
ollama pull llama2 - Install:
pip install langchain-ollama - Modify
agent.py:
from langchain_ollama import ChatOllama
# In __init__, replace:
self.llm = ChatOllama(model="llama2", temperature=temperature)
# Remove API key requirementNote: Claude Sonnet 4 performs best for mathematical reasoning. Other models may require prompt adjustments for optimal results.
| Issue | Solution |
|---|---|
M2: command not found |
brew install macaulay2 |
API key not found |
Check .env in project root |
Module not found |
pip install -r requirements.txt |
| Timeout errors | Increase timeout in Macaulay2Tool(timeout=60) |
.envis gitignored by default- Never commit API keys
- Use
.env.examplefor templates
MIT