From the course: Introduction to Prompt Engineering for Generative AI

Interacting with language models using an API

From the course: Introduction to Prompt Engineering for Generative AI

Interacting with language models using an API

- [Instructor] So here I am at the example section at OpenAI. And what I'm about to show you here is very similar in AI21's Jurassic model examples. So I'm going to choose this Summarize for a 2nd Grader here and you'll notice that there is code you can paste here, in a few languages. But what I like to do is open a playground up and you can make modifications. You can say something like, summarize for a second grader with rhyme. I'm going to go ahead and say View Code. And like magic, you have this code you can use in your project. You can do Python or Node.js. You can do curl, which basically means you can do anything. And now, I'm going to go ahead and copy this. Now, I'll head over to my editor of choice. I'm using VS Code. I've created a file called rhymerize and I'm going to go ahead and paste this. And finally, I'll say print response. Now, it's going to need the OpenAI library and to install that I'm going to type in python3 -m pip install openai and make sure you've spelled this correctly and that you install the right library. Go ahead and go and hit enter. And if you don't have Python installed, you can always get it at python.org. I'm going to go ahead and clear this. It's also going to need an OpenAI API key and I'm going to go ahead and copy this. Now, I don't like putting keys in code. I use environment variables and this example does, as well. And the way to get this in an environment variable is to type in export, go ahead and paste this and then your API key, which is not this. You're going to need to obtain one. So the way to obtain one is to head over to OpenAI. And right here, on this menu, you'll notice there is the View API Keys and you just generate a new key and copy it and you go ahead and paste it here. I already exported one before, so no need to do it. And let's see how this goes. I'm going to go ahead and type in python3 rhymerize.py and just like that, I have my response. And let's take a look at just the text here. So the way to do that would be to type in response, choices and say the zero one, text and I'm going to run this one more time. Great, and just like that, I have this in my code, which is incredibly powerful. Just think about how many things can be done. The possibilities are truly limitless.

Contents