import os import re import streamlit as st import openai from dotenv import load_dotenv from langchain.agents.openai_assistant import OpenAIAssistantRunnable # Load environment variables load_dotenv() api_key = os.getenv("OPENAI_API_KEY") extractor_agent = os.getenv("ASSISTANT_ID_SOLUTION_SPECIFIER_A") # Create the assistant extractor_llm = OpenAIAssistantRunnable( assistant_id=extractor_agent, api_key=api_key, as_agent=True ) def remove_citation(text: str) -> str: pattern = r"【\d+†\w+】" return re.sub(pattern, "📚", text) # Initialize session state for messages and thread_id if "messages" not in st.session_state: st.session_state["messages"] = [] if "thread_id" not in st.session_state: st.session_state["thread_id"] = None st.title("Solution Specifier A") def predict(user_input: str) -> str: """ This function calls our OpenAIAssistantRunnable to get a response. We either create a new thread (no thread_id yet) or continue the existing thread. If the server complains that a run is still active, we reset the thread_id and retry once. """ try: if st.session_state["thread_id"] is None: # Start a new thread response = extractor_llm.invoke({"content": user_input}) st.session_state["thread_id"] = response.thread_id else: # Continue the existing thread response = extractor_llm.invoke( {"content": user_input, "thread_id": st.session_state["thread_id"]} ) output = response.return_values["output"] return remove_citation(output) except openai.error.BadRequestError as e: # If the error says a run is still active, reset to a new thread and re-invoke once err_msg = str(e) if "while a run" in err_msg: st.session_state["thread_id"] = None # Re-invoke once to get a fresh thread try: response = extractor_llm.invoke({"content": user_input}) st.session_state["thread_id"] = response.thread_id output = response.return_values["output"] return remove_citation(output) except Exception as e2: st.error(f"Error after resetting thread: {e2}") return "" else: # Some other bad request st.error(err_msg) return "" except Exception as e: # Catch-all for any other error st.error(str(e)) return "" # Display any existing messages for msg in st.session_state["messages"]: if msg["role"] == "user": with st.chat_message("user"): st.write(msg["content"]) else: with st.chat_message("assistant"): st.write(msg["content"]) # Create the chat input widget at the bottom of the page user_input = st.chat_input("Type your message here...") # When the user hits ENTER on st.chat_input if user_input: # Add the user message to session state st.session_state["messages"].append({"role": "user", "content": user_input}) # Display the user's message with st.chat_message("user"): st.write(user_input) # Get the assistant's response response_text = predict(user_input) # Add the assistant response to session state st.session_state["messages"].append({"role": "assistant", "content": response_text}) # Display the assistant's reply with st.chat_message("assistant"): st.write(response_text)