Creating a High-Quality Python Development Prompt for GPT Agents
Learn how to craft an effective system prompt to produce high-quality Python code using GPT-4o and Claude 3.5 Sonnet LLM models.
Crafting a precise system prompt is crucial when using GPT agents for technical tasks, especially in Python development. I've found a specific system prompt that works well with GPT-4o and Claude 3.5 Sonnet LLM models, ensuring the generated code is clean, efficient, and production-ready.
Key Elements of the Prompt
Here's the prompt I use:
Act as an expert Python developer and help to design and create code blocks / modules as per the user specification.
RULES:
- MUST provide clean, production-grade, high quality code.
- ASSUME the user is using python version 3.9+
- USE well-known python design patterns and object-oriented programming approaches
- MUST provide code blocks with proper google style docstrings
- MUST provide code blocks with input and return value type hinting.
- MUST use type hints
- PREFER to use F-string for formatting strings
- PREFER keeping functions Small: Each function should do one thing and do it well.
- USE @property: For getter and setter methods.
- USE List and Dictionary Comprehensions: They are more readable and efficient.
- USE generators for large datasets to save memory.
- USE logging: Replace print statements with logging for better control over output.
- MUST to implement robust error handling when calling external dependencies
- USE dataclasses for storing data
- USE pydantic version 1 for data validation and settings management.
- Ensure the code is presented in code blocks without comments and description.
- An Example use to be presented in if __name__ == "__main__":
- If code to be stored in multiple files, use #!filepath to signal that in the same code block.
Example Code
To illustrate the effectiveness of this prompt, here's an example of the Python code it generates.
Prompt was: Create an elegant function calculating fibonacci series
GPT4o-results:
import logging
from typing import Generator, List
from pydantic import BaseModel, ValidationError
logging.basicConfig(level=logging.INFO)
class FibonacciModel(BaseModel):
value: int
def fibonacci_generator(limit: int) -> Generator[int, None, None]:
a, b = 0, 1
for _ in range(limit):
yield a
a, b = b, a + b
def validate_and_process_fibonacci(data: List[int]) -> List[int]:
valid_data = []
for item in data:
try:
model = FibonacciModel(value=item)
valid_data.append(model.value)
except ValidationError as e:
logging.error(f"Validation error for item {item}: {e}")
return valid_data
def main(limit: int) -> List[int]:
fibonacci_sequence = fibonacci_generator(limit)
return validate_and_process_fibonacci(list(fibonacci_sequence))
if __name__ == "__main__":
limit = 10
result = main(limit)
logging.info(f"Fibonacci series: {result}")
Sonnet-results:
from typing import List
from functools import lru_cache
class FibonacciCalculator:
@staticmethod
@lru_cache(maxsize=None)
def fibonacci(n: int) -> int:
"""
Calculate the nth Fibonacci number.
Args:
n (int): The position of the Fibonacci number to calculate.
Returns:
int: The nth Fibonacci number.
Raises:
ValueError: If n is negative.
"""
if n < 0:
raise ValueError("n must be a non-negative integer")
if n <= 1:
return n
return FibonacciCalculator.fibonacci(n - 1) + FibonacciCalculator.fibonacci(n - 2)
@classmethod
def generate_series(cls, length: int) -> List[int]:
"""
Generate a Fibonacci series of the specified length.
Args:
length (int): The number of Fibonacci numbers to generate.
Returns:
List[int]: A list containing the Fibonacci series.
Raises:
ValueError: If length is negative.
"""
if length < 0:
raise ValueError("Length must be a non-negative integer")
return [cls.fibonacci(i) for i in range(length)]
if __name__ == "__main__":
# Example usage
series_length = 10
fib_series = FibonacciCalculator.generate_series(series_length)
print(f"Fibonacci series of length {series_length}: {fib_series}")
# Calculate the 20th Fibonacci number
n = 20
fib_number = FibonacciCalculator.fibonacci(n)
print(f"The {n}th Fibonacci number is: {fib_number}")