OpenAI recently launched ChatGPT API for everyone to use them in their products/Code/POCs etc. ChatGPT took the internet world by storm when it first released in Nov’22 by registering 1 Million users in just 5 days and they did not stop to this only. They crossed 100 Million users on their platform in record 2 months which is the highest among all the available services on the internet.
ChatGPT Applications | Scope
ChatGPT uses GPT-3.5-turbo which is stronger than GPT-3 in some aspects. Using Chat API from OpenAI we can create applications which can have the capabilities to do things like:
- Draft an email or other piece of writing
- Write Python code
- Answer questions about a set of documents
- Create conversational agents
- Give your software a natural language interface
- Tutor in a range of subjects
- Translate languages
- Simulate characters for video games and much more
Instructions
To use ChatGPT API in Python, you first need to install the OpenAI API client. This can be done by running the following command in your terminal:
Code language: Python (python)pip install openai
Next, you will need to obtain an API key from OpenAI. This can be done by creating an account on the OpenAI website and following the instructions to generate an API key.
Once you have installed the OpenAI client and obtained an API key, you can use ChatGPT API in your Python code by importing the openai module and initializing the CHATGPT model with your API key. Which will look like this
import openai
openai.api_key = "<your-api-key>"
message = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "2019 cricket world cup was played in how many over format?"},
{"role": "assistant", "content": "The 2019 Cricket World Cup was played in the standard One Day International (ODI) format of 50 overs per innings. Each team played a maximum of 50 overs in their innings, with each over consisting of six legal deliveries."},
{"role": "user", "content": "Where was it played?"},
{"role": "assistant", "content": "The 2019 Cricket World Cup was hosted by England and Wales. The tournament was played across multiple venues in England and Wales."},
{"role": "user", "content": "Who won the match ?"},
]
)
print(message)
Code language: JavaScript (javascript)
Once you run this script, You will get an output something like this in the terminal
{
'id': 'chatcmpl-6p9XYPYSTTRi0xEviKjjilqrWU2Ve',
'object': 'chat.completion',
'created': 1679221755,
'model': 'gpt-3.5-turbo',
'usage': {'prompt_tokens': 70, 'completion_tokens': 41, 'total_tokens': 111},
'choices': [
{
'message': {
'role': 'assistant',
'content': 'The Cricket World Cup 2019 was won by the England cricket team. They defeated New Zealand in a thrilling final that was tied after both the regulation match and the Super Over. England were declared winners on the boundary countback rule as they scored more boundaries in the match. It was their first-ever World Cup victory.'},
'finish_reason': 'stop',
'index': 0
}
]
}
Code language: JavaScript (javascript)
In this very easy way, You can use ChatGPT API and implement it with your own ideas or ask ChatGPT for the ideas and start working on them.
In the next article, We will create a chatbot capable of understanding the context in which the conversation is taking place and respond accordingly. Till then Keep Supporting Python Warriors