Spaces:
Sleeping
Sleeping
Update graph_tool.py
Browse files- graph_tool.py +57 -32
graph_tool.py
CHANGED
|
@@ -94,35 +94,60 @@ def Create_Graph_Tool(graph_config: str) -> str:
|
|
| 94 |
logger.error(f"Error in graph generation: {e}")
|
| 95 |
return f'<p style="color:red;">Error creating graph: {str(e)}</p>'
|
| 96 |
|
| 97 |
-
# --- Tool
|
| 98 |
-
class
|
| 99 |
-
"""
|
| 100 |
|
| 101 |
-
def __init__(self):
|
| 102 |
-
self.
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
# --- System Prompt ---
|
| 128 |
SYSTEM_PROMPT = """You are Mimir, 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.
|
|
@@ -267,11 +292,11 @@ class Qwen25SmallLLM:
|
|
| 267 |
|
| 268 |
# --- Modern Agent Implementation ---
|
| 269 |
class Educational_Agent:
|
| 270 |
-
"""Modern LangChain agent with
|
| 271 |
|
| 272 |
def __init__(self):
|
| 273 |
self.llm = Qwen25SmallLLM(model_path="Qwen/Qwen2.5-3B-Instruct")
|
| 274 |
-
self.
|
| 275 |
self.memory = ConversationBufferWindowMemory(
|
| 276 |
memory_key="chat_history",
|
| 277 |
return_messages=True,
|
|
@@ -279,8 +304,8 @@ class Educational_Agent:
|
|
| 279 |
)
|
| 280 |
|
| 281 |
def should_use_tools(self, query: str) -> bool:
|
| 282 |
-
"""
|
| 283 |
-
return self.
|
| 284 |
|
| 285 |
def create_prompt_template(self, has_tools: bool = False):
|
| 286 |
"""Create prompt template based on whether tools are available"""
|
|
|
|
| 94 |
logger.error(f"Error in graph generation: {e}")
|
| 95 |
return f'<p style="color:red;">Error creating graph: {str(e)}</p>'
|
| 96 |
|
| 97 |
+
# --- Tool Decision Engine ---
|
| 98 |
+
class Tool_Decision_Engine:
|
| 99 |
+
"""Uses LLM to intelligently decide when visualization tools would be beneficial"""
|
| 100 |
|
| 101 |
+
def __init__(self, llm):
|
| 102 |
+
self.decision_llm = llm
|
| 103 |
+
self.decision_prompt = """Analyze this educational query and determine if creating a graph, chart, or visual representation would significantly enhance learning and understanding.
|
| 104 |
+
|
| 105 |
+
Query: "{query}"
|
| 106 |
+
|
| 107 |
+
Consider these factors:
|
| 108 |
+
1. Would visualization make a concept clearer or easier to understand?
|
| 109 |
+
2. Does the topic involve data, relationships, comparisons, or trends?
|
| 110 |
+
3. Could a graph help illustrate abstract concepts concretely?
|
| 111 |
+
4. For practice questions, would including visual elements be educational?
|
| 112 |
+
|
| 113 |
+
Examples that BENEFIT from visualization:
|
| 114 |
+
- Explaining mathematical functions or statistical concepts
|
| 115 |
+
- Creating practice questions that involve data interpretation
|
| 116 |
+
- Teaching about scientific trends or relationships
|
| 117 |
+
- Comparing quantities, performance, or outcomes
|
| 118 |
+
- Illustrating economic principles or business metrics
|
| 119 |
+
|
| 120 |
+
Examples that do NOT need visualization:
|
| 121 |
+
- Simple definitions or explanations
|
| 122 |
+
- General conversation or greetings
|
| 123 |
+
- Text-based study strategies
|
| 124 |
+
- Qualitative discussions without data
|
| 125 |
+
|
| 126 |
+
Answer with exactly: YES or NO
|
| 127 |
+
|
| 128 |
+
Decision:"""
|
| 129 |
+
|
| 130 |
+
def should_use_visualization(self, query: str) -> bool:
|
| 131 |
+
"""Use LLM reasoning to determine if visualization would be beneficial"""
|
| 132 |
+
try:
|
| 133 |
+
# Create decision prompt
|
| 134 |
+
decision_query = self.decision_prompt.format(query=query)
|
| 135 |
+
|
| 136 |
+
# Get LLM decision
|
| 137 |
+
decision_response = self.decision_llm.invoke(decision_query)
|
| 138 |
+
|
| 139 |
+
# Parse response - look for YES/NO
|
| 140 |
+
decision_text = decision_response.strip().upper()
|
| 141 |
+
|
| 142 |
+
# Log the decision for debugging
|
| 143 |
+
logger.info(f"Tool decision for '{query[:50]}...': {decision_text}")
|
| 144 |
+
|
| 145 |
+
return "YES" in decision_text and "NO" not in decision_text
|
| 146 |
+
|
| 147 |
+
except Exception as e:
|
| 148 |
+
logger.error(f"Error in tool decision making: {e}")
|
| 149 |
+
# Default to no tools if decision fails
|
| 150 |
+
return False
|
| 151 |
|
| 152 |
# --- System Prompt ---
|
| 153 |
SYSTEM_PROMPT = """You are Mimir, 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.
|
|
|
|
| 292 |
|
| 293 |
# --- Modern Agent Implementation ---
|
| 294 |
class Educational_Agent:
|
| 295 |
+
"""Modern LangChain agent with LLM-based tool decision making"""
|
| 296 |
|
| 297 |
def __init__(self):
|
| 298 |
self.llm = Qwen25SmallLLM(model_path="Qwen/Qwen2.5-3B-Instruct")
|
| 299 |
+
self.tool_decision_engine = Tool_Decision_Engine(self.llm)
|
| 300 |
self.memory = ConversationBufferWindowMemory(
|
| 301 |
memory_key="chat_history",
|
| 302 |
return_messages=True,
|
|
|
|
| 304 |
)
|
| 305 |
|
| 306 |
def should_use_tools(self, query: str) -> bool:
|
| 307 |
+
"""Use LLM reasoning to determine if tools are needed"""
|
| 308 |
+
return self.tool_decision_engine.should_use_visualization(query)
|
| 309 |
|
| 310 |
def create_prompt_template(self, has_tools: bool = False):
|
| 311 |
"""Create prompt template based on whether tools are available"""
|