I'm trying to use AI to generate boilerplate for a new Data Science project. However, the code often lacks comments or follows outdated libraries. How can I improve my prompt engineering to ensure the AI follows PEP 8 and uses the latest versions of Pandas and Scikit-learn?
3 answers
To get great code, your prompt engineering needs to be highly prescriptive. Don't just say "Write a script." Say "Act as a Senior Data Scientist. Write a Python script using Pandas 2.0+ that follows PEP 8. Include a docstring and type hints for every function." Providing the "latest library" context is crucial because the model's training data might be a year old. In my 2023 projects, I started including a "Coding Standards" section in every prompt. This simple prompt engineering tweak improved the quality of the generated boilerplate by a huge margin.
Have you tried the "Chain-of-Verification" method in your prompt engineering? It asks the model to generate the code, then find three potential bugs in its own work.
Always ask for "Modular" code. It forces the prompt engineering to produce small, testable functions instead of one giant, unmanageable script.
Modularization is key. I've found that combined with Pamela’s advice on PEP 8, it makes the prompt engineering much more useful for real-world projects.
I haven't tried CoVe yet, Douglas! Does it really work for syntax errors, or is it better for logical bugs? I’ll definitely incorporate a "self-correction" loop into my prompt engineering for the next data pipeline I build. It seems like a great way to handle those annoying little edge cases that the AI usually misses on the first pass, like handling empty CSV files or malformed date strings.