An important part in creating a data API whether based on generative systems or not is being able to reliably generate responses in the format you need. Variable data structures and irrelevant information could break your application or frustrate the users of your API, resulting in downtime or lost customers.
Using the messaging API, we can train GPT to become more aware of our needs by using System roles to instruct GPT on behavior and User to Assistant examples to give GPT precedent to base its next response on.
Instructor: [0:00] picking up with a new example where we have a generate post form and if we enter our topic such as gaming, we can generate a new post. Once the response comes back, we can see that we have a new post based on the topic of gaming.
[0:12] To make this work, I created a blog endpoint similar to the attributes we did in a previous lesson where I'm using a shape of content where I want to get my blog content in order to ask GPT to create a new blog post with a dynamic prompt and then ultimately return it in the response with the shape of that shape object.
[0:31] Inside of my application code itself, I'm simply posting that prompt or the topic to that endpoint. As we also realize in one of the past lessons that this isn't totally reliable where GPT doesn't always respect our wishes to return that as a properly formatted JSON response.
[0:47] The way that we set this up, and the way that we're currently doing this here, is by trying to jam all that information into one single message, where the Chat Completion API allows you to pass in messages using different roles.
[0:59] For instance, we could use the System role to provide some initial setup, and then the User and Assistant roles one after another, in order to provide an example, where then finally, we can use another User role to finally ask our question.
[1:12] To see how this works, I'm going to dive back into my blog endpoint, where instead of just passing only a User role message, I'm going to pass a first new message, where my role will be System, where I'm going to create a new message with my content, where, like we see in this example where they're saying, "You are a helpful assistant."
[1:30] We can say, "You are an assistant that creates new blog posts on a given topic. You should provide a title and HTML content in JSON format." Now, just as a quick check, let's go ahead and try it again and generate a post, where depending on the generation, we still may have mixed results, where other times, we'll get a response that we can dump on the page.
[1:53] Now before we start to fix the HTML, which was intentionally added to make it easier to consume this API, we can make more changes to our messaging so that we can have more confidence that we're going to get the right response.
[2:03] The first thing I'm going to do is create a new message, and I'm going to set the role as User. For my content, I'm going to pass in a message as if I were asking it to create a blog post based on a topic, so let's say, something like technology.
[2:16] Now, following up on this message, I want to create a new message from the Assistant to act like it was responding to this User message. What this will do is set up an example so GPT has an idea of how to respond to future messages.
[2:31] If I create another message, but this time, I set my role as Assistant, and for my content, I'm going to json.stringify the shape that I previously have defined for how I want this content to look. Maybe I'll update this content to say something along the lines of "A blog post about current technology".
[2:51] That way, hopefully, it can recognize that shared topic between the user and the assistant response. I know I also want this content to be in HTML format, I can add the paragraph tags around that content so it knows to return it in that format.
[3:05] Finally, I also want a title on this. I can just add something simple like Technology title. Now because I already have some of these rules defined and I'm also defining this example now, I'm going to get rid of that original user message that defines that shape to simplify things.
[3:21] Now, if I try to generate another post, we can see that we do now get that blog post. If we try it again, I'm going to get yet another blog post. We can see that we're getting much more confident responses every time we click Generate.
[3:33] Now, to finish out this example, let's render this HTML, where with my response I'm currently setting my post as data content. Now, the first thing I'm going to do is set the full data object as my post. That way, I also will have access to the title. Then I can start to update the UI, where maybe I'll add an H1, where I'll include the post.title.
[3:53] For the post content, I'm going to create a new div, where I'm going to dangerouslySetInnerHTML, where inside I'm going to add my HTML attribute, where I'm going to set post.content and get rid of that original post.
[4:07] Now arguably, you could probably create a more complex structure where this content has an array of all the different nodes along with the different associations. For the sake of this example, we can keep it simplified with this rendered HTML.
[4:19] Now, when we try to go back and generate a post, we can see that I get my title and I get a full post that talks about my topic.
[4:26] In a later lesson, we'll learn how to do this with the fine-tuning mechanism, where we can construct our own model rather than creating those messages every single time. For now, this gives us a way to provide context and examples for how we want to formulate our responses from GPT.
[4:41] In review, in order to enhance our ability to build responses from GPT, we can use different messages with different roles, such as system, user, and assistant, where we can provide more context to GPT but also provide examples for how we want GPT to respond, where now when we hit this endpoint we can more confidently get a response that can build our UI and generate the information that we need.
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
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!