Spaces:
Sleeping
Sleeping
Update config.py
Browse files46 unmodified lines474849505152535455565758596061621 unmodified line64656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915084 unmodified lines2352362372382392402412422432442452462472482492503 unmodified lines25425525625725825946 unmodified lines# ============================================================================# DATABASE PATHS# ============================================================================# These directories store different types of data:# - learning_data: Text files with information about the user (personal data, preferences, etc.)# - chats_data: JSON files containing past conversation history# - vector_store: FAISS index files for fast similarity searchLEARNING_DATA_DIR = BASE_DIR / "database" / "learning_data"CHATS_DATA_DIR = BASE_DIR / "database" / "chats_data"VECTOR_STORE_DIR = BASE_DIR / "database" / "vector_store"# Create directories if they don't exist so the app can run without manual setup.# parents=True creates parent folders; exist_ok=True avoids error if already present.LEARNING_DATA_DIR.mkdir(parents=True, exist_ok=True)CHATS_DATA_DIR.mkdir(parents=True, exist_ok=True)VECTOR_STORE_DIR.mkdir(parents=True, exist_ok=True)1 unmodified line# ============================================================================# GROQ API CONFIGURATION# ============================================================================# Groq is the LLM provider we use for generating responses.# You can set one key (GROQ_API_KEY) or multiple keys for fallback:# GROQ_API_KEY, GROQ_API_KEY_2, GROQ_API_KEY_3, ... (no upper limit).# PRIMARY-FIRST: Every request tries the first key first. If it fails (rate limit,# timeout, etc.), the server tries the second, then third, until one succeeds.# If all keys fail, the user receives a clear error message.# Model determines which AI model to use (llama-3.3-70b-versatile is latest).def _load_groq_api_keys() -> list:"""Load all GROQ API keys from the environment.Reads GROQ_API_KEY first, then GROQ_API_KEY_2, GROQ_API_KEY_3, ... untila number has no value. There is no upper limit on how many keys you can set.Returns a list of non-empty key strings (may be empty if GROQ_API_KEY is not set)."""keys = []# First key: GROQ_API_KEY (required in practice; validated when building services).first = os.getenv("GROQ_API_KEY", "").strip()if first:keys.append(first)# Additional keys: GROQ_API_KEY_2, GROQ_API_KEY_3, GROQ_API_KEY_4, ...i = 2while True:k = os.getenv(f"GROQ_API_KEY_{i}", "").strip()if not k:# No key for this number; stop (no more keys).breakkeys.append(k)i += 1return keysGROQ_API_KEYS = _load_groq_api_keys()# Backward compatibility: single key name still used in docs; code uses GROQ_API_KEYS.GROQ_API_KEY = GROQ_API_KEYS[0] if GROQ_API_KEYS else ""GROQ_MODEL = os.getenv("GROQ_MODEL", "llama-3.3-70b-versatile")# ============================================================================# TAVILY API CONFIGURATION# ============================================================================# Tavily is a fast, AI-optimized search API designed for LLM applications# Get API key from: https://tavily.com (free tier available)# Tavily returns English-only results by default and is faster than DuckDuckGoTAVILY_API_KEY = os.getenv("TAVILY_API_KEY", "")# ============================================================================# BRAIN MODEL (Query Classification — Jarvis Mode)# ============================================================================# The brain classifies each query as "general" or "realtime" using Groq.# Uses the same GROQ_API_KEYS with rotation (brain and chat never use the same key).GROQ_BRAIN_MODEL = os.getenv("GROQ_BRAIN_MODEL", "llama-3.1-8b-instant")# ============================================================================# TTS (TEXT-TO-SPEECH) CONFIGURATION# ============================================================================# edge-tts uses Microsoft Edge's free cloud TTS. No API key needed.# Voice list: run `edge-tts --list-voices` to see all available voices.# Default: en-GB-RyanNeural (male British voice, fitting for JARVIS).# Override via TTS_VOICE in .env (e.g. TTS_VOICE=en-US-ChristopherNeural).TTS_VOICE = os.getenv("TTS_VOICE", "en-GB-RyanNeural")TTS_RATE = os.getenv("TTS_RATE", "+22%")# ============================================================================# EMBEDDING CONFIGURATION# ============================================================================# Embeddings convert text into numerical vectors that capture meaning# We use HuggingFace's sentence-transformers model (runs locally, no API needed)# CHUNK_SIZE: How many characters to split documents into# CHUNK_OVERLAP: How many characters overlap between chunks (helps maintain context)EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2"CHUNK_SIZE = 1000 # Characters per chunkCHUNK_OVERLAP = 200 # Overlap between chunks# Maximum conversation turns (user+assistant pairs) sent to the LLM per request.# Older turns are kept on disk but not sent to avoid context/token limits.MAX_CHAT_HISTORY_TURNS = 20# Maximum length (characters) for a single user message. Prevents token limit errors# and abuse. ~32K chars ≈ ~8K tokens; keeps total prompt well under model limits.MAX_MESSAGE_LENGTH = 32_000# ============================================================================84 unmodified lines"""Load and concatenate the contents of all .txt files in learning_data.Reads every .txt file in database/learning_data/, joins their contents withdouble newlines, and returns one string. Used by code that needs the rawlearning text (e.g. optional utilities). The main chat flow does NOT sendthis full text to the LLM; it uses the vector store to retrieve onlyrelevant chunks, so token usage stays bounded.Returns:str: Combined content from all .txt files, or "" if none exist or all fail to read."""context_parts = []# Sorted by path so the order is always the same across runs.text_files = sorted(LEARNING_DATA_DIR.glob("*.txt"))for file_path in text_files:try:3 unmodified linescontext_parts.append(content)except Exception as e:logger.warning("Could not load learning data file %s: %s", file_path, e)# Join all file contents with double newline; empty string if no files or all failed.return "\n\n".join(context_parts) if context_parts else ""46 unmodified lines474849505152535455561 unmodified line58596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310484 unmodified lines1891901911921931941951961973 unmodified lines20120220320446 unmodified lines# ============================================================================# DATABASE PATHS# ============================================================================LEARNING_DATA_DIR = BASE_DIR / "database" / "learning_data"CHATS_DATA_DIR = BASE_DIR / "database" / "chats_data"VECTOR_STORE_DIR = BASE_DIR / "database" / "vector_store"LEARNING_DATA_DIR.mkdir(parents=True, exist_ok=True)CHATS_DATA_DIR.mkdir(parents=True, exist_ok=True)VECTOR_STORE_DIR.mkdir(parents=True, exist_ok=True)1 unmodified line# ============================================================================# GROQ API CONFIGURATION# ============================================================================def _load_groq_api_keys() -> list:keys = []first = os.getenv("GROQ_API_KEY", "").strip()if first:keys.append(first)i = 2while True:k = os.getenv(f"GROQ_API_KEY_{i}", "").strip()if not k:breakkeys.append(k)i += 1return keysGROQ_API_KEYS = _load_groq_api_keys()GROQ_API_KEY = GROQ_API_KEYS[0] if GROQ_API_KEYS else ""GROQ_MODEL = os.getenv("GROQ_MODEL", "llama-3.3-70b-versatile")# ============================================================================# TAVILY API CONFIGURATION# ============================================================================TAVILY_API_KEY = os.getenv("TAVILY_API_KEY", "")# ============================================================================# BRAIN MODEL (Query Classification — Jarvis Mode)# ============================================================================GROQ_BRAIN_MODEL = os.getenv("GROQ_BRAIN_MODEL", "llama-3.1-8b-instant")# ============================================================================# TTS (TEXT-TO-SPEECH) CONFIGURATION# ============================================================================TTS_VOICE = os.getenv("TTS_VOICE", "en-GB-RyanNeural")TTS_RATE = os.getenv("TTS_RATE", "+22%")# ============================================================================# EMBEDDING CONFIGURATION# ============================================================================EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2"CHUNK_SIZE = 1000CHUNK_OVERLAP = 200MAX_CHAT_HISTORY_TURNS = 20MAX_MESSAGE_LENGTH = 32_000# ============================================================================84 unmodified lines"""Load and concatenate the contents of all .txt files in learning_data.Reads every .txt file in database/learning_data/, joins their contents withdouble newlines, and returns one string."""context_parts = []text_files = sorted(LEARNING_DATA_DIR.glob("*.txt"))for file_path in text_files:try:3 unmodified linescontext_parts.append(content)except Exception as e:logger.warning("Could not load learning data file %s: %s", file_path, e)return "\n\n".join(context_parts) if context_parts else ""
config.py+3-58