{"id":29860285,"url":"https://github.com/mrigank005/text-to-neo4j-knowledge-graph-builder","last_synced_at":"2025-07-30T03:17:22.455Z","repository":{"id":307001869,"uuid":"1027998852","full_name":"Mrigank005/Text-to-Neo4j-Knowledge-Graph-Builder","owner":"Mrigank005","description":null,"archived":false,"fork":false,"pushed_at":"2025-07-28T21:43:50.000Z","size":8,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-07-28T23:27:10.141Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Mrigank005.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-07-28T21:37:49.000Z","updated_at":"2025-07-28T21:43:54.000Z","dependencies_parsed_at":"2025-07-28T23:37:23.847Z","dependency_job_id":null,"html_url":"https://github.com/Mrigank005/Text-to-Neo4j-Knowledge-Graph-Builder","commit_stats":null,"previous_names":["mrigank005/text-to-neo4j-knowledge-graph-builder"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/Mrigank005/Text-to-Neo4j-Knowledge-Graph-Builder","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mrigank005%2FText-to-Neo4j-Knowledge-Graph-Builder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mrigank005%2FText-to-Neo4j-Knowledge-Graph-Builder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mrigank005%2FText-to-Neo4j-Knowledge-Graph-Builder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mrigank005%2FText-to-Neo4j-Knowledge-Graph-Builder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Mrigank005","download_url":"https://codeload.github.com/Mrigank005/Text-to-Neo4j-Knowledge-Graph-Builder/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mrigank005%2FText-to-Neo4j-Knowledge-Graph-Builder/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267803358,"owners_count":24146517,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-30T02:00:09.044Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-07-30T03:17:18.692Z","updated_at":"2025-07-30T03:17:22.434Z","avatar_url":"https://github.com/Mrigank005.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Text to Neo4j Knowledge Graph Builder\n\n![Python](https://img.shields.io/badge/python-3.8%2B-blue)\n![Neo4j](https://img.shields.io/badge/Neo4j-4.4%2B-green)\n![Ollama](https://img.shields.io/badge/Ollama-3.2:1B-orange)\n![LangChain](https://img.shields.io/badge/LangChain-Latest-red)\n\nA Python script that extracts entities and relationships from text files using Ollama's LLM and builds a knowledge graph in Neo4j.\n\n## Features\n\n- 📄 Processes multiple `.txt` files from an `Input` folder\n- 🧠 Uses Ollama's LLM (llama3.2:1b by default) for intelligent entity and relationship extraction\n- 🗃️ Stores extracted knowledge as nodes and relationships in Neo4j\n- 🔄 Handles large documents with smart text chunking using LangChain\n- 🏷️ Automatic node labeling with sanitized types and relationship typing\n- 🔗 Structured output using Pydantic models for reliable data extraction\n\n## Prerequisites\n\n### Required Software\n- **Python 3.8+**\n- **[Neo4j Desktop](https://neo4j.com/download/) or Neo4j Community Server** running locally on port 7687\n- **[Ollama](https://ollama.ai/)** running locally on port 11434\n- **llama3.2:1b model** (or your preferred model) downloaded in Ollama\n\n### Required Python Packages\n```bash\npip install neo4j python-dotenv langchain langchain-core langchain-ollama\n```\n\n## Installation\n\n1. **Clone or download the project:**\n   ```bash\n   # Create project directory\n   mkdir text-to-neo4j\n   cd text-to-neo4j\n   ```\n\n2. **Install Python dependencies:**\n   ```bash\n   pip install neo4j python-dotenv langchain langchain-core langchain-ollama\n   ```\n\n3. **Set up Neo4j:**\n   - Start your Neo4j database\n   - Note your password (default user is `neo4j`)\n\n4. **Set up Ollama:**\n   ```bash\n   # Install and pull the model\n   ollama pull llama3.2:1b\n   ```\n\n5. **Configure the script:**\n   Edit `Text_to_Neo4J.py` and update these variables:\n   ```python\n   NEO4J_PASSWORD = \"your_actual_password_here\"  # Replace with your Neo4j password\n   OLLAMA_MODEL = \"llama3.2:1b\"  # Or your preferred model\n   ```\n\n6. **Create the Input folder:**\n   ```bash\n   mkdir Input\n   ```\n\n## Project Structure\n\n```\ntext-to-neo4j/\n├── Text_to_Neo4J.py          # Main script\n├── Input/                    # Place your .txt files here\n│   ├── document1.txt\n│   ├── document2.txt\n│   └── ...\n└── README.md\n```\n\n## Usage\n\n1. **Place your text files** in the `Input` folder (only `.txt` files are processed)\n\n2. **Run the script:**\n   ```bash\n   python Text_to_Neo4J.py\n   ```\n\n3. **View your knowledge graph** in Neo4j Browser at `http://localhost:7474`\n\n4. **Query your graph** using Cypher. Try these example queries:\n   ```cypher\n   // View all nodes and relationships\n   MATCH (n)-[r]-\u003e(m) RETURN n, r, m LIMIT 50\n   \n   // View all entities\n   MATCH (n:Entity) RETURN n LIMIT 25\n   \n   // Find specific entity types\n   MATCH (n:Person) RETURN n.name\n   \n   // Explore relationships of a specific entity\n   MATCH (n {id: \"EntityName\"})-[r]-(connected) \n   RETURN n, r, connected\n   ```\n\n5. **Optional: Clear existing data** before running (as suggested by the script):\n   ```cypher\n   MATCH (n) DETACH DELETE n\n   ```\n\n## How It Works\n\n1. **Text Processing**: Files are split into chunks of 512 characters with 50-character overlap\n2. **Entity Extraction**: Each chunk is processed by Ollama to identify entities and relationships\n3. **Graph Storage**: Extracted data is stored in Neo4j with:\n   - Nodes labeled by sanitized entity types\n   - Relationships with UPPERCASE_SNAKE_CASE types\n   - All nodes also labeled as `:Entity` for easy querying\n\n## Input/Output Example\n\n**Input text (example.txt):**\n```\nElon Musk founded SpaceX in 2002. Tesla, another company he leads, develops electric vehicles.\n```\n\n**Resulting Neo4j Graph:**\n- **Nodes:**\n  - `Elon Musk` (Person, Entity)\n  - `SpaceX` (Company, Entity)  \n  - `Tesla` (Company, Entity)\n  - `Electric Vehicles` (Technology, Entity)\n\n- **Relationships:**\n  - `(Elon Musk)-[:FOUNDED]-\u003e(SpaceX)`\n  - `(Elon Musk)-[:LEADS]-\u003e(Tesla)`\n  - `(Tesla)-[:DEVELOPS]-\u003e(Electric Vehicles)`\n\n## Configuration Options\n\n| Parameter | Description | Default Value |\n|-----------|-------------|---------------|\n| `NEO4J_URI` | Neo4j connection URI | `bolt://localhost:7687` |\n| `NEO4J_USER` | Neo4j username | `neo4j` |\n| `NEO4J_PASSWORD` | Neo4j password | `password` |\n| `OLLAMA_MODEL` | Ollama model name | `llama3.2:1b` |\n| `OLLAMA_BASE_URL` | Ollama service URL | `http://localhost:11434` |\n| `chunk_size` | Text chunk size for processing | `512` |\n| `chunk_overlap` | Overlap between text chunks | `50` |\n\n## Troubleshooting\n\n### Common Issues\n\n- **❌ Failed to connect to Neo4j**: \n  - Verify Neo4j is running on port 7687\n  - Check your username/password in the script\n  - Ensure bolt connector is enabled\n\n- **❌ No .txt files found**: \n  - Make sure you have an `Input` folder in the same directory as the script\n  - Verify your files have `.txt` extension\n\n- **❌ Error invoking Ollama**: \n  - Ensure Ollama is running: `ollama serve`\n  - Check if your model is available: `ollama list`\n  - Try pulling the model again: `ollama pull llama3.2:1b`\n\n- **⚠️ Nothing extracted**: \n  - Try a larger/different Ollama model\n  - Adjust chunk size for your text type\n  - Check if your text contains clear entities and relationships\n\n### Performance Tips\n\n- Use smaller models (like `llama3.2:1b`) for faster processing\n- Adjust chunk sizes based on your document structure\n- For large datasets, consider processing files in batches\n\n## Dependencies\n\nThe script uses these key libraries:\n- `neo4j`: Neo4j Python driver\n- `langchain`: Text splitting and LLM integration\n- `langchain-ollama`: Ollama integration for LangChain\n- `python-dotenv`: Environment variable loading (optional)\n\n## License\n\nMIT License - Feel free to use and modify as needed.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrigank005%2Ftext-to-neo4j-knowledge-graph-builder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmrigank005%2Ftext-to-neo4j-knowledge-graph-builder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrigank005%2Ftext-to-neo4j-knowledge-graph-builder/lists"}