jdesiree commited on
Commit
ded6624
·
verified ·
1 Parent(s): dcb1c37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -70
app.py CHANGED
@@ -12,6 +12,7 @@ from langchain.agents import initialize_agent, AgentType
12
  from langchain_community.llms import HuggingFaceHub
13
  from langchain.memory import ConversationBufferWindowMemory
14
  from langchain.prompts import PromptTemplate
 
15
  from pydantic import BaseModel, Field
16
  from typing import Type, Optional
17
 
@@ -54,46 +55,6 @@ class CreateGraphTool(BaseTool):
54
  except Exception as e:
55
  return f"<p style='color:red;'>Error creating graph: {str(e)}</p>"
56
 
57
-
58
- # --- LangChain Setup ---
59
- def create_langchain_agent():
60
- """Initialize LangChain agent with tools and memory."""
61
-
62
- # Initialize LLM
63
- llm = HuggingFaceHub(
64
- repo_id="Qwen/Qwen2.5-VL-7B-Instruct",
65
- huggingfacehub_api_token=hf_token,
66
- model_kwargs={
67
- "temperature": 0.7,
68
- "max_new_tokens": 1000,
69
- "top_p": 0.9,
70
- "return_full_text": False
71
- }
72
- )
73
-
74
- # Initialize tools
75
- tools = [CreateGraphTool()]
76
-
77
- # Initialize memory
78
- memory = ConversationBufferWindowMemory(
79
- memory_key="chat_history",
80
- k=10,
81
- return_messages=True
82
- )
83
-
84
- # Create agent
85
- agent = initialize_agent(
86
- tools=tools,
87
- llm=llm,
88
- agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
89
- memory=memory,
90
- verbose=False,
91
- max_iterations=3,
92
- early_stopping_method="generate"
93
- )
94
-
95
- return agent
96
-
97
  # --- System Prompt ---
98
  SYSTEM_PROMPT = """You are EduBot, an expert multi-concept tutor designed to facilitate genuine learning and understanding. Your primary mission is to guide students through the learning process rather than providing direct answers to academic work.
99
 
@@ -151,9 +112,57 @@ When using the create_graph tool, format data as JSON strings:
151
  - Encourage students to explain their thinking and reasoning
152
  - Provide honest, accurate feedback even when it may not be what the student wants to hear
153
 
154
- Your goal is to be an educational partner who empowers students to succeed through understanding, not a service that completes their work for them.
 
 
 
 
155
 
156
- Question: {input}"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
 
158
  # --- Global Agent Instance ---
159
  agent = None
@@ -164,23 +173,6 @@ def get_agent():
164
  if agent is None:
165
  agent = create_langchain_agent()
166
  return agent
167
-
168
- # --- Force Light Mode Script ---
169
- force_light_mode = '''
170
- <script>
171
- // Force light theme in Gradio
172
- window.addEventListener('DOMContentLoaded', function () {
173
- const gradioURL = window.location.href;
174
- const url = new URL(gradioURL);
175
- const currentTheme = url.searchParams.get('__theme');
176
-
177
- if (currentTheme !== 'light') {
178
- url.searchParams.set('__theme', 'light');
179
- window.location.replace(url.toString());
180
- }
181
- });
182
- </script>
183
- '''
184
 
185
  # --- UI: MathJax Configuration ---
186
  mathjax_config = '''
@@ -213,6 +205,23 @@ html_head_content = '''
213
  <title>EduBot - AI Educational Assistant</title>
214
  '''
215
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
216
  # --- Core Logic Functions ---
217
  def smart_truncate(text, max_length=3000):
218
  """Truncates text intelligently to the last full sentence or word."""
@@ -228,18 +237,19 @@ def smart_truncate(text, max_length=3000):
228
  return ' '.join(words[:-1]) + "... [Response truncated]"
229
 
230
  def generate_response_with_langchain(message, max_retries=3):
231
- """Generate response using LangChain agent."""
232
 
233
  for attempt in range(max_retries):
234
  try:
235
  # Get the agent
236
  current_agent = get_agent()
237
 
238
- # Format the prompt
239
- formatted_prompt = SYSTEM_PROMPT.format(input=message)
240
 
241
- # Get response from agent
242
- response = current_agent.run(formatted_prompt)
 
243
 
244
  return smart_truncate(response)
245
 
@@ -258,7 +268,7 @@ def chat_response(message, history=None):
258
  start_time = time.time()
259
  metrics_tracker.log_interaction(message, "user_query", "chat_start")
260
 
261
- # Generate response with LangChain
262
  response = generate_response_with_langchain(message)
263
 
264
  # Log metrics with timing context
@@ -275,7 +285,7 @@ def chat_response(message, history=None):
275
  def respond_with_enhanced_streaming(message, history=None):
276
  """Enhanced streaming response function."""
277
  try:
278
- response = chat_response(message, history)
279
  yield response
280
  except Exception as e:
281
  logger.error(f"Error in streaming response: {e}")
@@ -304,13 +314,14 @@ def respond_and_update(message, history):
304
  yield history, ""
305
 
306
  def clear_chat():
307
- """Clear the chat history."""
308
- # Reset agent memory
309
- global agent
310
  if agent is not None:
311
  agent.memory.clear()
 
312
  return [], ""
313
 
 
314
  # --- UI: Interface Creation ---
315
  def create_interface():
316
  """Creates and configures the complete Gradio interface."""
@@ -329,7 +340,7 @@ def create_interface():
329
  title="EduBot",
330
  fill_width=True,
331
  fill_height=True,
332
- theme=gr.themes.Base()
333
  ) as demo:
334
  # Add head content and MathJax
335
  gr.HTML(html_head_content)
 
12
  from langchain_community.llms import HuggingFaceHub
13
  from langchain.memory import ConversationBufferWindowMemory
14
  from langchain.prompts import PromptTemplate
15
+ from langchain.schema import SystemMessage, HumanMessage, AIMessage
16
  from pydantic import BaseModel, Field
17
  from typing import Type, Optional
18
 
 
55
  except Exception as e:
56
  return f"<p style='color:red;'>Error creating graph: {str(e)}</p>"
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  # --- System Prompt ---
59
  SYSTEM_PROMPT = """You are EduBot, an expert multi-concept tutor designed to facilitate genuine learning and understanding. Your primary mission is to guide students through the learning process rather than providing direct answers to academic work.
