Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from langchain.prompts import ChatPromptTemplate
|
| 3 |
from langchain_huggingface import HuggingFaceEndpoint
|
| 4 |
-
from langchain.schema import HumanMessage, SystemMessage
|
| 5 |
import os
|
| 6 |
import time
|
| 7 |
import logging
|
|
@@ -118,12 +118,31 @@ def respond_with_enhanced_streaming(message, history):
|
|
| 118 |
"""Streams the bot's response, detecting the subject and handling errors."""
|
| 119 |
try:
|
| 120 |
template, mode = detect_subject(message)
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
yield f"*{mode}*\n\nGenerating response..."
|
| 124 |
|
| 125 |
logger.info(f"Processing {mode} query: {message[:50]}...")
|
| 126 |
|
|
|
|
|
|
|
| 127 |
response = chain.invoke({
|
| 128 |
"question": message,
|
| 129 |
"system_message": "You are EduBot, an expert AI learning assistant. Provide comprehensive, educational responses that help students truly understand concepts."
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from langchain.prompts import ChatPromptTemplate
|
| 3 |
from langchain_huggingface import HuggingFaceEndpoint
|
| 4 |
+
from langchain.schema import HumanMessage, SystemMessage, AIMessage
|
| 5 |
import os
|
| 6 |
import time
|
| 7 |
import logging
|
|
|
|
| 118 |
"""Streams the bot's response, detecting the subject and handling errors."""
|
| 119 |
try:
|
| 120 |
template, mode = detect_subject(message)
|
| 121 |
+
|
| 122 |
+
# Build conversation history with proper LangChain message objects
|
| 123 |
+
messages = []
|
| 124 |
+
|
| 125 |
+
# Add system message
|
| 126 |
+
system_msg = SystemMessage(content="You are EduBot, an expert AI learning assistant. Provide comprehensive, educational responses that help students truly understand concepts.")
|
| 127 |
+
messages.append(system_msg)
|
| 128 |
+
|
| 129 |
+
# Add conversation history if available
|
| 130 |
+
if history:
|
| 131 |
+
for exchange in history[-5:]: # Keep last 5 exchanges for context
|
| 132 |
+
if exchange.get("role") == "user":
|
| 133 |
+
messages.append(HumanMessage(content=exchange["content"]))
|
| 134 |
+
elif exchange.get("role") == "assistant":
|
| 135 |
+
messages.append(AIMessage(content=exchange["content"]))
|
| 136 |
+
|
| 137 |
+
# Add current user message
|
| 138 |
+
messages.append(HumanMessage(content=message))
|
| 139 |
|
| 140 |
yield f"*{mode}*\n\nGenerating response..."
|
| 141 |
|
| 142 |
logger.info(f"Processing {mode} query: {message[:50]}...")
|
| 143 |
|
| 144 |
+
# Use the chain with proper message history
|
| 145 |
+
chain = template | llm
|
| 146 |
response = chain.invoke({
|
| 147 |
"question": message,
|
| 148 |
"system_message": "You are EduBot, an expert AI learning assistant. Provide comprehensive, educational responses that help students truly understand concepts."
|