-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.py
More file actions
163 lines (130 loc) · 5.65 KB
/
Copy pathconfig.py
File metadata and controls
163 lines (130 loc) · 5.65 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import os
from typing import Any, Dict
from dotenv import load_dotenv
from loguru import logger
from utils.security import mask_sensitive
# Load environment variables
load_dotenv()
class Config:
"""Application configuration settings"""
# Service provider selection
ASR_PROVIDER = os.getenv("ASR_PROVIDER", "azure")
LLM_PROVIDER = os.getenv("LLM_PROVIDER", "openai")
TTS_PROVIDER = os.getenv("TTS_PROVIDER", "azure")
# ASR settings
ASR_LANGUAGE = os.getenv("ASR_LANGUAGE", "en-US")
VOICE_ENERGY_THRESHOLD = float(os.getenv("VOICE_ENERGY_THRESHOLD", "0.05"))
# Azure Speech service
AZURE_SPEECH_KEY = os.getenv("AZURE_SPEECH_KEY")
AZURE_SPEECH_REGION = os.getenv("AZURE_SPEECH_REGION")
AZURE_TTS_VOICE = os.getenv("AZURE_TTS_VOICE", "en-US-AriaNeural")
# MiniMax TTS
MINIMAX_API_KEY = os.getenv("MINIMAX_API_KEY")
MINIMAX_VOICE_ID = os.getenv("MINIMAX_VOICE_ID", "male-qn-qingse")
# OpenAI API
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
OPENAI_BASE_URL = os.getenv("OPENAI_BASE_URL")
OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-3.5-turbo")
OPENAI_SYSTEM_PROMPT = os.getenv(
"OPENAI_SYSTEM_PROMPT",
"You are an intelligent voice assistant. Please provide concise, conversational answers.",
)
# WebSocket settings
WEBSOCKET_PING_INTERVAL = int(os.getenv("WEBSOCKET_PING_INTERVAL", "30"))
# Session settings
SESSION_TIMEOUT = int(os.getenv("SESSION_TIMEOUT", "600"))
# Debug settings
DEBUG = os.getenv("DEBUG", "false").lower() == "true"
@classmethod
def _validate_provider_config(cls) -> Dict[str, bool]:
"""Validate provider-specific configurations"""
validation_results: Dict[str, bool] = {}
# Azure Speech validation (ASR/TTS)
azure_credentials_valid = bool(cls.AZURE_SPEECH_KEY and cls.AZURE_SPEECH_REGION)
validation_results["azure"] = azure_credentials_valid
# OpenAI validation
openai_valid = bool(cls.OPENAI_API_KEY)
validation_results["openai"] = openai_valid
# MiniMax validation
minimax_valid = bool(cls.MINIMAX_API_KEY)
validation_results["minimax"] = minimax_valid
return validation_results
@classmethod
def validate(cls) -> bool:
"""Validate required configuration"""
validation_errors = []
provider_validations = cls._validate_provider_config()
# Check ASR provider configuration
if cls.ASR_PROVIDER == "azure" and not provider_validations["azure"]:
validation_errors.append(
"Azure Speech credentials missing: AZURE_SPEECH_KEY and AZURE_SPEECH_REGION required for ASR"
)
# Check LLM provider configuration
if cls.LLM_PROVIDER == "openai" and not provider_validations["openai"]:
validation_errors.append("OpenAI API key missing: OPENAI_API_KEY required for LLM")
# Check TTS provider configuration
if cls.TTS_PROVIDER == "azure" and not provider_validations["azure"]:
validation_errors.append(
"Azure Speech credentials missing: AZURE_SPEECH_KEY and AZURE_SPEECH_REGION required for TTS"
)
elif cls.TTS_PROVIDER == "minimax" and not provider_validations["minimax"]:
validation_errors.append("MiniMax API key missing: MINIMAX_API_KEY required for TTS")
# Report validation errors
if validation_errors:
for error in validation_errors:
logger.error(error)
return False
logger.info("Configuration validated successfully")
return True
@classmethod
def get_service_config(cls, service_type: str) -> Dict[str, Any]:
"""Get provider-specific configuration for a service type"""
config = {"provider": getattr(cls, f"{service_type.upper()}_PROVIDER")}
if service_type.upper() == "ASR":
config.update(
{
"language": cls.ASR_LANGUAGE,
"energy_threshold": cls.VOICE_ENERGY_THRESHOLD,
}
)
if config["provider"] == "azure":
config.update(
{
"speech_key": cls.AZURE_SPEECH_KEY,
"speech_region": cls.AZURE_SPEECH_REGION,
}
)
elif service_type.upper() == "LLM":
if config["provider"] == "openai":
config.update(
{
"api_key": cls.OPENAI_API_KEY,
"base_url": cls.OPENAI_BASE_URL,
"model": cls.OPENAI_MODEL,
"system_prompt": cls.OPENAI_SYSTEM_PROMPT,
}
)
elif service_type.upper() == "TTS":
if config["provider"] == "azure":
config.update(
{
"speech_key": cls.AZURE_SPEECH_KEY,
"speech_region": cls.AZURE_SPEECH_REGION,
"voice": cls.AZURE_TTS_VOICE,
}
)
elif config["provider"] == "minimax":
config.update(
{
"api_key": cls.MINIMAX_API_KEY,
"voice_id": cls.MINIMAX_VOICE_ID,
}
)
return config
@classmethod
def get_service_config_masked(cls, service_type: str) -> Dict[str, str]:
"""Get provider-specific configuration with sensitive data masked (for logging)"""
config = cls.get_service_config(service_type)
return mask_sensitive(config)
# Validate configuration
Config.validate()