|
|
1 місяць тому | |
|---|---|---|
| backend | 1 місяць тому | |
| docs | 1 місяць тому | |
| mobile | 1 місяць тому | |
| .gitattributes | 1 місяць тому | |
| .gitignore | 1 місяць тому | |
| LICENSE | 1 місяць тому | |
| README.md | 1 місяць тому | |
| docker-compose.yml | 1 місяць тому |
一个支持多AI平台、好感度系统和动态主动消息的移动端AI对话应用。
phoneapp/
├── backend/ # FastAPI后端
│ ├── app/
│ │ ├── api/ # API路由
│ │ │ ├── auth.py # 认证API
│ │ │ ├── characters.py # 角色管理API
│ │ │ ├── conversations.py # 对话API
│ │ │ └── affection.py # 好感度API
│ │ ├── models/ # SQLAlchemy模型
│ │ │ ├── user.py
│ │ │ ├── character.py
│ │ │ ├── conversation.py
│ │ │ ├── affection.py
│ │ │ └── proactive_message.py
│ │ ├── schemas/ # Pydantic Schema
│ │ ├── services/ # 业务逻辑
│ │ │ ├── llm_provider.py # AI适配层
│ │ │ ├── affection_service.py # 好感度服务
│ │ │ └── proactive_message_service.py
│ │ ├── core/ # 核心功能
│ │ │ ├── config.py # 配置管理
│ │ │ ├── database.py # 数据库连接
│ │ │ ├── redis.py # Redis连接
│ │ │ └── security.py # 安全(JWT、加密)
│ │ ├── tasks/ # Celery任务
│ │ │ ├── celery_app.py
│ │ │ └── proactive_tasks.py
│ │ └── main.py # FastAPI应用入口
│ ├── alembic/ # 数据库迁移
│ ├── requirements.txt
│ ├── Dockerfile
│ └── .env.example
├── mobile/ # React Native前端(待实现)
├── docker-compose.yml # Docker编排
└── README.md # 项目文档
复制环境变量文件
cd backend
cp .env.example .env
生成加密密钥
# 在Python中生成32字节密钥
python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"
将生成的密钥复制到.env文件的ENCRYPTION_KEY字段。
启动所有服务
docker-compose up -d
运行数据库迁移
docker-compose exec backend alembic upgrade head
访问应用
安装Python依赖
cd backend
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt
配置环境变量
cp .env.example .env
# 编辑.env文件,填写数据库连接等信息
运行数据库迁移
alembic upgrade head
启动FastAPI服务
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
启动Celery Worker(新终端)
celery -A app.tasks.celery_app worker --loglevel=info
启动Celery Beat(新终端)
celery -A app.tasks.celery_app beat --loglevel=info
POST /api/auth/register
Content-Type: application/json
{
"username": "testuser",
"email": "test@example.com",
"password": "password123"
}
POST /api/auth/login
Content-Type: application/x-www-form-urlencoded
username=testuser&password=password123
响应:
{
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGc...",
"refresh_token": "eyJ0eXAiOiJKV1QiLCJhbGc...",
"token_type": "bearer"
}
POST /api/auth/api-keys
Authorization: Bearer {access_token}
Content-Type: application/json
{
"provider": "openai",
"api_key": "sk-proj-...",
"model": "gpt-3.5-turbo"
}
POST /api/characters
Authorization: Bearer {access_token}
Content-Type: application/json
{
"name": "小雪",
"personality": "温柔体贴,善解人意",
"background_story": "大学文学系学生,喜欢阅读和写作",
"avatar_url": "https://example.com/avatar.jpg",
"language": "zh",
"llm_provider": "openai",
"llm_model": "gpt-3.5-turbo",
"config": {
"temperature": 0.9
}
}
POST /api/conversations/{conversation_id}/messages
Authorization: Bearer {access_token}
Content-Type: application/json
{
"content": "今天天气真好!",
"stream": false
}
响应:
{
"id": 123,
"conversation_id": 1,
"role": "assistant",
"content": "是呀!这样的好天气适合出去走走,你有什么计划吗?",
"tokens_used": 28,
"is_proactive": false,
"created_at": "2025-12-07T10:30:00Z",
"affection_change": 2
}
GET /api/affection/{character_id}
Authorization: Bearer {access_token}
响应:
{
"character_id": 1,
"current_score": 65,
"level": "朋友",
"next_level_score": 80,
"total_interactions": 127,
"last_interaction": "2025-12-07T10:30:00Z",
"recent_changes": [
{
"id": 1,
"score_change": 3,
"reason": "用户分享积极情绪",
"created_at": "2025-12-07T10:30:00Z"
}
]
}
等级划分(-100 ~ 100分):
好感度变化因素:
触发规则(根据好感度):
| 好感度 | 检测间隔 | 随机发送间隔 |
|---|---|---|
| 90-100分 | 用户3分钟无对话 | 1-10分钟 |
| 80-89分 | 用户10分钟无对话 | 10-30分钟 |
| 60-79分 | 用户30分钟无对话 | 30-120分钟 |
| 40-59分 | 用户2小时无对话 | 2-6小时 |
| <40分 | 不发送主动消息 | - |
实现方式:
user_activity)支持的平台:
统一接口:
provider = get_llm_provider("openai") # 或 "claude", "qwen", "ernie"
response = await provider.chat_completion(messages, api_key, model)
.env文件管理,不提交到Git移动端将使用React Native开发,支持iOS和Android。
确保PostgreSQL已启动,检查.env文件中的DATABASE_URL配置。
确保Redis已启动,检查CELERY_BROKER_URL配置。
检查用户是否配置了API Key,确认API Key有效且有余额。
确保.env文件中的ENCRYPTION_KEY是32字节的Fernet密钥。
MIT License
欢迎提交Issue和Pull Request!
如有问题,请提交Issue或联系开发者。
祝你使用愉快!🎉