Image by the author generated with Dalle3

Building a Simple LLM Powered Github Agent with LangChain

Giving agents the ability to commit and push Python code

Lucas Soares
3 min readNov 7, 2023

--

So, with the advent of LLM Agents one use case that I’ve been thinking about a lot is Agents as developers. Now, beyond the hype of thinking about LLM agents as replacements for human programmers and so on, I am interested in how would one start to implement something like a developer agent in a real-world environment where the agent can read and write code as well as make commits and push them to a repository.

In this article, let’s learn to build a simple LLM agent with LangChain that can make commits and push code to a github repository.

Steps for Building a Simple LLM Github Agent

The steps to build such an agent are as follows:

  1. Set up the LLM
  2. Define the tool or toolkit
  3. Define your agent
  4. Query the agent

Let’s get started!

1. Set up the LLM

To start we’ll just use langchain to set up the access to the OpenAI API and use ChatGPT:

# setup the llm
from langchain.chat_models import ChatOpenAI

llm = ChatOpenAI(temperature=0)

2. Define the tool or toolkit

Now, let’s set up our first tool for our LLM agent. That tool will be the ability to commit to a repository (assuming we have one created in the current folder)

from langchain.tools import tool

@tool
def github_commit_tool(commit_message="Some commit"):
"""This function uses the subprocess package to make commits to a github repo pre-defined."""
subprocess.run(["git", "add", "."])
subprocess.run(["git", "commit", "-m", commit_message])
subprocess.run(["git", "push", "-u", "origin", "main"])

return "Committed to Github"

tools = [github_commit_tool]

LangChain makes it really easy to set up tools now where we just need the @tool decorator to be placed on the top of a Python function.

3. Define your agent

--

--

Lucas Soares

Machine Learning Engineer. I write about AI | Data Science | Productivity & Learning. Join Medium at: https://lucas-soares.medium.com/membership