|
1 | | -# text2cypher-react-agent-example |
2 | | -A Text2Cypher ReAct agent built with LangGraph and the Neo4j Cypher MCP server. |
| 1 | +# Text2Cypher ReAct Agent Example |
| 2 | + |
| 3 | +A conversational AI agent that connects to a Neo4j Movies database and can answer movie-related questions using natural language. Built with LangGraph's implementation of a ReAct agent, the Neo4j Cypher MCP server, and a custom movie recommendations tool. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Natural Language to Cypher**: Ask questions in plain English and get answers from your Neo4j database |
| 8 | +- **ReAct Agent Pattern**: Uses reasoning and acting loops for complex reasoning |
| 9 | +- **Schema-Aware**: Automatically retrieves and uses database schema for accurate query generation |
| 10 | +- **Interactive CLI**: Chat-based interface for easy interaction |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +- Python 3.10 or higher |
| 15 | +- Neo4j Aura account or local Neo4j instance with Movies database |
| 16 | +- OpenAI API key |
| 17 | +- `uv` package manager (recommended) or `pip` |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +### Option 1: Using uv (Recommended) |
| 22 | + |
| 23 | +1. **Install uv** (if not already installed): |
| 24 | + |
| 25 | + [Install Documentation](https://docs.astral.sh/uv/getting-started/installation/) |
| 26 | + ```bash |
| 27 | + pip install uv |
| 28 | + ``` |
| 29 | + |
| 30 | +2. **Clone and setup the project**: |
| 31 | + ```bash |
| 32 | + git clone neo4j-field/text2cypher-react-agent-example |
| 33 | + cd text2cypher-react-agent-example |
| 34 | + ``` |
| 35 | + |
| 36 | +3. **Install dependencies**: |
| 37 | + ```bash |
| 38 | + uv sync |
| 39 | + ``` |
| 40 | + |
| 41 | +### Option 2: Using pip |
| 42 | + |
| 43 | +1. **Clone and setup the project**: |
| 44 | + ```bash |
| 45 | + git clone neo4j-field/text2cypher-react-agent-example |
| 46 | + cd text2cypher-react-agent-example |
| 47 | + ``` |
| 48 | + |
| 49 | +2. **Create and activate virtual environment**: |
| 50 | + ```bash |
| 51 | + python -m venv venv |
| 52 | + source venv/bin/activate # On Windows: venv\Scripts\activate |
| 53 | + ``` |
| 54 | + |
| 55 | +3. **Install dependencies**: |
| 56 | + ```bash |
| 57 | + pip install -r requirements.txt |
| 58 | + ``` |
| 59 | + |
| 60 | +## Configuration |
| 61 | + |
| 62 | +1. **Copy the example environment file**: |
| 63 | + ```bash |
| 64 | + cp .env.example .env |
| 65 | + ``` |
| 66 | + |
| 67 | +2. **Edit `.env` with your credentials**: |
| 68 | + ```bash |
| 69 | + OPENAI_API_KEY=your_openai_api_key_here |
| 70 | + NEO4J_USERNAME=neo4j |
| 71 | + NEO4J_PASSWORD=your_neo4j_password |
| 72 | + NEO4J_URI=neo4j+s://your-instance.databases.neo4j.io |
| 73 | + NEO4J_DATABASE=neo4j |
| 74 | + ``` |
| 75 | + |
| 76 | +## Agent Components |
| 77 | + |
| 78 | +### Core Agent (`agent.py`) |
| 79 | +- **LangGraph ReAct Agent**: Implements reasoning and acting loops for complex queries |
| 80 | +- **Neo4j Cypher MCP Server**: Provides schema introspection and query execution |
| 81 | +- **Custom Recommendations Tool**: Custom tool for movie recs |
| 82 | +- **Interactive CLI**: Command-line chat interface |
| 83 | + |
| 84 | +### Key Tools Available to the Agent |
| 85 | + |
| 86 | +1. **`get_neo4j_schema`**: Retrieves database schema for informed query writing |
| 87 | +2. **`read_neo4j_cypher`**: Executes read-only Cypher queries against the database |
| 88 | +3. **`find_movie_recommendations`**: Custom recommendation engine that finds movies liked by users who also enjoyed a target movie |
| 89 | + |
| 90 | +## Running the Agent |
| 91 | + |
| 92 | +### Using uv (Recommended) |
| 93 | +```bash |
| 94 | +make run-agent-uv |
| 95 | +# or |
| 96 | +uv run python3 agent.py |
| 97 | +``` |
| 98 | + |
| 99 | +### Using pip/standard Python |
| 100 | +```bash |
| 101 | +make run-agent |
| 102 | +# or |
| 103 | +python3 agent.py |
| 104 | +``` |
| 105 | + |
| 106 | +## Usage Examples |
| 107 | + |
| 108 | +Once running, you can ask questions like: |
| 109 | + |
| 110 | +**Example Queries:** |
| 111 | +- "What movies are in the database?" |
| 112 | +- "Tell me about The Matrix" |
| 113 | +- "Recommend me some films like The Dark Knight." |
| 114 | + |
| 115 | +## Exit Commands |
| 116 | +To exit the agent, type any of: |
| 117 | +- `exit` |
| 118 | +- `quit` |
| 119 | +- `q` |
| 120 | + |
| 121 | +## Development |
| 122 | + |
| 123 | +### Code Formatting |
| 124 | +```bash |
| 125 | +make format |
| 126 | +``` |
| 127 | + |
| 128 | +``` |
| 129 | +
|
| 130 | +## Dependencies |
| 131 | +
|
| 132 | +**Core Libraries:** |
| 133 | +- `langchain` - LangChain framework |
| 134 | +- `langchain-mcp-adapters` - MCP (Model Context Protocol) adapters |
| 135 | +- `langchain-openai` - OpenAI integration |
| 136 | +- `langgraph` - Graph-based agent framework |
| 137 | +- `neo4j` - Neo4j Python driver |
| 138 | +- `openai` - OpenAI API client |
| 139 | +- `pydantic` - Data validation |
| 140 | +
|
| 141 | +**Development:** |
| 142 | +- `ruff` - Code formatting and linting |
| 143 | +
|
| 144 | +## Troubleshooting |
| 145 | +
|
| 146 | +**Connection Issues:** |
| 147 | +- Verify your Neo4j credentials in `.env` |
| 148 | +- Ensure your Neo4j instance is running and accessible |
| 149 | +
|
| 150 | +**OpenAI Issues:** |
| 151 | +- Verify your OpenAI API key is valid |
| 152 | +- Check your API usage limits |
| 153 | +
|
| 154 | +**MCP Server Issues:** |
| 155 | +- Ensure `uvx` is available in your PATH |
| 156 | +- The agent automatically installs `mcp-neo4j-cypher@0.3.0` via uvx |
| 157 | +
|
| 158 | +**Python Issues:** |
| 159 | +- Ensure Python 3.10+ is installed |
| 160 | +- Try recreating your virtual environment if using pip |
| 161 | +
|
| 162 | +## License |
| 163 | +
|
| 164 | +This project is provided as an example for educational and demonstration purposes. |
0 commit comments