From the course: OpenAI API and MCP Development

Building a content generator

From the course: OpenAI API and MCP Development

Building a content generator

OpenAI has gained a huge and widespread popularity since 2022, following the release of ChatGPT, the famous chat app powered by the GPT models. Since then, OpenAI has released multiple language models, which are much faster and also more powerful with more capabilities. Let's follow this link to start building with the OpenAI platform. Here we're going to discover multiple guides offered by open AI with multiple tutorials and examples to start building fast and easy with the open AI platform. On the left, you have the core concepts with the text and code generation. You can learn to create images, how to process images. You can even learn to describe what is on one image using the vision model. You can also explore the audio and the speech features using the audio API to convert audio to text, and vice versa, go from text to audio. So let's go back to the text generation section. What we want to do in this video is to learn to design a content generator using the Responses API. And so you have access to different endpoints to process text outputs. You can do it with the Chat Completions API or the Responses API. So we're going to see a simple example using the Responses API below to generate text from a simple prompt. So you want to make sure that you select Python from the dropdown, and we're going to copy this example. So before getting started with this first demo, we want to go through the steps for the installation and the setup. First, you want to make sure that Python is properly set up on your machine, starting with the version 3.11 and beyond. Then optional, you can create and activate a virtual environment, then install the packages, And very important, you want to set up an OpenAI secret key, and this is to be authorized to interact with the APIs and the language models offered by OpenAI. So let's go to the code editor, find the corresponding exercise files with the starter projects. And for the entire course, for your information, I'm gonna be using Visual Studio Code for the code editor. And for every project, I'm providing with a readme file, with instructions as to how to get started. So starting with the Python installation, so you can check which version of Python is installed on your machine with this command. And you'll see that I'm providing with instructions for both Mac and Windows. And let's go straight to how to create and activate a virtual environment. So I'm gonna use this command for Mac. So my virtual environment is already created. And to activate it, you just need to run this command, and then you'll see this between parentheses, meaning that this is properly activated. Next, you want to install the packages by running this command. So this is already done on my end. So I'm gonna run it, but this is already done. So you'll see that it's just gonna use cached installation. And next, you're ready to go. So what we wanna do now is to create our generator, content generator using the responses API. So let's go back to the documentation and copy from line four all the way down to nine. I don't need to copy this because this is already provided in the starter project. Let's go back. All right, so now we have this API request which is defined using the responses API with a model and an input. So let's actually change to this model, 3.5 Turbo, which is much more cost effective. So you just need to know and remember that this is costing you tokens every time that you process a request with an API. And this one is cheaper. So this is much more cost effective. And this is going to be more than enough for the next demonstrations. We also need to provide with an input. So what is an input? This is a text input in natural language, instructions that we send to the language models in order to guide the response and the behavior of the language model. So this is just to tell the language model how to behave and how to act, and what is the output expected. So for this next example, we just want the language models to write a one-sentence bedtime story about a unicorn. So let's run it to see what is the output. All right, so here we can read that once upon a time, a magical unicorn pranced through dreamland, et cetera, et cetera. So I'm going to let you read the rest. For the next example, because you can also learn to define requests by using actually the API reference, for the next example, I'm going to show you also how to create a model response and how to define your request body in order to control better what is the response and the output which is generated, meaning that you need to understand what is the request body and what are the parameters of a request body. So here, all the way to the bottom, you're going to find one parameter, which is the temperature, which is important to understand, to control, and guide the response of the language models. And we're going to use the temperature to allow randomness and also variations between the different responses, because you have to know that the responses of the language models are never deterministic. And this is up to us developers to give and provide with detailed and accurate instructions in order to get the desired output. So this is the art of prompting. So this is how we can actually better control the behavior of the language model. So this is a value between 0 and 2. Closer to 0 will allow the responses to be as close, very similar. And in order to allow more variations and creativity and randomness between generation tasks and outputs, you can increase the value. And this is a good value when you go close to one or two whenever you want to generate tasks for creative stories, for example. Now let's go back. And here, I'm just going to make sure that I specify a temperature value. You can also control the number of tokens in order to also control how much you pay, so your usage for every API request. So that's also good options to control. And here, I'm just going to change this to write one sentence actually I'm gonna say a two sentence horror story just to make things a little bit more interesting and intriguing horror story about about about the haunted house let's use this inputs okay so I'm gonna generate again run my content generator oh and for this one I think that actually we're just going to go back to the API because it should be a different parameter. So I'm just going to go find what is actually the parameter that we should be using. Oh, remember, it's not available. It's actually only available with the chat completions API. So I'm just going to remove it. Anyway, so that's just one good example for you to understand that you should always check out the API reference. So I'm just going to remove it because it's not available with this API. You can only control with the temperature. So let's run it again. All right. All right. And here we have an output that was generated. So as Sarah explored the abandoned mansion, she heard a faint whisper. So I'm just going to let you read the rest, because it may be too scary and creepy for me. So just to show you, for you to understand, we can go check out also for the chat completions API. And that's going to be here, Completions API. Here it is. So how to create a Completions API. And I think that for this one, this is where you can actually specify the max-stock-conspirator, just for you to understand. OK, so always remember to check out the documentations and the API reference to understand how to define your API requests. So that's it for our first, very first demonstrations to create a content generator using the OpenAI platform.

Contents