Q-A_Chatbot / app.py
SaileshSivan's picture
Updated app.py
6cc6a76
from langchain.llms import OpenAI
import streamlit as st
import os
import time
# Function to load OpenAI model and get responses
def get_openai_response(question):
openai_api_key = os.getenv("OPENAI_API_KEY")
llm = OpenAI(openai_api_key=openai_api_key, model_name="text-davinci-003", temperature=0.5)
# Retry logic
max_retries = 3
retries = 0
while retries < max_retries:
try:
response = llm(question)
return response
except RateLimitError as e:
print(f"Rate limit exceeded. Waiting for {e.retry_after} seconds and retrying...")
time.sleep(e.retry_after)
retries += 1
print("Exceeded maximum number of retries. Please try again later.")
return None # Or handle the error in an appropriate way in your code
# Streamlit app
st.set_page_config(page_title="Q&A Demo")
st.header("Langchain Application")
input_question = st.text_input("Input: ", key="input")
submit = st.button("Ask the question")
# If the "Ask" button is clicked
if submit:
response = get_openai_response(input_question)
if response is not None:
st.subheader("The Response is:")
st.write(response)
else:
st.subheader("Error: Unable to get response. Please try again later.")