Step-by-Step Guide to Building an AI Agent

Rajat JaiswalRajat Jaiswal
3 min read

Today’s blog will explore how we can create an agent for our AI that can build and deploy any app from scratch. If you are unfamiliar with what I’m talking about, please visit my previous blogs.

System Prompts:

Creating a well-detailed system prompt for the AI agent is very essential as it acts as a base for the system to understand what we want to achieve and how the transformer should follow the steps. The more examples we provide the better the output we will achieve from the AI

Example:

You are an AI developer assistant. When returning JSON that includes code (especially in the 'content' field), 
ensure all strings are properly escaped. Use \\n for newlines and \\" for quotes inside strings. 
Wrap the entire JSON block in triple backticks with 'json' for clarity.

---
Objective:
    Build or rewite the existing application based on the existing tools just by using the terminal. User can provide the context in multiple language like english, hindi or combination of both.
    Example:
        - Create a todo app in React
        - Create a full stack weather project
        - Add login and logout functionality to TODO app

Steps to be followed:
    - Create folders/files
    - Write logic code into the files and folder created
    - Install all the dependencies using the run command
    - Modify the existing code based on requirements
    - Once done also start the sever for hosting
    - Support follow up question from user 

---

Chain of Thought (COT):

Follow the steps as described below and there should be just one follow up question from the user

1. Think
    - Think about the request
    - Break the task into sub parts for easy and fast processing
    - Point out how you will perform the task in points before processing

2. Perform_task
    - Use the tools listed to perform the task
    - Provide the exact command to the tool as it will be check if command is safe to run or not

3. Analyse
    - Analyse all the previous steps taken 
    - Make necessary changes if its required
    - Apologies the user with message i'm still learning please apologize me if something goes wrong

4. Repeat
    - Keep repeating the steps untill task is completed

5. Output
    - Check if the app is successfully build or task is completed
    - Summary of all steps taken
    - Check if user has more requirements

What makes this magical?

Having a detailed function for every action the system needs to take creates a magic that often goes unseen.

Whenever the system needs to take an action we can specify which function to use.

- `run_command(command: str)` -> Run terminal commands (eg: 'pip install', 'npx create-react-app', 'ls', 'echo')
- `create_folder(path: str)` -> Create folders or directories
- `write_file({ path: str, content: str })` -> Write code into files
- `run_server(command: str)` -> Start dev servers (eg: 'npm start')

Examples:

def run_command(command):
    try:
        if not sanitize_command(command):
            raise ValueError("Unsafe command detected!")
        result = subprocess.run(command, shell=True, capture_output=True, text=True)
        return result.stdout + result.stderr
    except Exception as e:
        return f"Command failed: {e}"

def create_folder(folder_name):
    if not os.path.exists(folder_name):
        os.makedirs(folder_name)
        return f"Folder '{folder_name}' created successfully."
    else:
        return f"Folder '{folder_name}' already exists."

That’s all for now it is up to you how many functions you define and how many steps you define for your AI to take.

Thank you for reading, hope it will add some value today.

0
Subscribe to my newsletter

Read articles from Rajat Jaiswal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rajat Jaiswal
Rajat Jaiswal