Spaces:
Sleeping
Sleeping
added history in json
Browse filesRecord the chat history in a json file, named based on the pinecone index or chroma collection name
- app.py +23 -3
- chat_history/empty_chat_hist.json +0 -0
app.py
CHANGED
|
@@ -14,7 +14,7 @@ import langchain
|
|
| 14 |
import pinecone
|
| 15 |
import streamlit as st
|
| 16 |
import shutil
|
| 17 |
-
|
| 18 |
|
| 19 |
OPENAI_API_KEY = ''
|
| 20 |
PINECONE_API_KEY = ''
|
|
@@ -52,7 +52,6 @@ def save_file(files):
|
|
| 52 |
shutil.copyfileobj(file, f)
|
| 53 |
|
| 54 |
|
| 55 |
-
@st.cache_data()
|
| 56 |
def load_files():
|
| 57 |
all_texts = []
|
| 58 |
n_files = 0
|
|
@@ -142,6 +141,20 @@ def setup_em_llm(OPENAI_API_KEY, temperature):
|
|
| 142 |
return embeddings, llm
|
| 143 |
|
| 144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
pinecone_index_name, chroma_collection_name, persist_directory, docsearch_ready, directory_name = init()
|
| 146 |
|
| 147 |
|
|
@@ -179,6 +192,7 @@ def main(pinecone_index_name, chroma_collection_name, persist_directory, docsear
|
|
| 179 |
persist_directory = "./vectorstore"
|
| 180 |
|
| 181 |
if pinecone_index_name or chroma_collection_name:
|
|
|
|
| 182 |
if r_ingest.lower() == 'yes':
|
| 183 |
files = st.file_uploader(
|
| 184 |
'Upload Files', accept_multiple_files=True)
|
|
@@ -207,12 +221,18 @@ def main(pinecone_index_name, chroma_collection_name, persist_directory, docsear
|
|
| 207 |
placeholder='Summarize the context.')
|
| 208 |
if query:
|
| 209 |
# Generate a reply based on the user input and chat history
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
reply, source = get_response(query, chat_history, CRqa)
|
| 211 |
# Update the chat history with the user input and system response
|
| 212 |
chat_history.append(('User', query))
|
| 213 |
chat_history.append(('Bot', reply))
|
|
|
|
|
|
|
| 214 |
chat_history_str = '\n'.join(
|
| 215 |
-
[f'{x[0]}: {x[1]}' for x in
|
| 216 |
st.text_area('Chat record:', value=chat_history_str, height=250)
|
| 217 |
# Display sources
|
| 218 |
for i, source_i in enumerate(source):
|
|
|
|
| 14 |
import pinecone
|
| 15 |
import streamlit as st
|
| 16 |
import shutil
|
| 17 |
+
import json
|
| 18 |
|
| 19 |
OPENAI_API_KEY = ''
|
| 20 |
PINECONE_API_KEY = ''
|
|
|
|
| 52 |
shutil.copyfileobj(file, f)
|
| 53 |
|
| 54 |
|
|
|
|
| 55 |
def load_files():
|
| 56 |
all_texts = []
|
| 57 |
n_files = 0
|
|
|
|
| 141 |
return embeddings, llm
|
| 142 |
|
| 143 |
|
| 144 |
+
def load_chat_history(CHAT_HISTORY_FILENAME):
|
| 145 |
+
try:
|
| 146 |
+
with open(CHAT_HISTORY_FILENAME, 'r') as f:
|
| 147 |
+
chat_history = json.load(f)
|
| 148 |
+
except (FileNotFoundError, json.JSONDecodeError):
|
| 149 |
+
chat_history = []
|
| 150 |
+
return chat_history
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
def save_chat_history(chat_history, CHAT_HISTORY_FILENAME):
|
| 154 |
+
with open(CHAT_HISTORY_FILENAME, 'w') as f:
|
| 155 |
+
json.dump(chat_history, f)
|
| 156 |
+
|
| 157 |
+
|
| 158 |
pinecone_index_name, chroma_collection_name, persist_directory, docsearch_ready, directory_name = init()
|
| 159 |
|
| 160 |
|
|
|
|
| 192 |
persist_directory = "./vectorstore"
|
| 193 |
|
| 194 |
if pinecone_index_name or chroma_collection_name:
|
| 195 |
+
session_name = pinecone_index_name + chroma_collection_name
|
| 196 |
if r_ingest.lower() == 'yes':
|
| 197 |
files = st.file_uploader(
|
| 198 |
'Upload Files', accept_multiple_files=True)
|
|
|
|
| 221 |
placeholder='Summarize the context.')
|
| 222 |
if query:
|
| 223 |
# Generate a reply based on the user input and chat history
|
| 224 |
+
CHAT_HISTORY_FILENAME = f"chat_history/{session_name}_chat_hist.json"
|
| 225 |
+
chat_history = load_chat_history(CHAT_HISTORY_FILENAME)
|
| 226 |
+
chat_history = [(user, bot)
|
| 227 |
+
for user, bot in chat_history]
|
| 228 |
reply, source = get_response(query, chat_history, CRqa)
|
| 229 |
# Update the chat history with the user input and system response
|
| 230 |
chat_history.append(('User', query))
|
| 231 |
chat_history.append(('Bot', reply))
|
| 232 |
+
save_chat_history(chat_history, CHAT_HISTORY_FILENAME)
|
| 233 |
+
latest_chats = chat_history[-4:]
|
| 234 |
chat_history_str = '\n'.join(
|
| 235 |
+
[f'{x[0]}: {x[1]}' for x in latest_chats])
|
| 236 |
st.text_area('Chat record:', value=chat_history_str, height=250)
|
| 237 |
# Display sources
|
| 238 |
for i, source_i in enumerate(source):
|
chat_history/empty_chat_hist.json
ADDED
|
File without changes
|