fix rate limit

This commit is contained in:
BuffTechTalk
2025-01-10 15:58:09 -06:00
parent 42c6d7a0db
commit 340c66dfac
46584 changed files with 42 additions and 4096537 deletions

View File

@@ -21,11 +21,11 @@ def buffbot():
with st.expander("See Source Code"):
with open(__file__, "r", encoding="utf-8") as f:
st.code(f.read(), language="python")
st.divider()
# Select AI model for chatbot
model_options = ["llama3.2:1b", "deepseek-chat", ]
# on_change callback to clear chat history when model is changed
selected_model = st.selectbox("**👉 Please select a model to start**", model_options, on_change=clear_chat)
selected_model = st.selectbox("**👉Please select a model to start**", model_options, on_change=clear_chat)
# Initialize session state to store chat history and message count
if "messages" not in st.session_state:
@@ -85,31 +85,37 @@ def buffbot():
with st.chat_message(message["role"], avatar=avatar):
st.markdown(message["content"])
# Get user input
if prompt := st.chat_input("Type your message here..."):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display user message with cowboy avatar
with st.chat_message("user", avatar="🤠"):
st.markdown(prompt)
if st.session_state.message_count < MAX_USER_MESSAGES:
# Get user input
if prompt := st.chat_input("Type your message here..."):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
st.session_state.message_count += 1
# Display user message with cowboy avatar
with st.chat_message("user", avatar="🤠"):
st.markdown(prompt)
# Generate reply
with st.chat_message("assistant", avatar="🦬"):
with st.spinner('Thinking...'):
# Call the selected model API to generate a response
stream = client.chat.completions.create(
model=selected_model,
messages=[
{"role": m["role"], "content": m["content"]}
for m in st.session_state.messages
],
stream=True, # stream the response
)
# Display the response from the model API
response = st.write_stream(stream)
# Add the AI assistant response to the chat history
st.session_state.messages.append({"role": "assistant", "content": response})
# Generate reply
with st.chat_message("assistant", avatar="🦬"):
with st.spinner('Thinking...'):
# Call the selected model API to generate a response
stream = client.chat.completions.create(
model=selected_model,
messages=[
{"role": m["role"], "content": m["content"]}
for m in st.session_state.messages
],
stream=True, # stream the response
)
# Display the response from the model API
response = st.write_stream(stream)
# Add the AI assistant response to the chat history
st.session_state.messages.append({"role": "assistant", "content": response})
else:
st.warning("You have reached the maximum number of messages allowed.\
Please switch to another model to continue chatting.")
# Clear chat history
if st.button("Clear Chat"):
clear_chat()