How to Build a Personal AI Coding Assistant in Under 2 Hours
How to Build a Personal AI Coding Assistant in Under 2 Hours
If you’re a solo founder or indie hacker, you know how precious time is. Imagine having a coding assistant that can help you debug, generate code snippets, or even write documentation. The good news is that you can build a personal AI coding assistant in under 2 hours. Here's how to do it practically, without the fluff.
Prerequisites: What You Need to Get Started
Before diving in, make sure you have the following tools and accounts set up:
- A code editor: VS Code is a great choice (free).
- An OpenAI API key: Sign up at OpenAI and get your API key (pricing starts at $0.0004 per token).
- Node.js installed: This will help you run your scripts locally (free).
- Git: For version control (free).
Step 1: Set Up Your Development Environment (30 minutes)
-
Install VS Code: Download and install from here.
-
Install Node.js: Go to Node.js and download the LTS version.
-
Create a new project folder: Open your terminal and run:
mkdir ai-coding-assistant cd ai-coding-assistant npm init -y -
Install Axios: This will help you make API calls.
npm install axios
Step 2: Write the Core Functionality (30 minutes)
Now, let’s write a simple script that interacts with the OpenAI API:
-
Create a new file named
assistant.jsand add the following code:const axios = require('axios'); const API_KEY = 'YOUR_OPENAI_API_KEY'; const endpoint = 'https://api.openai.com/v1/chat/completions'; async function getResponse(prompt) { const response = await axios.post(endpoint, { messages: [{ role: 'user', content: prompt }], model: 'gpt-3.5-turbo', }, { headers: { 'Authorization': `Bearer ${API_KEY}`, 'Content-Type': 'application/json', }, }); return response.data.choices[0].message.content; } (async () => { const userPrompt = "Write a function to reverse a string."; const assistantResponse = await getResponse(userPrompt); console.log(assistantResponse); })(); -
Replace
YOUR_OPENAI_API_KEYwith your actual API key. -
Run your script:
node assistant.js
You should see a response with a function to reverse a string.
Step 3: Enhance Functionality (30 minutes)
Now that you have the basic functionality, let’s enhance it:
- Add more commands: Modify your script to accept user input from the command line.
- Implement error handling: Make sure to handle API errors gracefully.
- Store previous queries: You can use a simple JSON file to log requests and responses.
Here’s a quick enhancement to accept user input:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('What coding help do you need? ', async (userPrompt) => {
const assistantResponse = await getResponse(userPrompt);
console.log(assistantResponse);
rl.close();
});
Troubleshooting: What Could Go Wrong
- API Key Issues: Ensure your API key is correct and has the necessary permissions.
- Network Errors: Check your internet connection if you receive network-related errors.
- Rate Limiting: If you exceed usage limits, you’ll get errors. Monitor your usage in the OpenAI dashboard.
What's Next: Expanding Your AI Assistant
Once you have the basic assistant running, consider expanding its features:
- Integrate with GitHub to pull issues and provide coding help based on open issues.
- Add a GUI using Electron for a more user-friendly experience.
- Experiment with other AI models like Claude or LLaMA for different outputs.
Conclusion: Start Here
Building a personal AI coding assistant is not just feasible; it's practical and can significantly boost your productivity. Start with the basic setup above, and iterate on it to suit your needs. In our experience, this setup works great for quick coding help but may struggle with complex queries or deep domain-specific questions.
What We Actually Use
We primarily use OpenAI's API for our coding assistant, as it provides quick and relevant responses. If you're looking for alternatives, consider Cohere or Anthropic, but be aware that they may have different capabilities and pricing structures.
Follow Our Building Journey
Weekly podcast episodes on tools we're testing, products we're shipping, and lessons from building in public.