Using generative AI to write code: a guide for researchers

Some tips for making the most of generative AI from the Turing’s software engineers

Wednesday 07 Feb 2024

Filed under

Generative AI is increasingly used to draft, edit, annotate and debug code. It’s not just industry software developers who are taking advantage of the tools it has to offer. Those who develop and use software for academic research are benefitting from them too. So how do you make the best use of generative AI for coding in a research context? As software engineers with The Alan Turing Institute’s Research Engineering Group (REG) and PhD Enrichment scheme, we’ve been asking ourselves just this question. Here, we share our thoughts, along with some tips for researchers.

How can generative AI help?

In the same way that generative AI tools like ChatGPT and Midjourney can be used to generate or modify written text, images or video – based on a ‘prompt’ – they can be used to generate or modify code. For example, using a section of code as a prompt, you can add “explain each line of this code”, to which the AI will respond with a line-by-line explanation, perhaps modifying the code itself with helpful comments. These new tools can help to reduce the time spent trawling through search engine results, reading online tutorials and posting coding questions on websites like Stack Overflow. For researchers, they mean less time spent learning to code and more time on the research questions at hand.

Which tools can I use?

As well as website-hosted chatbots like ChatGPT and Google Bard, you might consider GitHub Copilot, which is available as a plugin for code editors such as Visual Studio Code and can generate code suggestions as you type.

Another option is Ollama, a downloadable software allowing you to install and run various open-source models on your own computer, including Code Llama and WizardCoder. These specialised code generation tools can be particularly useful when working with code that shouldn’t be shared publicly (or if you’d rather your code wasn’t used by the tool provider for further training of their models).

All these tools rely on effective ‘prompt engineering’ to achieve the best results.

What tasks can I get help with?

  • Basic scripts and queries: Generative AI tools excel in cases when the code logic can be defined succinctly in written language, but you don’t know the precise syntax for the programming language in question. For example, a quick query of a database can be much faster if you can translate to SQL from plain English!
  • Incremental changes: Combining generative AI with version control systems like Git allows you to make gradual changes to your code whilst ensuring you retain an understanding of how it has changed. With GitHub Copilot, you can even automate the naming of commit messages used to label code changes (though we’ve found it doesn’t always describe the change well).
  • Best practices: You can use generative AI to reduce the time you spend documenting your work and ensuring that your methods and results can be reproduced by others. Try prompting it with a sample of the code and instructing it to write software tests, documentation or tutorials. You will often get more comprehensive results than if you had carried out these tasks manually.
  • Debugging: To fix an error in your code, prompt generative AI with the error message alongside an explanation of what you’re trying to achieve and the piece of code being run. This will likely result in a suggested set of steps to debug the issue.
  • Optimisation: To optimise computer resource usage, performance (e.g. speed), or readability, try writing a prompt including some code and a note. Pair this approach with testing to ensure suggestions work as expected. 
  • Working with web frameworks: Even the most comprehensively documented frameworks can be laborious to navigate, especially if you’re new to them. We’ve found ChatGPT particularly helpful for modifying code based on common web frameworks (such as Django). It can tell you which parts of the code (and files) to edit, and which language to use for particular tasks.
  • Making use of the language model capability: For example, a prompt to generate a database query could read “group these table data rows based on the words in column_x, with synonyms in the same group”. To construct such a query manually would involve several prerequisite steps, including finding and importing a language package, and figuring out the correct function to use.
  • Academic writing: The good news is you no longer need to learn how to code that fiddly table in LaTeX for your manuscripts! And of course, you can always use generative AI to refine your abstract.

What should I be wary of?

  • Accuracy: Proposed coding solutions may sometimes be wrong or not work as described by the AI. If you’re struggling to generate solutions to novel or more complex coding challenges, try prompting with questions that aid your understanding of the problem.
  • Iteration: When ChatGPT is asked to improve code generated in response to a previous prompt it may add new lines of code – but check it hasn’t left out crucial parts of the previous iteration. Point out the mistake and ask it to incorporate the missing code.
  • Data security: Any prompts you type into ChatGPT will be saved in the search history and likely persist in OpenAI’s databases, so before pasting in any sensitive or confidential information, ask yourself whether you would be comfortable posting it on Stack Overflow or social media. (If not, use models that run locally, such as those installed with Ollama.)
  • Code security: It may be appropriate to test AI-generated code in a sandboxed environment to ensure that code including potential IT vulnerabilities does not pose a security threat to any important computer systems.
  • Copyright: Accidentally or otherwise, generative AI models may include code in their training data that comes from repositories with restrictive licences. Whilst there’s no obvious solution to this issue, it’s something that’s worth being aware of if you intend to publish your code.
  • Cost: Copilot is not currently free for everyone, whilst to use the best ChatGPT model you need an OpenAI subscription. Where cost is a factor, Ollama or other free, open-source tools may be more appropriate.

 

Top image: Christina Morillo