Coding with LLMs

Prerequisites

Before starting this lesson, you should be familiar with:

Learning Objectives

After completing this lesson, learners should be able to:
  • Use a large language model (LLM) to create bioimage analysis code

  • Use a LLM to understand bioimage analysis code

Motivation

Creating and understanding bioimage anlaysis code can be very challenging, especially for coding beginners. Large language models (LLMs) are very useful tools to generate code based on instructions formulated in normal language. Moreover, LLMs can also be asked to explain parts of the code in normal language. Therefore, using LLMs can save a lot of time when creating code for bioimage analysis or other tasks.

Concept map

graph TD P("Prompt") --> LLM("Large language model (LLM)") LLM --> C("Code & explanations")



Figure


The figure was created by an AI model. It should show how a human asks a computer to create code. This figure could be improved... :-)






Activities

Create code using a LLM

Ask an LLM to create code that implements a bioimage analysis task.

Activities that could be solved using a LLM:

Fixing errors:

Understanding the code:


Show activity for:  

chatGPT python

  • Go to the openAI chatGPT website: https://chat.openai.com/
    • You may have to log in, e.g. using your Google account, but it should be for free
    • You may also have to solve some annoying puzzles to prove that you are human
  • Get started:
    • Prompt: Which programming languages and which libraries would you recommend for bioimage analysis?
    • Compare what other course participants got; is it identical?
  • Now ask it to write some code:
    • Prompt: Hi, please write python code that opens and displays the image at https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__nuclei_high_dynamic_range.tif
    • Execute the code in a python environment (e.g. the one described here) and check whether it runs!
      • If it does not run, you may need to tell which libraries you have in your environment, e.g. let it know how you installed your conda environment
      • You may also ask it how to install the libraries needed to run the code (often it tells you anyway)
    • Even if it runs, you may want to ask it to use different libraries for certain tasks
    • Ask it to explain you some lines of code in detail
  • Experiment with different prompts for the same task, e.g.
    • Tell it that you are a beginner and would like to have everything explained in detail
    • Tell it you don’t have much time and you only want the most concise code, no explanations
    • Tell it that you would like to see three different implementations of the same task
  • Continue by asking it to write code for more complex image analysis workflows as suggested in the activity preface






Assessment

True or false

  1. If you ask a large language model to generate code for you it will reprodcibly give you the exact same answer.
  2. The code that is produced by a large language model can be used to do reproducible science.

Solution

  1. This is generally wrong; several common models have some randomicity in their outputs.
  2. This is true; you just have to store code once it has been generated, e.g. using version control systems such as git.




Follow-up material

Recommended follow-up modules:

Learn more: