Build you own AI agent from scratch in 30 mins using simple Python

GP Sandhu
5 min readJan 7, 2024

--

Introduction

OpenAI released ChatGPT on November 30, 2022, and due to its success, 2023 became the year of AI chat apps. We saw similar offerings from competitors and even started building our own chat apps using Retrieval Augmented Generation (RAG). Now, as 2024 unfolds, the spotlight is shifting to AI Agents. These agents are set to do more for us than merely answering questions. They combine Large Language Models (LLMs) with specific tools (and memory) that enable them to perform various tasks. Think of it like using a screwdriver or a leaf-blower; AI agents employ digital tools such as fetching web URLs, reading unread Gmail, etc., to enhance their capabilities and assist us.

In this post, I’ll guide you through building an AI Agent from scratch using OpenAI models and Python, with a particular focus on the Langchain library. We’ll integrate this agent into a Slack app for seamless usability. Next, we’ll dockerize it, ensuring straightforward running and deployment. This step is essential before we start adding more custom tools specifically designed to handle some of our cognitive grunt-work. Here’s the link to the code. The best part? It should take you no more than 30 minutes to achieve this (provided you’re familiar with Python, Slack apps, and Docker), allowing you to create your own personal AI Agent!

Step 1: Hello World AI using GPT-4 LLM

First, let’s set up a virtual environment to manage our dependencies in a clean, isolated setup. The primary Python libraries we’ll need are OpenAI and Langchain. Additionally, you’ll need to provide your OpenAI API key for making API calls. This should be stored in an environment variable named OPENAI_API_KEY, which Langchain will automatically utilize. We'll save this key in a .env file and use the python-dotenv library to load it for our development environment.

For the core code, we’ll begin with a file named agent.py and initiate by making a simple API call to OpenAI's GPT-4. Here's what the code looks:

# Import all the necessary libraries
from dotenv import load_dotenv
from langchain_community.chat_models import ChatOpenAI

# Load the environment variables
load_dotenv()

# Define which LLM to use
# gpt-4-11-06-preview is the GPT-4 turbo model launched by OpenAI at their Dev day in 2023
llm = ChatOpenAI(model="gpt-4-1106-preview", temperature=0)

output = llm.invoke("What would be the AI equivalent of Hello World?")
print(output)

At this point, you should be at the commit first commit for this repo — https://github.com/gpsandhu23/agent_tutorial/commit/1d88b788a712b307bc1f41d30c3ece7883cc424c

Step 2: Bootstrap Basic AI Agent

Now, let’s bootstrap the AI agent in agent.py using Langchain's agent implementation. This involves several key components:

  1. Prompt: This defines the agent’s purpose and instructions. This is where you can inject personalization, influencing the tone or style you desire for the agent.
  2. Tools: Here, you specify the tools available for the agent to accomplish tasks. Initially, we’ll start with an empty list and progressively add tools to enhance the agent’s capabilities.
  3. LLM (Large Language Model): We’ll utilize OpenAI’s GPT-4 Turbo (best available model at the time of this writing).
  4. Memory: This feature enables the agent to recall past interactions, facilitating natural follow-up questions.

Once these elements are set up, you can run the agent by creating an agent executor and invoking it with an input. Here’s how agent.py will look at this stage:

# Import all the necessary libraries
from dotenv import load_dotenv
from langchain_community.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.agents.format_scratchpad import format_to_openai_function_messages
from langchain.agents import AgentExecutor
from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser

# Load the environment variables
load_dotenv()

# gpt-4-11-06-preview is the GPT-4 turbo model launched by OpenAI at their Dev day in 2023
llm = ChatOpenAI(model="gpt-4-1106-preview", temperature=0)
# Add chat history to the Agent as short term memory
chat_history = []

# Add tools to the Agent to extent capabilities
tools = []

# Define the chat prompt
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful personal AI assistant named TARS. You have a geeky, clever, sarcastic, and edgy sense of humor."),
MessagesPlaceholder(variable_name="chat_history"),
("user", "{input}"),
MessagesPlaceholder(variable_name="agent_scratchpad"),
])

# Define the agent
agent = (
{
"input": lambda x: x["input"],
"agent_scratchpad": lambda x: format_to_openai_function_messages(x["intermediate_steps"]),
"chat_history": lambda x: x["chat_history"],
}
| prompt
| llm
| OpenAIFunctionsAgentOutputParser()
)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

# Run the agent
user_task = "What would be the AI equivalent of Hello World?"
output = agent_executor.invoke({"input": user_task, "chat_history": chat_history})
print(output['output'])

You can check out the code at this state in this commit — https://github.com/gpsandhu23/agent_tutorial/commit/f5d4ff06c75c77b269dc14e499610a20b03598b0

Step 3: Add Slack Integration

Now that we have a basic agent running in the terminal, it’s time to make it more accessible by integrating it with Slack. Given that many of us have Slack open throughout the day, it’s practical to bring AI into this familiar space, rather than expecting people to adapt to a new environment for AI interaction.

If you haven’t created a Slack app before, don’t worry, it’s pretty straightforward. Your AI agent will need the following scopes to function properly:

oauth_config:
scopes:
bot:
- chat:write
- files:read
- im:history
- im:read
- im:write
settings:
event_subscriptions:
bot_events:
- message.im

After setting up your app, add the following details to your .env file:

SLACK_APP_TOKEN="xapp-..."
SLACK_BOT_TOKEN="xoxb-..."
SLACK_SIGNING_SECRET="..."

Next, update your requirements.txt to include slack_bolt, which is the main SDK for building Slack apps. Now, let's create a main.py that will handle direct messages (DMs) from Slack and invoke the agent we created in agent.py.

You can see the code at this stage in this commit: https://github.com/gpsandhu23/agent_tutorial/commit/9fa17ff67a42b90b1c31d2dfdf69eaf4a664f321.

Step 4: Dockerize the App

The next step is to Dockerize the app, which will enable easy deployment across various environments without worrying about execution compatibility. This involves creating a few key files:

  1. Dockerfile: This file contains the set of instructions for Docker to build the app's image.
  2. docker-compose.yml: This YAML file defines how Docker containers should behave in production.
  3. .dockerignore: Similar to .gitignore, this file tells Docker which files and folders to ignore when building the image.

With these components in place, your application will be ready for containerized deployment, ensuring consistent and reliable operation across different systems.

You can view the code at this stage of development in this commit: https://github.com/gpsandhu23/agent_tutorial/commit/a24bf65966c4602afca99df6d5772ed8b54d03c4.

Step 5: Send DMs

The agent will now be able to receive the DMs via Slack and respond. Congratulations! you’ve just built your own AI agent!

Conclusion

I hope this guide has given you an understanding of what an AI agent is and how you can build one yourself. In the upcoming posts, I’ll walk you through adding more tools to this agent, enhancing its capabilities to handle more of your cognitive grunt-work. This includes tasks like reading emails, managing Slack DMs, filling out paperwork, and more. Stay tuned to transform your AI agent into a more powerful personal assistant for your daily digital chores!

--

--