Site icon TestingDocs.com

OpenAI API Request

Overview

This tutorial will teach us how to execute an OpenAI API Request. A Python project is a software development endeavor using the Python language. It is a collection of source code files, configuration files, a Python interpreter, etc., created using an IDE.

Executing the API needs some setup on the machine. The required setup is outlined on the tutorial page.

New Python Project

The steps to create a new project using Spyder IDE are as follows:

Launch Spyder IDE on Windows.

We can usually do this by searching for Spyder in the Search from the Taskbar.

Choose the Spyder App from the search results. This will launch the IDE.

Choose Projects >> New Projects from the top menu bar.

Enter the following project details:

Click on the Create button.

The project should appear on the left panel.

Please select the project and right-click on it.

Execute OpenAI API Request

Choose New >> Python File…  menu option.

Name the file with .py file extension.

Add Python code to create an AI model and invoke an appropriate API request.

Code

“””
Simple API Request
AI Model: gpt-3.5
@author: TestingDocs

— OpenAI Tutorials — www.TestingDocs.com
“””

from openai import OpenAI
client = OpenAI()

completion = client.chat.completions.create(
          model=”gpt-3.5-turbo”,
         messages=[
          {“role”: “system”, “content”: “You are a poetic assistants.”},
         {“role”: “user”, “content”: “Compose a poem about Nature.”}
         ]
)

print(completion.choices[0].message)

 

Run the code

Run the Python code.

Choose the Run >> Run menu option.

Analyze the API Response. The response should be successful, and you should get the model output. If there are any errors, you need to debug and fix them.

OpenAI API Tutorials

OpenAI tutorials on this website can be found at:

Exit mobile version