Chat with GPT-4 using Streamlit and the OpenAI API

Anurag Chatterjee
3 min readJan 20, 2024

--

GPT-4 is at the cutting edge of the chatbot evolution. While ChatGPT Plus and Bing offers users ways to use GPT-4. They are either on the costly side if the usage is sporadic or have some limitations. Those with OpenAI API access could then use their API credits to use the GPT-4 models for their use cases in their own chat app.

With the latest features of Streamlit framework, developers can now create conversational apps using the OpenAI APIs seamlessly. The chat-with-gpt4-streamlit repo brings this power directly to your fingertips.

Getting Your Chat App Up and Running

Setting up the app is straightforward. Begin by securing your OpenAI API key as a Streamlit secret in the provided .streamlit/secrets.toml file. You can buy your OpenAI API credits following the steps mentioned here. You can start with the minimal amount of $5 and increase based on usage as required.

Then, clone the source code using the GitHub CLI. Create a virtual environment on Unix/macOS, install the required dependencies, and with a simple streamlit run main.py command, you can start the streamlit application from your local server. Check out a sample run of the app below:

Sample execution of the app

Features That Make Chatting a Breeze

The app has the below features:

  • Instant Streaming Responses: Say goodbye to waiting around for your AI to respond.
  • Save Conversations: Use the Export feature to save on API calls and document your interactions efficiently. Exported conversations are conveniently saved as a timestamped CSV file.
Export your conversation to reduce token usage
Exported conversation in a CSV file
  • Token Tracking: Monitor the total tokens used in your conversations thanks to the tiktoken library integration.
Total tokens used

Tailor the Chat to Your Liking

The app is not just user-friendly; it’s customizable! Dive into configs.py to choose the OpenAI model that suits your needs, with the full list of models available here and here. Also, you have the freedom to adjust where your chat exports are saved by altering the EXPORT_DIR parameter.

Check out the source code by visiting the repository on GitHub today. Happy hacking!

PS: This first draft of this post was generated by GPT-4-Turbo using the streamlit app mentioned in the post!

--

--

Anurag Chatterjee
Anurag Chatterjee

Written by Anurag Chatterjee

I am an experienced professional who likes to build solutions to real-world problems using innovative technologies and then share my learnings with everyone.

Responses (1)