File size: 1,295 Bytes
3055acb
 
 
6cc6a76
f2a6896
6cc6a76
3055acb
6cc6a76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3055acb
 
 
6cc6a76
 
3055acb
6cc6a76
3055acb
6cc6a76
f2a6896
6cc6a76
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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.")