110 lines
2.9 KiB
Python
110 lines
2.9 KiB
Python
"""
|
||
为测试角色生成测试聊天记录
|
||
"""
|
||
import json
|
||
import uuid
|
||
from datetime import datetime
|
||
from pathlib import Path
|
||
|
||
def create_test_chat(character_name, first_mes, chat_name="默认聊天"):
|
||
"""
|
||
为角色创建测试聊天记录
|
||
|
||
Args:
|
||
character_name: 角色名称
|
||
first_mes: 开场白
|
||
chat_name: 聊天名称
|
||
"""
|
||
# 聊天文件路径
|
||
chat_dir = Path(f"data/characters/{character_name}/chats")
|
||
chat_dir.mkdir(parents=True, exist_ok=True)
|
||
chat_file = chat_dir / f"{chat_name}.jsonl"
|
||
|
||
# 如果文件已存在,跳过
|
||
if chat_file.exists():
|
||
print(f"跳过已存在的聊天: {character_name}/{chat_name}")
|
||
return
|
||
|
||
# 生成header
|
||
header = {
|
||
"user_name": "User",
|
||
"character_name": character_name,
|
||
"integrity": str(uuid.uuid4()),
|
||
"chat_id_hash": str(uuid.uuid4()),
|
||
"note_prompt": "",
|
||
"note_interval": 0,
|
||
"note_position": 0,
|
||
"note_depth": 0,
|
||
"note_role": 0,
|
||
"extensions": {},
|
||
"timedWorldInfo": {},
|
||
"variables": {},
|
||
"tainted": False,
|
||
"lastInContextMessageId": -1
|
||
}
|
||
|
||
# 生成AI开场白消息(floor 1)
|
||
ai_message = {
|
||
"name": character_name,
|
||
"is_user": False,
|
||
"is_system": False,
|
||
"floor": 1,
|
||
"send_date": str(int(datetime.now().timestamp() * 1000)),
|
||
"mes": first_mes,
|
||
"extra": {},
|
||
"swipes": [],
|
||
"swipe_id": 0
|
||
}
|
||
|
||
# 写入文件
|
||
with open(chat_file, 'w', encoding='utf-8') as f:
|
||
f.write(json.dumps(header, ensure_ascii=False) + '\n')
|
||
f.write(json.dumps(ai_message, ensure_ascii=False) + '\n')
|
||
|
||
print(f"✓ 已创建聊天: {character_name}/{chat_name}")
|
||
|
||
|
||
def main():
|
||
"""主函数"""
|
||
characters_dir = Path("data/characters")
|
||
|
||
if not characters_dir.exists():
|
||
print("错误: characters目录不存在")
|
||
return
|
||
|
||
print("开始为测试角色生成聊天记录...\n")
|
||
|
||
for char_folder in characters_dir.iterdir():
|
||
if not char_folder.is_dir():
|
||
continue
|
||
|
||
char_name = char_folder.name
|
||
char_json = char_folder / "character.json"
|
||
|
||
if not char_json.exists():
|
||
print(f"跳过 {char_name}: 没有character.json文件")
|
||
continue
|
||
|
||
try:
|
||
# 读取角色信息
|
||
with open(char_json, 'r', encoding='utf-8') as f:
|
||
char_data = json.load(f)
|
||
|
||
first_mes = char_data.get('first_mes', '')
|
||
|
||
if not first_mes:
|
||
print(f"跳过 {char_name}: 没有开场白")
|
||
continue
|
||
|
||
# 创建测试聊天
|
||
create_test_chat(char_name, first_mes)
|
||
|
||
except Exception as e:
|
||
print(f"处理 {char_name} 时出错: {e}")
|
||
|
||
print("\n完成!")
|
||
|
||
|
||
if __name__ == '__main__':
|
||
main()
|