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

Structuring the Output

00:00 In this lesson, you’re going to learn how to structure the output you’re getting from your bot. So far, it’s been kind of unstructured text, which can make it tricky to incorporate the output into a process or into another app.

00:16 To structure the output, you’re going to need a library called pydantic. Now, the OpenAI Python library includes pydantic as a dependency in most, but not all, environments. So to install pydantic in case it’s not included in your environment, please make sure that your virtual environment is activated.

00:38 And then for Linux and Mac, type python -m pip install pydantic, or for some Linux distros, that’ll be python3 -m pip install pydantic.

00:50 And for Windows, it’s python -m pip install pydantic. So once you’ve installed pydantic, you can move on to some coding.

01:00 The first thing to do is to, from pydantic, import BaseModel. And BaseModel is in camel case, so capital B, capital M.

01:11 And using BaseModel or inheriting from BaseModel, you’re going to build a CodeOutput class.

01:21 class, CodeOutput, again, camel case, because this is a class after all. And that inherits from BaseModel.

01:30 And this CodeOutput class will have four class attributes. And the idea is that these four attributes will be the attributes of your output.

01:41 So the first one is function_name.

01:46 And that’s a string. In fact, all four of them will be strings. Second one is code.

01:53 Third one is explanation.

01:57 And then finally, example_usage.

02:03 So those are your four attributes. To implement this CodeOutput class, you’re going to have to make two changes to your code_response object.

02:15 Firstly, move to line sixteen if you please. And instead of responses.create(), make sure it says responses.parse(). That’s the first change.

02:27 And then if you scroll further down, you’re going to have to add a third input parameter. So you have model and input so far. So model on seventeen, input on line eighteen. So now you’re going to introduce another input parameter called text_format.

02:46 The input parameter ends on line thirty. You can see that’s the end of the list. Add a comma. And then the parameter is called text_format.

02:58 And then you type CodeOutput. So that’s the name of the class you just created, but no parentheses. So you don’t instantiate the class. This is just CodeOutput without the parentheses.

03:11 And then don’t forget a comma.

03:14 So as I said at the beginning, the intention was to make sure that your output had a certain structure to it, that your output had the four attributes that you created in the CodeOutput class.

03:26 So it would make sense to make sure you have an object that captures your output. So I’m going to go on line thirty-four and type code_result.

03:38 That’s the object. And that’s code_response dot, and now it’s .output_parsed.

03:48 So this is different from what you’re currently seeing on line thirty-six. There we had code_response dot .output_text. So now we’re not taking text as an output, but we’re parsing the output such that we have four attributes of the code_result object.

04:08 Now to show you how you can use that, I’m going to replace this print line of code here and replace it with four lines. Actually five, but I mean four in terms of four attributes.

04:21 So the first one prints function name and then from code_result.function_name. So that’s the first attribute of code_result. And if I scroll back up, you’ll find that that’s the first class attribute of CodeOutput. So we have code, explanation, and example_usage.

04:42 And if you scroll back down, you’ll find that .code, .explanation, and .example_usage are here being used in the output.

04:53 So if you make sure that you save that and then go back to your terminal and run the app again, you’ll get your prompt. Again, I’ll paste in the standard prompt now.

05:03 So “Please write a simple Python function to add two numbers.”

05:09 And then this is the output you get. So you have function name and then add two numbers. You have code and then you have the actual code. You have explanation and then the actual explanation.

05:23 And then you have example usage and then of course the usage of the example. And that coincides with the code you wrote here in line thirty-six to line forty.

05:36 So function_name, code, explanation, and example_usage.

05:43 So structuring your output that way will make it much more straightforward to use the output further in another process or another program because you can use attributes to access particular parts of the output.

05:58 Talking about introducing your outputs in new programs, new apps, in the next lesson, you’ll be looking at some next steps that you could take.

Become a Member to join the conversation.