ChatGPT Prompt Engineering for Developers ChatGPT 08: Chatbot 开发人员提示工程 08: 聊天机器人

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
one of the exciting things about a large language model is you could use it to build a custom chat bot with only modest amounts of effort check GPT the web interface is a way for you to have a conversational interface a conversation via a large language model but one of the cool things is you can also use a large language model to build your custom chatbot to maybe play the role of an AI customer service agent or an AI order taker for restaurants um and in this video you learn how to do that for yourself I'm going to describe the components of the open AI chat completions format in more detail and then you're going to build a chatbot yourself so let's get into it so first we'll set up the open AI python package as usual so chat models like chat gbt are actually trained to take a series of messages as input and return a model generated message as output and so although the chat format is designed to make multi-tan conversations like this easy we've kind of seen through the previous videos that it's also just as useful for single turn tasks without any conversation and so next we're going to kind of Define two helper functions so this is the one that we've been using throughout all the videos and it's the Gap completion function but if you kind of look at it we give a prompt but then kind of inside the function what we're actually doing is putting this prompt into what looks um what looks like some kind of user message and this is because the chat gbt um model of the chat model which means it's trained to take a series of messages as input and then return model generated messages output so the user message is kind of the input and then the assistant messages the output so in this video we're going to actually use a different helper function and instead of kind of putting a single prompt as input and getting a single completion we're going to pass in a list of messages and these messages can be kind of from a variety of different roles so I'll describe those so here's an example of a list of messages and so the first message is a system message which kind of gives an overall instruction and then after this message we have kind of turns between the user and the assistant and this would kind of continue to go on and if you've ever used chat gbt the web interface then you your messages are the user messages and then chat gpt's messages are the assistant messages so the system message helps to kind of set the behavior and Persona of the assistant and it acts as kind of a high level instruction for the conversation so you can kind of think of it as whispering in the assistant's ear and kind of guiding its responses without the user being aware of the system message so as the user if you've ever used chat gbt you probably don't know what's in chat gbt's system message the benefit of the system message is that it provides you the developer with a way to kind of frame the conversation without making the request itself part of the conversation so you can kind of guide the assistant and kind of whisper in its ear and guide its responses without making the user aware so now let's try to use these messages in a conversation so we'll use our new helper function to get the completion from the messages and we're also using a higher temperature so the system message says you are an assistant that speaks like Shakespeare So This Is Us kind of describing to the assistant how it should behave and then the first user messages tell me a joke the next is why did the chicken cross the road and then the final user message is I don't know so if we run this the response is to get to the other side let's try again to get to the other side faster or Madame this is an Olden classic that never fails so there's our Shakespearean response and let's actually try one more thing because I want to make it even clearer that this is the assistant message so here let's just go and print the um the entire message response so just to make this even clearer this response is an assistant message so the role as assistant and then the the content is the message itself so that's what's happening in this helper function we're just kind of passing out the content of the message so now let's do another example so here our messages are this is the message is you're a friendly chatbot and the first user message is hi my name is Issa and we want to um get the first user message so let's execute this for the first assistant message and so the first message is hello Issa it's nice to meet you how can I assist you today now let's try another example so here our messages are um system message you're a friendly chat bot and the first user messages yes can can you remind me what what what is my name and let's get the response and as you can see the model doesn't actually know my name so each conversation with a language model is a standalone interaction which means that you must provide all relevant messages for the model to draw from in the current conversation if you want the model to draw from or quote unquote remember earlier parts of the conversation you must provide the earlier exchanges in the input to the model and so we'll refer to this as context so let's try this so now we've kind of given the context that the model needs which is my name in the previous messages and we'll ask the same question so we'll ask what my name is and the model is able to respond because it has all of the contacts it needs in this kind of list of messages that we input to it so now you're going to build your own chat bot this chatbot is going to be called Autobot and we're going to automate the collection of user prompts and assistant responses in order to build this Autobot and it's going to take orders at a pizza restaurant so first we're going to Define this helper function and what this is doing is it's going to kind of collect our user messages so we can avoid typing them in by hand in the same in the way that we did above and this is going to kind of collect prompts from a user interface that will build below and then append it to a list called context and then it will call the model with that context every time and the model response is then also added to the context so the kind of model message is added to the context the user message is added to the context so on so it just kind of grows longer and longer this way the model has the information it needs to determine what to do next and so now we'll set up and run this kind of UI to display the autobot and so here's the context and it contains the system message that contains the menu and note that every time we call the language model we're going to use the same context and the context is building up over time and then let's execute this okay I'm going to say hi I would like to order a pizza and the assistant says great what pizza would you like to order we have pepperoni cheese and eggplant Pizza hmm how much are they great okay we have the prices I think I'm feeling a medium eggplant Pizza so as you can imagine we could kind of continue this conversation and let's kind of look at what we've put in the system message so you are Autobot an automated service to collect orders for a pizza restaurant you first greet the customer then collect the order and then ask if it's a pickup or delivery you wait to collect the entire order then summarize it and check for a final time if the customer wants to add anything else if it's a delivery you can ask for an address finally you collect the payment make sure to clarify all options extras and sizes to uniquely identify the item from the menu your spot you respond in a short very conversational friendly style the menu includes and then here we have the menu so let's go back to our conversation and let's see if the assistant kind of has been following the instructions okay great the assistant asks if we want any toppings which we kind of specified in the system message so I think we want no extra toppings sure thing is there anything else we'd like to order hmm let's get some water actually fries small or large and this is great because we kind of um asked the assistant in the system message to kind of clarify extras and sides so and so you get the idea and please feel free to play with this yourself you can pause the video and just go ahead and run this in your own notebook on the left and so now we can ask the model to create a Json summary that we could send to the order system based on the conversation so we're now appending another system message which is an instruction and we're saying create a Json summary of the previous food order itemize the price for each item the field should be one pizza include side two list of toppings three lists of drinks and four list of sides and finally the total price and you could also um use a user message here this does not have to be a system message so let's execute this and notice in this case we're using a lower temperature because for these kind of tasks we want the output to be fairly predictable for a conversational agent you might want to use a higher temperature however in this case I would maybe use a lower temperature as well because for a customer's assistant chatbot you might want the the output to be to be a bit more predictable as well and so here we have the summary of our order and so we could submit this to the the order system if we wanted to so there we have it you've built your very own order chat bot feel free to kind of customize it yourself and play around with the system message to kind of change the behavior of the chatbot and kind of get it to act as different personas with different knowledge
Info
Channel: 盛少
Views: 137
Rating: undefined out of 5
Keywords: ChatGPT, AI, artificial intelligence, natural language processing, NLP, GPT, OpenAI, machine learning, ML, deep learning, prompt engineering, chatbot development, AI chatbot, AI-powered conversations, AI language model, AI tutorial, GPT tutorial, AI applications, AI development, AI techniques
Id: x-BJcyJt9MA
Channel Id: undefined
Length: 12min 19sec (739 seconds)
Published: Thu May 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.