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")
Figure
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:
Tips for creating the prompt:
- Often the instructions given in the activity can be a good starting point to create the LLM prompt.
- Tell the LLM in which programming language you would like the code to be implemented.
- Sometimes it may also be good to tell it which libraries to use. For example, chatGPT seems to like “openCV” while “skimage” is more what we may typically use in bioimage anlaysis.
- Tell the LLM where exactly the input data is stored on your computer such that it can write the code to open the data.
Fixing errors:
- If you get an error executing the code, create a new prompt with this error and ask the LLM to fix it.
Understanding the code:
- If you do not understand parts of the code ask the LLM to explain them to you.
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.
- To get started, enter the following prompt:
Which programming language and which libraries would you recommend for bioimage analysis?
- You may get an answer such as: “For bioimage analysis, Python is a popular choice due to its versatility, extensive libraries, and ease of use. Here are some recommended libraries: 1. scikit-image: This library provides a collection of algorithms for image processing. It’s user-friendly and efficient, making it suitable for various tasks in bioimage analysis. 2. OpenCV: OpenCV is a powerful library for computer vision tasks, including image processing and analysis. It offers a wide range of functionalities for tasks such as image segmentation, feature extraction, and object detection. 3. NumPy: NumPy is essential for scientific computing in Python. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays efficiently…”.
- Check whether your answer differs from this, also compare what other course participants got.
- Now ask it to create some code:
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
- Depending on the python environment that you are working with you may need to specify which library to use to open the image also how to display it.
- Continue by asking it to write code for more complex image analysis workflows as are suggested in the activity preface.
Assessment
True or false
- If you ask a large language model to generate code for you it will reprodcibly give you the exact same answer.
- The code that is produced by a large language model can be used to do reproducible science.
Solution
- This is generally wrong; several common models have some randomicity in their outputs.
- This is true; you just have to store code once it has been generated, e.g. using version control systems such as git.
Explanations
Follow-up material
Recommended follow-up modules:
Learn more: