Zero, One, and Few Shot Prompting with Langchain and OpenAI LLMs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
F shop prompts build out the template for your large language models they Define the input as well as the output which leads to a much better model performance hey guys welcome back to another video in this series where we are covering open AI Lang chain and large language models today's video we're going to be taking a look at zero one and few shot prompts showing you specifically why you want to provide multiple examples to your model to get much better performance and we're going to be doing that through the programming language of python this is the second video in the series I talked about prompt templates in the video before as well as setting up the open AI API key if you're not familiar with either of those I highly recommend you watch that video before you jump into this one and it's going to be linked down below if you know how to do both of those you have the pre-record that'ss down for this video Let's Get coding all right so let's get coding few things I have a Google collab up here feel free to rename your file and because I already prepped my code I will not be doing that so first thing I am going to do is going to build out a bunch of cells and we're going to start off with doing a pip install so pip install open Ai and we're also going to pip install Lang chain so let's do that to pip install Lang chain and both these are going to run and build out a bunch of text down below so let's do that next thing what I am going to do is create an environment variable for my open AI API key now my key is going to be blurred out so that way you guys can't use it but use your own key I showed you how to create it and open add gives you free credit to start in the beginning so you shouldn't be paying out of pocket so just import OS and then what we're going to say is os. n v i r o n put in over here open AI API score key like that while this uh continues to build out and then we're going to say that equals to and then put your own API key now I'm just going to paste mine in over here I'm going to run that and then I'm going to hide that specifically and then one more thing that I am going to do I'm going to hide warnings over here so I'm just going to say import warnings and then warnings. filter warnings like that and I think it should be warnings here not warning and then I'm going to say ignore and then category equals depreciation warning and the only reason why I'm saying that is I did get some depreciation warnings when I started prepping the code this morning morning so probably some changes are going to be going into effect later on this year or so probably some changes are going to be going on uh down the road but I don't want to continue to see These Warnings everywhere and uh did misspell de priation so I have fixed that I'm just going to run this cell and we are good all right now we can start so let's start off with our basic example of a zero shot so zero shot example let's take you through this all right so we're going to start we're going to go from Lang chain. llms Import in open AI that should be orted awesome and then what we're going to do is just run a very basic LM we're going to say LM equals open AI I'm going to set my temperature I talked about it last video essentially how interesting the results get 0 to one I'm going to put a 0.6 for this llm probably use it later on as well and then what I'm going to say over here is text and this is what I'm sending over to the LM I'm just going to say Nolan Ryan uh very famous pitcher I'm named after him and we could get a lot of different results for this because there's like no specification of what I wanted specifically to do with and Ryan this is a horrible prompt don't do that but for demonstration purposes this is a great zero shot right zero examples no bueno so let's put text over here right all I'm doing is throwing this text into our llm and let's see what happens the first time that we run this and we're going to run it a few times so it says Nolan Rhino is a professional baseball player who played 27 Seasons impressive fast ball clocked at over 100 miles an hour play for Mets Angels Astros and Rangers holds a record so just talks about the history of Nolan Ryan right and the Ryan Express who was his nickname blah blah blah all right let's run this again so what I'm G do is print that and check this out this is the story of how Nolan Ryan became a legendary pitcher and one of the most iconic figures in baseball history early life Lyn Nolan Ryan Jr talks about this born in Texas from a young age she love baseball Professional Care 1965 age 18 to by the Mets in 12th round traded so and it just stopped there it didn't even they say it's going to talk about his history but um literally just stopped fast ball and so each time I run this and I could run it a third time we're going to get different results again fact when I was prepping this video I got different results from both of these but the point stands is we didn't give any examples and this isn't the best approach to that so how we can do this at least at the very basic level let's set up a one shot so one specific example and let's do that by importing in a few different libraries and let's do that by importing in a few different things real quick so I'm going to say from Lang chain. prompts do view shot import VI shot prompt template and then what I'm going to do is from Lang chain. prompts do prompt import prompt template like that okay so both of those are going to be in here next what I'm going to do is set our example so again one shot is going to be one example better than no examples but it's not ideal so we're going to say example we're going to put these brackets over here I'm going to press enter so this opens this up then I'm going to to set these curly brackets and we're inside over here so first thing I'm going to say is player and I'm going to put over here Sandy kofax so Sandy kofax okay another really great picture then I actually need a comma here at the end of that one then what I'm going to say is we have stats over here and what I'm going to say is strikeouts and I'm gonna put his total of strikeouts now I already researched this before on baseball reference or I could have taken a look at a baseball card but I'm going to say strikeouts is 2396 and then Innings pitched and what I'm going to say over here is 23 24.1 and this should be stats not State then I'm going to close this out and I'm going to put a coma here at the end so this is just an example right so what I want to do is put in a player like I did above with Nolan Ryan this time I'll put Sandy kofax and I want to see essentially the strikeouts and the Innings pitched so I'm just going to run this one over here no errors and what I'm going to do is set up an example prompt this time so what I'm going to set up is example prompt like that and then that's going to be equal to a prompt template which we went over those quite a bit in that previous video so prompt template like that then we're going to put our input variables so input variables equal and we're going to say for input variable we're going to have player and our stats so put those in here stats and then our template equal to player ER put our variable in here so we're going to have player and then set a new line sln and then we're going to put our stats in here like that and run that did have a small error on here let's see what happened input variables equals and I believe it's because I forgot this comma here at the end and now it does work awesome and let's print out our example that we have over here so print and we're going to say example prompt we're going to say format and two stars and we're going to say example zero remember python starts at zero so let's put our first example in over here and you can see it prints other example that we have over here so Sandy kofax right we have a new line and it says strike outs over here 2396 and innings pitch 23 24.1 so exactly what I wanted to see which is great now let's actually set up our specific prompt so to do that we're going to say prompt this time we're going to see that's equal to a fuse shot promp template fuse shot prompt template auto complete love it and open this up so first thing is examples so this ideally wants you to have multiple examples we're just going to put example in over here then you set up your example prompt so example prompt equals and we already have our example prompt right we defined that above so put our example prompt there okay great then your suffix so suffix over here it's going to be equal to player like this and then that's going to be our input so input over here great and then lastly we're going to put our input variables input Vari variables equal to input right that's what we're defining in this model is that input again player which we're putting in over here think about that as input variables all right that has run so now let's do another print over here so print this time we're going to say prompt. format then we're going to put over here input equals and let's put n and Ryan so this won't show us the result yet of everything but it'll show you what's going to be inputed into our large language model so this is our input right so imagine like we're putting this in chat GP we have ovar Cy kofax it his strikeouts 2396 Innings pitch 232 24.1 then what I'm saying is player Nolan Ryan and I want the model to essentially emulate the same exact formatting that I defined remember I have over here a space and then we have strikeouts and innings pitched that's what I expect think about this as what I would have on that specific screen so let's do that and very easy we can literally just copy the code from above then we put in our llm which again is going to be a 0.6 temperature we Define that and just put the L on the outside of this prompt again we're just throwing this prompt right over here into this LM and watch assuming this works perfectly strikeouts 5714 and innings pitched 5 386.3 86 awesome and it says strikeouts 5714 574 really really good so now let's take a look at a fuse shot and it's recommended for a fuse shot that you at least give two examples you a lot of people go anywhere in the range from two to specifically five in this case uh for time sakes I'm going to do three examples you should be able to know how the formatting works and uh expand it out as you want right so let's go and do that and I'm going to start with this example I have over here I'm going make some modifications so I'm going to call this this time examples since we're going to be showing multiple examples and I'm going to make one other change to this one so underneath player I want to have a team listed out or teams if a player played on multiple teams then we're going to have a space and then we're going to print out strikeouts and innings pitched so a little bit more complex formatting but let's put over here teams like this now Sandy kofax only played for one team his entire career which was the Dodgers so we can put Dodgers in over here then just put a comma here like that and then I'm also going to put a comma over here and now let's grab our second example so since we just did Nolan Ryan and we don't have to look up Nolan Ryan's stats let's do that so we can put Nolan Ryan now nol and Ryan played for four teams so he first played for the Mets then he went over to the angels then he went over to the Astros and then he went over to the Rangers so pretty uncommon for that to happen for a player like noan Ryan's caliber but it does happen and like even Modern Baseball today it's kind of what's happening with Wan sto so strikeouts Nolan Ryan had 5714 and then Innings pitched right he had 5386 so 5386 and then point zero uh so we had full Innings over there okay great now what I'm going to do is I'm going to copy this again and we're going to go to our third player now our third player what we're going to do is we're going to change it up so Nolan Ryan and also Sandy kofax they played 50s and 60s for kofax Ryan played from 60s 7s 80s and 90s so let's go way back and grab a player named Walter Johnson many people consider one of the greatest pitchers of all time now Walter Johnson played on the Nationals so we'll put the Nationals over here like that then strikeouts wise 3509 first pitcher to have 3,000 strikeouts Innings pitched so back then they pitched a lot 591 14.1 it's quite a lot of innings and look we are done with our formatting I'm going to just bring this up a little bit just to make our code a little bit cleaner but we have three examples right so we have Sandy kofax we show that he played for one team n Ryan example is perfect because we're pretty much showing that n Ryan played for four teams and a player could have played for multiple teams and we want to expect that output if we just showed examples of each player having one team maybe our open AI API thinks let's just put the team a player is most known for and that's not always the case in baseball so this Nolan Ryan one is perfect and then we have Walter Johnson over here again showed one team but also shows his stats so that's why we want to have a few shot example over here so we run that for our examples then what I'm going to do next is create this example prompt again so I'm just going to copy this over we're going to paste it down below it's going to be very similar right we have our player over here next since we added in something else we're going to put teams over here comma and then down below what I'm going to do is put our teams again so put teams like that and again I'm going to put sln after that so new line great we have that we have our player teams and stats so that should be good there I'm going to go up here once again and we're going to copy this prompt and we're going to change example over here to examples now we already have everything else right our suffix player all we're inputting in is a player nothing has changed there we're going to define the team through our model so our input variable is going to be the same the suffix is going to be the same example prompt is going to be the essentially the same except we added in teams and our examples we added in the more examples so that's why I threw an extra S at the very end on this side of things so that is running and let's see if this specifically works so what I'm going to do is grab this over here this format and what I'm going to put for a player name is we're going to go a little bit forward in time and grab a player named Lefty Grove one of the greatest pitchers in the 1930s very underrated baseball wise because a lot of people don't know who he is I think of him like as a clean Kershaw and we're going to input this so so you can see player Sandy kofax Dodgers strikeouts Innings pitch 23 over here player Nolan Ryan Mets Angels Astros Rangers strikeouts Innings pitched player Walter Johnson strikeouts Innings pitched now we have player Lefty growth so let's finish this right now we're going to have one more line of code let's just throw this over here to an llm and when we do that we should get I'll just put this over here muy Grove not Gomez P go Gomez is a Hall Famer as well so we should be getting Innings pitched 39 40.2 22 66 strikeouts and then we look at his teams he played for two Athletics and then also Boston now it's going to say Boston Red Sox let's see specifically Works Athletics and the Red Sox 2266 3090 402 so hope this all made sense again just a recap zero no examples we can continue to get weird outputs each time we run really good example just throwing in that specific name Nolan Ryan one shot we give our prompt one specific example this one it worked because it was a pretty basic example but if you don't really Define what you want to do in that example it it it's not going to always be correct so just keep that in mind and then when we got to the fuse shot right and you were from 2 to five I did three in this video you can start making a little bit more complex but show how things can change specifically like with this teams how a player could play for one team or multiple teams and lastly feel free to experiment with this stuff the more that you practice with it the better you are going to get hey guys I appreciate you making it to the end of this video I hope you able to learn something new about fuse shop prompts and that way you can put this into your AI toolkit now if you did learn something new I do appreciate a subscribe to the channel 100% for free but it does allow YouTube to share this video and other videos on my Channel with other people that are trying to dive deep into the section of learning now with that being said I am building out a full playlist so expect a lot more videos over here because I upload two to three times a week
Info
Channel: Ryan Nolan Data
Views: 2,226
Rating: undefined out of 5
Keywords: Data Analyst, Data Scientist, Langchain, OpenAI, Language Models, Natural Language Processing, Zero-shot Prompting, One-shot Prompting, Few-shot Prompting, AI Technology, Language Generation, AI Innovation, NLP, Langchain and OpenAI Partnership, Advanced Language Models, Cutting-edge Technology, AI Research, Language Understanding, Tech Breakthroughs, AI Integration, Machine Learning, Next-gen Language Models, AI Exploration, YouTube, Tech Trends, python, llm
Id: SNxe2kwgPi4
Channel Id: undefined
Length: 20min 24sec (1224 seconds)
Published: Thu Jan 11 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.