Code Prompt Library
Boost your development workflow with AI-powered code generation, debugging, and optimization prompts. From simple scripts to complex architectures.
89 Code Prompts Available
All
89
Python
24
JavaScript
18
API
15
Debugging
12
Web Dev
20
Python • API
(31)
OpenAI API Integration with Python
Write a Python function that connects to the OpenAI API, handles errors, and processes the response. Include rate limiting and retry logic.
Python
import openai
import time
from datetime import datetime
class OpenAIClient:
def __init__(self, api_key):
openai.api_key = api_key
self.rate_limit_delay = 1 # seconds between requests
self.max_retries = 3
def call_api(self, prompt, model="gpt-3.5-turbo"):
for attempt in range(self.max_retries):
try:
# Add rate limiting
time.sleep(self.rate_limit_delay)
response = openai.ChatCompletion.create(
model=model,
messages=[{"role": "user", "content": prompt}],
max_tokens=500
)
return {
"success": True,
"content": response.choices[0].message.content,
"model": response.model,
"tokens_used": response.usage.total_tokens
}
except openai.error.RateLimitError as e:
if attempt < self.max_retries - 1:
wait_time = 2 ** attempt # Exponential backoff
print(f"Rate limit hit. Waiting {wait_time} seconds...")
time.sleep(wait_time)
else:
return {"success": False, "error": "Rate limit exceeded after retries"}
except Exception as e:
return {"success": False, "error": str(e)}
return {"success": False, "error": "Max retries exceeded"}
# Usage example
client = OpenAIClient("YOUR_API_KEY")
result = client.call_api("Explain quantum computing in simple terms")
if result["success"]:
print(result["content"])
else:
print(f"Error: {result['error']}")
Example Input:
Prompt: "Explain quantum computing in simple terms"
Model: GPT-3.5-Turbo or GPT-4
Max Retries: 3 with exponential backoff
JavaScript • Web Dev
(27)
AI Chat Interface Component
Create a React component for an AI chat interface with message history, typing indicators, and markdown rendering.
JavaScript
import React, { useState, useEffect } from 'react';
import ReactMarkdown from 'react-markdown';
import './ChatInterface.css';
const ChatInterface = ({ messages = [], isTyping = false, onSendMessage }) => {
const [inputValue, setInputValue] = useState('');
const handleSubmit = (e) => {
e.preventDefault();
if (inputValue.trim()) {
onSendMessage(inputValue);
setInputValue('');
}
};
return (
<div className="chat-container">
<div className="message-history">
{messages.map((msg, idx) => (
<div key={idx} className={`message-bubble ${msg.role}`}>
<div className="message-role">
{msg.role === 'user' ? 'You' : 'Assistant'}
</div>
<div className="message-content">
<ReactMarkdown>{msg.content}</ReactMarkdown>
</div>
<div className="message-time">
{msg.timestamp}
</div>
</div>
))}
{isTyping && (
<div className="typing-indicator">
<div className="typing-dots">
<span></span><span></span><span></span>
</div>
AI is thinking...
</div>
)}
</div>
<form onSubmit={handleSubmit} className="message-input-form">
<textarea
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
placeholder="Type your message here..."
rows="3"
/>
<button type="submit" disabled={!inputValue.trim()}>
Send
</button>
</form>
</div>
);
};
export default ChatInterface;
Example Usage:
Messages Array: [{role: 'user', content: 'Hello AI!', timestamp: '10:30 AM'}]
Features: Markdown rendering, typing indicator, message history
Debugging • Python
(19)
Debug Python Code with AI
Analyze this Python code, identify bugs, suggest fixes, and explain the reasoning behind each change.
Python
from langchain_openai import ChatOpenAI
import openai
# Buggy code to analyze and fix:
openai.api_key = "YOUR_API_KEY"
chat_history = [
"What is your name?",
"How are you today?",
]
# Initialize the chat model
chat_openai = ChatOpenAI(model_name="gpt-3.5-turbo")
# This line has incorrect method name and parameter format
response = chat_openai.chatCompletion( # Should be: chat_openai() or chat_openai.predict()
model="gpt-3.5-turbo",
messages=chat_history,
)
print(response)
print(response.choices[0].message.content)
# Issues to fix:
# 1. Incorrect method call - should use __call__() or predict()
# 2. chat_history format is wrong - should be list of dictionaries
# 3. No error handling
# 4. Incorrect attribute access for response
Common Issues to Fix:
Method Name: chatCompletion → predict() or __call__()
Message Format: Strings → Dictionaries with role/content
Response Access: response.choices[0].message.content → response.content
Code Playground
Test these prompts directly in our interactive code playground. Run Python, JavaScript, and other languages with real-time AI assistance.
Launch Code PlaygroundPart of the MasteringOpenAI.com Prompt Library • Explore all categories • Submit your own prompt