How To Create A ChatGPT API Using Python Code
How To Create A ChatGPT API Using Python Code
PS: This Assignment is Part of “Leveraging ChatGTP for SAP” Hands-On Instructor Led Online
Training.
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
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
ASSIGNMENT 1 ©https://zapyard.com/
P A G E |3
Create a ChatGPT API using Python Code
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
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:
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
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.
Let’s then create a chatbot.py file in the python-chatgpt project folder, with the
following content:
import openai
# 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.
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
cf push
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
ASSIGNMENT 1 ©https://zapyard.com/
P A G E |9