Skip to content

How to make an AI agent to do Macaulay2 Computation for you. (I know most people using Macaulay 2 are smart enough to do it, this should just save some of your time from actually type the code)

License

Notifications You must be signed in to change notification settings

Retieun/macaulay2-AI-agent-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Macaulay2 AI Agent with Claude

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.

Features

  • 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

Prerequisites

  • macOS 10.15+
  • Python 3.8+
  • Homebrew
  • Anthropic API key with billing enabled

Quick Start

# 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

Installation Details

1. Install Homebrew (if needed)

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

2. Install Macaulay2

brew tap macaulay2/tap
brew install macaulay2
M2 --version  # Verify installation

3. Setup Project

mkdir macaulay2-agent && cd macaulay2-agent
python3 -m venv venv
source venv/bin/activate
pip install anthropic langchain langchain-anthropic python-dotenv

4. Get API Key

  1. Sign up at https://console.anthropic.com/
  2. Add billing (minimum $5)
  3. Create API key
  4. Add to .env file:
echo 'ANTHROPIC_API_KEY=sk-ant-api03-your-key-here' > .env
chmod 600 .env

Usage

source venv/bin/activate
python3 agent.py

Example Queries

  • Compute 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.

Project Structure

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

Architecture

User Query → Claude Agent → Macaulay2Tool → Executor → M2 → Results → Claude → User

Cost

Using Other LLMs

The agent is designed to work with Claude but can be adapted for other LLMs:

OpenAI (GPT-4)

  1. Install: pip install langchain-openai openai
  2. Update .env:
OPENAI_API_KEY=sk-proj-your-key-here
  1. Modify agent.py:
from langchain_openai import ChatOpenAI  # Replace import

# In __init__, replace:
self.llm = ChatOpenAI(model="gpt-4", temperature=temperature, max_tokens=4096)

Google Gemini

  1. Install: pip install langchain-google-genai google-generativeai
  2. Update .env:
GOOGLE_API_KEY=your-key-here
  1. Modify agent.py:
from langchain_google_genai import ChatGoogleGenerativeAI

# In __init__, replace:
self.llm = ChatGoogleGenerativeAI(model="gemini-pro", temperature=temperature)

Ollama (Local/Free)

  1. Install Ollama: https://ollama.ai
  2. Pull a model: ollama pull llama2
  3. Install: pip install langchain-ollama
  4. Modify agent.py:
from langchain_ollama import ChatOllama

# In __init__, replace:
self.llm = ChatOllama(model="llama2", temperature=temperature)
# Remove API key requirement

Note: Claude Sonnet 4 performs best for mathematical reasoning. Other models may require prompt adjustments for optimal results.

Troubleshooting

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)

Security

  • .env is gitignored by default
  • Never commit API keys
  • Use .env.example for templates

License

MIT

Links

About

How to make an AI agent to do Macaulay2 Computation for you. (I know most people using Macaulay 2 are smart enough to do it, this should just save some of your time from actually type the code)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages