File size: 3,748 Bytes
60d1d13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Configuration utilities for ViettelPay Agent
Supports both Streamlit secrets.toml and environment variables
"""

import os
from typing import Optional, Any


def get_secret(
    key: str, section: str = "api_keys", default: Optional[str] = None
) -> Optional[str]:
    """
    Get secret from Streamlit secrets.toml or environment variables

    Args:
        key: The secret key name
        section: Section in secrets.toml (default: "api_keys")
        default: Default value if secret not found

    Returns:
        Secret value or default
    """
    try:
        # Try to get from Streamlit secrets first
        import streamlit as st

        if hasattr(st, "secrets") and section in st.secrets:
            return st.secrets[section].get(key, default)
    except (ImportError, AttributeError, KeyError):
        pass

    # Fallback to environment variables
    return os.getenv(key, default)


def get_config(
    key: str, section: str = "models", default: Optional[str] = None
) -> Optional[str]:
    """
    Get configuration from Streamlit secrets.toml or environment variables

    Args:
        key: The config key name
        section: Section in secrets.toml (default: "models")
        default: Default value if config not found

    Returns:
        Config value or default
    """
    try:
        # Try to get from Streamlit secrets first
        import streamlit as st

        if hasattr(st, "secrets") and section in st.secrets:
            return st.secrets[section].get(key, default)
    except (ImportError, AttributeError, KeyError):
        pass

    # Fallback to environment variables
    return os.getenv(key, default)


def get_path(
    key: str, section: str = "paths", default: Optional[str] = None
) -> Optional[str]:
    """
    Get path configuration from Streamlit secrets.toml or environment variables

    Args:
        key: The path key name
        section: Section in secrets.toml (default: "paths")
        default: Default value if path not found

    Returns:
        Path value or default
    """
    try:
        # Try to get from Streamlit secrets first
        import streamlit as st

        if hasattr(st, "secrets") and section in st.secrets:
            return st.secrets[section].get(key, default)
    except (ImportError, AttributeError, KeyError):
        pass

    # Fallback to environment variables
    return os.getenv(key, default)


def is_streamlit_environment() -> bool:
    """
    Check if running in Streamlit environment

    Returns:
        True if running in Streamlit, False otherwise
    """
    try:
        import streamlit as st

        return hasattr(st, "secrets")
    except ImportError:
        return False


# Common API keys
def get_gemini_api_key() -> Optional[str]:
    """Get Gemini API key from secrets or environment"""
    return get_secret("GEMINI_API_KEY")


def get_openai_api_key() -> Optional[str]:
    """Get OpenAI API key from secrets or environment"""
    return get_secret("OPENAI_API_KEY")


def get_cohere_api_key() -> Optional[str]:
    """Get Cohere API key from secrets or environment"""
    return get_secret("COHERE_API_KEY")


# Common configurations
def get_embedding_model() -> str:
    """Get embedding model name"""
    return get_config(
        "EMBEDDING_MODEL", default="dangvantuan/vietnamese-document-embedding"
    )


def get_llm_provider() -> str:
    """Get LLM provider"""
    return get_config("LLM_PROVIDER", default="gemini")


def get_knowledge_base_path() -> str:
    """Get knowledge base path"""
    return get_path("KNOWLEDGE_BASE_PATH", default="./knowledge_base")


def get_documents_folder() -> str:
    """Get documents folder path"""
    return get_path("DOCUMENTS_FOLDER", default="./viettelpay_docs")