4  GenerativeAI for Python

Generative AI, GenAI, is a type of artificial intelligence that doesn’t just analyse data and make decisions, it creates new things from scratch. Think of it as a super-smart assistant that can produce original content, whether it’s text, images, music, or even complex problem-solving ideas.

You have probably encountered genAI without even realising it. Have you ever used an app that enhances your photos or turns them into stunning pieces of art? Or maybe you’ve interacted with a chatbot that felt almost human in its responses. These are everyday examples of how generative AI weaves its magic into our lives. It is like having a digital Michelangelo, Mozart, and Shakespeare all rolled into one, ready to bring your ideas to life.

Now, you might wonder how this digital wizardry actually works. At its core, genAI learns by mimicking the way our brains process information. It studies vast amounts of data like books, images, or songs and identifies patterns and structures within them. Then, when you give it a prompt or a task, it uses what it has learned to create something new. It is as if you taught a robot how to cook by showing it thousands of recipes, and now it can whip up gourmet dishes on its own!

In this chapter you will learn how to use GenAI to write programming code, debug errors, transform dataset among many other time consuming tasks. By leveraging generative AI, you can accelerate data science workflows, quickly prototype solutions using libraries like Pandas and NumPy, and focus more on interpreting results rather than getting bogged down in coding intricacies.

4.1 What is a prompt?

In the realm of genAI, the concept of a prompt is fundamental to the interaction between the user and the AI model. A prompt serves as a specific input or instruction that guides the AI in generating the desired output.

A prompt is typically a textual phrase, sentence, or question provided by the user to the AI model. It functions as a catalyst, requesting the AI to commence its generative process. The prompt encapsulates the user’s intent, conveying it to the AI in a structured manner. For instance, if one inputs the prompt, “Compose an essay on the economic impacts of climate change,” the AI comprehends this instruction and endeavours to generate a corresponding essay, refer to the following figure.

GenAI process

The mechanism by which a prompt operates within a generative AI system involves several key stages:

  1. Input Reception: The user provides a prompt to the generative AI model. This input serves as the initial trigger for the model to begin processing.

  2. Interpretation: The AI model, equipped with advanced algorithms and trained on extensive datasets, interprets the prompt. This involves understanding the context, intent, and nuances embedded in the input.

  3. Completion: Guided by the prompt, the AI utilises its learned patterns and structures to generate new content. This content aligns with the parameters and expectations set forth by the prompt. For example, if the prompt is “Generate a painting description in the style of Vincent van Gogh,” the AI will produce a description that emulates van Gogh’s artistic style.

  4. Output Delivery: The AI presents the generated content to the user, thus fulfilling the prompt’s directive. This output is the materialisation of the user’s initial instruction as interpreted and created by the AI.

Prompt-based interaction
Using Generative AI. Who was Ada Lovelace?

Prompt Development: best practices

When utilising generative AI models, the quality and precision of the outcomes are heavily dependent on the prompts provided. Properly constructed prompts can significantly enhance the relevance, accuracy, and creativity of the generated content. The following best practices are recommended for formulating effective prompts in generative AI:

Use clear and specific language: Ensure that the prompt is devoid of ambiguity. Use precise and unambiguous language to clearly convey the intended instruction to the AI model. For example, instead of a vague prompt like “Write about technology,” a more specific prompt would be “Write an essay discussing the impact of quantum computing on modern cryptography.”

Focused Directives: Address a single task or concept at a time within the prompt. Multiple instructions within a single prompt may lead to confusion and suboptimal results from the AI.

Detailed Descriptions: Providing ample context within the prompt can guide the AI to produce more relevant and tailored outputs. Including specific details about the desired content, style, format, or relevant background information can significantly improve the quality of the generated result. For instance, “Generate a 300-word article on the benefits of renewable energy sources, with a focus on solar power.”

Provide Examples: When possible, include examples of the desired output to illustrate expectations clearly. This can help the AI model understand the format and style more effectively.

Concise yet Comprehensive Prompts: Strive to balance brevity with comprehensiveness. The prompt should be brief enough to remain clear and manageable, yet comprehensive enough to encapsulate all necessary details. Overly lengthy prompts may confuse the AI, while excessively short prompts may lack sufficient guidance.

Review and Adjust: Generative AI often requires iterative refinement. Review the initial outputs and adjust the prompt accordingly to achieve the desired result. Small modifications can make a significant difference. For instance, if the prompt “Describe a futuristic city” provides overly generic results, refining it to “Describe a futuristic city focusing on sustainable architecture and green technology” may yield better outcomes.

Incremental Complexity: Start with simpler prompts and gradually introduce complexity as the model demonstrates its capability to handle more detailed instructions.

Standardised Prompts: For tasks requiring consistent output, standardise the prompt structure. This approach is particularly useful in scenarios such as automated report generation or content curation, where uniformity is crucial.

Responsible Use: Encourage the generation of content that aligns with ethical standards and social responsibility. Avoid prompts that could lead to harmful, misleading, or inappropriate content.

Using Generative AI. Iterative Prompt Development

the following example illustrates an iterative prompt development

4.2 Generative AI as a coding companion

One of the most exciting roles for genAI is in helping people like you with software programming. GenAI can offer you code suggestions and autocompletion, making writing code faster and easier. It can detect and suggest fixes for bugs, saving you (some of) the frustration of debugging.

