diff --git a/main/.env.example b/main/.env.example new file mode 100644 index 000000000..783b93cc0 --- /dev/null +++ b/main/.env.example @@ -0,0 +1,31 @@ +# Live Agent Environment Configuration +# Copy this file to .env and update the values + +# ==================== S3/MinIO Configuration ==================== +# For local development with MinIO(compatible with S3) +AWS_ACCESS_KEY_ID=minioadmin +AWS_SECRET_ACCESS_KEY=minioadmin +AWS_REGION=us-east-1 +S3_BUCKET_NAME=live-agent-files +S3_ENDPOINT_URL= +S3_PUBLIC_BASE_URL= + +# ==================== Security ==================== +SECRET_KEY=dev-secret-key-change-in-production + +# ==================== Token Configuration ==================== +TOKEN_EXPIRE_DAYS=7 + +# ==================== Live Agent API Server Configuration ==================== +# These are optional, defaults are set in settings.py +# LIVE_AGENT_API_HOST=0.0.0.0 +# LIVE_AGENT_API_PORT=8080 +# LIVE_AGENT_API_DEBUG=False + +# ==================== Database Configuration ==================== +DATABASE_URL=postgresql+asyncpg://postgres:postgres@localhost:5432/live_agent + +FISH_API_KEY=your-fish-api-key +OPENAI_API_KEY=your-openai-key +GROQ_API_KEY=your-groq-api-key +OPENAI_MODEL= \ No newline at end of file diff --git a/main/docker-compose_local_all.yml b/main/docker-compose_local_all.yml index 695e7df25..641e5cf69 100644 --- a/main/docker-compose_local_all.yml +++ b/main/docker-compose_local_all.yml @@ -8,6 +8,8 @@ services: dockerfile: Dockerfile image: xiaozhi-esp32-server:local container_name: xiaozhi-esp32-server + env_file: + - .env depends_on: xiaozhi-esp32-server-db: condition: service_healthy @@ -211,7 +213,7 @@ services: dockerfile: Dockerfile container_name: live-agent-api env_file: - - ./live-agent-api/.env + - .env environment: DATABASE_URL: postgresql+asyncpg://postgres:postgres@live-agent-postgres:5432/live_agent S3_ENDPOINT_URL: http://live-agent-minio:9000 diff --git a/main/live-agent-api/.env.example b/main/live-agent-api/.env.example deleted file mode 100644 index 0666c9a15..000000000 --- a/main/live-agent-api/.env.example +++ /dev/null @@ -1,35 +0,0 @@ -# Live Agent API Environment Configuration -# Copy this file to .env and update the values - -# ==================== S3/MinIO Configuration ==================== -# For local development with MinIO -AWS_ACCESS_KEY_ID=minioadmin -AWS_SECRET_ACCESS_KEY=minioadmin -AWS_REGION=us-east-1 -S3_BUCKET_NAME=live-agent-files - -# For production with AWS S3, use your actual credentials: -# AWS_ACCESS_KEY_ID=your-aws-access-key -# AWS_SECRET_ACCESS_KEY=your-aws-secret-key -# AWS_REGION=ap-northeast-1 -# S3_BUCKET_NAME=your-production-bucket - -# ==================== Security ==================== -SECRET_KEY=dev-secret-key-change-in-production - -# ==================== Token Configuration ==================== -TOKEN_EXPIRE_DAYS=7 - -# ==================== Server Configuration ==================== -# These are optional, defaults are set in settings.py -# HOST=0.0.0.0 -# PORT=8080 -# DEBUG=False - -# ==================== Database Configuration ==================== -# This is overridden in docker-compose.yml for container environment -# For local development without Docker, use: -# DATABASE_URL=postgresql+asyncpg://postgres:postgres@localhost:5432/live_agent - -FISH_API_KEY=your-fish-api-key - diff --git a/main/xiaozhi-server/config/config_loader.py b/main/xiaozhi-server/config/config_loader.py index 1e66e234a..c0e3c3a2b 100644 --- a/main/xiaozhi-server/config/config_loader.py +++ b/main/xiaozhi-server/config/config_loader.py @@ -1,9 +1,51 @@ import os +import re import yaml from collections.abc import Mapping from config.manage_api_client import init_service, get_server_config, get_agent_models from dotenv import load_dotenv + +# Environment variable pattern: ${VAR_NAME} or ${VAR_NAME:-default_value} +ENV_VAR_PATTERN = re.compile(r'\$\{([^}:]+)(?::-([^}]*))?\}') + + +def expand_env_vars(config): + """ + Recursively expand environment variable placeholders in config. + + Supports two formats: + - ${VAR_NAME} - Required, raises error if not set + - ${VAR_NAME:-default} - Optional, uses default if not set + + Examples: + api_key: ${OPENAI_API_KEY} + base_url: ${OPENAI_BASE_URL:-https://api.openai.com/v1} + """ + if isinstance(config, dict): + return {k: expand_env_vars(v) for k, v in config.items()} + elif isinstance(config, list): + return [expand_env_vars(item) for item in config] + elif isinstance(config, str): + def replace_env_var(match): + var_name = match.group(1) + default_value = match.group(2) # None if no default specified + + env_value = os.environ.get(var_name) + + if env_value is not None: + return env_value + elif default_value is not None: + return default_value + else: + # Keep original placeholder if env var not set and no default + # This allows optional configs to remain as placeholders + return match.group(0) + + return ENV_VAR_PATTERN.sub(replace_env_var, config) + return config + + def get_project_dir(): """获取项目根目录""" return os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "/" @@ -24,34 +66,19 @@ def load_config(): if cached_config is not None: return cached_config - # default_config_path = get_project_dir() + "config.yaml" custom_config_path = get_project_dir() + "custom_config.yaml" - # 加载默认配置 - # default_config = read_config(default_config_path) - config = read_config(custom_config_path) - config["read_config_from_live_agent_api"] = True - # load environment variables + # Load environment variables from .env file load_dotenv() - # tts - select_tts_module = config["selected_module"]["TTS"] - if os.environ.get("REGION") == "LOCAL" and select_tts_module == "FishSpeech": - config["TTS"][select_tts_module]["api_key"] = os.environ.get("FISH_API_KEY") - # asr - select_asr_module = config["selected_module"]["ASR"] - if os.environ.get("REGION") == "LOCAL" and select_asr_module == "GroqASR": - config["ASR"][select_asr_module]["api_key"] = os.environ.get("GROQ_API_KEY") - # llm - select_llm_module = config["selected_module"]["LLM"] - if os.environ.get("REGION") == "LOCAL" and select_llm_module == "GroqLLM": - config["LLM"][select_llm_module]["api_key"] = os.environ.get("GROQ_API_KEY") - if os.environ.get("REGION") == "LOCAL" and select_llm_module == "OpenaiLLM": - config["LLM"][select_llm_module]["api_key"] = os.environ.get("OPENAI_API_KEY") - # if custom_config.get("manager-api", {}).get("url"): - # config = get_config_from_api(custom_config) - # else: - # # 合并配置 - # config = merge_configs(default_config, custom_config) + + # Read YAML config + config = read_config(custom_config_path) + + # Expand environment variable placeholders (e.g., ${OPENAI_API_KEY}) + config = expand_env_vars(config) + + config["read_config_from_live_agent_api"] = True + # 初始化目录 ensure_directories(config) diff --git a/main/xiaozhi-server/custom_config.yaml b/main/xiaozhi-server/custom_config.yaml index d8f29d6c5..9e63bfb7e 100644 --- a/main/xiaozhi-server/custom_config.yaml +++ b/main/xiaozhi-server/custom_config.yaml @@ -416,7 +416,7 @@ ASR: # 2.创建api-key https://console.groq.com/keys # 3.模型可以选择whisper-large-v3-turbo或whisper-large-v3(distil-whisper-large-v3-en仅支持英语转录) type: openai - api_key: your groq api key + api_key: ${GROQ_API_KEY} base_url: https://api.groq.com/openai/v1 model_name: whisper-large-v3-turbo output_dir: tmp/ @@ -542,7 +542,7 @@ LLM: type: openai model_name: gpt-4o base_url: https://api.openai.com/v1 - api_key: your openai api key + api_key: ${OPENAI_API_KEY} OllamaLLM: # 定义LLM API类型 type: ollama @@ -782,7 +782,7 @@ TTS: channels: 1 rate: 44100 sample_rate: 16000 - api_key: "your fish speech api key" + api_key: ${FISH_API_KEY} GPT_SOVITS_V2: # 定义TTS API类型 #启动tts方法: