Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,31 +10,33 @@ def get_api_key():
|
|
10 |
st.error("API_KEY not found in Streamlit secrets. Please add it.")
|
11 |
return None
|
12 |
|
13 |
-
# Function to interact with the OpenAI API
|
14 |
-
def generate_response(prompt, model_name, api_key):
|
15 |
openai.api_key = api_key
|
16 |
try:
|
17 |
-
|
|
|
18 |
model=model_name,
|
19 |
-
messages=
|
|
|
20 |
)
|
21 |
-
return
|
22 |
-
except openai.APIError as e:
|
23 |
st.error(f"OpenAI API Error with {model_name}: {e}")
|
24 |
return None
|
25 |
-
except openai.RateLimitError as e:
|
26 |
st.error(f"OpenAI Rate Limit Error with {model_name}: {e}")
|
27 |
return None
|
28 |
-
except openai.AuthenticationError as e:
|
29 |
st.error(f"OpenAI Authentication Error with {model_name}: {e}")
|
30 |
return None
|
31 |
-
except Exception as e:
|
32 |
st.error(f"An unexpected error occurred with {model_name}: {e}")
|
33 |
return None
|
34 |
|
35 |
# Main Streamlit app
|
36 |
def main():
|
37 |
-
st.title("Chatbot with Model Switching")
|
38 |
|
39 |
# Initialize conversation history in session state
|
40 |
if "messages" not in st.session_state:
|
@@ -55,24 +57,31 @@ def main():
|
|
55 |
st.markdown(prompt)
|
56 |
|
57 |
# Define model priority
|
58 |
-
models = ["gpt-4", "gpt-3.5-turbo"] # Add more models as needed
|
59 |
|
60 |
# Get API key
|
61 |
api_key = get_api_key()
|
62 |
if not api_key:
|
63 |
return
|
64 |
|
65 |
-
|
66 |
for model in models:
|
67 |
-
|
68 |
-
if
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
# Add bot message to state
|
73 |
-
st.session_state.messages.append({"role": "assistant", "content":
|
74 |
-
with st.chat_message("assistant"):
|
75 |
-
st.markdown(response)
|
76 |
|
77 |
if __name__ == "__main__":
|
|
|
78 |
main()
|
|
|
10 |
st.error("API_KEY not found in Streamlit secrets. Please add it.")
|
11 |
return None
|
12 |
|
13 |
+
# Function to interact with the OpenAI API with streaming
|
14 |
+
def generate_response(prompt, model_name, api_key, extra_instructions=""):
|
15 |
openai.api_key = api_key
|
16 |
try:
|
17 |
+
messages = [{"role": "user", "content": prompt + "\n" + extra_instructions}]
|
18 |
+
stream = client.chat.completions.create(
|
19 |
model=model_name,
|
20 |
+
messages=messages,
|
21 |
+
stream=True,
|
22 |
)
|
23 |
+
return stream
|
24 |
+
except openai.APIError as e:
|
25 |
st.error(f"OpenAI API Error with {model_name}: {e}")
|
26 |
return None
|
27 |
+
except openai.RateLimitError as e:
|
28 |
st.error(f"OpenAI Rate Limit Error with {model_name}: {e}")
|
29 |
return None
|
30 |
+
except openai.AuthenticationError as e:
|
31 |
st.error(f"OpenAI Authentication Error with {model_name}: {e}")
|
32 |
return None
|
33 |
+
except Exception as e:
|
34 |
st.error(f"An unexpected error occurred with {model_name}: {e}")
|
35 |
return None
|
36 |
|
37 |
# Main Streamlit app
|
38 |
def main():
|
39 |
+
st.title("Chatbot with Model Switching and Streaming")
|
40 |
|
41 |
# Initialize conversation history in session state
|
42 |
if "messages" not in st.session_state:
|
|
|
57 |
st.markdown(prompt)
|
58 |
|
59 |
# Define model priority
|
60 |
+
models = ["gpt-4-1106-preview", "gpt-3.5-turbo"] # Add more models as needed
|
61 |
|
62 |
# Get API key
|
63 |
api_key = get_api_key()
|
64 |
if not api_key:
|
65 |
return
|
66 |
|
67 |
+
full_response = ""
|
68 |
for model in models:
|
69 |
+
stream = generate_response(prompt, model, api_key, extra_instructions="Please address the user as Jane Doe. The user has a premium account.")
|
70 |
+
if stream:
|
71 |
+
with st.chat_message("assistant"):
|
72 |
+
message_placeholder = st.empty()
|
73 |
+
for chunk in stream:
|
74 |
+
if chunk.choices and chunk.choices[0].delta and chunk.choices[0].delta.content:
|
75 |
+
full_response += chunk.choices[0].delta.content
|
76 |
+
message_placeholder.markdown(full_response + "▌")
|
77 |
+
message_placeholder.markdown(full_response)
|
78 |
+
break # Break after successful response
|
79 |
+
full_response = "" # Reset for the next model attempt
|
80 |
+
|
81 |
+
if full_response:
|
82 |
# Add bot message to state
|
83 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
|
|
|
|
84 |
|
85 |
if __name__ == "__main__":
|
86 |
+
client = openai.OpenAI() # Initialize the client here
|
87 |
main()
|