You can even describe what you need in plain English, and the AI generates the corresponding code. Additionally, it assists in code refactoring, making your code cleaner and more efficient. AI tools also aid in learning new programming languages or tasks by providing examples and explanations, automating documentation, and generating test cases. Essentially, generative AI streamlines the development process, enhances productivity, and facilitates continuous learning, making it an invaluable assistant for developers. It’s like having a supercharged co-developer who’s never too tired or too busy to help. This means more time for innovation and less time stuck on frustrating technical issues.

Next we will illustrate how to make the most of generative AI for coding purposes. You will develop and execute prompts on OpenaI ChatGPT, Google Gemini and Microsoft CoPilot.

4.2.1 OpenAI chatGPT

Surely you have used ChatGPT already. ChatGPT is a conversational AI model developed by OpenAI, basically designed to communicate with humans in a natural, human-like manner. It’s built on the GPT (Generative Pre-trained Transformer) architecture, and its main job is to understand the text provided by the user, generate appropriate and coherent responses, and assist with various tasks such as answering questions, offering explanations, and even carrying out some creative writing tasks.

OpenAI chatGPT. Signing Up

Feel free to signup on chatGPT if you haven’t done so before

Example (Learning new concepts)

You can use chatGPT to learn new programming concepts or clarify questions you might have.

For instance you can instruct chatGPT to help you with Python variables by issuing the following prompt: “types of variables in Python, provide examples”. ChatGPT, hopefully, will produce a response similar to the one in the figure below.

chatGPT response

Example (Develop new code)

You can use chatGPT to develop code for you

Let’s try the following prompt: “develop code in Python to: (1) take two variables, (2) compute the addition, (3) print the result”. You will get something similar to the figure below.

chatGPT response

4.2.2 Google Gemini

Google Gemini is Google’s next-gen AI conversational model. Think of it as Google’s answer to OpenAI’s GPT-4. It brings together the strengths of two prior AI systems: Google’s original AI research and DeepMind’s cutting-edge techniques from their “Gemini” project.

Google Gemini. Signing Up

Feel free to signup on Goole Gemini if you haven’t done so before

Example (Learning new concepts)

Indeed You can use Gemini to learn and practice software programming.

For instance you can instruct Gemini to practice mathematical operations with the following prompt: “Develop instructional material and exercises to learn and practice mathematical operations in Python”. Gemini, hopefully, will produce a response similar to the one in the figure below.

Gemini response

Example (Debug code)

You can use Gemini, as well as chatGPT, to help you debugging code.

Say we are finding that the following code returns a software error.

MyString='Hi there'
MyVariable=56

print("MyString"+ MyVariable)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[1], line 4
      1 MyString='Hi there'
      2 MyVariable=56
----> 4 print("MyString"+ MyVariable)

TypeError: can only concatenate str (not "int") to str

We can try to determine what is wrong with the code we have just developed by submitting the following prompt: “Help me debug the following code” including the previous code in the prompt

You will get something similar to the figure below.

Gemini response

4.2.3 Microsoft CoPilot

Microsoft Copilot is yet another generative AI assistant designed to enhance productivity and creativity across various Microsoft applications. Similarly to chatGPT and Gemini it leverages advanced language models, like GPT-4, to assist users in tasks such as generating text, summarising information, creating images, and writing code.

Microsoft CoPilot. Signing Up

Feel free to signup on CoPilot if you haven’t done so before

Example (Coding companion)

You can use CoPilot to help you implement high level requests in Python.

Say that you need to multiply two 3X3 Matrices and compute the inverse of the result.

You can instruct CoPilot to help you by issuing the following prompt: “Give Python code to multiply two 3X3 matrices, then compute the inverse of the result”

CoPilot response

Example (Code annotation)

You can use CoPilot to annotate code you have developed. For instance given the following code:

import math
import numpy
import random

random_number = random.randint(1, 100)

print(random_number)

random_number_log=math.log(random_number)

print(random_number_log)
25
3.2188758248682006

You can instruct CoPilot to help you annotating the previous code by issuing the following prompt: “Please annotate the following code” adding the previous code.

CoPilot will produce a response similar to the one in the figure below.

CoPilot response

4.3 Conclusion

This chapter introduces generative artificial intelligence (GenAI) and explores its transformative role in Python programming for data science. It explains how GenAI models such as OpenAI’s ChatGPT, Google’s Gemini, and Microsoft’s Copilot—can create new content from scratch, offering capabilities like writing code, debugging errors, and transforming datasets. The chapter emphasises the concept of a prompt as the fundamental way users interact with GenAI models and provides best practices for crafting effective prompts to achieve desired outcomes. In the next chapter you will meet Pandas, the swiss knife of data science.

4.4 Further Readings

Some additional references on the use of GenAI in data science contexts are:

OpenAI’s Documentation and Tutorials:

A Comprehensive guide on using OpenAI’s GPT models with Python, including code examples: https://platform.openai.com/docs/introduction.

A collection of Python code examples for interacting with OpenAI’s models: https://github.com/openai/openai-cookbook.

Hugging Face Transformers:

Learn how to use pre-trained generative models in Python: https://huggingface.co/docs/transformers/index

A free course on transformer models and how to implement them using Python: https://huggingface.co/course/chapter1

Microsoft Learn:

Guides on using OpenAI models with Python in Azure: https://learn.microsoft.com/en-us/azure/cognitive-services/openai/

Kaggle Notebooks:

Explore notebooks and datasets related to generative AI, with Python code examples: https://www.kaggle.com/search?q=generative+ai

Official Documentation for AI Assistants:

Learn how to use GitHub Copilot within your Python development workflow: https://docs.github.com/en/copilot

Guides on using generative AI models with Python on Google Cloud.: https://cloud.google.com/ai-platform/generative-ai