Skip to content

Settings

AzureAISearchEnvironmentVariables

Bases: BaseEnvironmentVariables

Represents environment variables for configuring Azure AI Search and Azure Storage.

Source code in src/settings.py
 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
class AzureAISearchEnvironmentVariables(BaseEnvironmentVariables):
    """Represents environment variables for configuring Azure AI Search and Azure Storage."""

    ################ Azure Search settings ################
    ENABLE_AZURE_SEARCH: bool = False
    AZURE_SEARCH_SERVICE_ENDPOINT: Optional[str] = None
    AZURE_SEARCH_INDEX_NAME: Optional[str] = None
    AZURE_SEARCH_INDEXER_NAME: Optional[str] = None
    AZURE_SEARCH_API_KEY: Optional[str] = None
    AZURE_SEARCH_TOP_K: Optional[str] = "2"
    SEMENTIC_CONFIGURATION_NAME: Optional[str] = None
    # Azure Storage settings
    AZURE_STORAGE_ACCOUNT_NAME: Optional[str] = None
    AZURE_STORAGE_ACCOUNT_KEY: Optional[str] = None
    AZURE_CONTAINER_NAME: Optional[str] = None

    def get_azure_search_env_vars(self):
        items_dict = {
            "ENABLE_AZURE_SEARCH": self.ENABLE_AZURE_SEARCH,
            "SEMENTIC_CONFIGURATION_NAME": self.SEMENTIC_CONFIGURATION_NAME,
            "AZURE_STORAGE_ACCOUNT_NAME": self.AZURE_STORAGE_ACCOUNT_NAME,
            "AZURE_STORAGE_ACCOUNT_KEY": self.AZURE_STORAGE_ACCOUNT_KEY,
            "AZURE_CONTAINER_NAME": self.AZURE_CONTAINER_NAME,
        }

        items_dict.update(
            {key: value for key, value in vars(self).items() if key.startswith("AZURE_SEARCH")}
        )
        return items_dict

    @model_validator(mode="after")
    def check_ai_search_keys(self: Self) -> Self:
        """Validate API keys based on the selected provider after model initialization."""
        if self.ENABLE_AZURE_SEARCH:
            azure_search_vars = self.get_azure_search_env_vars()
            if any(value is None for value in azure_search_vars.values()):
                loguru_logger.error(
                    "\nAZURE_SEARCH environment variables must be provided when ENABLE_AZURE_SEARCH is True."
                    f"\n{pretty_repr(azure_search_vars)}"
                )
                raise ValueError(
                    "\nAZURE_SEARCH environment variables must be provided when ENABLE_AZURE_SEARCH is True."
                    f"\n{pretty_repr(azure_search_vars)}"
                )
        return self

check_ai_search_keys()

Validate API keys based on the selected provider after model initialization.

Source code in src/settings.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
@model_validator(mode="after")
def check_ai_search_keys(self: Self) -> Self:
    """Validate API keys based on the selected provider after model initialization."""
    if self.ENABLE_AZURE_SEARCH:
        azure_search_vars = self.get_azure_search_env_vars()
        if any(value is None for value in azure_search_vars.values()):
            loguru_logger.error(
                "\nAZURE_SEARCH environment variables must be provided when ENABLE_AZURE_SEARCH is True."
                f"\n{pretty_repr(azure_search_vars)}"
            )
            raise ValueError(
                "\nAZURE_SEARCH environment variables must be provided when ENABLE_AZURE_SEARCH is True."
                f"\n{pretty_repr(azure_search_vars)}"
            )
    return self

ChatEnvironmentVariables

Bases: OpenAIChatEnvironmentVariables, AzureOpenAIChatEnvironmentVariables

Represents environment variables for configuring the chatbot and promptfoo providers.

