<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[momentum.sh]]></title><description><![CDATA[momentum.sh]]></description><link>https://blog.momentum.sh</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1718961248161/bf4b8d4b-e03a-4f6a-a37e-f9ac673f188b.png</url><title>momentum.sh</title><link>https://blog.momentum.sh</link></image><generator>RSS for Node</generator><lastBuildDate>Mon, 07 Sep 2026 15:13:43 GMT</lastBuildDate><atom:link href="https://blog.momentum.sh/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Leveraging Codebase Knowledge Graphs for Agentic Code Generation]]></title><description><![CDATA[An agent generating code with language models can face several challenges without sufficient context about the codebase:

Duplicating functionality due to lack of awareness of existing functions and classes

Violating design patterns and architecture...]]></description><link>https://blog.momentum.sh/leveraging-codebase-knowledge-graphs-for-agentic-code-generation</link><guid isPermaLink="true">https://blog.momentum.sh/leveraging-codebase-knowledge-graphs-for-agentic-code-generation</guid><category><![CDATA[langchain]]></category><category><![CDATA[Artificial Intelligence]]></category><category><![CDATA[tools]]></category><category><![CDATA[graph database]]></category><category><![CDATA[vector database]]></category><category><![CDATA[devtools]]></category><category><![CDATA[AI]]></category><category><![CDATA[Weaviate]]></category><dc:creator><![CDATA[Dhiren Mathur]]></dc:creator><pubDate>Mon, 01 Jul 2024 18:33:36 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1719923253662/3357681e-3368-4f4d-ab91-86b7bd820af2.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>An agent generating code with language models can face several challenges without sufficient context about the codebase:</p>
<ul>
<li><p>Duplicating functionality due to lack of awareness of existing functions and classes</p>
</li>
<li><p>Violating design patterns and architectures, such as directly querying the database instead of using a repository pattern</p>
</li>
<li><p>Incorrectly using new libraries and frameworks due to knowledge gap.</p>
</li>
<li><p>Misaligning with business requirements and not fulfilling intended use cases due to lack of context</p>
</li>
<li><p>Unable to integrate with existing data models and schemas, leading to duplicate fields and data inconsistencies</p>
</li>
</ul>
<p>This article presents an approach for providing complete codebase context to a <a target="_blank" href="https://react-lm.github.io/">ReAct agent</a> by:</p>
<ol>
<li><p>Mapping function call relationship graphs in a graph database, detecting entry points into the server.</p>
</li>
<li><p>Fetching relevant code at runtime along with file paths</p>
</li>
<li><p>Generating function and entry point level explanations for the corresponding flows for every entry point.</p>
</li>
<li><p>Serving the explanations and metadata through a vector database that the agent can query with natural language</p>
</li>
</ol>
<p><strong>Alternative systems we experimented with:</strong></p>
<ul>
<li><p>Iterative mapping of function &gt; class &gt; file relationships such that the RAG system has the data about every function, class, file and can provide much more specific answers.<br />  The cons associated with this approach were that:<br />  - It was slow<br />  - It was expensive<br />  - Caused us to hit rate limits on larger repositories</p>
<p>  For a use case which only involves understanding existing flows to derive context from them, moving to a system where we generate a flow level inference both for constituting functions of that flow and an overall intent of the flow provided the perfect trade-off between time, cost and quality.</p>
</li>
</ul>
<p><strong>System Architecture:</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1719856528316/cde10220-03f6-449e-b788-6f881d82299a.jpeg" alt class="image--center mx-auto" /></p>
<ul>
<li><p>Key components:</p>
<ul>
<li><p>Graph database (Neo4j) stores nodes representing functions and edges representing call relationships</p>
</li>
<li><p>Postgres database:</p>
<ul>
<li><p>Endpoints table with API paths and identifiers</p>
</li>
<li><p>Inferences table with intent extracted from explanations</p>
</li>
<li><p>Explanations table with natural language descriptions of functions</p>
</li>
<li><p>Pydantic table with data model definitions</p>
</li>
</ul>
</li>
<li><p>Relevant python services:<br />  These services are part of the broader <a target="_blank" href="https://github.com/getmomentum/momentum-core">getmomentum/momentum-core</a> project that is a code behaviour auditor which aims at analysing code behaviours at every git push.</p>
<ul>
<li><p><a target="_blank" href="https://github.com/getmomentum/momentum-core/blob/main/server/knowledge_graph/flow.py">flow.py</a>: Traverses full project graph to generate explanations for each entry point upfront</p>
</li>
<li><p><a target="_blank" href="https://github.com/getmomentum/momentum-core/blob/main/server/knowledge_graph/knowledge_graph.py">knowledge_graph.py</a>: Loads Postgres data into a vector database and provides method to query it.</p>
</li>
<li><p><a target="_blank" href="https://github.com/getmomentum/momentum-core/blob/main/server/test_agent/tools.py">tools.py</a>: Defines Langchain tools to fetch code, Pydantic definitions, query the knowledge graph. These tools will be used by code generation agents to gather pointed context.</p>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>Flow for Generating Complete Code Context</p>
<ol>
<li><p>In a processing step, <a target="_blank" href="http://flow.py">flow.py</a> is triggered for the entire project codebase right after parsing and endpoint detection is completed.</p>
</li>
<li><p>It captures all the functions, classes, entry points defined in the system.</p>
</li>
<li><p>For each entry point:</p>
<ul>
<li><p>It fetches the code for all functions in the flow from GitHub (we do not want to store code in our db.)</p>
</li>
<li><p>It generates detailed natural language explanations of each function in the flow, capturing input, response, business logic, exceptions.</p>
</li>
<li><p>It summarizes the overall intent of the endpoint from the function explanations. This is a single line statement of purpose of each API.</p>
</li>
</ul>
</li>
<li><p>The endpoint paths, inferred intents, function explanations, and Pydantic models are loaded into the vector DB. We are using <a target="_blank" href="https://weaviate.io/">Weaviate</a> as our vector DB of choice.</p>
</li>
<li><p>To load this data into Weaviate and query it, ideally, we would chunk, embed, set overlap, define query algorithm etc but for simplicity, here we use <a target="_blank" href="https://github.com/embedchain/embedchain">Embedchain</a> to build the RAG tool.</p>
</li>
<li><p>When a user requests code generation for a specific entry point:</p>
<ul>
<li><p>The ReAct agent uses tools to:</p>
<ul>
<li><p>Directly fetch relevant code and Pydantic definitions for that entry point</p>
</li>
<li><p>Query the vector DB with natural language to retrieve explanations and metadata for that endpoint and its functions</p>
</li>
</ul>
</li>
</ul>
</li>
<li><p>Equipped with granular code and high-level intent, the agent generates the requested code.</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1719854575563/e46cd1ca-256e-4905-b455-ede6ec1e4313.jpeg" alt class="image--center mx-auto" /></p>
<p><strong>Benefits</strong></p>
<ul>
<li><p>Providing explanations for the full codebase upfront allows rapidly serving context for any entry point.</p>
</li>
<li><p>Vector search enables dynamically retrieving only the most relevant explanations for the entry point of interest</p>
</li>
<li><p>Having context of other flows helps generate better results. Questions like "How can I create a document" can be answered with a reference to the POST /document/ API and file where it is present. This kind of queries asked by agents can help in tasks like writing a test for a DELETE /document/{id} endpoint that might need to insert a document first.</p>
</li>
</ul>
<p><strong>Conclusion</strong></p>
<ul>
<li><p>Mapping full codebases through a lens of entry points in vector databases provides a powerful tool for AI code generation agents</p>
</li>
<li><p>This approach equips language models with broad yet accessible context to generate code that respects the structure and intent of the full system</p>
</li>
<li><p>Techniques like this will be key to enabling agents to program by understanding entire codebases rather than just snippets in isolation</p>
</li>
</ul>
]]></content:encoded></item></channel></rss>