Build an AI Pair Programmer with Ollama Using Python
AI-assisted development has become a standard part of modern software engineering. While cloud-based coding assistants are popular, many developers prefer running large language models locally for privacy, lower latency, offline development, and complete control over their environment. Ollama makes this possible by allowing developers to run powerful open-source language models directly on their machines.
What is Ollama?
Ollama is a lightweight framework for downloading, managing, and serving large language models locally. It provides an HTTP API that applications can interact with just like cloud AI services.
Benefits include:
- Completely local execution
- No API usage costs
- Offline availability
- Better privacy
- Support for multiple open-source models
- Easy model switching
Popular supported models include:
- Llama 3
- Mistral
- DeepSeek
- Qwen
- Gemma
- CodeLlama
- Phi
Project Architecture
Developer
│
▼
Python Application
│
HTTP Request
│
▼
Ollama Server
│
▼
Local LLM
│
▼
Generated Code / Explanation
Prerequisites
Install Ollama.
Download from:
Install Python packages:
pip install requests
Pull a coding model:
ollama pull codellama
Or
ollama pull deepseek-coder
Run the model:
ollama run codellama
Understanding the Ollama API
The local API endpoint is:
POST http://localhost:11434/api/generate
Example payload:
{
"model":"codellama",
"prompt":"Write Python factorial code.",
"stream":false
}
Response:
{
"response":"def factorial(n): ..."
}
Creating the Pair Programmer
Project Structure
pair-programmer/
│── assistant.py
│── prompts.py
│── utils.py
│── README.md
Step 1: Create an Ollama Client
# Import the requests library to send HTTP requests
import requests
# Define the Ollama API endpoint
OLLAMA_URL = "http://localhost:11434/api/generate"
# Define a function to send prompts to Ollama
def ask_ai(prompt):
# Create the request payload
payload = {
"model": "codellama",
"prompt": prompt,
"stream": False
}
# Send the POST request to Ollama
response = requests.post(OLLAMA_URL, json=payload)
# Convert JSON response to Python dictionary
result = response.json()
# Return only the generated response
return result["response"]
Step 2: Ask Programming Questions
# Import the ask_ai function
from utils import ask_ai
# Define the programming question
question = """
Explain Python decorators with examples.
"""
# Send the question to Ollama
answer = ask_ai(question)
# Print the generated answer
print(answer)
Step 3: Generate Code
# Import the Ollama client
from utils import ask_ai
# Create a code generation prompt
prompt = """
Write a Flask CRUD API using SQLite.
"""
# Generate the code
code = ask_ai(prompt)
# Display the generated code
print(code)
Step 4: Debug Code
# Import the Ollama client
from utils import ask_ai
# Define the buggy code
buggy_code = """
def divide(a,b):
return a/b
print(divide(5,0))
"""
# Create a debugging prompt
prompt = f"""
Find the bug and suggest improvements.
{buggy_code}
"""
# Ask Ollama for debugging help
result = ask_ai(prompt)
# Print the suggestions
print(result)
Step 5: Explain Existing Code
# Import the Ollama client
from utils import ask_ai
# Provide code to explain
code = """
class Student:
pass
"""
# Create explanation prompt
prompt = f"""
Explain this code line by line.
{code}
"""
# Print explanation
print(ask_ai(prompt))
Step 6: Refactor Code
from utils import ask_ai
code = """
for i in range(len(items)):
print(items[i])
"""
prompt = f"""
Refactor this Python code following best practices.
{code}
"""
print(ask_ai(prompt))
Step 7: Generate Unit Tests
from utils import ask_ai
code = """
def add(a,b):
return a+b
"""
prompt = f"""
Write pytest unit tests.
{code}
"""
print(ask_ai(prompt))
Building an Interactive CLI
# Import the helper function
from utils import ask_ai
# Display a welcome message
print("AI Pair Programmer")
# Continuously accept user input
while True:
# Read a prompt from the user
prompt = input("You: ")
# Exit the loop if the user types 'exit'
if prompt.lower() == "exit":
break
# Get the AI's response
answer = ask_ai(prompt)
# Display the response
print("\nAI:\n")
print(answer)
Improving Prompt Quality
Instead of:
Write login code.
Use:
Generate a secure Flask login system.
Requirements:
- SQLite
- Password hashing
- SQLAlchemy
- JWT authentication
- Error handling
- Comments
Specific prompts generally produce more accurate and maintainable code.
Suggested Features
You can extend your AI pair programmer with capabilities such as:
- Context-aware conversations by preserving chat history.
- Project-wide code analysis using source file indexing.
- Git commit message generation from code changes.
- Automatic documentation generation.
- Code review with suggestions for readability and security.
- Test generation and coverage recommendations.
- Integration with editors like VS Code or Neovim.
- Retrieval-Augmented Generation (RAG) over project documentation.
- Streaming responses for a more interactive user experience.
Best Practices
- Choose a model optimized for code generation, such as CodeLlama or DeepSeek Coder.
- Keep prompts focused and include relevant context, such as language, framework, and coding standards.
- Validate AI-generated code through testing and review before using it in production.
- Handle network errors and unavailable Ollama services gracefully in your application.
- Use streaming responses for improved responsiveness in interactive tools.
- Consider maintaining conversation history to make the assistant aware of previous interactions.
Limitations
Running models locally provides greater privacy and control, but it also has trade-offs:
- Larger models require significant RAM and disk space.
- Response speed depends on available CPU or GPU resources.
- AI-generated code can still contain bugs or insecure patterns.
- Smaller models may struggle with large codebases or highly specialized tasks.
- Long prompts may exceed the model's context window, depending on the selected model.
Conclusion
An AI pair programmer built with Ollama offers a private, customizable, and cost-effective alternative to cloud-based coding assistants. By combining Ollama's local model serving with a simple Python client, you can create a tool that explains code, generates implementations, debugs issues, writes tests, and assists throughout the software development lifecycle. As your needs grow, you can enhance the application with conversation memory, project indexing, editor integrations, and retrieval-augmented generation to create a more capable development assistant.
Passionate content creator with a keen interest in Artificial Intelligence, emerging technologies, trending news, and current affairs. I enjoy exploring the latest innovations, breaking down complex tech topics into engaging content, and sharing insightful perspectives on global trends. My goal is to create informative, easy-to-read, and impactful content that keeps readers updated with the fast-changing digital world.