Source code in src/settings.py
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
class ChatEnvironmentVariables(OpenAIChatEnvironmentVariables, AzureOpenAIChatEnvironmentVariables):
    """Represents environment variables for configuring the chatbot and promptfoo providers."""

    LLM_PROVIDER: ProviderEnum  # openai or azure_openai
    # if you want to emulate azure_openai or openai using ollama or ollamazure
    OLLAMA_MODEL_NAME: Optional[str] = None  # "phi3:3.8b-mini-4k-instruct-q4_K_M"
    OLLAMA_EMBEDDING_MODEL_NAME: Optional[str] = None  # "all-minilm:l6-v2"

    @model_validator(mode="after")
    def check_chat_api_keys(self: Self) -> Self:
        """Validate API keys based on the selected provider after model initialization."""
        if self.LLM_PROVIDER == ProviderEnum.openai:
            openai_vars = self.get_openai_env_vars()
            openai_vars["LLM_PROVIDER"] = self.LLM_PROVIDER
            if any(value is None for value in openai_vars.values()):
                loguru_logger.error(
                    "\nOPENAI environment variables must be provided when LLM_PROVIDER is 'openai'."
                    f"\n{pretty_repr(openai_vars)}"
                )
                raise ValueError(
                    "\nOPENAI environment variables must be provided when LLM_PROVIDER is 'openai'."
                    f"\n{pretty_repr(openai_vars)}"
                )

        elif self.LLM_PROVIDER == ProviderEnum.azure_openai:
            azure_openai_vars = self.get_azure_openai_env_vars()
            azure_openai_vars["LLM_PROVIDER"] = self.LLM_PROVIDER
            if any(value is None for value in azure_openai_vars.values()):
                loguru_logger.error(
                    "\nAZURE_OPENAI environment variables must be provided when LLM_PROVIDER is 'azure_openai'."
                    f"\n{pretty_repr(azure_openai_vars)}"
                )
                raise ValueError(
                    "\nAZURE_OPENAI environment variables must be provided when LLM_PROVIDER is 'azure_openai'."
                    f"\n{pretty_repr(azure_openai_vars)}"
                )

        else:
            raise ValueError(f"Unknown LLM_PROVIDER: {self.LLM_PROVIDER}")

        return self

check_chat_api_keys()

Validate API keys based on the selected provider after model initialization.

Source code in src/settings.py
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
@model_validator(mode="after")
def check_chat_api_keys(self: Self) -> Self:
    """Validate API keys based on the selected provider after model initialization."""
    if self.LLM_PROVIDER == ProviderEnum.openai:
        openai_vars = self.get_openai_env_vars()
        openai_vars["LLM_PROVIDER"] = self.LLM_PROVIDER
        if any(value is None for value in openai_vars.values()):
            loguru_logger.error(
                "\nOPENAI environment variables must be provided when LLM_PROVIDER is 'openai'."
                f"\n{pretty_repr(openai_vars)}"
            )
            raise ValueError(
                "\nOPENAI environment variables must be provided when LLM_PROVIDER is 'openai'."
                f"\n{pretty_repr(openai_vars)}"
            )

    elif self.LLM_PROVIDER == ProviderEnum.azure_openai:
        azure_openai_vars = self.get_azure_openai_env_vars()
        azure_openai_vars["LLM_PROVIDER"] = self.LLM_PROVIDER
        if any(value is None for value in azure_openai_vars.values()):
            loguru_logger.error(
                "\nAZURE_OPENAI environment variables must be provided when LLM_PROVIDER is 'azure_openai'."
                f"\n{pretty_repr(azure_openai_vars)}"
            )
            raise ValueError(
                "\nAZURE_OPENAI environment variables must be provided when LLM_PROVIDER is 'azure_openai'."
                f"\n{pretty_repr(azure_openai_vars)}"
            )

    else:
        raise ValueError(f"Unknown LLM_PROVIDER: {self.LLM_PROVIDER}")

    return self

EvaluationEnvironmentVariables

Bases: BaseEnvironmentVariables

Represents environment variables for configuring Promptfoo and ragas.

Ragas uses LLM as a judge for its metrics.