60
 
 
112
  - Encourage students to explain their thinking and reasoning
113
  - Provide honest, accurate feedback even when it may not be what the student wants to hear
114
 
115
+ Your goal is to be an educational partner who empowers students to succeed through understanding, not a service that completes their work for them."""
116
+
117
+ # --- Improved LangChain Setup ---
118
+ # Global flag to track system prompt initialization
119
+ system_prompt_initialized = False
120
 
121
+ def initialize_system_prompt(agent):
122
+ """Initialize the system prompt as a SystemMessage in memory."""
123
+ global system_prompt_initialized
124
+ if not system_prompt_initialized:
125
+ system_message = SystemMessage(content=SYSTEM_PROMPT)
126
+ agent.memory.chat_memory.add_message(system_message)
127
+ system_prompt_initialized = True
128
+
129
+ def create_langchain_agent():
130
+ """Initialize LangChain agent with tools and memory."""
131
+
132
+ # Initialize LLM
133
+ llm = HuggingFaceHub(
134
+ repo_id="Qwen/Qwen2.5-VL-7B-Instruct",
135
+ huggingfacehub_api_token=hf_token,
136
+ model_kwargs={
137
+ "temperature": 0.7,
138
+ "max_new_tokens": 1000,
139
+ "top_p": 0.9,
140
+ "return_full_text": False
141
+ }
142
+ )
143
+
144
+ # Initialize tools
145
+ tools = [CreateGraphTool()]
146
+
147
+ # Initialize memory
148
+ memory = ConversationBufferWindowMemory(
149
+ memory_key="chat_history",
150
+ k=10,
151
+ return_messages=True
152
+ )
153
+
154
+ # Create agent WITHOUT system prompt in prefix (we'll add it to memory instead)
155
+ agent = initialize_agent(
156
+ tools=tools,
157
+ llm=llm,
158
+ agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
159
+ memory=memory,
160
+ verbose=False,
161
+ max_iterations=3,
162
+ early_stopping_method="generate"
163
+ )
164
+
165
+ return agent
166
 
167
  # --- Global Agent Instance ---
168
  agent = None
 
173
  if agent is None:
174
  agent = create_langchain_agent()
175
  return agent
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
 
177
  # --- UI: MathJax Configuration ---
178
  mathjax_config = '''
 
205
  <title>EduBot - AI Educational Assistant</title>
206
  '''
207
 
208
+ # --- Force Light Mode Script ---
209
+ force_light_mode = '''
210
+ <script>
211
+ // Force light theme in Gradio
212
+ window.addEventListener('DOMContentLoaded', function () {
213
+ const gradioURL = window.location.href;
214
+ const url = new URL(gradioURL);
215
+ const currentTheme = url.searchParams.get('__theme');
216
+
217
+ if (currentTheme !== 'light') {
218
+ url.searchParams.set('__theme', 'light');
219
+ window.location.replace(url.toString());
220
+ }
221
+ });
222
+ </script>
223
+ '''
224
+
225
  # --- Core Logic Functions ---
226
  def smart_truncate(text, max_length=3000):
227
  """Truncates text intelligently to the last full sentence or word."""
 
237
  return ' '.join(words[:-1]) + "... [Response truncated]"
238
 
239
  def generate_response_with_langchain(message, max_retries=3):
240
+ """Generate response using LangChain agent with proper message handling."""
241
 
242
  for attempt in range(max_retries):
243
  try:
244
  # Get the agent
245
  current_agent = get_agent()
246
 
247
+ # Initialize system prompt if not already done
248
+ initialize_system_prompt(current_agent)
249
 
250
+ # Use the agent directly with the message
251
+ # LangChain will automatically handle adding HumanMessage and AIMessage to memory
252
+ response = current_agent.run(input=message)
253
 
254
  return smart_truncate(response)
255
 
 
268
  start_time = time.time()
269
  metrics_tracker.log_interaction(message, "user_query", "chat_start")
270
 
271
+ # Generate response with LangChain (history is managed by LangChain memory)
272
  response = generate_response_with_langchain(message)
273
 
274
  # Log metrics with timing context
 
285
  def respond_with_enhanced_streaming(message, history=None):
286
  """Enhanced streaming response function."""
287
  try:
288
+ response = chat_response(message)
289
  yield response
290
  except Exception as e:
291
  logger.error(f"Error in streaming response: {e}")
 
314
  yield history, ""
315
 
316
  def clear_chat():
317
+ """Clear the chat history and reset system prompt flag."""
318
+ global agent, system_prompt_initialized
 
319
  if agent is not None:
320
  agent.memory.clear()
321
+ system_prompt_initialized = False
322
  return [], ""
323
 
324
+
325
  # --- UI: Interface Creation ---
326
  def create_interface():
327
  """Creates and configures the complete Gradio interface."""
 
340
  title="EduBot",
341
  fill_width=True,
342
  fill_height=True,
343
+ theme=gr.themes.Origin()
344
  ) as demo:
345
  # Add head content and MathJax
346
  gr.HTML(html_head_content)