Update app.py
Browse files
app.py
CHANGED
|
@@ -67,19 +67,28 @@ def chat_with_docs(message, history, api_key):
|
|
| 67 |
global chat_engine, conversation_history
|
| 68 |
|
| 69 |
if not api_key:
|
| 70 |
-
return history + [
|
| 71 |
|
| 72 |
if chat_engine is None:
|
| 73 |
-
return history + [
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
try:
|
| 76 |
response = chat_engine.chat(message)
|
| 77 |
conversation_history.append({"role": "user", "content": message})
|
| 78 |
conversation_history.append({"role": "assistant", "content": response.response})
|
| 79 |
|
| 80 |
-
return history + [
|
|
|
|
|
|
|
|
|
|
| 81 |
except Exception as e:
|
| 82 |
-
return history + [
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
# Function to save conversation
|
| 85 |
def save_conversation():
|
|
@@ -171,7 +180,11 @@ with gr.Blocks(title="Chat with Documents 💬 📚") as demo:
|
|
| 171 |
|
| 172 |
with gr.Row():
|
| 173 |
with gr.Column(scale=3):
|
| 174 |
-
chatbot = gr.Chatbot(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
msg = gr.Textbox(
|
| 176 |
label="Your Question",
|
| 177 |
placeholder="Ask a question about your documents..."
|
|
|
|
| 67 |
global chat_engine, conversation_history
|
| 68 |
|
| 69 |
if not api_key:
|
| 70 |
+
return history + [{"role": "assistant", "content": "Please enter your OpenAI API key first."}]
|
| 71 |
|
| 72 |
if chat_engine is None:
|
| 73 |
+
return history + [
|
| 74 |
+
{"role": "user", "content": message},
|
| 75 |
+
{"role": "assistant", "content": "Please upload and load documents first before asking questions."}
|
| 76 |
+
]
|
| 77 |
|
| 78 |
try:
|
| 79 |
response = chat_engine.chat(message)
|
| 80 |
conversation_history.append({"role": "user", "content": message})
|
| 81 |
conversation_history.append({"role": "assistant", "content": response.response})
|
| 82 |
|
| 83 |
+
return history + [
|
| 84 |
+
{"role": "user", "content": message},
|
| 85 |
+
{"role": "assistant", "content": response.response}
|
| 86 |
+
]
|
| 87 |
except Exception as e:
|
| 88 |
+
return history + [
|
| 89 |
+
{"role": "user", "content": message},
|
| 90 |
+
{"role": "assistant", "content": f"Error: {str(e)}"}
|
| 91 |
+
]
|
| 92 |
|
| 93 |
# Function to save conversation
|
| 94 |
def save_conversation():
|
|
|
|
| 180 |
|
| 181 |
with gr.Row():
|
| 182 |
with gr.Column(scale=3):
|
| 183 |
+
chatbot = gr.Chatbot(
|
| 184 |
+
label="Chat",
|
| 185 |
+
height=400,
|
| 186 |
+
type="messages" # Explicitly set type to messages
|
| 187 |
+
)
|
| 188 |
msg = gr.Textbox(
|
| 189 |
label="Your Question",
|
| 190 |
placeholder="Ask a question about your documents..."
|