File size: 1,821 Bytes
ebe9d57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import tempfile
from pathlib import Path

class Config:
    # Use temporary directories for HF Spaces
    BASE_DIR = Path(tempfile.gettempdir()) / "sirraya_xbrain"
    
    # Core directories
    KNOWLEDGE_DIR = BASE_DIR / "knowledge"
    VECTOR_STORE_PATH = BASE_DIR / "vector_store"
    BM25_STORE_PATH = BASE_DIR / "bm25_store.pkl"
    
    # Processing parameters
    CHUNK_SIZE = 1000
    CHUNK_OVERLAP = 200
    MAX_CONTEXT_CHUNKS = 5
    
    # Model settings
    EMBEDDING_MODEL = "sentence-transformers/all-mpnet-base-v2"
    FALLBACK_EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
    
    # HF Models (in order of preference)
    PRIMARY_LLM = "mistralai/Mistral-7B-Instruct-v0.1"
    FALLBACK_LLMS = [
        "microsoft/DialoGPT-medium",
        "google/flan-t5-base",
        "huggingface/CodeBERTa-small-v1"
    ]
    
    # API settings
    LLM_TEMPERATURE = 0.1
    MAX_NEW_TOKENS = 512
    REQUEST_TIMEOUT = 60
    
    @classmethod
    def setup_dirs(cls):
        """Setup required directories"""
        cls.BASE_DIR.mkdir(parents=True, exist_ok=True)
        cls.KNOWLEDGE_DIR.mkdir(parents=True, exist_ok=True)
        cls.VECTOR_STORE_PATH.mkdir(parents=True, exist_ok=True)
    
    @classmethod
    def get_hf_token(cls):
        """Get HuggingFace API token from environment"""
        return os.getenv("HUGGINGFACEHUB_API_TOKEN") or os.getenv("HF_TOKEN")
    
    @classmethod
    def cleanup_temp_dirs(cls):
        """Cleanup temporary directories"""
        try:
            import shutil
            if cls.BASE_DIR.exists():
                shutil.rmtree(cls.BASE_DIR)
        except Exception as e:
            print(f"Cleanup error: {e}")
    
    # Environment-specific settings
    IS_HF_SPACES = os.getenv("SPACE_ID") is not None
    IS_LOCAL = not IS_HF_SPACES