Files
memoscli/examples/config_example.py

72 lines
2.0 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.

"""
配置管理使用示例
演示如何使用配置管理模块
"""
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from src.config import ConfigManager, set_config, get_config
def main():
"""配置管理示例"""
print("=== 配置管理示例 ===")
# 方法1使用ConfigManager类
print("\n--- 方法1使用ConfigManager类 ---")
config_manager = ConfigManager()
# 显示当前配置
print("当前配置:")
config_manager.show_config()
# 设置配置
print("\n设置新的配置值...")
config_manager.set('MEMOS_BASE_URL', 'https://demo.memos.com')
config_manager.set('MEMOS_TOKEN', 'your-token-here')
# 获取配置
base_url = config_manager.get('MEMOS_BASE_URL')
print(f"获取到的MEMOS_BASE_URL: {base_url}")
# 验证配置
validation = config_manager.validate_config()
print(f"配置验证结果: {validation['is_valid']}")
# 方法2使用便捷函数
print("\n--- 方法2使用便捷函数 ---")
# 设置配置
set_config('MEMOS_AI_MODEL_NAME', 'gpt-3.5-turbo')
# 获取配置
model_name = get_config('MEMOS_AI_MODEL_NAME')
print(f"获取到的AI模型名称: {model_name}")
# 批量设置配置
print("\n--- 批量设置配置 ---")
new_configs = {
'MEMOS_AI_MODEL_API_KEY': 'sk-your-api-key',
'MEMOS_AI_MODEL_API_URL': 'https://api.openai.com/v1'
}
results = config_manager.set_multiple(new_configs)
for key, success in results.items():
status = "成功" if success else "失败"
print(f"设置 {key}: {status}")
# 显示所有配置
print("\n--- 所有配置 ---")
all_config = config_manager.get_all()
for key, value in all_config.items():
if 'TOKEN' in key or 'API_KEY' in key:
masked_value = '*' * 8 if value else '未设置'
print(f"{key}: {masked_value}")
else:
print(f"{key}: {value}")
print("\n=== 配置管理示例完成 ===")
if __name__ == "__main__":
main()