Part One :How to create a Local chatbot in Python using OpenAi API

Rohit Raj
3 min readNov 11, 2023

--

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 the following steps to create a local chatbot using OpenAi API

1 Sign up for OpenAI API at 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 are given below

2 Generate OpenAI key at OpenAI website. 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.

3 Install gradio and openai library by running following commands in terminal

pip install gradio
pip install openai

4 Run python file in terminal and 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.

Price of OpenAI gpt3.5 turbo API is 2 dollar per million output tokens. You can generate lots of output at a very low price using gpt3.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 gpt3.5 turbo until you realise 3000 pages contain around 1 million tokens.

If you liked the article, please clap and subscribe.

--

--

Rohit Raj
Rohit Raj

Written by Rohit Raj

Studied at IIT Madras and IIM Indore. Love Data Science

No responses yet