Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Building a Basic Example

00:00 To build your first example, please create a new file. Call that file function_assistant.py as I’ve highlighted in line one there, and please do that in the root of your project. And then on line three, enter your first line of code, and that is from openai import OpenAI.

00:21 Now the first openai is all in lowercase, the second one has a capital O, capital A, capital I. And that second one, that’s a class, that’s actually the client class.

00:34 And the next step is to instantiate this client class, which will then establish a connection to the OpenAI API. A quick reminder that if you chose to use the .env file approach, you should add these two lines of code to lines four and six, respectively, and then continue the code on line seven instead of line five. But here I’ll continue without these two lines, and therefore I’ll continue on line five.

01:06 So on line five, I’m going to use client, so client equals OpenAI(), so capital O, capital A, capital I, and then parentheses to instantiate the class.

01:19 The next thing to do then is to create a response request, and you’ll need two parameters for that. And I want to capture that response request in a response object, which I’m going to call code_response.

01:37 So I’m using client, the client that I created on line five, and then .responses.create(), open parentheses, hit enter, and then you’re going to need, as I said, two parameters.

01:51 The first one is model,

01:54 and model specifies which ChatGPT model you’ll be using, and you need to enter a string here, and the string to enter is "gpt-5", and then comma, and then your second parameter is input, and input is where you stick your prompt.

02:15 Now, to save you from watching me type, I’m just going to copy the prompt, and the prompt is “Please write a simple Python function to add two numbers.” So that’s what you’re asking the chatbot to do for you.

02:30 So the next thing to do is just to create some sort of output to see how it’s working, so use print(), and then an f-string, just type “Function”, and I’m adding a newline character just to make it look a little bit nicer, then curly brackets, and here is where you use the code_response object you created on line seven, because that object actually has attributes, and one of those attributes is .output_text, so type .output_text, and that attribute just holds the AI-generated response.

03:09 So that’s what you want to see on screen, so the next thing to do is just to test this code.

03:16 So in your terminal, type python function_assistant.py, hit enter, and there we go. This is the API being used, it’ll take a few seconds, but hopefully it’ll come back with a Python function that adds two numbers together, because that’s what you asked it to do.

03:42 And there you have it, it comes back with a function def add(a, b) that returns a + b. Just a quick note here that, of course, all AI language models, such as those behind ChatGPT, are non-deterministic by nature, so that means that the output you have might look slightly different from what you’re seeing on screen. Just please keep that in mind. So there you go, that’s your basic example.

04:10 In the next lesson, you’ll look at role-based messages.

Become a Member to join the conversation.