First dab at playing with an LLM

Author

Marie-Hélène Burle

In this section, we will download a text from a URL and get some info on it.

Open a chat

Open an LLM chat interface. If you are familiar with a particular LLM, feel free to use that one. If you have never used an LLM before, you can pick one in the non-exhaustive list I gave in the previous section.

Some interfaces have options worth looking into. Le Chat from Mistral for instance will not search the web automatically. This means that it will give answers based only on the data used to train the model. This works well in many situations, but it does not work when searching for events that happened after the data to train the model was harvested from the internet. To ask Python questions, not using the web search option is usually fine, but if you are looking for information on a new library or on a library that recently got updated, the answers you will get will be out of date.

It would be great if you could sit next to someone using a different model so that you could compare the outputs given by both models to similar prompts. Compare the results and talk with each other. Experiment and play with these tools to see how they work and how you can make them as useful to you as possible.

Open a notebook

Open a new notebook in JupyterLab.

When the LLM gives us some code, we will copy paste it in the notebook to run it.

LLM as instructor

LLMs are great at teaching programming concepts. For instance, you learnt about loops this morning. Let’s ask our LLM to explain Python loops.

Here is the question I asked the LLM:

Explain to me how to write Python loops.

Your turn:

Ask questions about variables, data types, dictionaries, and other things you learnt this mourning. In particular, ask questions about things you didn’t fully understand.

Now, let’s learn something that wasn’t covered this mourning: writing Python functions.

Your turn:

  • Ask the LLM what Python functions are.

  • Ask how to write them.

  • Ask the model to write a Python function that would give you the time.

LLM as coder

Download a text file from a URL

The snippet of text we will play with is in a text file I created containing the very beginning of the novel Going Postal by Terry Pratchett. I made it available at the URL https://mint.westdri.ca/python/data/pratchett.txt.

Let’s ask our LLM how to do this. Here is the prompt I use (feel free to write your own prompt):

How can I download the text from this url: https://mint.westdri.ca/python/data/pratchett.txt in Python?

On my end, I got some nice explanations and the following code:

import requests

url = "https://mint.westdri.ca/python/data/pratchett.txt"
response = requests.get(url)
text = response.text  # This contains the text content as a string

print(text)  # Prints the downloaded text
They say that the prospect of being hanged in the morning concentrates a man's mind wonderfully; unfortunately, what the mind inevitably concentrates on is that, in the morning, it will be in a body that is going to be hanged.
The man going to be hanged had been named Moist von Lipwig by doting if unwise parents, but he was not going to embarrass the name, insofar as that was still possible, by being hung under it. To the world in general, and particularly on that bit of it known as the death warrant, he was Alfred Spangler.
And he took a more positive approach to the situation and had concentrated his mind on the prospect of not being hanged in the morning, and, most particularly, on the prospect of removing all the crumbling mortar from around a stone in his cell wall with a spoon. So far the work had taken him five weeks and reduced the spoon to something like a nail file. Fortunately, no one ever came to change the bedding here, or else they would have discovered the world's heaviest mattress.
It was a large and heavy stone that was currently the object of his attentions, and, at some point, a huge staple had been hammered into it as an anchor for manacles.
Moist sat down facing the wall, gripped the iron ring in both hands, braced his legs against the stones on either side, and heaved.
His shoulders caught fire, and a red mist filled his vision, but the block slid out with a faint and inappropriate tinkling noise. Moist managed to ease it away from the hole and peered inside.
At the far end was another block, and the mortar around it looked suspiciously strong and fresh.
Just in front of it was a new spoon. It was shiny.
As he studied it, he heard the clapping behind him. He turned his head, tendons twanging a little riff of agony, and saw several of the wardens watching him through the bars.
"Well done, Mr. Spangler!" said one of them. "Ron here owes me five dollars! I told him you were a sticker!! 'He's a sticker,' I said!"
"You set this up, did you, Mr. Wilkinson?" said Moist weakly, watching the glint of light on the spoon.
"Oh, not us, sir. Lord Vetinari's orders. He insists that all condemned prisoners should be offered the prospect of freedom."
"Freedom? But there's a damn great stone through there!"
"Yes, there is that, sir, yes, there is that," said the warden. "It's only the prospect, you see. Not actual free freedom as such. Hah, that'd be a bit daft, eh?"
"I suppose so, yes," said Moist. He didn't say "you bastards." The wardens had treated him quite civilly these past six weeks, and he made a point of getting on with people. He was very, very good at it. People skills were part of his stock-in-trade; they were nearly the whole of it.
Besides, these people had big sticks. So, speaking carefully, he added: "Some people might consider this cruel, Mr. Wilkinson."
"Yes, sir, we asked him about that, sir, but he said no, it wasn't. He said it provided"--his forehead wrinkled "--occ-you-pay-shun-all ther-rap-py, healthy exercise, prevented moping, and offered that greatest of all treasures, which is Hope, sir."
"Hope," muttered Moist glumly.
"Not upset, are you, sir?"
"Upset? Why should I be upset, Mr. Wilkinson?"
"Only the last bloke we had in this cell, he managed to get down that drain, sir. Very small man. Very agile."

