Sanchayt commited on
Commit
104d2ae
·
1 Parent(s): 860b718
Files changed (1) hide show
  1. app.py +53 -47
app.py CHANGED
@@ -22,6 +22,8 @@ with st.sidebar:
22
  corpus_id = st.text_input("Vectara Corpus ID", value=str(os.getenv("CORPUS_ID", "")))
23
  openai_api_key = st.text_input("OpenAI API Key", value=os.getenv("OPENAI_API_KEY", ""))
24
  submit_button = st.button("Submit")
 
 
25
 
26
  # Constants
27
  CUSTOMER_ID = customer_id if customer_id else os.getenv("CUSTOMER_ID")
@@ -60,50 +62,54 @@ prompt = PromptTemplate.from_template(
60
  runnable = prompt | ChatOpenAI(streaming=True, callbacks=[StreamingStdOutCallbackHandler()], openai_api_key=OPENAI_API_KEY) | StrOutputParser()
61
 
62
  # Main Streamlit App
63
- st.title("Legal Consultation Chat")
64
-
65
- # Initialize chat history
66
- if "messages" not in st.session_state:
67
- st.session_state.messages = []
68
-
69
- # Display chat messages from history on app rerun
70
- for message in st.session_state.messages:
71
- with st.chat_message(message["role"]):
72
- st.markdown(message["content"])
73
-
74
- # Accept user input and run the main chat interaction
75
- if user_input := st.chat_input("Enter your issue:"):
76
- st.session_state.messages.append({"role": "user", "content": user_input})
77
- with st.chat_message("user"):
78
- st.markdown(user_input)
79
-
80
- knowledge_content = get_knowledge_content(vectara_client, user_input)
81
- print("__________________ Start of knowledge content __________________")
82
- print(knowledge_content)
83
- response = runnable.invoke({"knowledge": knowledge_content, "issue": user_input})
84
-
85
- response_words = response.split()
86
- with st.chat_message("assistant"):
87
- message_placeholder = st.empty()
88
- full_response = ""
89
- for word in response_words:
90
- full_response += word + " "
91
- time.sleep(0.05)
92
- message_placeholder.markdown(full_response + "▌")
93
- message_placeholder.markdown(full_response)
94
-
95
- st.session_state.messages.append({"role": "assistant", "content": full_response})
96
-
97
- # Run when the submit button is pressed
98
- if submit_button and uploaded_file:
99
- with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmpfile:
100
- tmpfile.write(uploaded_file.getvalue())
101
- tmp_filename = tmpfile.name
102
-
103
- try:
104
- vectara_client.add_files([tmp_filename])
105
- st.sidebar.success("PDF file successfully uploaded to Vectara!")
106
- except Exception as e:
107
- st.sidebar.error(f"An error occurred: {str(e)}")
108
- finally:
109
- os.remove(tmp_filename) # Clean up temporary file
 
 
 
 
 
22
  corpus_id = st.text_input("Vectara Corpus ID", value=str(os.getenv("CORPUS_ID", "")))
23
  openai_api_key = st.text_input("OpenAI API Key", value=os.getenv("OPENAI_API_KEY", ""))
24
  submit_button = st.button("Submit")
25
+
26
+ keys_provided = all([customer_id, api_key, corpus_id, openai_api_key])
27
 
28
  # Constants
29
  CUSTOMER_ID = customer_id if customer_id else os.getenv("CUSTOMER_ID")
 
62
  runnable = prompt | ChatOpenAI(streaming=True, callbacks=[StreamingStdOutCallbackHandler()], openai_api_key=OPENAI_API_KEY) | StrOutputParser()
63
 
64
  # Main Streamlit App
65
+ if keys_provided:
66
+ st.title("Legal Consultation Chat")
67
+
68
+ # Initialize chat history
69
+ if "messages" not in st.session_state:
70
+ st.session_state.messages = []
71
+
72
+ # Display chat messages from history on app rerun
73
+ for message in st.session_state.messages:
74
+ with st.chat_message(message["role"]):
75
+ st.markdown(message["content"])
76
+
77
+ # Accept user input and run the main chat interaction
78
+ if user_input := st.chat_input("Enter your issue:"):
79
+ st.session_state.messages.append({"role": "user", "content": user_input})
80
+ with st.chat_message("user"):
81
+ st.markdown(user_input)
82
+
83
+ knowledge_content = get_knowledge_content(vectara_client, user_input)
84
+ print("__________________ Start of knowledge content __________________")
85
+ print(knowledge_content)
86
+ response = runnable.invoke({"knowledge": knowledge_content, "issue": user_input})
87
+
88
+ response_words = response.split()
89
+ with st.chat_message("assistant"):
90
+ message_placeholder = st.empty()
91
+ full_response = ""
92
+ for word in response_words:
93
+ full_response += word + " "
94
+ time.sleep(0.05)
95
+ message_placeholder.markdown(full_response + "▌")
96
+ message_placeholder.markdown(full_response)
97
+
98
+ st.session_state.messages.append({"role": "assistant", "content": full_response})
99
+
100
+ # Run when the submit button is pressed
101
+ if submit_button and uploaded_file:
102
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmpfile:
103
+ tmpfile.write(uploaded_file.getvalue())
104
+ tmp_filename = tmpfile.name
105
+
106
+ try:
107
+ vectara_client.add_files([tmp_filename])
108
+ st.sidebar.success("PDF file successfully uploaded to Vectara!")
109
+ except Exception as e:
110
+ st.sidebar.error(f"An error occurred: {str(e)}")
111
+ finally:
112
+ os.remove(tmp_filename) # Clean up temporary file
113
+ else:
114
+ # Not all keys are provided, instruct the user to input them
115
+ st.warning("Please input all required API keys in the sidebar to proceed.")