Aside: Few Shot Approach

Tom Chant
InstructorTom Chant
Share this video with your friends

Social Share Links

Send Tweet
Published 8 months ago
Updated 8 months ago

In this lesson we explore the challenges of using the zero-shot approach for complex requests and discover a more effective strategy called the few-shot approach.

Tom introduces an app called Advertify, which generates advertising copy based on user inputs. However, when using the zero-shot approach, the results often fall short of expectations, resulting in off-topic or incomplete responses. To address this, Tom demonstrates how to utilize examples within the prompt to guide OpenAI's understanding of the desired output.

By providing a complete example and then a partially completed one with variables, the AI can generate advertising copy tailored to the given inputs. Tom also suggests separating the instruction, context, and completion requests using hash symbols for better readability.

This few-shot approach proves to be more reliable in producing the desired copy, as demonstrated in the example of generating advertising content for a vegan fish cream product. This lesson also introduces the ability to add multiple examples for further contextual guidance and emphasizing the importance of considering prompt length due to cost implications.

[00:00] So far, when writing prompts, we've just provided one or more sentences asking for what we want. You'll often hear this referred to as the zero-shot approach. And what that means is it's just an instruction. Now, the zero-shot approach works really well for many simple requests. But when we work with more complex requests,

[00:19] we run a greater risk of getting back results that don't really fulfill our needs. So we might get completions which are off topic or too long, or perhaps the format is just not what we want. Now, I've got an example of that happening right here. Let me introduce you to Advertify, which is an app I've made to generate copy

[00:38] for advertising purposes. It's pretty simple. The user can input a product name, give a description, and a target market. They hit generate copy, and they get a bunch of text they can use in their adverts. And if we just have a quick look at the code, there's nothing unfamiliar going on here. We're bringing in the user inputs right here

[00:57] from the inputs and this text area. And then down here, we've got this one-line prompt, and it's a basic instruction, and it's just using the product name, product description, and the product target. So the instruction is just create 50 words of advertising copy for this product,

[01:15] which can be described as the product description and aimed at the product target market. And we might just tidy up that last back tick and bring it all onto one line. Let's see this in action. But before we do that, I'm just going to log out the response. Now I'm going to go for the same vegan fish cream example

[01:33] that we've got in the placeholder text. And I'll hit generate copy, and let's see what we get. And okay, it's working, but look, it's given me an ordered list. We've got one, two, three, four, five, and six. And it looks a little bit longer than 50 words, but if we open up the console, we can actually see that we've got this,

[01:53] and I'll just paste it in here. It says finish reason of length. And as I'm sure you'll recall, that means that OpenAI has actually cut us short. And if you look, number six is not complete. It just says, grab your vegan dash. And at that point, we know that OpenAI was trying to give us more.

[02:12] Now I've tried this several times before recording with various prompts, and OpenAI really doesn't work that well with word counts. The five words or less that we used in a previous challenge does seem to work okay, but longer word counts often fail. Now, interestingly, OpenAI does know how long a tweet is.

[02:30] If we were to change this prompt and ask for a tweet, it will nearly always deliver something that's tweet length and throw in some hashtags as well. Okay, let's try something else and see if we can make this Advertify app work a little bit better. We're going to have a look at the few-shot approach. The few-shot approach is where we add

[02:50] one or more examples to our prompt. We do this because it helps the AI understand what we want. This is great for more complex tasks. So let's refactor our prompt. Firstly, I'm going to remove all of the variables and actually rewrite this instruction. So I've said, use a product name, a product description,

[03:09] and a target market to create advertising copy for a product. At the moment, we're not doing anything with the user input. So that's just going to give us something really, really random. But what we're going to do next is lay out an example. So staying inside the backticks, I'll come down onto a new line and here's my example.

[03:28] So I've got a product name, which is Flask Tie, a product description, a tie with a pouch to hold liquids and a straw to drink through, and the product target market is Office Workers. Now, I've also put some advertising copy right here

[03:45] and that advertising copy is about 54 words long, which is the kind of length I'm looking for. Now, before this is going to work, we need to do a couple of things. Firstly, we need to bring back our variables and we're going to lay them out in the same style as our example. So in fact, I'm just going to copy and paste all of that text.

[04:04] And now let's bring in the user inputs. So Flask Tie, we can replace with product name. Product description, we can replace with product desk. And of course, target market will be product target. Now, advertising copy, we are just going to leave that empty. So can you see what we've done here?

[04:25] We've given an example and then we've given a partly completed example. And this space right here is an invitation for OpenAI to go and complete. And if you'll remember, we're using the create completion endpoint. So OpenAI is just going to complete this block of text here

[04:44] in the same style as this block of text here. It's going to recognize that we want it to give us something like that, but tailored to the three variables that we've got right here. If we tested this right now, it would likely work, but there's one more thing that we can do to help the AI and to make our code more readable.

[05:03] What we're going to do is separate out our instructions and context. Now, OpenAI docs suggest that you can do this with three inverted commas or three hash symbols. I'm just going to go for the hash symbols and I'm going to put one set right here after the instruction. And then I'll put another set down here in between my example

[05:24] and where we're asking OpenAI to do its thing. And so all that's doing is breaking up this body of text a little bit so OpenAI can see that it's dealing with different entities, an instruction, an example, and a request to go and complete. Okay, let's hit save and see if it works.

[05:43] So I'm going to put in the same vegan fish cream and I'll hit generate copy. Okay, let's check out the copy we've got. So firstly, we haven't got this ordered list. There's no numbers one to six. What we've got is a body of advertising text and that is what I wanted.

[06:00] Secondly, I've just checked the word count of this and it's actually coming in as 63 words. Now, the example that I put here is actually 54 words. So there's only nine words between the two and that is pretty good. And if we have a look down in the console, we can see that the finish reason is stop.

[06:20] That makes sense because this is very much the end of the sentence and the end of the text. So OpenAI has given us everything that it wanted to give us. So the example is doing a really good job of allowing us to show OpenAI what it is we want. Now, remember, this is called the few shot

[06:39] and we can add one or more examples. We should remember, however, that longer prompts cost more so you don't wanna go too far. But let's go ahead and add one more example and actually this is something that you're going to do as a challenge. So let's get straight onto that in the next scrim.

egghead
egghead
~ a minute ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today