Skip to main content

Code-Graph-RAG

A graph-based RAG system that parses multi-language codebases with Tree-sitter, builds knowledge graphs in Memgraph, and enables natural language querying, editing, and optimisation.

Install

pip install code-graph-rag

With all Tree-sitter grammars (Python, JS, TS, Rust, Go, Java, Scala, C, C++, C#, PHP, Lua, Dart):

pip install 'code-graph-rag[treesitter-full]'

With semantic code search (UniXcoder embeddings):

pip install 'code-graph-rag[semantic]'

Qdrant is the default vector store for semantic search. To use Milvus Lite, install code-graph-rag[semantic,milvus], then set CGR_VECTOR_STORE_BACKEND=milvus and MILVUS_URI=./.milvus_code_embeddings.db before indexing.

To compute embeddings on an OpenAI-compatible endpoint (OpenAI, Ollama, vLLM) instead of locally, set CGR_EMBEDDING_PROVIDER=openai with OPENAI_EMBEDDING_BASE_URL and OPENAI_EMBEDDING_MODEL; torch and transformers are then not required locally.

Prerequisites

  • Python 3.12+
  • Docker (for Memgraph)
  • cmake (for building pymgclient)
  • ripgrep (rg) (for shell command text searching)

CLI Quick Start

The package installs a cgr command.

Start Memgraph, parse a repo, and query it:

cgr daemon up                              # start Memgraph + Qdrant
cgr start --repo-path ./my-project \
          --update-graph --clean           # parse & launch interactive chat

Index to protobuf for offline use:

cgr index -o ./index-output --repo-path ./my-project

Export knowledge graph to JSON:

cgr export -o graph.json

AI-guided optimisation:

cgr optimize python --repo-path ./my-project

Find dead code (functions unreachable from any entry point):

cgr dead-code                                   # scan the indexed project
cgr dead-code -e main --exclude '*.gen.*'       # add roots, skip generated code
cgr dead-code --format json --fail-on-found     # CI-friendly report

Results are candidates for review, not a guaranteed delete list. See the Dead Code Detection guide.

Run as an MCP server (for Claude Code):

cgr mcp-server

Check your setup:

cgr doctor

Python SDK

The cgr package provides short imports for programmatic use.

Load and query an exported graph

from cgr import load_graph

graph = load_graph("graph.json")
print(graph.summary())

functions = graph.find_nodes_by_label("Function")
for fn in functions[:5]:
    rels = graph.get_relationships_for_node(fn.node_id)
    print(f"{fn.properties['name']}: {len(rels)} relationships")

Query Memgraph with Cypher

from cgr import MemgraphIngestor

with MemgraphIngestor(host="localhost", port=7687) as db:
    rows = db.fetch_all("MATCH (f:Function) RETURN f.name LIMIT 10")
    for row in rows:
        print(row)

Generate Cypher from natural language

import asyncio
from cgr import CypherGenerator

async def main():
    gen = CypherGenerator()
    cypher = await gen.generate("Find all classes that inherit from BaseModel")
    print(cypher)

asyncio.run(main())

Semantic code search

Requires the semantic extra.

from cgr import embed_code

embedding = embed_code("def authenticate(user, password): ...")
print(f"Embedding dimension: {len(embedding)}")

Configuration

from cgr import settings

settings.set_orchestrator("openai", "gpt-4o", api_key="sk-...")
settings.set_cypher("google", "gemini-2.5-flash", api_key="your-key")

Environment Variables

Configure via .env or environment variables:

Variable Default Description
MEMGRAPH_HOST localhost Memgraph hostname
MEMGRAPH_PORT 7687 Memgraph port
ORCHESTRATOR_PROVIDER Provider: google, openai, anthropic, azure, ollama, minimax, litellm_proxy
ORCHESTRATOR_MODEL Model ID (e.g. gpt-4o, gemini-2.5-pro)
ORCHESTRATOR_API_KEY API key for the provider (not needed for ollama)
CYPHER_PROVIDER Provider for Cypher generation
CYPHER_MODEL Model ID for Cypher generation (e.g. codellama, gpt-4o-mini)
CYPHER_API_KEY API key for Cypher provider (not needed for ollama)
TARGET_REPO_PATH . Default repository path

Documentation

Full documentation, architecture details, and contribution guide: docs.code-graph-rag.com

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

code_graph_rag-0.0.484.tar.gz (663.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

code_graph_rag-0.0.484-py3-none-any.whl (776.7 kB view details)

Uploaded Python 3

File details

Details for the file code_graph_rag-0.0.484.tar.gz.

File metadata

  • Download URL: code_graph_rag-0.0.484.tar.gz
  • Upload date:
  • Size: 663.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for code_graph_rag-0.0.484.tar.gz
Algorithm Hash digest
SHA256 e595c2e1b401c7d28cbc0b8d3d59b1b1c600faa6f03ba4f27e02310de74cf080
MD5 b8ea6bcc3bf9b4d4e60fdc4d6428d32f
BLAKE2b-256 95ea0a7eae69ab7557ae05edce8047dd44bff733f8f4e1b148ace021db37f589

See more details on using hashes here.

Provenance

The following attestation bundles were made for code_graph_rag-0.0.484.tar.gz:

Publisher: publish.yml on vitali87/code-graph-rag

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file code_graph_rag-0.0.484-py3-none-any.whl.

File metadata

File hashes

Hashes for code_graph_rag-0.0.484-py3-none-any.whl
Algorithm Hash digest
SHA256 2c849c1d8da16e6bcb4153a7d51137585bb8076b7097476d408f7f63544cf7c7
MD5 3a8e55180a8d94f6cb84ecaa55c04eb1
BLAKE2b-256 436273176c69d8d9e2fe27ddf066a3289fbe377f9b5c056ef0ce6dccbebb1faa

See more details on using hashes here.

Provenance

The following attestation bundles were made for code_graph_rag-0.0.484-py3-none-any.whl:

Publisher: publish.yml on vitali87/code-graph-rag

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page