Few days back OpenAI introduced GPT-4-Turbo model. It is based on GPT-4 model trained with data up to April 2023. GPT-4 Turbo also has an enlarged 128K context window, which means it can take prompts equivalent to around 300 pages of text. Despite these improvements, the price of GPT-4 Turbo is three times cheaper than that of GPT-4 model.
Bill Gates wrote a blog on the future of AI agents. He believes that in the next five years, LLMs will completely change how we interact with information and computers.
So, I believe everyone should use LLM and equivalents.
In this article I will show you how you can use OpenAI API for use in your own PC.
GPT-3.5-turbo is free to use under Chatgpt subscription. But if you want to use GPT-4, you have to pay 20 dollars per month for Chatgpt plus subscription. This is a steep price for people who are not heavy user. They can use GPT-4 using its API for a much lesser cost per month.
You can undertake following steps to create local chatbot using OpenAI API.
Sign up for OpenAI API at the following website
On registration, you will get credits worth 18 dollars, valid for three months. Later on, you can top up credits by card. The prices of various models can be found here pricing.
Generate OpenAI key at OpenAI website
Generate you API access key and Paste the key into below code.
import gradio as gr
from openai import OpenAI
api_key = "sk-....." # Replace with your key
def predict(message, history):
history_openai_format = []
for human, assistant in history:
history_openai_format.append({"role": "user", "content": human })
history_openai_format.append({"role": "assistant", "content":assistant})
history_openai_format.append({"role": "user", "content": message})
client = OpenAI(
api_key=api_key,)
response = client.chat.completions.create(
messages=history_openai_format,
# model="gpt-3.5-turbo" # gpt 3.5 turbo
# model="gpt-4",
model = "gpt-4-1106-preview", #gpt-4 turbo
stream = True
)
partial_message = ""
for chunk in response:
text = (chunk.choices[0].delta.content)
if text is not None:
partial_message = partial_message + text
yield partial_message
gr.ChatInterface(predict).queue().launch()
Save the code in a python file in your PC.
Package Installation
Install gradio and OpenAI library by running following commands in terminal.
pip install gradio
pip install openai
Run python file in terminal
After runing the python file open http://localhost:7860/ link in browser. You will get an interface like below in your browser.
Now you can talk to chatgpt model like chatgpt web application.
Reflection:
Price of OpenAI GPT-3.5 turbo API is 2 dollar per million output tokens. You can generate lots of output at a very low price using GPT-3.5 turbo model. For important tasks you can switch to gpt 4 turbo model. Its price is 30 dollars per million output token. This seems high compared to GPT-3.5 turbo until you realise 3000 pages contain around 1 million tokens.
Hope you were able to successfully create a chatbot on your local machine in python using OpenAI API. Schedule a call with us to know more about using OpenAI API's for you bussiness or a project.