first commit: New project
This commit is contained in:
72
examples/config_example.py
Normal file
72
examples/config_example.py
Normal file
@@ -0,0 +1,72 @@
|
||||
"""
|
||||
配置管理使用示例
|
||||
演示如何使用配置管理模块
|
||||
"""
|
||||
|
||||
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()
|
||||
55
examples/send_example.py
Normal file
55
examples/send_example.py
Normal file
@@ -0,0 +1,55 @@
|
||||
"""
|
||||
使用示例:发送Memo到Memos服务器
|
||||
|
||||
在使用前,请确保在 .env 文件中配置以下环境变量:
|
||||
MEMOS_BASE_URL=<your_memos_base_url>
|
||||
MEMOS_TOKEN=<your_token>
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from src.send import MemosAPI, send_memo
|
||||
|
||||
def main():
|
||||
"""发送Memo示例"""
|
||||
try:
|
||||
# 方法1:使用便捷函数
|
||||
print("=== 方法1:使用便捷函数 ===")
|
||||
result = send_memo(
|
||||
content="这是通过Memos CLI发送的测试内容",
|
||||
visibility="PRIVATE",
|
||||
tags=["测试", "CLI"]
|
||||
)
|
||||
print(f"Memo发送成功!ID: {result.get('id')}")
|
||||
print(f"内容: {result.get('content')}")
|
||||
print(f"创建时间: {result.get('createdTs')}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"便捷函数发送失败: {e}")
|
||||
|
||||
try:
|
||||
# 方法2:使用API客户端类
|
||||
print("\n=== 方法2:使用API客户端类 ===")
|
||||
api = MemosAPI()
|
||||
|
||||
# 测试连接
|
||||
if api.test_connection():
|
||||
print("成功连接到Memos服务器")
|
||||
|
||||
# 发送Memo
|
||||
result = api.send_memo(
|
||||
content="这是通过Memos API客户端发送的内容",
|
||||
visibility="PUBLIC",
|
||||
tags=["API测试", "Python"]
|
||||
)
|
||||
print(f"Memo发送成功!ID: {result.get('id')}")
|
||||
else:
|
||||
print("无法连接到Memos服务器")
|
||||
|
||||
except Exception as e:
|
||||
print(f"API客户端发送失败: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user