Chatbot with api key validation

Hi, I have a case were I needed a streamlit chatbot to assist with some prompts.
The issue is an application that is running local using some LLM but the prompt uses gpt-4. I wanted a validation of the api key, if the user has a default openai enviroment key it should use that, if it has become invalid due to changes as they shift to Project API keys it should prompt the user for a key text_input save it to .env in the folder reload it and override the invalid key, next rerun should not prompt user for key, if cache is cleared load it from .env.

I made a working example here in Chatbot.py gist with the vanilla streamlit chatbot.

  1. Although making an API call to validate the key is effective, this cant be the only option, does openai api not provide a direct method? not that the cost and rate limits or an issue here but still.
  2. I am trying to do a silent testing of API Keys: we first try to validate the API key from the environment variable (api_key_env) using validate_api_key(). If it’s valid, we set it as the client.api_key. If not, we try the API key from the session state (api_key_session). If both fail, we prompt the user to enter their API key using get_api_key(). It is prone to break, and assumes OPENAI_KEY_API is the name of the key. Any suggestions for improvement is there a component for it? after setting a valid api key the st.form is deleted just to keep the sidebar clear for other options. not sure if there are other options for this.

place the chatbot.py in same folder as app.py
in the app.py add:

import streamlit as st
from chatbot import ChatBot

def main():
    chatbot = ChatBot()
    chatbot.run()

if __name__ == "__main__":
    main()
1 Like