jdesiree commited on
Commit
07748f3
·
verified ·
1 Parent(s): 84d5953

Applied changes to resolve noted issues.

Browse files

https://github.com/Jdesiree112/Prompt_Engineering/blob/main/CaseStudy_Mimir/Issue_ProblemSolvingCaseStudy/Issue-Solutions_Overview.md

Files changed (1) hide show
  1. app.py +63 -27
app.py CHANGED
@@ -3,8 +3,8 @@ from graph_tool import generate_plot
3
  from metrics import MimirMetrics
4
  import os
5
 
 
6
  os.environ['HF_HOME'] = '/tmp/huggingface'
7
- os.environ['TRANSFORMERS_CACHE'] = '/tmp/huggingface'
8
  os.environ['HF_DATASETS_CACHE'] = '/tmp/huggingface'
9
 
10
  import time
@@ -14,7 +14,8 @@ import re
14
  from langchain_core.tools import tool
15
  from langchain_core.messages import HumanMessage, SystemMessage, AIMessage
16
  from langchain_core.prompts import ChatPromptTemplate
17
- from langchain_core.runnables import RunnableBranch
 
18
  from langchain.agents import initialize_agent, AgentType
19
  from langchain.memory import ConversationBufferWindowMemory
20
  from typing import Optional, List, Any, Type
@@ -103,28 +104,42 @@ class Tool_Decision_Engine:
103
  self.decision_llm = llm
104
  self.decision_prompt = """Analyze this educational query and determine if creating a graph, chart, or visual representation would significantly enhance learning and understanding.
105
  Query: "{query}"
106
- Consider these factors:
107
- 1. Would visualization make a concept clearer or easier to understand?
108
- 2. Does the topic involve data, relationships, comparisons, or trends?
109
- 3. Could a graph help illustrate abstract concepts concretely?
110
- 4. For practice questions, would including visual elements be educational?
111
- Examples that BENEFIT from visualization:
112
- - Explaining mathematical functions or statistical concepts
113
- - Creating practice questions that involve data interpretation
114
- - Teaching about scientific trends or relationships
115
- - Comparing quantities, performance, or outcomes
116
- - Illustrating economic principles or business metrics
117
- Examples that do NOT need visualization:
118
- - Simple definitions or explanations
119
- - General conversation or greetings
120
- - Text-based study strategies
121
- - Qualitative discussions without data
122
  Answer with exactly: YES or NO
123
  Decision:"""
124
 
125
  def should_use_visualization(self, query: str) -> bool:
126
- """Use LLM reasoning to determine if visualization would be beneficial"""
127
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  # Create decision prompt
129
  decision_query = self.decision_prompt.format(query=query)
130
 
@@ -137,7 +152,10 @@ Decision:"""
137
  # Log the decision for debugging
138
  logger.info(f"Tool decision for '{query[:50]}...': {decision_text}")
139
 
140
- return "YES" in decision_text and "NO" not in decision_text
 
 
 
141
 
142
  except Exception as e:
143
  logger.error(f"Error in tool decision making: {e}")
@@ -191,11 +209,14 @@ You have the ability to create graphs and charts to enhance your explanations. U
191
  - Provide honest, accurate feedback even when it may not be what the student wants to hear
192
  Your goal is to be an educational partner who empowers students to succeed through understanding, not a service that completes their work for them."""
193
 
194
- # --- LLM Class Unchanged ---
195
  logger = logging.getLogger(__name__)
196
 
197
- class Qwen25SmallLLM:
 
 
198
  def __init__(self, model_path: str = "Qwen/Qwen2.5-3B-Instruct", use_4bit: bool = True):
 
199
  logger.info(f"Loading model: {model_path} (use_4bit={use_4bit})")
200
 
201
  try:
@@ -212,12 +233,12 @@ class Qwen25SmallLLM:
212
  llm_int8_skip_modules=["lm_head"]
213
  )
214
 
215
- # Try quantized load
216
  self.model = AutoModelForCausalLM.from_pretrained(
217
  model_path,
218
  quantization_config=quant_config,
219
  device_map="auto",
220
- torch_dtype=torch.bfloat16,
221
  trust_remote_code=True,
222
  low_cpu_mem_usage=True
223
  )
@@ -236,13 +257,20 @@ class Qwen25SmallLLM:
236
  """Fallback if quantization fails."""
237
  self.model = AutoModelForCausalLM.from_pretrained(
238
  model_path,
239
- torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
240
  device_map="auto" if torch.cuda.is_available() else None,
241
  trust_remote_code=True,
242
  low_cpu_mem_usage=True
243
  )
244
 
245
- def invoke(self, prompt: str, stop: Optional[List[str]] = None) -> str:
 
 
 
 
 
 
 
246
  try:
