vitali87/code-graph-rag
GitHub: vitali87/code-graph-rag
基于 Tree-sitter 和知识图谱的 RAG 系统,支持用自然语言查询、理解和编辑多语言大型代码库。
Stars: 2316 | Forks: 385
# Code-Graph-RAG: A Graph-Based RAG System for Any Codebases
An accurate Retrieval-Augmented Generation (RAG) system that analyzes multi-language codebases using Tree-sitter, builds comprehensive knowledge graphs, and enables natural language querying of codebase structure and relationships as well as editing capabilities.
# Examples:
cgr language add-grammar c-sharp
cgr language add-grammar php
cgr language add-grammar ruby
cgr language add-grammar kotlin
#### Custom Grammar Repositories
For languages hosted outside the standard tree-sitter organization:
# Add a language from a custom repository
cgr language add-grammar --grammar-url https://github.com/custom/tree-sitter-mylang
#### What Happens Automatically
When you add a language, the tool automatically:
1. **Downloads the Grammar**: Clones the tree-sitter grammar repository as a git submodule
2. **Detects Configuration**: Auto-extracts language metadata from `tree-sitter.json`
3. **Analyzes Node Types**: Automatically identifies AST node types for:
- Functions/methods (`method_declaration`, `function_definition`, etc.)
- Classes/structs (`class_declaration`, `struct_declaration`, etc.)
- Modules/files (`compilation_unit`, `source_file`, etc.)
- Function calls (`call_expression`, `method_invocation`, etc.)
4. **Compiles Bindings**: Builds Python bindings from the grammar source
5. **Updates Configuration**: Adds the language to `codebase_rag/language_config.py`
6. **Enables Parsing**: Makes the language immediately available for codebase analysis
#### Example: Adding C# Support
$ cgr language add-grammar c-sharp
🔍 Using default tree-sitter URL: https://github.com/tree-sitter/tree-sitter-c-sharp
🔄 Adding submodule from https://github.com/tree-sitter/tree-sitter-c-sharp...
✅ Successfully added submodule at grammars/tree-sitter-c-sharp
Auto-detected language: c-sharp
Auto-detected file extensions: ['cs']
Auto-detected node types:
Functions: ['destructor_declaration', 'method_declaration', 'constructor_declaration']
Classes: ['struct_declaration', 'enum_declaration', 'interface_declaration', 'class_declaration']
Modules: ['compilation_unit', 'file_scoped_namespace_declaration', 'namespace_declaration']
Calls: ['invocation_expression']
✅ Language 'c-sharp' has been added to the configuration!
📝 Updated codebase_rag/language_config.py
#### Managing Languages
# List all configured languages
cgr language list-languages
# Remove a language (this also removes the git submodule unless --keep-submodule is specified)
cgr language remove-language
#### Language Configuration
The system uses a configuration-driven approach for language support. Each language is defined in `codebase_rag/language_config.py` with the following structure:
"language-name": LanguageConfig(
name="language-name",
file_extensions=[".ext1", ".ext2"],
function_node_types=["function_declaration", "method_declaration"],
class_node_types=["class_declaration", "struct_declaration"],
module_node_types=["compilation_unit", "source_file"],
call_node_types=["call_expression", "method_invocation"],
),
#### Troubleshooting
**Grammar not found**: If the automatic URL doesn't work, use a custom URL:
cgr language add-grammar --grammar-url https://github.com/custom/tree-sitter-mylang
**Version incompatibility**: If you get "Incompatible Language version" errors, update your tree-sitter package:
uv add tree-sitter@latest
**Missing node types**: The tool automatically detects common node patterns, but you can manually adjust the configuration in `language_config.py` if needed.
## 📦 Building a binary
You can build a binary of the application using the `build_binary.py` script. This script uses PyInstaller to package the application and its dependencies into a single executable.
python build_binary.py
The resulting binary will be located in the `dist` directory.
## 🐛 Debugging
1. **Check Memgraph connection**:
- Ensure Docker containers are running: `docker-compose ps`
- Verify Memgraph is accessible on port 7687
2. **View database in Memgraph Lab**:
- Open http://localhost:3000
- Connect to memgraph:7687
3. **For local models**:
- Verify Ollama is running: `ollama list`
- Check if models are downloaded: `ollama pull llama3`
- Test Ollama API: `curl http://localhost:11434/v1/models`
- Check Ollama logs: `ollama logs`
## 🤝 Contributing
Please see [CONTRIBUTING.md](CONTRIBUTING.md) for detailed contribution guidelines.
Good first PRs are from TODO issues.
## 🙋♂️ Support
For issues or questions:
1. Check the logs for error details
2. Verify Memgraph connection
3. Ensure all environment variables are set
4. Review the graph schema matches your expectations
## 💼 Enterprise Services
Code-Graph-RAG is open source and free to use. For organizations that need more, we offer **fully managed cloud-hosted solutions** and **on-premise deployments**:
- **Cloud-Hosted Deployment** — Managed cloud infrastructure for both the graph database and AI agent connection. Zero infrastructure overhead — we handle scaling, updates, and availability so your team can focus on building.
- **On-Premise & Air-Gapped Deployment** — Deploy Code-Graph-RAG entirely within your own environment, including air-gapped networks. Full data sovereignty for regulated industries and security-sensitive organizations.
We also offer custom development, integration consulting, technical support contracts, and team training.
**[View plans & pricing at code-graph-rag.com →](https://code-graph-rag.com/enterprise)**
## Star History
[](https://www.star-history.com/#vitali87/code-graph-rag&Date)
## Fork History
[](https://fork-history.site/#vitali87/code-graph-rag)
标签:AI编程助手, AI风险缓解, RAG, SOC Prime, 代码索引, 开发工具, 请求拦截, 逆向工具