0% found this document useful (0 votes)
90 views

How To Create A ChatGPT API Using Python Code

The document provides steps to create a ChatGPT API using Python. It explains how to set up an OpenAI account, create a Python project, install the OpenAI Python client library, provide a demo Python code to call the ChatGPT API, and deploy the application to SAP Cloud Foundry.

Uploaded by

legionario15
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

How To Create A ChatGPT API Using Python Code

The document provides steps to create a ChatGPT API using Python. It explains how to set up an OpenAI account, create a Python project, install the OpenAI Python client library, provide a demo Python code to call the ChatGPT API, and deploy the application to SAP Cloud Foundry.

Uploaded by

legionario15
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Assignment 1

Create a ChatGPT API using


Python Code
Create a ChatGPT API using Python Code

PS: This Assignment is Part of “Leveraging ChatGTP for SAP” Hands-On Instructor Led Online
Training.

Before going to create an API, you must have an Open AI account.

STEP 1: Steps to set up an Open AI Account


Go to the below link to create an account.

https://platform.openai.com/
You can Sign Up using Google, Microsoft or with Apple. We will ask you to log in
with your Google account.

ASSIGNMENT 1 ©https://zapyard.com/
P A G E |1
Create a ChatGPT API using Python Code

Enter your Gmail address and password to log in.


Once you successfully logged in, you will be navigated to the Home page of Open
AI.

Click on the User Icon on the top right corner and select the Manage account.

ASSIGNMENT 1 ©https://zapyard.com/
P A G E |2
Create a ChatGPT API using Python Code

Click on the API Keys

Click on + Create New API Keys

ASSIGNMENT 1 ©https://zapyard.com/
P A G E |3
Create a ChatGPT API using Python Code

Give any name and click on Create secret key

You will find a new API Key, copy the API key.

Note: Keep your API secret key safe, you won’t be able to copy it further.
If you click on the Playground

ASSIGNMENT 1 ©https://zapyard.com/
P A G E |4
Create a ChatGPT API using Python Code

Here you can see different models are available and there is an option as View
Code.
You can find the Python Code.

You can see there are two options as OPEN_API_KEY & Model are given. You
have to use your API Key while building the application.

ASSIGNMENT 1 ©https://zapyard.com/
P A G E |5
Create a ChatGPT API using Python Code

STEP 2: Create a Python App in your local system

To create a Python, obviously, you have to install Python installed in your system.
Click the below link and download the Python version above or equal to the 3.7+
version.
https://www.python.org/downloads/windows/
For Windows users, please follow the below link to download and install Python
in your system.
https://www.simplilearn.com/tutorials/python-tutorial/python-installation-on-
windows
For the Mac users, follow the below link.
https://www.dataquest.io/blog/installing-python-on-mac/
-------------------------------------------------------------------------------------------------------------

Once you have setup Python in your system, then follow the below
steps:

Create Python ChatGPT Project

Now you have the API key, the next step is to create a ChatGPT project:

$ mkdir python-chatgpt
$ cd python-chatgpt
$ python3.7 -m learning openai
$ source learning/bin/active

ASSIGNMENT 1 ©https://zapyard.com/
P A G E |6
Create a ChatGPT API using Python Code

Install OpenAI Python Client Library

The OpenAI Python library provides convenient access to the OpenAI API from
applications written in the Python language. It includes a pre-defined set of classes
for API resources that initialize themselves dynamically from API responses which
makes it compatible with a wide range of versions of the OpenAI API.

For detailed information about this library, go


to: https://github.com/openai/openai-python

Demo Python Code

Let’s then create a chatbot.py file in the python-chatgpt project folder, with the
following content:

import openai

# Define OpenAI API key


openai.api_key = "YOUR_API_KEY"

# Set up the model and prompt


model_engine = "text-davinci-003"
prompt = "Once upon a time, in a land far, far away, there was a princess
who..."

# Generate a response
completion = openai.Completion.create(
engine=model_engine,
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,

ASSIGNMENT 1 ©https://zapyard.com/
P A G E |7
Create a ChatGPT API using Python Code
)

response = completion.choices[0].text
print(response)

The above code will generate a response to the defined prompt using the ChatGPT
model. The response will be returned as a string in the response variable.

The temperature parameter controls the level of randomness in the output. The
higher the temperature, the more varied and less coherent response output, you can
adjust based on your experiment. stop defines the stop condition.

Deploy the app to SAP CF

Now deploy the python to the SAP CF subaccount and use the application using CF8 CLI URL as an API.
To do that, please enter the below commands
cf login

** Provide your SAP BTP Email & Password

cf push

** To deploy the app into the subaccount.

ASSIGNMENT 1 ©https://zapyard.com/
P A G E |8
Create a ChatGPT API using Python Code

If you want to Learn How to Integrate ChatGPT with SAP then ENROLL
Today.
Course Curriculum – Leveraging ChatGPT for SAP

Enrollment Link – Leveraging ChatGPT for SAP


Course Days – 10, 11, 17, 18, 24, 25 June, 1, 2, 8 & 9 July 2023 IST Dates
Time: 7:30 AM - 10:00 AM IST

Server Access till 23 July 2023

WhatsApp +1-251-727-9273 – for quick response


email:[email protected]

ASSIGNMENT 1 ©https://zapyard.com/
P A G E |9

You might also like