Creating our own AI coder


Today we are going to create a terminal based AI coder to do vibe coding for us. If you don’t have any prior info in GenAI I would suggest you to visit my previous articles. In this article I will be using OpenAI API but you can do this with any model the process is basically the same.
Creating a Good system prompt
For AI Agents how good they performe is majorly dependant on the quality of their system prompt after the selected model. If you have written a fabulous system prompt giving detailed and multiple thorough examples of various scenarios, it’s output will be worlds apart from a simple zero-shot prompt. An example System prompt is below:
SYSTEM_PROMPT = f"""
You are an helpfull AI Assistant who is specialized in resolving user query.
You work on start, plan, action, observe mode.
For the given user query and available tools, plan the step by step execution, based on the planning,
select the relevant tool from the available tool. and based on the tool selection you perform an action to call the tool.
Wait for the observation and based on the observation from the tool call resolve the user query.
Rules:
- Follow the Output JSON Format.
- Always perform one step at a time and wait for next input
- Carefully analyse the user query
Output JSON Format:
{{
"step": "string",
"content": "string",
"function": "The name of function if the step is action",
"input": "The input parameter for the function",
}}
Available Tools:
- "run_command": Takes linux command as a string and executes the command and returns the output after executing it.
- "write_file": Takes filepath and content as parameters to write content to a file.
Example:
User Query: Create a file with content
Output: {{ "step": "plan", "content": "The user wants to create a file with some content" }}
Output: {{ "step": "plan", "content": "I should use the write_file tool to create the file" }}
Output: {{ "step": "action", "function": "write_file", "input": {{"filepath": "test.txt", "content": "Hello World"}} }}
Output: {{ "step": "observe", "output": "File written successfully" }}
Output: {{ "step": "output", "content": "File has been created with the specified content" }}
"""
Yes, heavily inspired by Piyush Garg Sir. A tip from my experience don’t overly complex it’s thinking steps it’s gets confused in them exponential. keep it to the point and concise.
The magic that makes AI file editing possible
We create individual functions as tools for the code to call when AI reaches the Action step and calls for the tool name, and it’s arguments if any. An example flow of it is below:
if parsed_response.get("step") == "action":
tool_name = parsed_response.get("function")
tool_input = parsed_response.get("input")
print(f"🛠️: Calling Tool:{tool_name} with input {tool_input}")
if tool_name in available_tools:
if tool_name == "write_file":
output = available_tools[tool_name](tool_input["filepath"], tool_input["content"])
else:
output = available_tools[tool_name](tool_input)
messages.append({ "role": "user", "content": json.dumps({ "step": "observe", "output": output }) })
continue
if parsed_response.get("step") == "output":
print(f"🤖: {parsed_response.get('content')}")
break
And for the tools or functions that make it possible to write create and edit files are below, but do not that you have to make sure it is compatible to run on both Unix based and windows system.
def run_command(cmd: str):
if os.name == 'nt':
result = subprocess.run(['cmd', '/c', cmd], capture_output=True, text=True)
else:
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
return result.stdout
def write_file(filepath: str, content: str):
with open(filepath, 'w', encoding='utf-8') as f:
f.write(content)
available_tools = {
"run_command": run_command,
"write_file": write_file
}
And that’s pretty much it. Now it’s up to you as to how many functionalities, powers or say tools you want your AI agent to possess. You can find multiple API to call like to get location of the user through IP address to show weather, or connect google meet and mails, the possibilities are endless though limited by your imagination and wallet. Google Maps Api can bankrupt you before you know it. Be sure to thoroughly check every API’s pricing before you go all apeshit on it. not everything’s free.
Subscribe to my newsletter
Read articles from VESHANT DAHIYA directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
