How to build an AI code assistant
Quick answer
Build an AI code assistant by integrating a large language model like
gpt-4o via the OpenAI API to generate, explain, and debug code. Use Python with the openai SDK, sending user prompts and receiving code completions to create an interactive coding helper.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the official OpenAI Python SDK and set your API key as an environment variable for secure access.
pip install openai>=1.0 Step by step
This example shows a simple AI code assistant that takes a user prompt describing a coding task and returns generated Python code using the gpt-4o model.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
user_prompt = "Write a Python function to check if a number is prime."
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": user_prompt}]
)
code_output = response.choices[0].message.content
print("AI code assistant response:\n", code_output) output
AI code assistant response:
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True Common variations
- Use
asynccalls for non-blocking requests. - Switch to other models like
claude-3-5-sonnet-20241022for better code generation. - Implement streaming to display code as it generates.
import os
import asyncio
from openai import OpenAI
async def async_code_assistant(prompt: str):
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = await client.chat.completions.acreate(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}]
)
print("Async AI code assistant response:\n", response.choices[0].message.content)
asyncio.run(async_code_assistant("Write a Python class for a linked list.")) output
Async AI code assistant response:
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def append(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
return
last = self.head
while last.next:
last = last.next
last.next = new_node Troubleshooting
- If you get authentication errors, verify your
OPENAI_API_KEYenvironment variable is set correctly. - For rate limit errors, reduce request frequency or upgrade your API plan.
- If code output is incomplete, try increasing
max_tokensin the API call.
Key Takeaways
- Use the OpenAI Python SDK with
gpt-4oto generate code from natural language prompts. - Set your API key securely via environment variables to avoid leaks.
- Async and streaming calls improve responsiveness for interactive coding assistants.
- Switch models or tune parameters like
max_tokensto optimize code generation quality. - Check API errors carefully and handle rate limits to maintain smooth operation.