Spaces:
Sleeping
Sleeping
File size: 9,195 Bytes
b9c5775 56e69d2 c710ae2 b9c5775 c710ae2 b9c5775 56e69d2 b9c5775 a33c96d 56e69d2 c710ae2 56e69d2 6953aaf b9c5775 a33c96d c710ae2 a33c96d c710ae2 a33c96d c710ae2 a33c96d c710ae2 a33c96d c710ae2 a33c96d c710ae2 a33c96d c710ae2 a33c96d c710ae2 a33c96d c710ae2 a33c96d c710ae2 a33c96d c710ae2 a33c96d c710ae2 a33c96d c710ae2 a33c96d c710ae2 a33c96d c710ae2 a33c96d c710ae2 a33c96d c710ae2 a33c96d c710ae2 a33c96d d0d37ab 6953aaf c710ae2 a33c96d c710ae2 a33c96d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | ---
title: Mimir
emoji: 📚
colorFrom: yellow
colorTo: yellow
sdk: gradio
sdk_version: 5.44.1
app_file: app.py
pinned: true
python_version: '3.10'
short_description: Advanced prompt engineering for educational AI systems.
thumbnail: >-
https://cdn-uploads.huggingface.co/production/uploads/68700e7552b74a1dcbb2a87e/Z7P8DJ57rc5P1ozA5gwp3.png
hardware: zero-gpu-dynamic
hf_oauth: true
hf_oauth_expiration_minutes: 120
---
# Mimir: Educational AI Assistant
## Advanced Prompt Engineering Portfolio Project
### Project Overview
Mimir demonstrates sophisticated prompt engineering techniques applied to educational technology, showcasing the implementation of context-aware AI systems that prioritize pedagogical effectiveness over simple answer generation. A key feature is its ability to **dynamically generate custom data visualizations**, determined by an intelligent decision engine that assesses whether a visual aid will enhance the pedagogical explanation. This project exemplifies professional-grade prompt design for educational applications, embodying the role of an educational partner that guides students to discover answers for themselves.
***
### Technical Architecture
**Core Technologies:**
* **LangChain**: Prompt template management and conversation chain orchestration.
* **LangGraph**: Orchestrates the application's flow as a state machine (**StateGraph**). It manages the conditional logic for the tool-use decision engine, routing user queries between the LLM, a pre-built **ToolNode** for graph generation, and the final response node.
* **Gradio**: Full-stack web interface with custom CSS styling.
* **Hugging Face Inference API**: Model deployment and response generation.
* **Python**: Backend logic and integration layer.
* **Matplotlib**: Powers the dynamic, in-memory generation of educational graphs and charts.
**Key Frameworks:**
* `langchain.prompts.ChatPromptTemplate` for dynamic prompt construction.
* `langchain_huggingface.HuggingFaceEndpoint` for model interface.
* `langchain.schema` message objects (HumanMessage, AIMessage, SystemMessage).
* `langgraph.graph.StateGraph` & `langgraph.prebuilt.ToolNode` for building and executing the conditional logic graph.
* `langgraph.checkpoint.memory.MemorySaver` for persistent conversation state.
***
### Prompt Engineering Techniques Demonstrated
#### 1. Unified System Prompt Architecture
Employs a single, comprehensive system prompt that establishes the AI's core persona as **Mimir, an expert multi-concept tutor**. This foundational prompt meticulously defines the AI's behavior, tone, and pedagogical mission. It integrates:
* **Core Educational Principles**: A directive to prioritize teaching methodology, foster critical thinking, and provide comprehensive explanations over direct answers.
* **Defined Persona & Tone**: Specific instructions to maintain an engaging, supportive, and intellectually appropriate tone for high school students, while avoiding fluff and emojis.
* **Specific Response Guidelines**: Contextual rules for handling different academic tasks, such as explaining concepts in math problems instead of solving them, or discussing research strategies for essays rather than writing them.
#### 2. Instructional Design Integration
The core prompt incorporates evidence-based instructional design principles:
* **Scaffolding**: Breaking complex concepts into manageable components.
* **Socratic Method**: Guiding discovery rather than providing direct answers.
* **Metacognitive Strategies**: Teaching learning-how-to-learn approaches.
#### 3. Academic Integrity Constraints
Implemented ethical AI guidelines directly into the system prompt:
* Explicit instructions to avoid homework completion.
* Focus on **process over product delivery**.
* Critical thinking skill development emphasis.
#### 4. Two-Stage Tool-Use Prompting
A sophisticated two-stage prompting strategy governs the use of the `Create_Graph_Tool`:
* **Tool-Use Decision Prompt**: A highly-constrained template is used by the `Tool_Decision_Engine` to determine if a tool should be used. This prompt forces a **YES** or **NO** response based on whether a visual aid would significantly enhance learning, using explicit **INCLUDE** and **EXCLUDE** criteria.
* **Tool-Execution Guidance**: The main system prompt contains separate, explicit instructions on how to use the tool once the decision has been made. It provides the exact **JSON structure** the model must output, including fields like `data`, `plot_type`, and `educational_context`, ensuring the generated graphs are pedagogically sound.
***
### Advanced Implementation Features
#### Intelligent Graphing Tool Integration
A custom, dynamic visualization system was developed to provide multi-modal educational responses.
* **LLM-Powered Analysis**: For relevant queries, a targeted LLM call is made using the specialized YES/NO decision prompt.
* **Dynamic Visualization Tool (`Create_Graph_Tool`)**: Designed and implemented a custom visualization tool using **matplotlib**. The tool receives a JSON configuration from the LLM and generates high-quality bar, line, or pie charts. The entire process occurs in-memory:
* The plot is rendered into a `BytesIO` buffer.
* The image is encoded into a **base64 string**.
* The final output is an HTML `<img>` tag with the embedded base64 data, which is displayed directly in the chat interface, eliminating the need for file I/O.
* The tool's docstring provides a clear schema and usage instructions for the LLM, ensuring reliable and pedagogically sound visualizations.
#### Stateful Conversation Management with LangGraph
Implements persistent, multi-turn conversations using LangGraph's **MemorySaver**. This allows the application's state, including the full message history (`add_messages`), to be saved and resumed, ensuring robust context management even when tool use is involved.
#### Response Streaming & Truncation
* Smart text truncation preserving sentence integrity.
* Real-time response streaming for improved UX.
* Error handling and fallback mechanisms.
#### Template Chaining Architecture
The core logic utilizes **LangChain Expression Language (LCEL)** to pipe inputs through templates, models, and tools.
***
### User Interface Engineering
* **Gradio Layout & Custom Styling**: The interface is built with `gr.Blocks`, using `gr.Column` and `gr.Row` to structure the main components. A custom `styles.css` file is loaded to apply specific theming, responsive design, and layout rules, moving beyond default Gradio styling for a tailored user experience.
* **Component Architecture**: Modular Gradio component structure with custom CSS class integration and accessibility-compliant patterns.
***
### Prompt Engineering Methodologies Applied
* **Template Parameterization**: Dynamic variable injection for contextual responses.
* **Persona-Driven Response Generation**: Crafting a detailed persona within the system prompt to guide the AI's tone, style, and pedagogical approach consistently.
* **Domain-Specific Language Modeling**: Educational vocabulary and pedagogical terminology integration.
* **Multi-Modal Response Formatting**: Structured output generation with educational formatting.
* **Agentic Tool Routing**: Designing prompts and logic that enable an AI system to intelligently decide which tool is appropriate for a given task, simulating agent-like behavior.
***
### Professional Applications
This project demonstrates competency in:
* **Enterprise-Grade Prompt Design**: Scalable template and tool-use architecture.
* **Educational Technology Integration**: Designing AI tutors with robust pedagogical frameworks and dynamic, multi-modal response capabilities.
* **Ethical AI Implementation**: Academic integrity safeguards and responsible AI practices.
* **Full-Stack AI Application Development**: End-to-end system implementation.
* **Intelligent Agent & Tool Development**: Building AI agents that can utilize custom tools to solve complex problems.
***
### Technical Specifications
**Dependencies:**
* **Core ML/AI**: `transformers`, `torch`, `accelerate`
* **LangChain & LangGraph**: `langgraph`, `langchain-core`, `langchain-community`, `langchain-huggingface`
* **UI Framework**: `gradio`
* **Visualization**: `matplotlib`, `plotly`, `pandas`, `numpy`, `scipy`
* **Utilities**: `python-dotenv`
* **Monitoring**: `langsmith` (Optional)
**Deployment:**
* Hugging Face Spaces compatible.
* Environment variable configuration for API keys.
* Production-ready error handling and logging.
***
### Results & Impact
Mimir represents a synthesis of prompt engineering best practices with educational technology requirements. The integration of an intelligent, conditional graphing tool demonstrates the ability to create AI systems that augment and enhance human learning processes, embodying the role of an educational partner who empowers students to succeed through genuine understanding.
> **Portfolio Demonstration**: This project evidences advanced prompt engineering capabilities, full-stack AI application development, and domain-specific AI system design suitable for enterprise educational technology environments. |