fix err handling

This commit is contained in:
if 2023-03-29 20:37:10 +03:00
parent 8e9ac262be
commit 5a6c809807
2 changed files with 6 additions and 3 deletions

View File

@ -13,6 +13,7 @@ def send_ai_request(ai_model: str, messages: List[dict]) -> dict:
completion = openai.ChatCompletion.create( completion = openai.ChatCompletion.create(
model=ai_model, model=ai_model,
messages=messages, messages=messages,
# temperature=0.7,
) )
logging.warning("completion:") logging.warning("completion:")
logging.warning(completion) logging.warning(completion)

View File

@ -2,7 +2,7 @@ import streamlit as st
from openai.error import InvalidRequestError, OpenAIError from openai.error import InvalidRequestError, OpenAIError
from streamlit_chat import message from streamlit_chat import message
from src.utils.ai_interaction import send_ai_request from src.utils.agi.chat_gpt import send_ai_request
from src.utils.tts import show_player from src.utils.tts import show_player
@ -58,11 +58,13 @@ def show_conversation(user_content: str, model: str, role: str) -> None:
show_chat(ai_content, user_content) show_chat(ai_content, user_content)
st.markdown("---") st.markdown("---")
show_player(ai_content) show_player(ai_content)
except InvalidRequestError as e: except InvalidRequestError as err:
if e.code == "context_length_exceeded": if err.code == "context_length_exceeded":
st.session_state.messages.pop(1) st.session_state.messages.pop(1)
if len(st.session_state.messages) == 1: if len(st.session_state.messages) == 1:
st.session_state.user_text = "" st.session_state.user_text = ""
show_conversation(st.session_state.user_text, st.session_state.model, st.session_state.role) show_conversation(st.session_state.user_text, st.session_state.model, st.session_state.role)
else:
st.error(err)
except (OpenAIError, UnboundLocalError) as err: except (OpenAIError, UnboundLocalError) as err:
st.error(err) st.error(err)