Note that the comments in the code are not mine but from the LLM, which is nice.

It looks like the code works.

Count a few things

Now, let’s count the number of words in the text.

I asked the follow-up question (here again, you can phrase the question however you want. It is good to experiment):

Write Python code to count the number of words in this text.

Here again, I got some explanations and the following code:

# Split the text into words using whitespace
words = text.split()
word_count = len(words)

print(f"Number of words in the text: {word_count}")
Number of words in the text: 590

Write Python code to find the number of times the word “the” occurs in the text.

Here, the LLM warned me that the following answer counts all instances of “the” (ignoring case) and that if this was not what I wanted, I just had to say so in a follow-up chat entry. This is what I wanted, so I am keeping the code as is:

import re

# Use regular expressions to find all whole-word, case-insensitive matches of "the"
the_count = len(re.findall(r'\bthe\b', text, flags=re.IGNORECASE))

print(f'The word "the" occurs {the_count} times in the text.')
The word "the" occurs 36 times in the text.

Your turn:

  • Ask your LLM to explain the regular expression in the code.

  • We all know that LLMs hallucinate and make plenty of mistakes. You should never trust the code (or any LLM answer for that matter) uncritically. How could you double-check that this code is correct?

Extracting quotes

Now, let’s extract all quotes from the text.

My prompt:

Write Python code that would extract all the quotes from the text

Here is the code I got in the answer:

# Extract all quotes enclosed in double quotes
quotes = re.findall(r'"(.*?)"', text)

# Example: print the first 10 quotes
for quote in quotes[:10]:
    print(quote)
Well done, Mr. Spangler!
Ron here owes me five dollars! I told him you were a sticker!! 'He's a sticker,' I said!
You set this up, did you, Mr. Wilkinson?
Oh, not us, sir. Lord Vetinari's orders. He insists that all condemned prisoners should be offered the prospect of freedom.
Freedom? But there's a damn great stone through there!
Yes, there is that, sir, yes, there is that,
It's only the prospect, you see. Not actual free freedom as such. Hah, that'd be a bit daft, eh?
I suppose so, yes,
you bastards.
Some people might consider this cruel, Mr. Wilkinson.

Your turn:

  • This looks good, but what if we want all the quotes from the text?

  • What does quotes[:10] mean?

  • What kind of structure is this:

for quote in quotes[:10]:
    print(quote)
  • How do such structures work?

LLM as debugger

Here is a Python code snippet:

import string

clean_text = text.translate(str.maketrans(string.punctuation))
print(clean_text)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[5], line 3
      1 import string
----> 3 clean_text = text.translate(str.maketrans(string.punctuation))
      4 print(clean_text)

TypeError: if you give only one argument to maketrans it must be a dict

It doesn’t work 🙁

Your turn:

  • What is wrong with it?

  • What would be the proper syntax?

  • What does that code do?