Files
tts/scripts/tools/perplexity_config.py
2026-01-19 10:27:41 +08:00

72 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
Perplexity API配置管理
Author: AI Assistant
Date: 2026-01-12
Version: 1.0
这个文件提供了Perplexity API的配置管理功能
允许通过环境变量或配置文件设置API密钥和其他参数。
"""
import os
from typing import Optional
class PerplexityConfig:
"""
Perplexity API配置类
"""
def __init__(self):
# 从环境变量加载配置
self.api_key: Optional[str] = os.getenv("PERPLEXITY_API_KEY")
self.api_base: str = os.getenv("PERPLEXITY_API_BASE", "https://api.perplexity.ai")
self.model: str = os.getenv("PERPLEXITY_MODEL", "pplx-70b-online")
def validate(self) -> bool:
"""
验证配置是否有效
"""
if not self.api_key:
print("警告: PERPLEXITY_API_KEY 环境变量未设置")
return False
return True
def get_api_key(self) -> Optional[str]:
"""
获取API密钥
"""
return self.api_key
def get_api_base(self) -> str:
"""
获取API基础URL
"""
return self.api_base
def get_model(self) -> str:
"""
获取默认模型
"""
return self.model
def set_api_key(self, api_key: str):
"""
设置API密钥
"""
self.api_key = api_key
# 同时设置环境变量,以便其他使用环境变量的代码也能访问
os.environ["PERPLEXITY_API_KEY"] = api_key
# 创建全局配置实例
perplexity_config = PerplexityConfig()
def get_perplexity_config() -> PerplexityConfig:
"""
获取全局Perplexity配置实例
"""
return perplexity_config