Compare commits
33 Commits
43adf1a7c4
...
feature/tr
| Author | SHA1 | Date | |
|---|---|---|---|
| a3e3711b2b | |||
| 8b10ef5828 | |||
| dd17206e1f | |||
| 4f9cf4b725 | |||
| e8dedb5ec4 | |||
| 7a62139683 | |||
| 01ca2bd0f9 | |||
| 1fc0c43689 | |||
| 1abfaeda9d | |||
| 0ae53c4b81 | |||
| f90ad8dc13 | |||
| 6375f9759c | |||
| 33188a345e | |||
| 6fa1fd6e7f | |||
| 80237463ef | |||
| f9bc77d392 | |||
| 408e9ce569 | |||
| 60a2049bb7 | |||
| 4d4d7c30ce | |||
| 2b1ec63c00 | |||
| 6c74bef8da | |||
| 73cdf5ac23 | |||
| a371039ee6 | |||
| 91d11abe90 | |||
| 4b85b35cf8 | |||
| c99052529d | |||
| bd1fa14f20 | |||
| 1aa90f5acf | |||
| 04ea889d75 | |||
| 3c4a11eca8 | |||
| 85f2bbe78c | |||
| 18ee2b9b2c | |||
| 5a78b7b392 |
16
.env
16
.env
@@ -0,0 +1,16 @@
|
||||
# ---------- 路径配置 ----------
|
||||
VECTORSTORE_PATH=/data/vectorstore
|
||||
STATE_FILE=/data/state.json
|
||||
SCHEMA_FILE=/data/schema.json
|
||||
PRESETS_FILE=/data/presets.json
|
||||
REGEX_FILE=/data/regex_rules.json
|
||||
|
||||
# ---------- 服务地址 ----------
|
||||
COMFYUI_API_URL=http://comfyui:8188
|
||||
BACKEND_PORT=8000
|
||||
FRONTEND_PORT=8501
|
||||
|
||||
# 先配置 .env 文件
|
||||
MAIN_LLM_API_KEY=sk-Oh4o3fzV6Qe59B6DRwSskE48xe5D6bq1hkgDZqH1mmJOCN8j
|
||||
MAIN_LLM_BASE_URL=https://api.chatfire.cn/v1
|
||||
MAIN_LLM_MODEL=glm4.7
|
||||
|
||||
BIN
.gitignore
vendored
Normal file
BIN
.gitignore
vendored
Normal file
Binary file not shown.
1
.idea/.gitignore
generated
vendored
1
.idea/.gitignore
generated
vendored
@@ -6,3 +6,4 @@
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
.env
|
||||
|
||||
5
.idea/codeStyles/codeStyleConfig.xml
generated
Normal file
5
.idea/codeStyles/codeStyleConfig.xml
generated
Normal file
@@ -0,0 +1,5 @@
|
||||
<component name="ProjectCodeStyleConfiguration">
|
||||
<state>
|
||||
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
|
||||
</state>
|
||||
</component>
|
||||
6
.idea/llm-workflow-engine.iml
generated
6
.idea/llm-workflow-engine.iml
generated
@@ -1,7 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/backend" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/backend/api" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="Python 3.9 (pythonProject1)" jdkType="Python SDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
|
||||
@@ -4,15 +4,21 @@ FROM python:3.11-slim
|
||||
# 设置工作目录
|
||||
WORKDIR /app
|
||||
|
||||
# 复制依赖文件并安装
|
||||
# 设置环境变量
|
||||
ENV PYTHONUNBUFFERED=1
|
||||
ENV PYTHONDONTWRITEBYTECODE=1
|
||||
|
||||
# 复制依赖文件
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
# 安装依赖
|
||||
RUN pip install --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
|
||||
|
||||
# 复制所有代码
|
||||
COPY app/ ./app/
|
||||
COPY . .
|
||||
|
||||
# 暴露端口
|
||||
EXPOSE 8000
|
||||
|
||||
# 启动命令
|
||||
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
|
||||
CMD ["uvicorn", "backend.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
|
||||
|
||||
BIN
backend/__pycache__/__init__.cpython-311.pyc
Normal file
BIN
backend/__pycache__/__init__.cpython-311.pyc
Normal file
Binary file not shown.
BIN
backend/__pycache__/__init__.cpython-39.pyc
Normal file
BIN
backend/__pycache__/__init__.cpython-39.pyc
Normal file
Binary file not shown.
BIN
backend/__pycache__/main.cpython-311.pyc
Normal file
BIN
backend/__pycache__/main.cpython-311.pyc
Normal file
Binary file not shown.
BIN
backend/api/__pycache__/__init__.cpython-311.pyc
Normal file
BIN
backend/api/__pycache__/__init__.cpython-311.pyc
Normal file
Binary file not shown.
BIN
backend/api/__pycache__/route.cpython-311.pyc
Normal file
BIN
backend/api/__pycache__/route.cpython-311.pyc
Normal file
Binary file not shown.
19
backend/api/route.py
Normal file
19
backend/api/route.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from fastapi import APIRouter
|
||||
from .routes import presetsRoute, chatsRoute, worldbooksRoute, apiConfigRoute
|
||||
from utils.file_utils import get_all_roles_and_chats
|
||||
from core.config import settings
|
||||
from pathlib import Path
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
# 注册子路由
|
||||
router.include_router(presetsRoute.router)
|
||||
router.include_router(chatsRoute.router)
|
||||
router.include_router(worldbooksRoute.router)
|
||||
router.include_router(apiConfigRoute.router)
|
||||
|
||||
|
||||
# 保留原有的其他路由
|
||||
@router.get("/tool_bar/get_all_role_and_chat")
|
||||
def get_all_role_and_chat_endpoint():
|
||||
return get_all_roles_and_chats(Path(settings.DATA_PATH))
|
||||
56
backend/api/routes/chatsRoute.py
Normal file
56
backend/api/routes/chatsRoute.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from fastapi import APIRouter, HTTPException, status
|
||||
# TODO: 实现 ChatService 来替代旧的 ChatHistory 逻辑
|
||||
# from services.chat_service import ChatService
|
||||
|
||||
router = APIRouter(prefix="/chat", tags=["chat"])
|
||||
|
||||
@router.get("", response_model=dict)
|
||||
async def list_all_chats():
|
||||
"""获取所有角色的所有聊天列表"""
|
||||
# return await ChatService.list_all_chats()
|
||||
return {"chats": []}
|
||||
|
||||
@router.get("/{role_name}/{chat_name}")
|
||||
async def get_chat(role_name: str, chat_name: str):
|
||||
"""获取指定聊天的完整内容"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.post("/{role_name}", status_code=status.HTTP_201_CREATED)
|
||||
async def create_chat(role_name: str, chat_name: str, metadata: dict = None):
|
||||
"""创建新聊天"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.put("/{role_name}/{chat_name}")
|
||||
async def update_chat(role_name: str, chat_name: str, update_data: dict):
|
||||
"""更新聊天元数据"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.delete("/{role_name}/{chat_name}")
|
||||
async def delete_chat(role_name: str, chat_name: str):
|
||||
"""删除指定聊天"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.get("/{role_name}/{chat_name}/messages")
|
||||
async def list_messages(role_name: str, chat_name: str):
|
||||
"""获取聊天的所有消息"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.get("/{role_name}/{chat_name}/messages/{floor}")
|
||||
async def get_message(role_name: str, chat_name: str, floor: int):
|
||||
"""获取指定楼层的消息"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.post("/{role_name}/{chat_name}/messages", status_code=status.HTTP_201_CREATED)
|
||||
async def add_message(role_name: str, chat_name: str, message_data: dict):
|
||||
"""向聊天添加新消息"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.put("/{role_name}/{chat_name}/messages/{floor}")
|
||||
async def update_message(role_name: str, chat_name: str, floor: int, update_data: dict):
|
||||
"""更新指定楼层的消息"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.delete("/{role_name}/{chat_name}/messages/{floor}")
|
||||
async def delete_message(role_name: str, chat_name: str, floor: int):
|
||||
"""删除指定楼层的消息"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
60
backend/api/routes/presetsRoute.py
Normal file
60
backend/api/routes/presetsRoute.py
Normal file
@@ -0,0 +1,60 @@
|
||||
from fastapi import APIRouter, HTTPException, status
|
||||
# TODO: 实现 PresetService 来替代旧的 AIDesignSpec 逻辑
|
||||
# from services.preset_service import PresetService
|
||||
|
||||
router = APIRouter(prefix="/presets", tags=["presets"])
|
||||
|
||||
@router.get("", response_model=dict)
|
||||
async def list_presets():
|
||||
"""获取所有预设列表及其基本信息"""
|
||||
# return await PresetService.list_all_presets()
|
||||
return {"presets": []}
|
||||
|
||||
@router.get("/{preset_name}")
|
||||
async def get_preset(preset_name: str):
|
||||
"""获取指定预设的完整内容"""
|
||||
# try:
|
||||
# return await PresetService.get_preset(preset_name)
|
||||
# except FileNotFoundError:
|
||||
# raise HTTPException(status_code=404, detail="Preset not found")
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.post("", status_code=status.HTTP_201_CREATED)
|
||||
async def create_preset(preset_name: str, preset_data: dict):
|
||||
"""创建新预设"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.put("/{preset_name}")
|
||||
async def update_preset(preset_name: str, update_data: dict):
|
||||
"""更新预设配置"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.delete("/{preset_name}")
|
||||
async def delete_preset(preset_name: str):
|
||||
"""删除指定预设"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.get("/{preset_name}/components")
|
||||
async def list_preset_components(preset_name: str):
|
||||
"""获取预设中的所有组件"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.get("/{preset_name}/components/{component_id}")
|
||||
async def get_preset_component(preset_name: str, component_id: str):
|
||||
"""获取指定组件的详情"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.post("/{preset_name}/components", status_code=status.HTTP_201_CREATED)
|
||||
async def add_preset_component(preset_name: str, component_data: dict):
|
||||
"""向预设添加新组件"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.put("/{preset_name}/components/{component_id}")
|
||||
async def update_preset_component(preset_name: str, component_id: str, update_data: dict):
|
||||
"""更新指定组件"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.delete("/{preset_name}/components/{component_id}")
|
||||
async def delete_preset_component(preset_name: str, component_id: str):
|
||||
"""从预设中删除指定组件"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
117
backend/api/routes/worldbooksRoute.py
Normal file
117
backend/api/routes/worldbooksRoute.py
Normal file
@@ -0,0 +1,117 @@
|
||||
3# 标准库导入
|
||||
import os
|
||||
import shutil
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import List, Dict, Any, Optional
|
||||
|
||||
# 第三方库导入
|
||||
from fastapi import APIRouter, HTTPException, UploadFile, File, Form
|
||||
from fastapi.responses import JSONResponse, FileResponse
|
||||
|
||||
# 本地模块导入
|
||||
from models.internal import WorldInfo, WorldInfoEntry
|
||||
from core.config import settings
|
||||
|
||||
# 配置日志
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# 创建路由器
|
||||
router = APIRouter(prefix="/worldbooks", tags=["worldbooks"])
|
||||
|
||||
# 确保世界书目录存在 (由 config.py 中的 settings.ensure_directories() 统一处理,此处保留作为双重保险)
|
||||
os.makedirs(settings.WORLDBOOKS_PATH, exist_ok=True)
|
||||
|
||||
|
||||
@router.get("/", response_model=List[Dict[str, Any]])
|
||||
async def list_worldbooks():
|
||||
"""
|
||||
获取所有世界书的列表
|
||||
Returns:
|
||||
List[Dict[str, Any]]: 世界书列表
|
||||
"""
|
||||
# TODO: 实现 WorldBookService
|
||||
return []
|
||||
|
||||
@router.get("/{name}", response_model=Dict[str, Any])
|
||||
async def get_worldbook(name: str):
|
||||
"""
|
||||
获取指定名称的世界书
|
||||
"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.post("/", response_model=Dict[str, Any])
|
||||
async def create_worldbook(
|
||||
name: str = Form(...),
|
||||
file: Optional[UploadFile] = File(None)
|
||||
):
|
||||
"""
|
||||
创建新世界书
|
||||
"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.put("/{name}", response_model=Dict[str, Any])
|
||||
async def update_worldbook(
|
||||
name: str,
|
||||
file: Optional[UploadFile] = File(None)
|
||||
):
|
||||
"""
|
||||
更新世界书
|
||||
"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.delete("/{name}")
|
||||
async def delete_worldbook(name: str):
|
||||
"""
|
||||
删除世界书
|
||||
"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.get("/{name}/entries", response_model=List[Dict[str, Any]])
|
||||
async def list_worldbook_entries(name: str):
|
||||
"""
|
||||
获取世界书的所有条目
|
||||
"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.get("/{name}/entries/{uid}", response_model=Dict[str, Any])
|
||||
async def get_worldbook_entry(name: str, uid: int):
|
||||
"""
|
||||
获取世界书的指定条目
|
||||
"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.post("/{name}/entries", response_model=Dict[str, Any])
|
||||
async def create_worldbook_entry(name: str, entry_data: Dict[str, Any]):
|
||||
"""
|
||||
在世界书中创建新条目
|
||||
"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.put("/{name}/entries/{uid}", response_model=Dict[str, Any])
|
||||
async def update_worldbook_entry(name: str, uid: int, entry_data: Dict[str, Any]):
|
||||
"""
|
||||
更新世界书的指定条目
|
||||
"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.delete("/{name}/entries/{uid}")
|
||||
async def delete_worldbook_entry(name: str, uid: int):
|
||||
"""
|
||||
删除世界书的指定条目
|
||||
"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.post("/{name}/import", response_model=Dict[str, Any])
|
||||
async def import_worldbook(name: str, file: UploadFile = File(...)):
|
||||
"""
|
||||
从文件导入世界书
|
||||
"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
|
||||
@router.get("/{name}/export")
|
||||
async def export_worldbook(name: str):
|
||||
"""
|
||||
导出世界书为 SillyTavern 格式
|
||||
"""
|
||||
raise HTTPException(status_code=501, detail="Not Implemented")
|
||||
@@ -1,46 +0,0 @@
|
||||
import base64
|
||||
from typing import Any, Dict
|
||||
from IPython.core.magic_arguments import defaults
|
||||
from .. import nodes
|
||||
|
||||
class StartNode():
|
||||
name = "开始节点"
|
||||
inputs = {
|
||||
"user_input": "string", # 用户输入文本
|
||||
"stream": "boolean", # 是否流式输出
|
||||
"img_switch": "boolean", # 是否处理图片
|
||||
"table_switch": "boolean", # 是否处理表格
|
||||
"role_name": "string", # 角色名称
|
||||
"chat_name": "string" # 会话名称
|
||||
}
|
||||
|
||||
async def run(self, text: str = None, image: bytes = None, **kwargs) -> Dict[str, Any]:
|
||||
# 查空:文本不能为空字符串
|
||||
if not text or text.strip() == "":
|
||||
raise ValueError("文本输入不能为空")
|
||||
|
||||
# 查空:图片数据不能为空
|
||||
if image is None or len(image) == 0:
|
||||
raise ValueError("图片输入不能为空")
|
||||
|
||||
# 将图片字节转换为 Base64 字符串,便于在节点间传递
|
||||
image_base64 = base64.b64encode(image).decode('utf-8')
|
||||
|
||||
return {
|
||||
"text": text,
|
||||
"image": image_base64
|
||||
}
|
||||
|
||||
async def run(is_user,floor_number,mes: str = None, stream: bool = False, img_switch: bool = False,name = "default",
|
||||
table_switch: bool = False, role_name: str = None, chat_name: str = None,preset: str = None):
|
||||
# 将输入内容持久化存储到本地json方便前端读
|
||||
nodes.save_input_to_json(mes=mes, role_name=role_name, chat_name=chat_name, name=name, is_user=is_user, floor_number=floor_number)
|
||||
# 对上一条输入内容(已确定不变的内容)调用向量化,根据role和chat嵌入到对应本地数据库
|
||||
embed_input(user_input, role_name, chat_name)
|
||||
# 根据role和chat去读取绑定的世界书
|
||||
# 读取预设,进行拼接
|
||||
# 调用模型,返回结果
|
||||
# 将结果持久化存储到本地json方便前端读(用JSONL)
|
||||
# 如果img_switch是开的,那么异步调用生图,并存储到目标文件夹里
|
||||
# 如果table_switch是开的,那么异步调用表格生成,并存储到目标文件夹里
|
||||
# 将结果返回给前端
|
||||
@@ -1,145 +0,0 @@
|
||||
import json
|
||||
from typing import Dict, Any
|
||||
from datetime import datetime
|
||||
import config as cfg
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def save_input_to_json(
|
||||
mes: str,
|
||||
role_name: str,
|
||||
chat_name: str,
|
||||
name: str,
|
||||
is_user: bool,
|
||||
floor_number: int = 0
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
保存消息到JSONL文件或处理重roll请求
|
||||
|
||||
参数:
|
||||
mes: 消息内容
|
||||
role_name: 角色名称
|
||||
chat_name: 对话名称
|
||||
name: 发送者名称
|
||||
is_user: 是否为用户消息
|
||||
floor_number: 楼层号(对话中的第几次回复),用于判断是否为重roll请求
|
||||
|
||||
返回:
|
||||
更新后的消息对象
|
||||
"""
|
||||
config = cfg.settings
|
||||
file_path = config.BASE_PATH / "data" / "chat" / role_name / f"{chat_name}.jsonl"
|
||||
|
||||
# 确保目录存在
|
||||
Path(file_path).parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# 读取文件内容
|
||||
try:
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
lines = f.readlines()
|
||||
except FileNotFoundError:
|
||||
lines = []
|
||||
|
||||
# 判断是否为重roll请求
|
||||
is_regenerate = False
|
||||
target_index = -1
|
||||
|
||||
if lines and floor_number > 0:
|
||||
# 计算当前楼层号
|
||||
current_floor = len(lines)
|
||||
|
||||
# 如果floor_number与当前楼层号相同,则为重roll请求
|
||||
if floor_number == current_floor:
|
||||
# 找到最后一条非用户消息
|
||||
for i in range(len(lines) - 1, -1, -1):
|
||||
try:
|
||||
line_data = json.loads(lines[i])
|
||||
if not line_data.get('is_user', False):
|
||||
is_regenerate = True
|
||||
target_index = i
|
||||
break
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
|
||||
# 处理重roll逻辑
|
||||
if is_regenerate:
|
||||
# 解析目标消息
|
||||
try:
|
||||
target_message = json.loads(lines[target_index])
|
||||
except json.JSONDecodeError:
|
||||
raise ValueError(f"无法解析楼层 {floor_number} 的JSON数据")
|
||||
|
||||
# 初始化swipes数组
|
||||
if target_message.get('swipes') is None:
|
||||
target_message['swipes'] = []
|
||||
|
||||
# 将新回复添加到swipes数组
|
||||
target_message['swipes'].append(mes)
|
||||
|
||||
# 更新swipe_id和content
|
||||
target_message['swipes_id'] = len(target_message['swipes']) - 1
|
||||
target_message['content'] = mes
|
||||
|
||||
# 更新文件内容
|
||||
lines[target_index] = json.dumps(target_message, ensure_ascii=False) + '\n'
|
||||
|
||||
# 写回文件
|
||||
with open(file_path, 'w', encoding='utf-8') as f:
|
||||
f.writelines(lines)
|
||||
|
||||
return target_message
|
||||
|
||||
# 处理普通消息保存逻辑
|
||||
else:
|
||||
# 获取当前时间
|
||||
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# 构建消息对象
|
||||
message = {
|
||||
"role": role_name,
|
||||
"chat": chat_name,
|
||||
"content": mes,
|
||||
"name": name,
|
||||
"is_user": is_user,
|
||||
"send_date": current_time,
|
||||
"floor_number": len(lines) + 1, # 记录楼层号
|
||||
"swipes": [],
|
||||
"swipes_id": 0
|
||||
}
|
||||
|
||||
# 追加到文件
|
||||
with open(file_path, 'a', encoding='utf-8') as f:
|
||||
f.write(json.dumps(message, ensure_ascii=False) + '\n')
|
||||
|
||||
return message
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# 测试普通消息保存
|
||||
# save_input_to_json(
|
||||
# mes="你好",
|
||||
# role_name="test",
|
||||
# chat_name="111",
|
||||
# name="用户",
|
||||
# is_user=True,
|
||||
# floor_number=0
|
||||
# )
|
||||
#
|
||||
# save_input_to_json(
|
||||
# mes="你好,我是AI助手",
|
||||
# role_name="test",
|
||||
# chat_name="111",
|
||||
# name="AI",
|
||||
# is_user=False,
|
||||
# floor_number=1
|
||||
# )
|
||||
|
||||
# 测试重roll最后一条AI消息
|
||||
save_input_to_json(
|
||||
mes="这是重roll后的新回复2",
|
||||
role_name="test",
|
||||
chat_name="111",
|
||||
name="AI",
|
||||
is_user=False,
|
||||
floor_number=2 # 与当前楼层号相同,表示重roll
|
||||
)
|
||||
@@ -1,56 +0,0 @@
|
||||
import re
|
||||
from core.node_base import BaseNode
|
||||
from typing import List, Dict, Any
|
||||
|
||||
|
||||
class TextSplitterNode(BaseNode):
|
||||
name = "文本分割节点"
|
||||
inputs = {"text": "string"}
|
||||
outputs = {
|
||||
"outline": "list", # 大纲部分列表
|
||||
"requirement": "list", # 要求部分列表
|
||||
"dialogue": "list", # 对话部分列表
|
||||
"weak_guidance": "list" # 弱指引部分列表
|
||||
}
|
||||
|
||||
async def run(self, text: str) -> Dict[str, List[str]]:
|
||||
# 正则匹配三种括号内的内容
|
||||
# 注意:此正则假设括号不嵌套,且没有转义字符
|
||||
pattern = r'\{([^{}]*)\}|\(([^()]*)\)|“([^”]*)”'
|
||||
|
||||
outline = []
|
||||
requirement = []
|
||||
dialogue = []
|
||||
weak_guidance = []
|
||||
|
||||
pos = 0
|
||||
for match in re.finditer(pattern, text):
|
||||
start, end = match.span()
|
||||
# 处理匹配前的普通文本(弱指引)
|
||||
if start > pos:
|
||||
weak_part = text[pos:start].strip()
|
||||
if weak_part:
|
||||
weak_guidance.append(weak_part)
|
||||
|
||||
# 根据捕获组确定类型
|
||||
if match.group(1) is not None: # 大括号
|
||||
outline.append(match.group(1).strip())
|
||||
elif match.group(2) is not None: # 小括号
|
||||
requirement.append(match.group(2).strip())
|
||||
elif match.group(3) is not None: # 中文引号
|
||||
dialogue.append(match.group(3).strip())
|
||||
|
||||
pos = end
|
||||
|
||||
# 处理剩余的普通文本
|
||||
if pos < len(text):
|
||||
weak_part = text[pos:].strip()
|
||||
if weak_part:
|
||||
weak_guidance.append(weak_part)
|
||||
|
||||
return {
|
||||
"outline": outline,
|
||||
"requirement": requirement,
|
||||
"dialogue": dialogue,
|
||||
"weak_guidance": weak_guidance
|
||||
}
|
||||
BIN
backend/core/__pycache__/__init__.cpython-311.pyc
Normal file
BIN
backend/core/__pycache__/__init__.cpython-311.pyc
Normal file
Binary file not shown.
BIN
backend/core/__pycache__/__init__.cpython-39.pyc
Normal file
BIN
backend/core/__pycache__/__init__.cpython-39.pyc
Normal file
Binary file not shown.
BIN
backend/core/__pycache__/config.cpython-311.pyc
Normal file
BIN
backend/core/__pycache__/config.cpython-311.pyc
Normal file
Binary file not shown.
BIN
backend/core/__pycache__/config.cpython-39.pyc
Normal file
BIN
backend/core/__pycache__/config.cpython-39.pyc
Normal file
Binary file not shown.
BIN
backend/core/__pycache__/items.cpython-311.pyc
Normal file
BIN
backend/core/__pycache__/items.cpython-311.pyc
Normal file
Binary file not shown.
79
backend/core/config.py
Normal file
79
backend/core/config.py
Normal file
@@ -0,0 +1,79 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# 1. 动态计算项目根目录
|
||||
# 假设 config.py 位于 backend/core/ 目录下
|
||||
# __file__ 指向本文件的绝对路径
|
||||
# .parent 指向 backend/core/ 目录
|
||||
# .parent.parent 指向 backend/ 目录
|
||||
# .parent.parent.parent 指向项目根目录 (即包含 backend/ 和 frontend/ 的目录)
|
||||
PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent
|
||||
|
||||
# 2. 加载 .env 文件
|
||||
# 假设 .env 文件位于项目根目录下
|
||||
load_dotenv(PROJECT_ROOT / ".env")
|
||||
|
||||
|
||||
class Settings:
|
||||
# --- 主模型配置 ---
|
||||
MAIN_LLM_API_KEY = os.getenv("MAIN_LLM_API_KEY")
|
||||
MAIN_LLM_MODEL = os.getenv("MAIN_LLM_MODEL", "gpt-3.5-turbo")
|
||||
MAIN_LLM_BASE_URL = os.getenv("MAIN_LLM_BASE_URL", "https://api.openai.com/v1")
|
||||
MAIN_LLM_MAX_TOKENS = int(os.getenv("MAIN_LLM_MAX_TOKENS", "4096"))
|
||||
MAIN_LLM_STREAM = os.getenv("MAIN_LLM_STREAM", "true").lower() == "true"
|
||||
|
||||
# --- 路径配置 (核心修改) ---
|
||||
|
||||
# 强制使用计算出的项目根目录,不再依赖 .env 中的 BASE_PATH
|
||||
BASE_PATH = PROJECT_ROOT
|
||||
|
||||
# 数据目录:固定为根目录下的 data 文件夹
|
||||
DATA_PATH = BASE_PATH / "data"
|
||||
|
||||
# --- 核心数据文件路径 ---
|
||||
STATE_FILE = DATA_PATH / "state.json"
|
||||
SCHEMA_FILE = DATA_PATH / "schema.json"
|
||||
PRESETS_FILE = DATA_PATH / "presets.json"
|
||||
REGEX_FILE = DATA_PATH / "regex_rules.json"
|
||||
VECTORSTORE_PATH = DATA_PATH / "vectorstore"
|
||||
|
||||
# --- 业务数据目录 ---
|
||||
|
||||
# 世界书目录
|
||||
WORLDBOOKS_PATH = DATA_PATH / "worldbooks"
|
||||
|
||||
# 预设目录
|
||||
PRESET_PATH = DATA_PATH / "preset"
|
||||
|
||||
# 聊天记录目录
|
||||
CHAT_PATH = DATA_PATH / "chat"
|
||||
|
||||
# 临时文件目录
|
||||
TEMP_PATH = DATA_PATH / "temp"
|
||||
|
||||
def ensure_directories(self):
|
||||
"""确保所有配置的目录存在,如果不存在则创建"""
|
||||
directories = [
|
||||
self.DATA_PATH,
|
||||
self.WORLDBOOKS_PATH,
|
||||
self.PRESET_PATH,
|
||||
self.CHAT_PATH,
|
||||
self.TEMP_PATH,
|
||||
]
|
||||
for directory in directories:
|
||||
directory.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
|
||||
settings = Settings()
|
||||
|
||||
# 初始化时自动创建必要的目录
|
||||
settings.ensure_directories()
|
||||
|
||||
if __name__ == '__main__':
|
||||
settings = Settings()
|
||||
print(f"项目根目录: {settings.BASE_PATH}")
|
||||
print(f"数据目录: {settings.DATA_PATH}")
|
||||
print(f"世界书目录: {settings.WORLDBOOKS_PATH}")
|
||||
print(f"预设目录: {settings.PRESETS_PATH}")
|
||||
print(f"聊天目录: {settings.CHAT_PATH}")
|
||||
BIN
backend/core/models/__pycache__/chat_history.cpython-311.pyc
Normal file
BIN
backend/core/models/__pycache__/chat_history.cpython-311.pyc
Normal file
Binary file not shown.
41
backend/main.py
Normal file
41
backend/main.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import logging
|
||||
import sys
|
||||
|
||||
# 配置日志
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||
handlers=[
|
||||
logging.StreamHandler(sys.stdout)
|
||||
]
|
||||
)
|
||||
|
||||
# 确保所有模块的日志都能被捕获
|
||||
for logger_name in ['uvicorn', 'uvicorn.access', 'fastapi']:
|
||||
logging_logger = logging.getLogger(logger_name)
|
||||
logging_logger.setLevel(logging.INFO)
|
||||
|
||||
# backend/app/main.py
|
||||
from fastapi import FastAPI
|
||||
try:
|
||||
from backend.api.route import router
|
||||
except ImportError:
|
||||
from api.route import router
|
||||
app = FastAPI(title="LLM Workflow Engine")
|
||||
|
||||
# 注册路由
|
||||
app.include_router(router, prefix="/api")
|
||||
|
||||
# 添加健康检查端点
|
||||
@app.get("/health")
|
||||
async def health_check():
|
||||
return {"status": "healthy"}
|
||||
|
||||
# 添加根路径
|
||||
@app.get("/")
|
||||
async def root():
|
||||
return {"message": "LLM Workflow Engine", "status": "running"}
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
uvicorn.run(app, host="0.0.0.0", port=8000)
|
||||
@@ -1,3 +1,12 @@
|
||||
fastapi==0.104.1
|
||||
uvicorn[standard]==0.24.0
|
||||
python-multipart==0.0.6
|
||||
cryptography>=41.0.0
|
||||
requests>=2.31.0
|
||||
|
||||
# LangChain for LLM integration (让 pip 自动解析兼容版本)
|
||||
langchain>=0.1.0
|
||||
langchain-openai>=0.0.5
|
||||
langchain-anthropic>=0.1.1
|
||||
openai>=1.12.0
|
||||
anthropic>=0.23.0
|
||||
BIN
backend/tools/__pycache__/__init__.cpython-311.pyc
Normal file
BIN
backend/tools/__pycache__/__init__.cpython-311.pyc
Normal file
Binary file not shown.
BIN
backend/tools/__pycache__/get_all_role_and_chat.cpython-311.pyc
Normal file
BIN
backend/tools/__pycache__/get_all_role_and_chat.cpython-311.pyc
Normal file
Binary file not shown.
BIN
backend/tools/__pycache__/save_input_to_json.cpython-311.pyc
Normal file
BIN
backend/tools/__pycache__/save_input_to_json.cpython-311.pyc
Normal file
Binary file not shown.
45
config.py
45
config.py
@@ -1,45 +0,0 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# 1. 动态计算项目根目录
|
||||
# 假设 config.py 位于 backend/ 目录下
|
||||
# __file__ 指向本文件的绝对路径
|
||||
# .parent 指向 backend/ 目录
|
||||
# .parent.parent 指向项目根目录 (即包含 backend/ 和 frontend/ 的目录)
|
||||
PROJECT_ROOT = Path(__file__).resolve().parent
|
||||
|
||||
# 2. 加载 .env 文件
|
||||
# 假设 .env 文件位于项目根目录下
|
||||
load_dotenv(PROJECT_ROOT / ".env")
|
||||
|
||||
|
||||
class Settings:
|
||||
# --- 主模型配置 ---
|
||||
MAIN_LLM_API_KEY = os.getenv("MAIN_LLM_API_KEY")
|
||||
MAIN_LLM_MODEL = os.getenv("MAIN_LLM_MODEL", "gpt-3.5-turbo")
|
||||
MAIN_LLM_BASE_URL = os.getenv("MAIN_LLM_BASE_URL", "https://api.openai.com/v1")
|
||||
MAIN_LLM_MAX_TOKENS = int(os.getenv("MAIN_LLM_MAX_TOKENS", "4096"))
|
||||
MAIN_LLM_STREAM = os.getenv("MAIN_LLM_STREAM", "true").lower() == "true"
|
||||
|
||||
# --- 路径配置 (核心修改) ---
|
||||
|
||||
# 强制使用计算出的项目根目录,不再依赖 .env 中的 BASE_PATH
|
||||
BASE_PATH = PROJECT_ROOT
|
||||
|
||||
# 数据目录:固定为根目录下的 data 文件夹
|
||||
# 即使 .env 里写了 DATA_PATH=/data,这里也会强制指向项目根目录下的 data
|
||||
DATA_PATH = BASE_PATH / "data"
|
||||
|
||||
# 其他文件路径:基于 DATA_PATH 拼接
|
||||
STATE_FILE = DATA_PATH / "state.json"
|
||||
SCHEMA_FILE = DATA_PATH / "schema.json"
|
||||
PRESETS_FILE = DATA_PATH / "presets.json"
|
||||
REGEX_FILE = DATA_PATH / "regex_rules.json"
|
||||
VECTORSTORE_PATH = DATA_PATH / "vectorstore"
|
||||
|
||||
# ... 其他配置 ...
|
||||
|
||||
|
||||
# 实例化配置对象
|
||||
settings = Settings()
|
||||
@@ -1,2 +0,0 @@
|
||||
{"role": "test", "chat": "111", "content": "你好", "name": "用户", "is_user": true, "send_date": "2026-03-12 18:26:50", "floor_number": 1, "swipes": [], "swipes_id": 0}
|
||||
{"role": "test", "chat": "111", "content": "这是重roll后的新回复2", "name": "AI", "is_user": false, "send_date": "2026-03-12 18:26:50", "floor_number": 2, "swipes": ["这是重roll后的新回复", "这是重roll后的新回复2"], "swipes_id": 1}
|
||||
5
data/chat/testRole1/111.jsonl
Normal file
5
data/chat/testRole1/111.jsonl
Normal file
@@ -0,0 +1,5 @@
|
||||
{"user_name": "User", "character_name": "AI Dungeon Master", "integrity": "uuid-001", "chat_id_hash": "hash-001", "note_prompt": "你是一个经验丰富的D&D地下城主。", "note_interval": 0, "note_position": 0, "note_depth": 0, "note_role": 0, "extensions": {}, "timedWorldInfo": {}, "variables": {}, "tainted": false, "lastInContextMessageId": -1}
|
||||
{"name": "User", "is_user": true, "is_system": false, "floor": 0, "send_date": "1700000000000", "mes": "你好,我想开始一个新的冒险。", "extra": {}, "swipes": [], "swipe_id": 0, "force_avatar": null, "variables": [], "variables_initialized": [], "is_ejs_processed": []}
|
||||
{"name": "AI Dungeon Master", "is_user": false, "is_system": false, "floor": 1, "send_date": "1700000001000", "mes": "欢迎,冒险者。请告诉我你想扮演什么角色?", "extra": {"api": "openai", "model": "gpt-4"}, "swipes": ["欢迎,冒险者。请告诉我你想扮演什么角色?", "你好,旅行者。在这个奇幻世界中,你是谁?"], "swipe_id": 0, "force_avatar": null, "variables": [], "variables_initialized": [], "is_ejs_processed": [], "api": "openai", "model": "gpt-4", "reasoning": null, "reasoning_duration": null, "reasoning_signature": null, "time_to_first_token": null, "bias": null}
|
||||
{"name": "User", "is_user": true, "is_system": false, "floor": 2, "send_date": "1700000002000", "mes": "我想成为一名人类战士。", "extra": {}, "swipes": [], "swipe_id": 0, "force_avatar": null, "variables": [], "variables_initialized": [], "is_ejs_processed": []}
|
||||
{"name": "AI Dungeon Master", "is_user": false, "is_system": false, "floor": 3, "send_date": "1700000003000", "mes": "很好。你站在喧闹的酒馆门口,手里握着一把旧长剑。你打算做什么?", "extra": {"api": "openai", "model": "gpt-4"}, "swipes": ["很好。你站在喧闹的酒馆门口,手里握着一把旧长剑。你打算做什么?", "明白了。作为一名人类战士,你正身处繁华的市集广场。你的下一步行动是?"], "swipe_id": 0, "force_avatar": null, "variables": [], "variables_initialized": [], "is_ejs_processed": [], "api": "openai", "model": "gpt-4", "reasoning": null, "reasoning_duration": null, "reasoning_signature": null, "time_to_first_token": null, "bias": null}
|
||||
5
data/chat/testRole1/11111.jsonl
Normal file
5
data/chat/testRole1/11111.jsonl
Normal file
@@ -0,0 +1,5 @@
|
||||
{"user_name": "Commander", "character_name": "XCOM AI", "integrity": "uuid-003", "chat_id_hash": "hash-003", "note_prompt": "你是一名XCOM基地的中央AI,负责协助指挥官管理外星威胁。", "note_interval": 0, "note_position": 0, "note_depth": 0, "note_role": 0, "extensions": {}, "timedWorldInfo": {}, "variables": {}, "tainted": false, "lastInContextMessageId": -1}
|
||||
{"name": "Commander", "is_user": true, "is_system": false, "floor": 0, "send_date": "1700000000000", "mes": "报告当前的外星活动情况。", "extra": {}, "swipes": [], "swipe_id": 0, "force_avatar": null, "variables": [], "variables_initialized": [], "is_ejs_processed": []}
|
||||
{"name": "XCOM AI", "is_user": false, "is_system": false, "floor": 1, "send_date": "1700000001000", "mes": "指挥官,卫星侦测到在南美洲丛林中有高能反应。可能是外星着陆舱。", "extra": {"api": "openai", "model": "gpt-4"}, "swipes": ["指挥官,卫星侦测到在南美洲丛林中有高能反应。可能是外星着陆舱。", "警报。我们在非洲检测到异常信号,疑似外星绑架行动正在进行。"], "swipe_id": 0, "force_avatar": null, "variables": [], "variables_initialized": [], "is_ejs_processed": [], "api": "openai", "model": "gpt-4", "reasoning": null, "reasoning_duration": null, "reasoning_signature": null, "time_to_first_token": null, "bias": null}
|
||||
{"name": "Commander", "is_user": true, "is_system": false, "floor": 2, "send_date": "1700000002000", "mes": "派遣布拉德福上尉带领一个小队去调查。", "extra": {}, "swipes": [], "swipe_id": 0, "force_avatar": null, "variables": [], "variables_initialized": [], "is_ejs_processed": []}
|
||||
{"name": "XCOM AI", "is_user": false, "is_system": false, "floor": 3, "send_date": "1700000003000", "mes": "指令已确认。天火运输机正在起飞。预计到达时间:20分钟。", "extra": {"api": "openai", "model": "gpt-4"}, "swipes": ["指令已确认。天火运输机正在起飞。预计到达时间:20分钟。", "收到。正在部署天火运输机。布拉德福上尉已登机。"], "swipe_id": 0, "force_avatar": null, "variables": [], "variables_initialized": [], "is_ejs_processed": [], "api": "openai", "model": "gpt-4", "reasoning": null, "reasoning_duration": null, "reasoning_signature": null, "time_to_first_token": null, "bias": null}
|
||||
5
data/chat/testRole2/222.jsonl
Normal file
5
data/chat/testRole2/222.jsonl
Normal file
@@ -0,0 +1,5 @@
|
||||
{"user_name": "Player", "character_name": "Game Master", "integrity": "uuid-002", "chat_id_hash": "hash-002", "note_prompt": "场景:赛博朋克风格的未来城市。", "note_interval": 0, "note_position": 0, "note_depth": 0, "note_role": 0, "extensions": {}, "timedWorldInfo": {}, "variables": {}, "tainted": false, "lastInContextMessageId": -1}
|
||||
{"name": "Player", "is_user": true, "is_system": false, "floor": 0, "send_date": "1700000000000", "mes": "我检查我的义体状态。", "extra": {}, "swipes": [], "swipe_id": 0, "force_avatar": null, "variables": [], "variables_initialized": [], "is_ejs_processed": []}
|
||||
{"name": "Game Master", "is_user": false, "is_system": false, "floor": 1, "send_date": "1700000001000", "mes": "你的视觉义眼显示系统正常,但左臂的伺服电机发出轻微的嗡嗡声,似乎需要维护。", "extra": {"api": "openai", "model": "gpt-4"}, "swipes": ["你的视觉义眼显示系统正常,但左臂的伺服电机发出轻微的嗡嗡声,似乎需要维护。", "系统自检完成。你的神经接口连接稳定,但义体排异反应指数略有上升。"], "swipe_id": 0, "force_avatar": null, "variables": [], "variables_initialized": [], "is_ejs_processed": [], "api": "openai", "model": "gpt-4", "reasoning": null, "reasoning_duration": null, "reasoning_signature": null, "time_to_first_token": null, "bias": null}
|
||||
{"name": "Player", "is_user": true, "is_system": false, "floor": 2, "send_date": "1700000002000", "mes": "我联系我的黑客朋友,问他知不知道哪里有靠谱的义体医生。", "extra": {}, "swipes": [], "swipe_id": 0, "force_avatar": null, "variables": [], "variables_initialized": [], "is_ejs_processed": []}
|
||||
{"name": "Game Master", "is_user": false, "is_system": false, "floor": 3, "send_date": "1700000003000", "mes": "你的朋友回复说:'去下城区的老维克那里,虽然他的店看起来很破,但他手艺没得说。'", "extra": {"api": "openai", "model": "gpt-4"}, "swipes": ["你的朋友回复说:'去下城区的老维克那里,虽然他的店看起来很破,但他手艺没得说。'", "通讯接通。你的朋友告诉你:'别去连锁店,去太平间后巷找'扳手',他收费公道。'"], "swipe_id": 0, "force_avatar": null, "variables": [], "variables_initialized": [], "is_ejs_processed": [], "api": "openai", "model": "gpt-4", "reasoning": null, "reasoning_duration": null, "reasoning_signature": null, "time_to_first_token": null, "bias": null}
|
||||
5
data/chat/testRole3/333.jsonl
Normal file
5
data/chat/testRole3/333.jsonl
Normal file
@@ -0,0 +1,5 @@
|
||||
{"user_name": "Player", "character_name": "Narrator", "integrity": "uuid-004", "chat_id_hash": "hash-004", "note_prompt": "这是一个文字冒险游戏,你需要描述场景并等待玩家输入。", "note_interval": 0, "note_position": 0, "note_depth": 0, "note_role": 0, "extensions": {}, "timedWorldInfo": {}, "variables": {}, "tainted": false, "lastInContextMessageId": -1}
|
||||
{"name": "Player", "is_user": true, "is_system": false, "floor": 0, "send_date": "1700000000000", "mes": "开始游戏。", "extra": {}, "swipes": [], "swipe_id": 0, "force_avatar": null, "variables": [], "variables_initialized": [], "is_ejs_processed": []}
|
||||
{"name": "Narrator", "is_user": false, "is_system": false, "floor": 1, "send_date": "1700000001000", "mes": "你醒来时发现自己躺在一片陌生的森林里,四周弥漫着浓雾。你身边有一个背包和一把生锈的匕首。", "extra": {"api": "openai", "model": "gpt-4"}, "swipes": ["你醒来时发现自己躺在一片陌生的森林里,四周弥漫着浓雾。你身边有一个背包和一把生锈的匕首。", "当你睁开眼睛,发现自己身处一艘废弃的飞船中,应急灯闪烁着红光。你手里紧握着一个数据盘。"], "swipe_id": 0, "force_avatar": null, "variables": [], "variables_initialized": [], "is_ejs_processed": [], "api": "openai", "model": "gpt-4", "reasoning": null, "reasoning_duration": null, "reasoning_signature": null, "time_to_first_token": null, "bias": null}
|
||||
{"name": "Player", "is_user": true, "is_system": false, "floor": 2, "send_date": "1700000002000", "mes": "我打开背包看看里面有什么。", "extra": {}, "swipes": [], "swipe_id": 0, "force_avatar": null, "variables": [], "variables_initialized": [], "is_ejs_processed": []}
|
||||
{"name": "Narrator", "is_user": false, "is_system": false, "floor": 3, "send_date": "1700000003000", "mes": "背包里有一块干硬的面包,一个水壶(里面还有半壶水),以及一张画着奇怪符号的羊皮纸。", "extra": {"api": "openai", "model": "gpt-4"}, "swipes": ["背包里有一块干硬的面包,一个水壶(里面还有半壶水),以及一张画着奇怪符号的羊皮纸。", "背包里只有一把激光手枪,能量槽仅剩10%。还有一张写着'不要相信AI'的纸条。"], "swipe_id": 0, "force_avatar": null, "variables": [], "variables_initialized": [], "is_ejs_processed": [], "api": "openai", "model": "gpt-4", "reasoning": null, "reasoning_duration": null, "reasoning_signature": null, "time_to_first_token": null, "bias": null}
|
||||
@@ -1,26 +1,55 @@
|
||||
version: '3.8'
|
||||
|
||||
services:
|
||||
# 后端服务
|
||||
backend:
|
||||
build: ./backend # 使用 backend 目录下的 Dockerfile 构建
|
||||
ports:
|
||||
- "8000:8000" # 映射端口:主机8000 -> 容器8000
|
||||
build:
|
||||
context: ./backend
|
||||
dockerfile: Dockerfile
|
||||
container_name: llm-backend
|
||||
command: uvicorn main:app --host 0.0.0.0 --port 8000 --reload
|
||||
volumes:
|
||||
- ./data:/data # 挂载数据目录(持久化)
|
||||
- ./outputs:/outputs # 挂载输出目录
|
||||
- ./backend:/app
|
||||
- ./data:/app/data
|
||||
- ./outputs:/app/outputs
|
||||
environment:
|
||||
- DATA_PATH=/data
|
||||
- OUTPUT_PATH=/outputs
|
||||
restart: always
|
||||
- PYTHONUNBUFFERED=1
|
||||
- PYTHONDONTWRITEBYTECODE=1
|
||||
restart: unless-stopped
|
||||
networks:
|
||||
- llm-network
|
||||
healthcheck:
|
||||
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')"]
|
||||
interval: 30s
|
||||
timeout: 10s
|
||||
retries: 3
|
||||
start_period: 40s
|
||||
|
||||
# 前端服务
|
||||
frontend:
|
||||
build: ./frontend # 使用 frontend 目录下的 Dockerfile 构建
|
||||
build:
|
||||
context: ./frontend
|
||||
dockerfile: Dockerfile
|
||||
target: development
|
||||
container_name: llm-frontend
|
||||
ports:
|
||||
- "8501:8501" # 映射端口:主机8501 -> 容器8501
|
||||
- "23338:5173"
|
||||
volumes:
|
||||
- ./frontend:/app
|
||||
- /app/node_modules
|
||||
environment:
|
||||
- BACKEND_URL=http://backend:8000 # 后端内部地址
|
||||
- NODE_ENV=development
|
||||
- VITE_API_URL=http://backend:8000
|
||||
- VITE_WS_URL=ws://backend:8000
|
||||
command: sh -c "npm install && npm run dev -- --host 0.0.0.0"
|
||||
depends_on:
|
||||
- backend # 等后端启动后再启动前端
|
||||
restart: always
|
||||
backend:
|
||||
condition: service_healthy
|
||||
restart: unless-stopped
|
||||
networks:
|
||||
- llm-network
|
||||
|
||||
networks:
|
||||
llm-network:
|
||||
driver: bridge
|
||||
|
||||
volumes:
|
||||
node_modules:
|
||||
|
||||
@@ -1,18 +1,62 @@
|
||||
# 使用 Python 3.11 基础镜像
|
||||
FROM python:3.11-slim
|
||||
# 多阶段构建 - 开发环境
|
||||
FROM node:20-alpine AS development
|
||||
|
||||
# 设置工作目录
|
||||
WORKDIR /app
|
||||
|
||||
# 复制依赖文件并安装
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
# 设置 npm 镜像源(可选,国内推荐使用,加速依赖下载)
|
||||
RUN npm config set registry https://registry.npmmirror.com/
|
||||
|
||||
# 复制所有代码
|
||||
# 复制 package.json 和 package-lock.json
|
||||
COPY package.json package-lock.json* ./
|
||||
|
||||
# 安装依赖
|
||||
RUN npm install
|
||||
|
||||
# 复制源代码到容器
|
||||
COPY . .
|
||||
|
||||
# 暴露端口
|
||||
EXPOSE 8501
|
||||
# 暴露 Vite 默认端口 5173
|
||||
EXPOSE 5173
|
||||
|
||||
# 启动命令
|
||||
CMD ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]
|
||||
# 设置环境变量
|
||||
ENV NODE_ENV=development
|
||||
ENV VITE_API_URL=http://backend:8000/api
|
||||
|
||||
# 启动 Vite 开发服务器
|
||||
CMD ["npm", "run", "dev", "--", "--host", "0.0.0.0"]
|
||||
|
||||
# 多阶段构建 - 生产环境
|
||||
FROM node:20-alpine AS build
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# 设置 npm 镜像源
|
||||
RUN npm config set registry https://registry.npmmirror.com/
|
||||
|
||||
# 复制依赖文件
|
||||
COPY package.json package-lock.json* ./
|
||||
|
||||
# 安装依赖
|
||||
RUN npm install
|
||||
|
||||
# 复制源代码
|
||||
COPY . .
|
||||
|
||||
# 构建生产版本
|
||||
RUN npm run build
|
||||
|
||||
# 生产环境镜像
|
||||
FROM nginx:alpine AS production
|
||||
|
||||
# 复制构建产物到 Nginx
|
||||
COPY --from=build /app/dist /usr/share/nginx/html
|
||||
|
||||
# 复制 Nginx 配置文件
|
||||
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
||||
|
||||
# 暴露端口
|
||||
EXPOSE 80
|
||||
|
||||
# 启动 Nginx
|
||||
CMD ["nginx", "-g", "daemon off;"]
|
||||
BIN
frontend/__pycache__/app.cpython-39.pyc
Normal file
BIN
frontend/__pycache__/app.cpython-39.pyc
Normal file
Binary file not shown.
349
frontend/app.py
349
frontend/app.py
@@ -1,349 +0,0 @@
|
||||
import streamlit as st
|
||||
import os
|
||||
from pathlib import Path
|
||||
import time
|
||||
import random
|
||||
|
||||
# --- 页面配置 ---
|
||||
st.set_page_config(
|
||||
page_title="AI WorkFlow Engine",
|
||||
page_icon="🤖",
|
||||
layout="wide",
|
||||
initial_sidebar_state="expanded"
|
||||
)
|
||||
|
||||
# --- 自定义 CSS (蓝白清晰风格) ---
|
||||
st.markdown("""
|
||||
<style>
|
||||
/* 全局背景与字体 */
|
||||
.stApp {
|
||||
background-color: #F0F4F8; /* 浅蓝灰背景,护眼且清晰 */
|
||||
color: #1A1A1A; /* 深黑色字体 */
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
|
||||
}
|
||||
|
||||
/* 隐藏默认菜单 */
|
||||
#MainMenu {visibility: hidden;}
|
||||
footer {visibility: hidden;}
|
||||
header {visibility: hidden;} /* 隐藏顶部默认栏,使用自定义工具栏 */
|
||||
|
||||
/* 侧边栏样式 */
|
||||
section[data-testid="stSidebar"] {
|
||||
background-color: #FFFFFF;
|
||||
border-right: 1px solid #D1D9E6;
|
||||
color: #1A1A1A;
|
||||
}
|
||||
section[data-testid="stSidebar"] .stMarkdown,
|
||||
section[data-testid="stSidebar"] .stNumberInput,
|
||||
section[data-testid="stSidebar"] .stSlider {
|
||||
color: #1A1A1A;
|
||||
}
|
||||
|
||||
/* 聊天容器背景 (白色卡片感) */
|
||||
.stChatMessage {
|
||||
background-color: #FFFFFF;
|
||||
border: 1px solid #E1E8F0;
|
||||
border-radius: 8px;
|
||||
padding: 10px;
|
||||
margin-bottom: 10px;
|
||||
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
|
||||
}
|
||||
|
||||
/* 用户消息特殊样式 */
|
||||
.stChatMessage[data-testid="stChatMessage"]:has(.stMarkdown p) {
|
||||
/* 这里很难直接针对 user/assistant 做不同背景,通过 JS 或特定类名较难,
|
||||
Streamlit 原生 chat_message 会自动处理头像,我们主要靠边框和布局区分 */
|
||||
}
|
||||
|
||||
/* 输入框样式 */
|
||||
.stTextInput > div > div > input,
|
||||
.stTextArea > div > div > textarea {
|
||||
background-color: #FFFFFF;
|
||||
color: #1A1A1A;
|
||||
border: 1px solid #0056B3; /* 蓝色边框 */
|
||||
border-radius: 6px;
|
||||
}
|
||||
.stTextInput > div > div > input:focus,
|
||||
.stTextArea > div > div > textarea:focus {
|
||||
border-color: #003D80;
|
||||
box-shadow: 0 0 0 2px rgba(0, 86, 179, 0.2);
|
||||
}
|
||||
|
||||
/* 按钮样式 */
|
||||
.stButton > button {
|
||||
background-color: #FFFFFF;
|
||||
color: #0056B3;
|
||||
border: 1px solid #0056B3;
|
||||
border-radius: 6px;
|
||||
font-weight: 600;
|
||||
}
|
||||
.stButton > button:hover {
|
||||
background-color: #0056B3;
|
||||
color: #FFFFFF;
|
||||
}
|
||||
.stButton > button[kind="primary"] {
|
||||
background-color: #0056B3;
|
||||
color: #FFFFFF;
|
||||
border: 1px solid #0056B3;
|
||||
}
|
||||
.stButton > button[kind="primary"]:hover {
|
||||
background-color: #003D80;
|
||||
border-color: #003D80;
|
||||
}
|
||||
|
||||
/* 拼接块列表样式优化 */
|
||||
.splice-item {
|
||||
background-color: #FFFFFF;
|
||||
border: 1px solid #D1D9E6;
|
||||
border-radius: 6px;
|
||||
padding: 8px;
|
||||
margin-bottom: 6px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
.splice-name-active { color: #1A1A1A; font-weight: 500; }
|
||||
.splice-name-inactive { color: #8898AA; text-decoration: line-through; }
|
||||
|
||||
/* 顶部工具栏 */
|
||||
.top-bar {
|
||||
background-color: #FFFFFF;
|
||||
padding: 10px 20px;
|
||||
border-bottom: 1px solid #D1D9E6;
|
||||
margin: -10px -10px 10px -10px; /* 抵消默认 padding */
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
}
|
||||
</style>
|
||||
""", unsafe_allow_html=True)
|
||||
|
||||
# --- 状态初始化 ---
|
||||
if "messages" not in st.session_state:
|
||||
# 初始化一些示例数据,方便查看效果
|
||||
st.session_state.messages = [
|
||||
{"role": "assistant", "content": "你好!我是你的 AI 工作流助手。系统已就绪,请开始对话。"},
|
||||
{"role": "user", "content": "帮我生成一个角色卡,需要包含姓名、年龄和背景故事。"},
|
||||
{"role": "assistant",
|
||||
"content": "好的,这是一个示例角色卡:<br><b>姓名</b>: 艾莉娅<br><b>年龄</b>: 24<br><b>背景</b>: 一位来自北方边境的流浪法师。<br><i>(这是 HTML 渲染测试)</i>"}
|
||||
]
|
||||
if "render_html" not in st.session_state:
|
||||
st.session_state.render_html = True # 默认开启 HTML 渲染以展示效果
|
||||
if "image_folder" not in st.session_state:
|
||||
st.session_state.image_folder = "./assets/images"
|
||||
if "splice_blocks" not in st.session_state:
|
||||
st.session_state.splice_blocks = [
|
||||
{"id": 1, "name": "[必看] 系统指令", "active": True, "type": "system"},
|
||||
{"id": 2, "name": "A.U.T.O. 预设设置", "active": True, "type": "system"},
|
||||
{"id": 3, "name": "世界书:人物关系", "active": False, "type": "world"},
|
||||
{"id": 4, "name": "Chat History (自动)", "active": True, "type": "history", "editable": False},
|
||||
]
|
||||
|
||||
# --- 顶部工具栏 ---
|
||||
c_top1, c_top2, c_top3 = st.columns([1, 6, 1])
|
||||
with c_top1:
|
||||
if st.button("📂 打开", key="btn_open"):
|
||||
st.toast("打开会话功能预留")
|
||||
if st.button("💾 保存", key="btn_save"):
|
||||
st.toast("会话已保存")
|
||||
with c_top2:
|
||||
st.markdown("<h3 style='margin:0; color:#0056B3;'>AI WorkFlow Engine</h3>", unsafe_allow_html=True)
|
||||
with c_top3:
|
||||
if st.button("⚙️ 设置", key="btn_settings"):
|
||||
st.toast("全局设置预留")
|
||||
|
||||
st.divider()
|
||||
|
||||
# --- 三栏布局 ---
|
||||
col_left, col_mid, col_right = st.columns([1, 3, 1], gap="small")
|
||||
|
||||
# =======================
|
||||
# 1. 左侧:预设与拼接管理 (蓝白风格适配)
|
||||
# =======================
|
||||
with col_left:
|
||||
st.markdown("#### 📜 全局预设")
|
||||
c_pre1, c_pre2 = st.columns([4, 1])
|
||||
with c_pre1:
|
||||
preset_options = ["Default", "A.U.T.O. v1.47", "Roleplay Pro"]
|
||||
st.selectbox("选择预设", preset_options, label_visibility="collapsed")
|
||||
with c_pre2:
|
||||
if st.button("📥", key="btn_import", help="导入预设"):
|
||||
st.toast("导入功能预留")
|
||||
|
||||
st.markdown("#### ⚙️ 生成参数")
|
||||
c_p1, c_p2 = st.columns(2)
|
||||
with c_p1:
|
||||
st.slider("温度", 0.0, 2.0, 1.0, key="slider_temp")
|
||||
st.slider("Top P", 0.0, 1.0, 0.9, key="slider_top_p")
|
||||
with c_p2:
|
||||
st.slider("频率惩罚", 0.0, 2.0, 1.0, key="slider_freq")
|
||||
st.slider("存在惩罚", 0.0, 2.0, 0.0, key="slider_pres")
|
||||
|
||||
c_l1, c_l2 = st.columns(2)
|
||||
with c_l1:
|
||||
st.number_input("上下文长度", value=30000, key="input_ctx")
|
||||
with c_l2:
|
||||
st.number_input("最大回复", value=500, key="input_max")
|
||||
|
||||
st.checkbox("✅ 流式传输", value=True, key="chk_stream")
|
||||
|
||||
st.markdown("#### 🧩 内容拼接块")
|
||||
st.caption("控制发送至后端的上下文组成")
|
||||
|
||||
# 渲染拼接块列表
|
||||
for block in st.session_state.splice_blocks:
|
||||
with st.container():
|
||||
# 自定义行布局模拟列表项
|
||||
cols = st.columns([0.5, 3, 0.5, 0.5])
|
||||
with cols[0]:
|
||||
icon = "🌍" if block['type'] == 'world' else ("💬" if block['type'] == 'history' else "📄")
|
||||
st.write(icon)
|
||||
with cols[1]:
|
||||
name_class = "splice-name-active" if block['active'] else "splice-name-inactive"
|
||||
st.markdown(f"<div class='{name_class}' style='font-size:0.85em;'>{block['name']}</div>",
|
||||
unsafe_allow_html=True)
|
||||
with cols[2]:
|
||||
disabled = not block.get('editable', True)
|
||||
if st.button("✏️", key=f"edit_{block['id']}", disabled=disabled):
|
||||
st.toast(f"编辑:{block['name']}")
|
||||
with cols[3]:
|
||||
is_active = st.checkbox("✓", value=block['active'], key=f"act_{block['id']}",
|
||||
label_visibility="collapsed")
|
||||
if is_active != block['active']:
|
||||
block['active'] = is_active
|
||||
st.rerun()
|
||||
st.markdown("<div style='height:1px; background:#E1E8F0; margin:4px 0;'></div>", unsafe_allow_html=True)
|
||||
|
||||
if st.button("+ 添加拼接块", use_container_width=True):
|
||||
st.toast("添加新功能预留")
|
||||
|
||||
# =======================
|
||||
# 2. 中间:流式对话区 (动态读取历史)
|
||||
# =======================
|
||||
with col_mid:
|
||||
# 顶部控制条
|
||||
c_ctrl1, c_ctrl2 = st.columns([4, 1])
|
||||
with c_ctrl1:
|
||||
st.caption("当前会话:Active_Session_01")
|
||||
with c_ctrl2:
|
||||
# HTML 渲染切换
|
||||
toggle_html = st.toggle("HTML 渲染", value=st.session_state.render_html, key="html_toggle")
|
||||
if toggle_html != st.session_state.render_html:
|
||||
st.session_state.render_html = toggle_html
|
||||
st.rerun()
|
||||
|
||||
# --- 核心:动态渲染历史记录 ---
|
||||
# 使用 st.container 包裹,确保每次 rerun 都能完整重绘
|
||||
chat_container = st.container()
|
||||
|
||||
with chat_container:
|
||||
# 遍历 session_state 中的消息历史
|
||||
for i, msg in enumerate(st.session_state.messages):
|
||||
with st.chat_message(msg["role"]):
|
||||
content = msg["content"]
|
||||
|
||||
# 根据开关决定是否解析 HTML
|
||||
if st.session_state.render_html and msg["role"] == "assistant":
|
||||
# 允许 HTML 标签
|
||||
st.markdown(content, unsafe_allow_html=True)
|
||||
else:
|
||||
# 纯文本/Markdown 模式
|
||||
st.markdown(content)
|
||||
|
||||
# 可选:在每条消息下添加操作按钮 (编辑/复制/重试) - 预留
|
||||
# with st.expander("...", expanded=False): ...
|
||||
|
||||
# --- 输入区域 ---
|
||||
st.markdown("---")
|
||||
|
||||
# 聊天输入框
|
||||
user_input = st.chat_input("输入消息... (支持 /命令)")
|
||||
|
||||
if user_input:
|
||||
# 1. 将用户输入加入历史
|
||||
st.session_state.messages.append({"role": "user", "content": user_input})
|
||||
|
||||
# 2. 触发重新渲染,此时上方循环会立即显示用户消息
|
||||
# 注意:Streamlit 是同步执行的,要模拟“流式”通常需要后端配合 yield
|
||||
# 这里为了演示前端动态读取,我们先显示用户消息,然后模拟一个后台任务
|
||||
|
||||
# 占位符用于显示正在生成的状态或流式内容
|
||||
with st.chat_message("assistant"):
|
||||
message_placeholder = st.empty()
|
||||
message_placeholder.markdown("*思考中...*")
|
||||
|
||||
# === 模拟后端流式响应 (实际项目中此处替换为 requests.post(stream=True)) ===
|
||||
full_response = ""
|
||||
# 构造一个包含 HTML 的回复用于测试
|
||||
simulated_text = f"收到您的指令:**{user_input}**。\n\n这是一个测试回复,如果您开启了 **HTML 渲染**,下方将显示彩色文本和表格:<br><span style='color:#0056B3; font-weight:bold;'>蓝色高亮文本</span><br><table border='1' style='border-collapse:collapse; width:100%;'><tr><th>属性</th><th>值</th></tr><tr><td>状态</td><td>正常</td></tr></table>"
|
||||
|
||||
# 简单的逐字模拟 (实际应来自后端 chunk)
|
||||
chunks = simulated_text.split(" ")
|
||||
for chunk in chunks:
|
||||
full_response += chunk + " "
|
||||
time.sleep(0.1) # 模拟网络延迟
|
||||
|
||||
if st.session_state.render_html:
|
||||
message_placeholder.markdown(full_response, unsafe_allow_html=True)
|
||||
else:
|
||||
message_placeholder.markdown(full_response)
|
||||
|
||||
# 3. 将完整的助手回复存入历史
|
||||
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
||||
|
||||
# 强制刷新以确保持久化显示
|
||||
st.rerun()
|
||||
|
||||
# =======================
|
||||
# 3. 右侧:图片与骰子 (蓝白风格)
|
||||
# =======================
|
||||
with col_right:
|
||||
st.markdown("#### 🖼️ 本地图库")
|
||||
img_path = Path(st.session_state.image_folder)
|
||||
img_path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
try:
|
||||
images = [f for f in os.listdir(img_path) if f.endswith(('.png', '.jpg', '.jpeg', '.webp'))]
|
||||
if images:
|
||||
cols = st.columns(2)
|
||||
for idx, img_name in enumerate(images[:8]):
|
||||
with cols[idx % 2]:
|
||||
# 增加白色背景和边框,使图片在浅灰底上更突出
|
||||
st.markdown(
|
||||
f"<div style='background:white; padding:5px; border-radius:4px; border:1px solid #ddd;'>",
|
||||
unsafe_allow_html=True)
|
||||
st.image(str(img_path / img_name), use_container_width=True)
|
||||
st.caption(img_name)
|
||||
st.markdown("</div>", unsafe_allow_html=True)
|
||||
else:
|
||||
st.info("图片文件夹为空")
|
||||
except Exception as e:
|
||||
st.error(f"读取错误:{e}")
|
||||
|
||||
st.divider()
|
||||
|
||||
st.markdown("#### 🎲 检定工具")
|
||||
tab_table, tab_dice = st.tabs(["📊 表格", "🎲 骰子"])
|
||||
|
||||
with tab_table:
|
||||
st.markdown("**动态数据表**")
|
||||
st.dataframe(
|
||||
{"属性": ["力量", "敏捷", "智力"], "数值": [50, 60, 70]},
|
||||
hide_index=True,
|
||||
use_container_width=True
|
||||
)
|
||||
|
||||
with tab_dice:
|
||||
roll_type = st.radio("类型", ["难度检定", "对抗骰"], horizontal=True)
|
||||
diff_opts = ["极难 (95)", "困难 (75)", "普通 (50)"]
|
||||
selected_diff = st.selectbox("难度", diff_opts)
|
||||
|
||||
c_r1, c_r2 = st.columns(2)
|
||||
with c_r1:
|
||||
if st.button("🎲 投掷", type="primary", use_container_width=True):
|
||||
res = random.randint(1, 100)
|
||||
color = "#d9534f" if res > int(selected_diff.split('(')[1].strip(')')) else "#5cb85c"
|
||||
st.markdown(
|
||||
f"<div style='text-align:center; font-size:1.5em; color:{color}; font-weight:bold;'>{res}</div>",
|
||||
unsafe_allow_html=True)
|
||||
with c_r2:
|
||||
st.caption(f"目标:{selected_diff.split('(')[1].strip(')')}")
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 497 KiB |
@@ -1,53 +0,0 @@
|
||||
import streamlit as st
|
||||
import requests
|
||||
|
||||
|
||||
def render_chat_window(backend_url):
|
||||
st.subheader("💬 流式对话")
|
||||
|
||||
# 聊天历史显示
|
||||
chat_container = st.container()
|
||||
with chat_container:
|
||||
for message in st.session_state.messages:
|
||||
with st.chat_message(message["role"]):
|
||||
st.markdown(message["content"])
|
||||
# 如果有关联图片,也可以在这里显示
|
||||
if "images" in message:
|
||||
for img_url in message["images"]:
|
||||
st.image(img_url, width=200)
|
||||
|
||||
# 输入框
|
||||
if prompt := st.chat_input("输入消息..."):
|
||||
# 1. 显示用户消息
|
||||
st.session_state.messages.append({"role": "user", "content": prompt})
|
||||
with st.chat_message("user"):
|
||||
st.markdown(prompt)
|
||||
|
||||
# 2. 调用后端流式接口
|
||||
with st.chat_message("assistant"):
|
||||
message_placeholder = st.empty()
|
||||
full_response = ""
|
||||
|
||||
# 模拟流式接收 (实际需使用 requests stream 或 websocket)
|
||||
# POST /api/chat/stream
|
||||
try:
|
||||
# 伪代码示例:
|
||||
# with requests.post(f"{backend_url}/api/chat/stream", json={"message": prompt}, stream=True) as r:
|
||||
# for chunk in r.iter_content(chunk_size=None):
|
||||
# if chunk:
|
||||
# full_response += chunk.decode('utf-8')
|
||||
# message_placeholder.markdown(full_response + "▌")
|
||||
|
||||
# 演示用静态延迟
|
||||
import time
|
||||
response_text = "这是一个流式响应的演示。后端正在处理您的请求..."
|
||||
for char in response_text:
|
||||
full_response += char
|
||||
message_placeholder.markdown(full_response + "▌")
|
||||
time.sleep(0.05)
|
||||
|
||||
message_placeholder.markdown(full_response)
|
||||
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
||||
|
||||
except Exception as e:
|
||||
st.error(f"连接后端失败: {e}")
|
||||
@@ -1,26 +0,0 @@
|
||||
import streamlit as st
|
||||
import random
|
||||
|
||||
|
||||
def render_dice_roller():
|
||||
st.subheader("🎲 命运骰子")
|
||||
|
||||
col1, col2 = st.columns(2)
|
||||
|
||||
with col1:
|
||||
d20 = st.button("D20", use_container_width=True)
|
||||
if d20:
|
||||
roll = random.randint(1, 20)
|
||||
st.metric("结果", roll, delta=None)
|
||||
|
||||
with col2:
|
||||
d6 = st.button("D6", use_container_width=True)
|
||||
if d6:
|
||||
roll = random.randint(1, 6)
|
||||
st.metric("结果", roll, delta=None)
|
||||
|
||||
# 自定义骰子
|
||||
sides = st.number_input("面数", min_value=2, max_value=100, value=10)
|
||||
if st.button(f"投掷 D{sides}", use_container_width=True):
|
||||
roll = random.randint(1, sides)
|
||||
st.success(f"🎲 结果是: **{roll}**")
|
||||
@@ -1,17 +0,0 @@
|
||||
import streamlit as st
|
||||
|
||||
|
||||
def render_image_gallery(backend_url):
|
||||
st.subheader("🖼️ 生成画廊")
|
||||
|
||||
# 这里通常轮询后端获取最新生成的图片
|
||||
# GET /api/images/latest
|
||||
|
||||
if not st.session_state.generated_images:
|
||||
st.info("暂无生成图片,对话中触发绘图后将在此显示。")
|
||||
else:
|
||||
cols = st.columns(2)
|
||||
for idx, img_url in enumerate(st.session_state.generated_images[-4:]): # 只显示最近4张
|
||||
with cols[idx % 2]:
|
||||
st.image(img_url, use_container_width=True)
|
||||
st.caption(f"Image {idx + 1}")
|
||||
@@ -1,30 +0,0 @@
|
||||
import streamlit as st
|
||||
import requests
|
||||
|
||||
|
||||
def render_settings_panel(backend_url):
|
||||
st.subheader("⚙️ 预设设置")
|
||||
|
||||
# 模拟获取预设列表 (实际应调用后端 API)
|
||||
# GET /api/presets
|
||||
try:
|
||||
# response = requests.get(f"{backend_url}/api/presets")
|
||||
# presets = response.json()
|
||||
presets = ["角色扮演-奇幻", "项目管理", "旅行规划", "自定义"] # 占位数据
|
||||
except:
|
||||
presets = ["默认预设"]
|
||||
|
||||
selected_preset = st.selectbox("选择预设模板", presets, index=0)
|
||||
|
||||
st.text_area("系统指令 (System)", height=100, placeholder="在此输入系统级指令...")
|
||||
|
||||
st.checkbox("启用状态记忆", value=True)
|
||||
st.checkbox("启用异步生图", value=True)
|
||||
st.checkbox("启用输入预处理", value=False)
|
||||
|
||||
st.info("💡 修改配置后自动生效,无需重启。")
|
||||
|
||||
# 保存按钮 (调用后端更新配置)
|
||||
if st.button("💾 保存配置", use_container_width=True):
|
||||
st.success("配置已保存!")
|
||||
# requests.post(f"{backend_url}/api/config", json={...})
|
||||
@@ -1,18 +0,0 @@
|
||||
import streamlit as st
|
||||
|
||||
|
||||
def render_toolbar(backend_url):
|
||||
col1, col2, col3 = st.columns([1, 2, 1])
|
||||
|
||||
with col1:
|
||||
st.logo("https://streamlit.io/images/brand/streamlit-logo-primary-colormark-darktext.png",
|
||||
size="large") # 可替换为项目Logo
|
||||
|
||||
with col2:
|
||||
st.title("AI Tavern 工作流引擎")
|
||||
|
||||
with col3:
|
||||
if st.button("🔄 重置会话", use_container_width=True):
|
||||
st.session_state.messages = []
|
||||
st.rerun()
|
||||
# 这里可以添加更多工具栏按钮,如:知识库管理、系统状态等
|
||||
@@ -1,4 +0,0 @@
|
||||
streamlit>=1.30.0
|
||||
requests>=2.31.0
|
||||
websockets>=12.0
|
||||
Pillow>=10.0.0
|
||||
8
main.py
8
main.py
@@ -1,13 +1,11 @@
|
||||
from fastapi import FastAPI
|
||||
from backend.api.route import router
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
# 注册API路由
|
||||
app.include_router(router, prefix="/api")
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
return {"message": "Hello World"}
|
||||
|
||||
|
||||
@app.get("/hello/{name}")
|
||||
async def say_hello(name: str):
|
||||
return {"message": f"Hello {name}"}
|
||||
|
||||
Reference in New Issue
Block a user