Source code in src/settings.py
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
class EvaluationEnvironmentVariables(BaseEnvironmentVariables):
    """Represents environment variables for configuring Promptfoo and ragas.

    Ragas uses LLM as a judge for its metrics.

    """

    ENABLE_EVALUATION: bool = False
    # LLM as a Judge for RAGAS metrics
    LLMAAJ_PROVIDER: Optional[ProviderEnum] = None  # openai or azure_openai

    # if judge provider is openai
    LLMAAJ_OPENAI_DEPLOYMENT_NAME: Optional[str] = None
    LLMAAJ_OPENAI_API_KEY: Optional[SecretStr] = None
    LLMAAJ_OPENAI_BASE_URL: Optional[str] = None
    LLMAAJ_OPENAI_EMBEDDING_DEPLOYMENT_NAME: Optional[str] = None

    # if judge provider is azure_openai
    LLMAAJ_AZURE_OPENAI_DEPLOYMENT_NAME: str = "phi3:3.8b-mini-4k-instruct-q4_K_M"
    LLMAAJ_AZURE_OPENAI_API_KEY: SecretStr = "1234"
    LLMAAJ_AZURE_OPENAI_BASE_URL: str = "http://localhost:4041"
    LLMAAJ_AZURE_OPENAI_API_VERSION: str = "2024-10-01-preview"
    LLMAAJ_AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME: Optional[str] = "all-minilm:l6-v2"

    def get_eval_env_vars(self):
        items_dict = {
            "ENABLE_EVALUATION": self.ENABLE_EVALUATION,
            "LLMAAJ_PROVIDER": self.LLMAAJ_PROVIDER,
        }
        if self.LLMAAJ_PROVIDER == ProviderEnum.openai:
            items_dict.update(
                {key: value for key, value in vars(self).items() if key.startswith("LLMAAJ_OPENAI")}
            )
        elif self.LLMAAJ_PROVIDER == ProviderEnum.azure_openai:
            items_dict.update(
                {
                    key: value
                    for key, value in vars(self).items()
                    if key.startswith("LLMAAJ_AZURE_OPENAI")
                }
            )
        else:
            raise ValueError(f"Unknown LLMAAJ_PROVIDER: {self.LLMAAJ_PROVIDER}")
        return items_dict

    @model_validator(mode="after")
    def check_eval_api_keys(self: Self) -> Self:
        """Validate API keys based on the selected provider after model initialization."""
        if self.ENABLE_EVALUATION:
            if not self.LLMAAJ_PROVIDER in [ProviderEnum.openai, ProviderEnum.azure_openai]:
                loguru_logger.error(
                    f"Unsupported env variable LLMAAJ_PROVIDER with value: {self.LLMAAJ_PROVIDER}"
                )
                raise ValueError(
                    f"Unsupported variable LLMAAJ_PROVIDER with value: {self.LLMAAJ_PROVIDER}"
                )
            eval_vars = self.get_eval_env_vars()
            if any(value is None for value in eval_vars.values()):
                loguru_logger.error(
                    "\nEVALUATION environment variables must be provided when ENABLE_EVALUATION is True."
                    f"\n{pretty_repr(eval_vars)}"
                )
                raise ValueError(
                    "\nEVALUATION environment variables must be provided when ENABLE_EVALUATION is True."
                    f"\n{pretty_repr(eval_vars)}"
                )

        return self

check_eval_api_keys()

Validate API keys based on the selected provider after model initialization.

Source code in src/settings.py
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
@model_validator(mode="after")
def check_eval_api_keys(self: Self) -> Self:
    """Validate API keys based on the selected provider after model initialization."""
    if self.ENABLE_EVALUATION:
        if not self.LLMAAJ_PROVIDER in [ProviderEnum.openai, ProviderEnum.azure_openai]:
            loguru_logger.error(
                f"Unsupported env variable LLMAAJ_PROVIDER with value: {self.LLMAAJ_PROVIDER}"
            )
            raise ValueError(
                f"Unsupported variable LLMAAJ_PROVIDER with value: {self.LLMAAJ_PROVIDER}"
            )
        eval_vars = self.get_eval_env_vars()
        if any(value is None for value in eval_vars.values()):
            loguru_logger.error(
                "\nEVALUATION environment variables must be provided when ENABLE_EVALUATION is True."
                f"\n{pretty_repr(eval_vars)}"
            )
            raise ValueError(
                "\nEVALUATION environment variables must be provided when ENABLE_EVALUATION is True."
                f"\n{pretty_repr(eval_vars)}"
            )

    return self

Settings

Bases: ChatEnvironmentVariables, AzureAISearchEnvironmentVariables, EvaluationEnvironmentVariables

Settings class for the application.

This class is automatically initialized with environment variables from the .env file. It inherits from the following classes and contains additional settings for streamlit and fastapi - ChatEnvironmentVariables - AzureAISearchEnvironmentVariables - EvaluationEnvironmentVariables

Source code in src/settings.py
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
class Settings(
    ChatEnvironmentVariables, AzureAISearchEnvironmentVariables, EvaluationEnvironmentVariables
):
    """Settings class for the application.

    This class is automatically initialized with environment variables from the .env file.
    It inherits from the following classes and contains additional settings for streamlit and fastapi
    - ChatEnvironmentVariables
    - AzureAISearchEnvironmentVariables
    - EvaluationEnvironmentVariables

    """

    FASTAPI_HOST: str = "localhost"
    FASTAPI_PORT: int = 8080
    STREAMLIT_PORT: int = 8501
    DEV_MODE: bool = True

    def get_active_env_vars(self):
        env_vars = {
            "DEV_MODE": self.DEV_MODE,
            "FASTAPI_PORT": self.FASTAPI_PORT,
            "STREAMLIT_PORT": self.STREAMLIT_PORT,
        }

        if self.LLM_PROVIDER == ProviderEnum.openai:
            env_vars.update(self.get_openai_env_vars())
        elif self.LLM_PROVIDER == ProviderEnum.azure_openai:
            env_vars.update(self.get_azure_openai_env_vars())
        else:
            raise ValueError(f"Unknown LLM_PROVIDER: {self.LLM_PROVIDER}")

        if self.ENABLE_AZURE_SEARCH:
            env_vars.update(self.get_azure_search_env_vars())

        if self.ENABLE_EVALUATION:
            env_vars.update(self.get_eval_env_vars())

        return env_vars