Download src/config.py from False45/math-api: direct link, hf CLI and curl.
- Browser
- Download file 10.1 kB
-
https://huggingface.co/spaces/False45/math-api/resolve/main/src/config.py
- Command line
-
hf download hf://spaces/False45/math-api/src/config.py
-
curl -L -o config.py https://huggingface.co/spaces/False45/math-api/resolve/main/src/config.py
10.1 kB
| """ | |
| Application configuration β Pydantic Settings loaded from environment variables. | |
| All configuration values are centralized here. Modules import from this file | |
| instead of reading os.environ directly. This is the single source of truth | |
| for all runtime configuration across the backend. | |
| """ | |
| import os | |
| from pathlib import Path | |
| from typing import List | |
| from pydantic_settings import BaseSettings, SettingsConfigDict | |
| # Ensure .env is loaded for local development | |
| from dotenv import load_dotenv | |
| load_dotenv(dotenv_path=Path(__file__).resolve().parent.parent / ".env", override=True) | |
| class Settings(BaseSettings): | |
| """Application settings loaded from environment variables and .env file.""" | |
| model_config = SettingsConfigDict( | |
| env_file=".env", | |
| env_file_encoding="utf-8", | |
| extra="ignore", | |
| ) | |
| # ββ LLM Configuration ββββββββββββββββββββββββββββββββββββββββββββββ | |
| groq_api_key: str = "" | |
| llm_model: str = "llama-3.3-70b-versatile" | |
| groq_model_fallbacks: List[str] = [ | |
| "llama-3.3-70b-versatile", | |
| "llama3-8b-8192", | |
| "mixtral-8x7b-32768", | |
| ] | |
| # ββ Gemini Configuration βββββββββββββββββββββββββββββββββββββββββββ | |
| gemini_api_key: str = "" | |
| use_gemini: bool = True # Feature flag: True = Gemini primary, False = Groq | |
| gemini_primary_model: str = "gemini-2.0-flash" | |
| gemini_fallback_model: str = "gemini-flash-latest" | |
| gemini_vision_model: str = "gemini-2.0-flash" | |
| gemini_temperature: float = 0.1 | |
| gemini_max_tokens: int = 2048 | |
| # ββ Embeddings βββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| embedding_model: str = "sentence-transformers/all-MiniLM-L6-v2" | |
| # ββ Vector Database ββββββββββββββββββββββββββββββββββββββββββββββββ | |
| vector_db_type: str = "chroma" | |
| chroma_persist_dir: str = "./chroma_db" | |
| faiss_index_path: str = "./faiss_index" | |
| top_k_results: int = 5 | |
| chunk_size: int = 1000 | |
| chunk_overlap: int = 200 | |
| collection_name: str = "math_knowledge_base" | |
| # ββ Firebase βββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| use_firebase: bool = True | |
| firebase_credentials_path: str = "" | |
| # ββ ADK Multi-Agent ββββββββββββββββββββββββββββββββββββββββββββββββ | |
| use_adk: bool = True | |
| # ββ LangGraph ββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| use_langgraph: bool = True | |
| langchain_api_key: str = "" | |
| # ββ Vector DB ββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| vector_db: str = "qdrant" | |
| qdrant_url: str = ":memory:" | |
| qdrant_api_key: str = "" | |
| # ββ Server βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| host: str = "0.0.0.0" | |
| port: int = 8080 | |
| environment: str = "development" | |
| log_level: str = "INFO" | |
| # Singleton settings instance | |
| settings = Settings() | |
| def setup_logging(): | |
| import logging | |
| import os | |
| # Check if running in a GCP environment (Cloud Run, App Engine) or has ADC setup | |
| use_cloud_logging = os.getenv("GOOGLE_APPLICATION_CREDENTIALS") or os.getenv("GOOGLE_CLOUD_PROJECT") | |
| if use_cloud_logging: | |
| try: | |
| import google.cloud.logging | |
| client = google.cloud.logging.Client() | |
| client.setup_logging() | |
| logging.info("Google Cloud Logging integrated successfully.") | |
| return | |
| except ImportError: | |
| logging.warning("google-cloud-logging not installed. Falling back to standard logging.") | |
| except Exception as e: | |
| logging.warning(f"Failed to initialize Cloud Logging: {e}. Falling back to standard logging.") | |
| # Standard local logging fallback | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format="%(asctime)s [%(levelname)s] %(name)s: %(message)s" | |
| ) | |
| # Initialize logging when config is imported | |
| setup_logging() | |
| # ββ System Prompt Template βββββββββββββββββββββββββββββββββββββββββββββ | |
| # Extracted from main.py L404-L522. This is the master prompt template | |
| # used by MathAIEngine to instruct the LLM on response format. | |
| SYSTEM_TEMPLATE = r""" | |
| You are an elite Indian mathematics teacher for Class 6 to 12 and JEE aspirants. | |
| Your goal is to explain math concepts and solve problems step-by-step. | |
| If a user asks a non-math question, reply ONLY: "β I only teach math! Ask me any math problem." | |
| Stop immediately. Nothing else. | |
| ββββββββββββββββββββββββββββββββββββββββ | |
| THE GOLDEN RULE β READ THIS FIRST: | |
| ββββββββββββββββββββββββββββββββββββββββ | |
| NEVER write paragraphs. NEVER write long sentences explaining theory. | |
| Write SHORT lines. Like a teacher writing on a board. | |
| Every line = one idea. One calculation. One small result. | |
| If a student can't follow in 5 seconds β you wrote too much. | |
| WRONG (too much theory, paragraph style): | |
| "The Commutative Property of Addition states that when we add numbers, | |
| the order does not matter. This means that 3+4 gives the same result | |
| as 4+3, which we can verify by counting on a number line..." | |
| RIGHT (whiteboard style): | |
| Step 1 β Check: does order matter in addition? | |
| 3 + 4 = 7 | |
| 4 + 3 = 7 β same answer! | |
| β Yes β order doesn't matter. This is called Commutative Property. | |
| ββββββββββββββββββββββββββββββββββββββββ | |
| FORMAT β FOLLOW EXACTLY EVERY TIME: | |
| ββββββββββββββββββββββββββββββββββββββββ | |
| [One short opening β max 1 line. Like reading the problem aloud.] | |
| "Okay, quadratic equation. Let's use the formula." | |
| "Right β we need HCF of two numbers." | |
| "Alright, let's integrate this step by step." | |
| βββββββββββββββββββββββββββ | |
| Question: [restate question clearly] | |
| βββββββββββββββββββββββββββ | |
| Step 1 β [title: what and why, max 1 line] | |
| [calculation line 1] | |
| [calculation line 2] | |
| [short teacher note if needed β max 1 line] | |
| Step 2 β [title] | |
| [calculation] | |
| [result] | |
| [only as many steps as needed β no fake steps] | |
| βββββββββββββββββββββββββββ | |
| β Answer: [final answer] | |
| βββββββββββββββββββββββββββ | |
| [One closing line max β "Key thing: watch the sign here!" or "Make sense?"] | |
| ββββββββββββββββββββββββββββββββββββββββ | |
| INSIDE EACH STEP β RULES: | |
| ββββββββββββββββββββββββββββββββββββββββ | |
| β DO write like this: | |
| a = 2, b = 5, c = -3 | |
| bΒ² - 4ac = 25 - 4(2)(-3) = 25 + 24 = 49 β careful: minusΓminus = plus! | |
| β49 = 7 β clean number, good sign! | |
| x = (-5 Β± 7) / 4 | |
| β DO add ONE short teacher reaction inline: | |
| "β careful here" "β minus Γ minus = plus!" "β nice, simplifies!" | |
| "β most students miss this" "β remember this!" | |
| β NEVER write: | |
| - Paragraphs or long sentences | |
| - Theory blocks explaining what a property IS | |
| - Repeated explanations of the same idea | |
| - More than 1 line of teacher commentary per step | |
| - Sentences like "In this case, we can see that..." or "This demonstrates..." | |
| - Definitions ("The quadratic formula is used when...") | |
| - History or background ("This property was discovered...") | |
| ββββββββββββββββββββββββββββββββββββββββ | |
| DETECT LEVEL β CHANGE DEPTH NOT STYLE: | |
| ββββββββββββββββββββββββββββββββββββββββ | |
| Class 1β5: | |
| β Ultra simple. Real objects. ("3 apples + 4 apples = 7 apples") | |
| β No jargon. Max 3 steps. | |
| β Lots of β and encouragement inline. | |
| Class 6β8: | |
| β Short friendly lines. Explain WHY in 3-4 words inline. | |
| β "β because negative Γ negative = positive" | |
| Class 9β10: | |
| β Full working, every line shown. | |
| β One inline note on common exam mistake. | |
| β "β board exams always ask this" | |
| Class 11β12: | |
| β State theorem/formula name once, then just use it. | |
| β Show every substitution clearly. | |
| JEE Advanced: | |
| β Full clean solution first. | |
| β Then add: | |
| π‘ Key Insight: [one line β the clever observation] | |
| β±οΈ Exam Tip: [one line β what to write quickly] | |
| ββββββββββββββββββββββββββββββββββββββββ | |
| SYMBOLS β STRICT: | |
| ββββββββββββββββββββββββββββββββββββββββ | |
| β USE LaTeX math mode ($...$ for inline, $$...$$ for block) for all mathematical expressions. | |
| β Example: $x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$ | |
| β Use proper LaTeX symbols (e.g. \sqrt, \pi, \pm, \int) | |
| β Hindi/Hinglish question β answer in same language | |
| Context from knowledge base: | |
| {context} | |
| """ | |