> ## Documentation Index
> Fetch the complete documentation index at: https://docs.learnway.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# AI Microservice

> Standalone AI inference service (learnway-ai-service/), Google Gemini models, prompt context injection, and Redis response caching.

# AI Microservice (`learnway-ai-service/`)

The **LearnWay AI Microservice** (`learnway-ai-service/`) is an isolated, scalable service handling generative AI workloads, student tutoring, onboarding skill assessments, and capstone project code evaluations.

***

## Service Architecture & Pipelines

```mermaid theme={null}
graph TD
    Client["Mobile App / Backend API"] -->|HTTP / gRPC Request| Gateway["AI Microservice Router"]
    
    Gateway --> Guard["Profanity & Safety Guard"]
    Guard --> CacheCheck{"Redis Cache Hit?"}
    
    CacheCheck -->|Yes - Return Cached Answer| Response["Fast Response (< 50ms)"]
    
    CacheCheck -->|No - Cache Miss| Pipeline["Context Injection Engine"]
    Pipeline --> DBQuery["Fetch Lesson Slides & Code Snippets"]
    DBQuery --> PromptBuilder["Prompt Template Assembler"]
    
    PromptBuilder --> GeminiAPI["Google Gemini 1.5 Pro / Flash"]
    GeminiAPI --> PostProcess["Output Parsing & Validation"]
    
    PostProcess --> RedisSet["Store in Redis (TTL: 24h)"]
    RedisSet --> ReturnNode["Return AI Response"]
```

***

## Core Capabilities

### 1. Interactive AI Mentor & Lesson Tutor

* **Prompt Types**: `EXPLAIN`, `SUMMARIZE`, `QUIZ_HINT`, `SIMPLIFY`, and `CUSTOM`.
* **Context Window Injection**: The service injects relevant slide content, key definitions, and preceding lesson context into Gemini's system prompt.
* **Deterministic Caching**: Requests for predefined prompt types (`EXPLAIN`, `SUMMARIZE`) on identical lesson IDs produce identical cache keys stored in Redis for 24 hours, dramatically cutting LLM API costs.

### 2. Onboarding Skill Assessment Engine

* Evaluates new learners across foundational domains (Web3, Programming, Financial Literacy).
* Dynamically adjusts subsequent question difficulty based on prior answers.
* Generates a tailored starting level recommendation (`BEGINNER`, `INTERMEDIATE`, `ADVANCED`).

### 3. Capstone Code & Project Evaluator

* Analyzes GitHub repositories, Solidity smart contracts, and project submissions against predefined rubrics.
* Evaluates code style, security best practices (Reentrancy, SafeMath/v0.8 overflow, access control), and test coverage.
* Returns structured JSON scores, detailed strengths, weaknesses, and actionable code refactoring tips.

***

## Resilience & Rate Limiting

* **Circuit Breaker**: Detects Gemini API latency spikes or upstream outages, gracefully failing over or returning cached responses.
* **Rate Limits**: Enforces daily per-user quotas (e.g. 20 queries/day for free users, 100 queries/day for Premium users) managed via Redis sliding window counters.
* **Content Safety**: Filters prompt injection attacks, malicious code, and inappropriate language before invoking the LLM.
