It's time for our first LM experiment at this point.
So some of this you may know well, you may know very well already.
For some people this might be new, but let me just explain.
The models that we're going to be using.
These frontier models have been trained in a particular way.
That means that they expect two different types of instruction from us the user.
One of them is known as the system prompt, and one of them is known as the user prompt.
The system prompt is something which explains the context of this conversation.
It tells them what kind of task they're performing, what tone they should use, and we'll be experimenting
with what it means to to change a system prompt and what kind of information that you can include in
the system prompt throughout this course.
The user prompt is the actual conversation itself.
And in our case right now, it's going to just be the the conversation starter.
And the role of the LM of the large language model is to figure out what is the most likely way that
it should respond, given this user prompt.
If it's given this user prompt, and in the context of this system prompt, what is the most likely
next text that will come after it?
That would come from an assistant responding to this user.
So that's the difference between the system prompt that sets the context, the user prompt that is the
conversation starter.
So we're going to set a system prompt.
And this is what it's going to say.
It's going to say you are an assistant that analyzes the contents of a website and provides a short
summary, ignoring texts that might be navigation related.
Respond in markdown.
You'll see more of what that means in in just a second.
So that is our system prompt for the user prompt.
It's going to take as a we're going to write a function user prompt for.
And it's going to take a website as the argument to the function.
And it's going to say you are looking at a website titled The Website.
The contents of this website is as follows.
Please provide a short summary of the website in markdown if it includes news or announcements.
Summarize these two and we then take the text from the website object that Beautifulsoup plucked out
for us, and we add that into the user prompt and we return that user prompt.
So let's just quickly let's run that cell right now and let's just have a look now.
So after doing that, if I just look at what system Prompt has.
It has that text of course that we just said.
And now if you remember earlier on we created a new website object and we stored it in this variable
editor.
So if I come here I should be able to say user prompt for and then pass in the object Ed.
And what we'll get is a prompt.
It might be easier if I print this so that it prints out empty lines.
And here is the user prompt string that we've created.
It says you're looking at a website titled blah blah blah.
The contents of this website is as follows.
Please provide a short summary.
Look, it looks like we should have a space right here, otherwise it might be confusing.
Let's try that again.
That's always why it's worth printing things as you go, because you'll spot little inconsistencies
like that.
I think it'll be nicer, actually, now that I look at that.
If we have a carriage return there like so.
Let's have a look at this prompt.
Now you're looking at the website and there we go on a separate line that looks good okay.
So let's talk about the messages object.
So OpenAI expects to receive a conversation in a particular format.
It's a format that OpenAI came up with and they used for their APIs, and it became so well used that
all of the other major frontier models decided to adopt the same convention.
So this has gone from being originally OpenAI's way of using the API to being something of a standard
across many different models to use this approach.
And here's how it works.
When you're trying to describe a conversation, you describe it using a list a Python list of dictionaries.
So it's a list where each element in the list is a dictionary.
And that dictionary looks like this.
It's a dictionary with two elements.
One of them has a key of role, and here the value is either system or user, a key of role.
And the value is system a key of content.
And the value is of course the system message.
There's another Dictionary where there's a key of role.
The value is user because it's the user message.
The user prompt content is where the user message goes.
User message and user prompt are the same thing.
So hopefully I didn't explain it very well, but it makes sense when you see it visually like this.
It's just a dictionary which has role and content, system and system, message user and the user message.
And there are some other roles as well, but we're going to get to them in good time.
This is all we need for now.
So this is how messages are built.
And if you look at this next function def messages for hopefully it's super clear to you that this is
creating.
This here is creating exactly this construct using code.
It's going to do it's going to put in there the generic system prompt we came up with.
And it's going to create the user prompt for the website.
So let's run that.
And now, presumably it's clear that if I say messages for Ed, which is the object for my website,
let's print it so that we see empty lines and stuff.
Actually, sorry, in this case it might be better if we don't print it.
If we just do this, it might look a bit clearer.
There we go.
And now you can see that it is it's a list of two things role system.
And there's a system message role user.
And there is the user message.
Okay.
It's time to bring this together.
It's time to actually do it.
The API for OpenAI to make a call to a frontier model to do this for us is super simple, and we're
going to be using this API all the time.
So whereas now it might look like it's a few things to remember.
You're going to get so used to this, but we're going to make a function called summarize.
And that is that's going to do the business that's going to solve our problem and summarize a URL that's
passed in.
It will first create a website for that URL, just like we did for editor.
And this is where we call OpenAI.
We say OpenAI, which is the the OpenAI object.
We created OpenAI dot chat, dot completions, dot create.
And that for now you can just learn it by rote.
We'll understand a lot more about that later.
But as far as OpenAI is concerned, this is known as the completions API because we're asking it to
complete this conversation, predict what would be most likely to come next.
We pass in the name of the model we're going to use.
We're going to use a model called GPT four mini that you'll get very familiar with.
It is the light, cheap version of GPT four, the the one of the finest models on the planet, and this
will cost fractions of a cent to use.
This, um, you pass in the model and then you pass in the messages and the messages we pass in, use
this structure that we've just created and that is all it takes.
What comes back we put in this this object response.
And when we get back the response we call response dot choices zero dot message dot content.
Now I'm going to explain what this is another day we don't need to know.
For now.
We just need to know that we're going to do response dot choices zero dot message dot content.
That's going to be it.
That is our summarize function.
And with that let's try summarizing my website we're running.
It's now connecting to OpenAI in the cloud.
It's making the call and back.
Here is a summary of my website.
We have just uh, spent a fraction of a cent and we have just summarized my website.
We can do a little bit better because we can print this in a nice style.
Uh, GPT four, we've asked to respond in markdown, and that means that it's responded with various
characters to represent headings, things in bold and so on.
And we can use a feature of Jupyter Labs that we can ask it to actually show that in a nice markdown
format.
So let's do that.
Let's use this display summary function and try again.
Again we're going to GPT for a mini in the cloud.
And here is a summary of my website.
Uh, it says something about me.
Uh, and it's uh yeah, very nicely formatted, very nicely structured.
Pretty impressive.
And apparently it highlights my work with proprietary LMS, offers resources related to AI and LMS,
showcasing his commitment to advancing knowledge in this field.
Good for you, GPT for mini.
That's a very nice summary.
Okay.
And now we can try some more websites.
Let's try summarizing cnn.com.
Uh, we'll see what this happens.
Obviously, CNN is a much bigger, uh, result you've got here.
Uh, and, uh, we get some information about what's going on.
I'm actually recording this right now on the 5th of November at, uh, in the evening, which is the
date of the 2024 elections going on right now.
So that, of course, is featured on CNN's web page.
We can also summarize anthropic, which is the website for Claude.
And they have a nice page.
And here you go.
And you can read more about it in this nice little summary of their web page.
All right.
And that wraps up our first instant gratification.
It's it's juicy.
It's something where we've actually done something useful.
We've scraped the web.
We've summarized summarization is one of the most common AI use cases.
So common it's useful for all sorts of purposes.
We'll be doing it a few different ways during during this course, even in our week eight a sticky solution
will be using something that will do some summarization.
So it's a great, uh, thing to have experimented with already.
So there are so many other business applications of summarization.
This is something you should be able to put to good use.
You should be able to think of some ways you could apply this to your day job right away, or be building
a couple of example projects in GitHub that show summarization in action.
You could apply it to summarizing the news, summarizing financial performance from a financial report,
a resume, and a cover letter.
You could you could take a resume and generate a cover letter.
Uh, there are so many different things you can do with summarization of of documents.
And also adding on to that the scraping the web angle of it.
So have a think about how you would apply summarization to your business and try extending this to do
some summarization.
There's also uh, for for the more technically inclined, uh, one of the things that you'll discover
quite quickly when you use this is that there are many websites that cannot be summarized with this
approach, and that's because they use JavaScript to render the web page and are rather simplistic.
Approach has just taken the the just just made the requests the server call and taken what we get back.
But there's a solution.
And the solution is to use a platform like selenium or others like it, or playwright, which would
allow you to render the page and and do it that way.
So if you're technically inclined and have some background with that kind of thing, then a really interesting
challenge is to turn this into something that's a bit beefier and add selenium to the mix.
Um, as it happens, someone has already done that.
Uh, one of the students, thank you very much.
And if you go into this folder community contributions, you'll see a few different solutions.
And one of them is a selenium based solution.
So you can always go in and just just look at that yourself.
Or you can have a shot at doing it too.
And you'll find the solution in there.
And if you do come up with a solution to that or to anything, I would love it if you were willing to
share your code so that others can benefit from it.
Ideally, put it in the community contributions folder and be sure to clear the output.
So you go to kernel restart kernel and clear outputs of all cells.
Otherwise, everything that you've got in your output would also get checked into code which which would
just clutter things up a bit.
So so do that.
And then if you could submit a PR, a pull request, I can then merge that into the code.
And if that's a new thing for you, it is a bit of a process.
There is a write up here for exactly what you need to do to make that work.
Anyways, this was the first project, the first of many.
It's a simple project, but it's an important one.
A very important business use case.
I hope you found it worthwhile.
I will see you for the next video when we wrap up.
Week one.
Day one.