INTRODUCTION
These days, ‘ ChatGPT ‘ is the hottest issue of all world !
https://openai.com/blog/chatgpt
Microsoft had realized ChatGPT’s potential and invested OpenAI a few years ago.
As you already know, Bing, Microsoft’s searching engine wasn’t popular enough, but when they applied chatGPT to Bing, everythings changed.
So, Let’s find out how to apply ChatGPT API to my code and make my own chatting software using python.
PREPARATION FOR THE CHATGPT API
You can get infomations about the OpenAI API in the link below.
https://platform.openai.com/docs/api-reference/introduction?lang=python
OpenAI API
An API for accessing new AI models developed by OpenAI
platform.openai.com
GET API KEY
First, Gets your own API Key.
https://platform.openai.com/account/api-keys
OpenAI API
An API for accessing new AI models developed by OpenAI
platform.openai.com
Caution!
You must write the API 키 to safe placebecause you can’t check it again.
DOWNLOAD PACKAGE
Next, download openai package.
pip install openai
Since I have been using ‘Python3.9’, I used ‘pip3’.
pip3 install openai
CHECK YOUR ACCOUNT
They give me $18 of free trial. The price is $0.002 / 1k 토큰.
CODE
Next, Let’s write the code for ChatGPT.
import openai
openai.api_key = "Your API Key"
message_list = ()
while True:
Input = input("->")
if Input == "":
break
message_list.append(Input)
print(message_list)
message = message_list(0)
completion = openai.ChatCompletion.create(
model = "gpt-3.5-turbo",
messages = (
{
"role": "system",
"content": "You are a kind elementary school teacher talking to your students "
},
{
"role": "user",
"content": message
},
),
)
print(completion)
Question : Explain What is the computer vision
CODE EXPLAIN
import openai
openai.api_key = "Your API Key"
Import the openai package and write down your API Key generated in above link.
message_list = ()
while True:
Input = input("->")
if Input == "":
break
message_list.append(Input)
print(message_list)
message = message_list(0)
You can just write down your message inside the code, but I prefer input version, because it’s a chatting. Right?
completion = openai.ChatCompletion.create(
model = "gpt-3.5-turbo",
There’re other chatGPT API like Fig. 2, Image Generation or Embeddings, so pick API that you needed.
messages = (
{
"role": "system",
"content": "You are a kind elementary school teacher talking to your students "
},
{
"role": "user",
"content": message
},
),
)
I assigned two roles, ‘system’ and ‘user’.
I assigned the role of elementary school teacher who talking to students to system. In common sense, most of the elementary school teachers used easy words and trying to treat their students kindly and lovely.
And ‘user’ message is the answer of ChatGPT.
RESULT
Let’s check the result.
{
"choices": (
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "Hello, my lovely students!
Have you ever wondered how
computers are able to understand and interpret images and videos
that we often see on our devices? Well, this is where the concept
of 'Computer Vision' comes in.\n\nComputer Vision is the field of
study where computers are given the ability to understand, interpret
and analyze digital images and videos with the help of Machine Learning
and Artificial Intelligence algorithms. In simpler words, it allows
computers to see and recognize images, just like humans.\n\n
For example, suppose you want to teach a computer how to identify
a cat in a picture. You'll need to feed the computer's algorithm
loads of cat pictures, and with the computer's capability to analyze
those images, it'll gradually learn to understand what a cat is and
be able to recognize it in any given picture.\n\nThis field is widely
used in areas such as facial recognition technology, autonomous vehicles,
medical imaging, surveillance systems, and many other applications.
It's exciting to see what kind of advancements you'll be able to make
using computer vision technology in the future!
\n\nHope that helps
answer your question, class.",
"role": "assistant"
}
}
),
"created": 1679447432,
"id": "chatcmpl-6whHkF1QjEtATpcb9nWQTKzyr0lW3",
"model": "gpt-3.5-turbo-0301",
"object": "chat.completion",
"usage": {
"completion_tokens": 220,
"prompt_tokens": 31,
"total_tokens": 251
}
}
As you can see, the answer reminds me my elementary school teachers (probably).
Check how many tokens are used, and calculate the price.
I hope this post helps you to make your own chatting code 🙂