Skip to main content

code-base-agent

Introduction

This repo introduces a method to represent a local code repository as a graph structure. The objective is to allow an LLM to traverse this graph to understand the code logic and flow. Providing the LLM with the power to debug, refactor, and optimize queries. However, several tasks are yet unexplored.

Technology Stack

We used a combination of llama-index, CodeHierarchy module, and tree-sitter-languages for parsing code into a graph structure, Neo4j for storing and querying the graph data, and langchain to create the agents.

Installation

Install the package:

pip install blar-graph

Set the env variables

NEO4J_URI=neo4j+s://YOUR_NEO4J.databases.neo4j.io
NEO4J_USERNAME=neo4j
NEO4J_PASSWORD=YOUR_NEO4J_PASSWORD
OPENAI_API_KEY=YOUR_OPEN_AI_KEY

If you are new to Neo4j you can deploy a free instance of neo4j with Aura. Also you can host your own version in AWS or GCP

Quick start guide

To build the graph, you have to instantiate the graph manager and constructor. The graph manager handles the connection with Neo4j, and the graph constructor processes the directory input to create the graph.

import traceback
import uuid

from blar_graph.db_managers import Neo4jManager
from blar_graph.graph_construction.core.graph_builder import GraphConstructor

repoId = str(uuid.uuid4())
entityId = str(uuid.uuid4())
graph_manager = Neo4jManager(repoId, entityId)

try:
    graph_constructor = GraphConstructor(graph_manager)
    graph_constructor.build_graph("YOUR_LOCAL_DIRECTORY")
    graph_manager.close()
except Exception as e:
    print(e)
    print(traceback.format_exc())
    graph_manager.close()

Now you can use our agent tools, or build your own, to create agents that resolves specific tasks. In the folder 'agents_tools' you will find all our tools (for now is just the Keyword search) and examples of agent implementations. For example, for a debugger agent you could do:

from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.agents.format_scratchpad.openai_tools import (
    format_to_openai_tool_messages,
)
from langchain.agents.output_parsers.openai_tools import (
    OpenAIToolsAgentOutputParser,
)
from blar_graph.agents_tools.tools.KeywordSearchTool import KeywordSearchTool
from blar_graph.db_managers.base_manager import BaseDBManager
from langchain.agents import AgentExecutor
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4-turbo-preview", temperature=0)

system_prompt = """
    You are a code debugger, Given a problem description and an initial function, you need to find the bug in the code.
    You are given a graph of code functions,
    We purposly omitted some code If the code has the comment '# Code replaced for brevity. See node_id ..... '.
    You can traverse the graph by calling the function keword_search.
    Prefer calling the function keword_search with query = node_id, only call it with starting nodes or neighbours.
    Explain why your solution solves the bug. Extensivley traverse the graph before giving an answer
"""


prompt = ChatPromptTemplate.from_messages(
    [
        (
            "system",
            system_prompt,
        ),
        ("user", "{input}"),
        MessagesPlaceholder(variable_name="agent_scratchpad"),
    ]
)

tools = [KeywordSearchTool(db_manager=graph_manager)]
llm_with_tools = llm.bind_tools(tools)

agent = (
    {
        "input": lambda x: x["input"],
        "agent_scratchpad": lambda x: format_to_openai_tool_messages(
            x["intermediate_steps"]
        ),
    }
    | prompt
    | llm_with_tools
    | OpenAIToolsAgentOutputParser()
)

Now you can ask your agent to perform a debugging process.

list(
    agent.stream(
        {
            "input": """
            The directory nodes generates multiples connections,
            it doesn't distinguish between different directories, can you fix it?
            The initial functions is run
            """
        }
    )
)

You can find more examples in the folder 'examples'. They are comprehensive jupiter notebooks that guide you from creating the graph to deploying the agent.

Note: The supported languages for now are python, javascript and typescript. We are going to include C and C++ (or other language) if you ask for it enough. So don't hesitate to reach out through the issues or directly to benjamin@blar.io or jose@blar.io

Metadata

Release files for blar-graph 1.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for blar-graph 1.1.6
File Size Uploaded Provenance Actions
blar_graph-1.1.6.tar.gz 27.5 kB Without provenance Details for blar_graph-1.1.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for blar-graph 1.1.6
File Interpreter ABI Platform Actions
blar_graph-1.1.6-py3-none-any.whl Python 3 none any Details for blar_graph-1.1.6-py3-none-any.whl

Total release size: 67.4 kB

Release files / blar_graph-1.1.6.tar.gz

Download URL blar_graph-1.1.6.tar.gz
Size 27.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d578bf8ce194e03093503f6eaa8b1434fdcb4d62a44e1c136fcecfcee52ecaaf
BLAKE2b-256 checksum
How to use checksums
53c23159305807a6890fb9f0c11a09689b44d215e96517421f4523cbb4942571
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.4 Darwin/23.6.0

Release files / blar_graph-1.1.6-py3-none-any.whl

Download URL blar_graph-1.1.6-py3-none-any.whl
Size 39.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a59b470888ed5a4cf188a78de8432cca57cf69d64ecac195e68afe386dfc5020
BLAKE2b-256 checksum
How to use checksums
85ab2081af5b5dfd2d7ac57677840066885c9fe59cf4df4ae55670dad67e81e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.4 Darwin/23.6.0

Security for blar-graph 1.1.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.1.6 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page