Spaces:
Sleeping
Sleeping
Applied changes to resolve noted issues.
Browse fileshttps://github.com/Jdesiree112/Prompt_Engineering/blob/main/CaseStudy_Mimir/Issue_ProblemSolvingCaseStudy/Issue-Solutions_Overview.md
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 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
-
|
| 115 |
-
-
|
| 116 |
-
-
|
| 117 |
-
|
| 118 |
-
-
|
| 119 |
-
|
| 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 |
-
"""
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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 |
-
|
| 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 |
-
|
| 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,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|
| 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()
|