247
  messages = [
248
  {"role": "system", "content": SYSTEM_PROMPT},
@@ -273,6 +301,14 @@ class Qwen25SmallLLM:
273
  logger.error(f"Generation error: {e}")
274
  return f"[Error generating response: {str(e)}]"
275
 
 
 
 
 
 
 
 
 
276
  # --- Modern Agent Implementation ---
277
  class Educational_Agent:
278
  """Modern LangChain agent with LLM-based tool decision making"""
@@ -633,7 +669,7 @@ if __name__ == "__main__":
633
 
634
  # Step 2: Warm up the model
635
  logger.info("Warming up model...")
636
- warmup_agent() # Comment out temporarily to avoid hanging
637
 
638
  interface = create_interface()
639
  interface.queue()
 
3
  from metrics import MimirMetrics
4
  import os
5
 
6
+ # Updated environment variables
7
  os.environ['HF_HOME'] = '/tmp/huggingface'
 
8
  os.environ['HF_DATASETS_CACHE'] = '/tmp/huggingface'
9
 
10
  import time
 
14
  from langchain_core.tools import tool
15
  from langchain_core.messages import HumanMessage, SystemMessage, AIMessage
16
  from langchain_core.prompts import ChatPromptTemplate
17
+ from langchain_core.runnables import RunnableBranch, Runnable
18
+ from langchain_core.runnables.utils import Input, Output
19
  from langchain.agents import initialize_agent, AgentType
20
  from langchain.memory import ConversationBufferWindowMemory
21
  from typing import Optional, List, Any, Type
 
104
  self.decision_llm = llm
105
  self.decision_prompt = """Analyze this educational query and determine if creating a graph, chart, or visual representation would significantly enhance learning and understanding.
106
  Query: "{query}"
107
+
108
+ EXCLUDE if query is:
109
+ - Greetings or casual conversation (hello, hi, hey)
110
+ - Simple definitions without data
111
+ - Test/warmup messages
112
+ - General explanations that don't involve data
113
+
114
+ INCLUDE if query involves:
115
+ - Mathematical functions or relationships
116
+ - Data analysis or statistics
117
+ - Comparisons that benefit from charts
118
+ - Trends or patterns over time
119
+ - Creating practice questions with data
120
+
 
 
121
  Answer with exactly: YES or NO
122
  Decision:"""
123
 
124
  def should_use_visualization(self, query: str) -> bool:
125
+ """Enhanced decision logic with explicit exclusions"""
126
  try:
127
+ # Explicit exclusions for common non-visual queries
128
+ exclusion_patterns = [
129
+ r'^(hello|hi|hey)\b',
130
+ r'warmup.*test',
131
+ r'(what is|define|explain)\s+\w+\s*(of|the)?',
132
+ r'capital\s+of',
133
+ r'^(greet|greeting)'
134
+ ]
135
+
136
+ query_lower = query.lower().strip()
137
+
138
+ # Check exclusions first
139
+ for pattern in exclusion_patterns:
140
+ if re.search(pattern, query_lower):
141
+ return False
142
+
143
  # Create decision prompt
144
  decision_query = self.decision_prompt.format(query=query)
145
 
 
152
  # Log the decision for debugging
153
  logger.info(f"Tool decision for '{query[:50]}...': {decision_text}")
154
 
155
+ # More strict parsing
156
+ if "YES" in decision_text and "NO" not in decision_text:
157
+ return True
158
+ return False
159
 
160
  except Exception as e:
161
  logger.error(f"Error in tool decision making: {e}")
 
209
  - Provide honest, accurate feedback even when it may not be what the student wants to hear
210
  Your goal is to be an educational partner who empowers students to succeed through understanding, not a service that completes their work for them."""
211
 
212
+ # --- Fixed LLM Class with Runnable inheritance ---
213
  logger = logging.getLogger(__name__)
214
 
215
+ class Qwen25SmallLLM(Runnable):
216
+ """LLM class that properly inherits from Runnable for LangChain compatibility"""
217
+
218
  def __init__(self, model_path: str = "Qwen/Qwen2.5-3B-Instruct", use_4bit: bool = True):
219
+ super().__init__()
220
  logger.info(f"Loading model: {model_path} (use_4bit={use_4bit})")
221
 
222
  try:
 
233
  llm_int8_skip_modules=["lm_head"]
234
  )
235
 
236
+ # Try quantized load with updated dtype parameter
237
  self.model = AutoModelForCausalLM.from_pretrained(
238
  model_path,
239
  quantization_config=quant_config,
240
  device_map="auto",
241
+ dtype=torch.bfloat16,
242
  trust_remote_code=True,
243
  low_cpu_mem_usage=True
244
  )
 
257
  """Fallback if quantization fails."""
258
  self.model = AutoModelForCausalLM.from_pretrained(
259
  model_path,
260
+ dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
261
  device_map="auto" if torch.cuda.is_available() else None,
262
  trust_remote_code=True,
263
  low_cpu_mem_usage=True
264
  )
265
 
266
+ def invoke(self, input: Input, config=None) -> Output:
267
+ """Main invoke method for Runnable compatibility"""
268
+ # Handle both string and dict inputs for flexibility
269
+ if isinstance(input, dict):
270
+ prompt = input.get('input', str(input))
271
+ else:
272
+ prompt = str(input)
273
+
274
  try:
275
  messages = [
276
  {"role": "system", "content": SYSTEM_PROMPT},
 
301
  logger.error(f"Generation error: {e}")
302
  return f"[Error generating response: {str(e)}]"
303
 
304
+ @property
305
+ def InputType(self) -> Type[Input]:
306
+ return str
307
+
308
+ @property
309
+ def OutputType(self) -> Type[Output]:
310
+ return str
311
+
312
  # --- Modern Agent Implementation ---
313
  class Educational_Agent:
314
  """Modern LangChain agent with LLM-based tool decision making"""
 
669
 
670
  # Step 2: Warm up the model
671
  logger.info("Warming up model...")
672
+ warmup_agent()
673
 
674
  interface = create_interface()
675
  interface.queue()