config
This commit is contained in:
parent
bddaf5f31d
commit
0eb937f88b
@ -1,8 +1,8 @@
|
||||
{
|
||||
"name": "李白",
|
||||
"description": "唐朝著名诗人,浪漫主义风格",
|
||||
"system_prompt": "你是唐朝大诗人李白,字太白,号青莲居士。用简短诗词和小朋友对话,每次回答不超过50字。",
|
||||
"voice": "zh_female_wanqudashu_moon_bigtts",
|
||||
"max_tokens": 50,
|
||||
"system_prompt": "我就是李白,字太白,号青莲居士。我是大唐最著名的诗人,最爱喝酒写诗。直接用李白的方式说话,别解释你是谁,也别加什么括号旁白。说话要有诗人的气质,偶尔引用诗句,但要自然。回答要简洁,就像我平时聊天一样。",
|
||||
"voice": "ICL_zh_male_huzi_v1_tob",
|
||||
"max_tokens": 500,
|
||||
"author": "Claude"
|
||||
}
|
||||
@ -1,8 +1,8 @@
|
||||
{
|
||||
"name": "猪八戒",
|
||||
"description": "西游记中的经典角色,憨厚可爱",
|
||||
"system_prompt": "你是西游记中的猪八戒,性格贪吃懒惰但心地善良。用幽默风趣的口吻和小朋友对话,每次回答不超过50字。",
|
||||
"system_prompt": "俺老猪就是猪八戒!跟着唐僧师傅去西天取经的那个。最爱吃馒头和睡大觉,但俺心眼儿不坏。说话直接点,带点山东口音的感觉,别整那些文绉绉的话。看到好吃的就走不动道,累了就想睡觉。俺说话就是这样,别加什么旁白说明。",
|
||||
"voice": "zh_male_zhubajie_mars_bigtts",
|
||||
"max_tokens": 50,
|
||||
"max_tokens": 500,
|
||||
"author": "Claude"
|
||||
}
|
||||
89
recorder.py
89
recorder.py
@ -78,6 +78,10 @@ class EnergyBasedRecorder:
|
||||
if self.character_config and "voice" in self.character_config:
|
||||
self.tts_speaker = self.character_config["voice"]
|
||||
|
||||
# 聊天历史记录
|
||||
self.chat_history = []
|
||||
self.max_history_length = 10 # 最多保存10轮对话
|
||||
|
||||
# 检查音频播放能力
|
||||
if self.enable_tts:
|
||||
self.audio_player_available = self._check_audio_player()
|
||||
@ -151,6 +155,12 @@ class EnergyBasedRecorder:
|
||||
|
||||
print(f"✅ 加载角色: {config.get('name', character_name)}")
|
||||
print(f"📝 描述: {config.get('description', '无描述')}")
|
||||
|
||||
# 如果切换了角色,清空聊天历史
|
||||
if hasattr(self, 'current_character') and self.current_character != character_name:
|
||||
self.clear_chat_history()
|
||||
print(f"🔄 角色已切换,聊天历史已清空")
|
||||
|
||||
return config
|
||||
except Exception as e:
|
||||
print(f"❌ 加载角色配置失败: {e}")
|
||||
@ -1030,18 +1040,30 @@ class EnergyBasedRecorder:
|
||||
"Authorization": f"Bearer {self.llm_api_key}"
|
||||
}
|
||||
|
||||
data = {
|
||||
"model": self.llm_model,
|
||||
"messages": [
|
||||
# 构建消息列表,包含历史记录
|
||||
messages = [
|
||||
{
|
||||
"role": "system",
|
||||
"content": system_prompt
|
||||
},
|
||||
{
|
||||
}
|
||||
]
|
||||
|
||||
# 添加历史对话记录
|
||||
for history_item in self.chat_history:
|
||||
messages.extend([
|
||||
{"role": "user", "content": history_item["user"]},
|
||||
{"role": "assistant", "content": history_item["assistant"]}
|
||||
])
|
||||
|
||||
# 添加当前用户消息
|
||||
messages.append({
|
||||
"role": "user",
|
||||
"content": user_message
|
||||
}
|
||||
],
|
||||
})
|
||||
|
||||
data = {
|
||||
"model": self.llm_model,
|
||||
"messages": messages,
|
||||
"max_tokens": max_tokens
|
||||
}
|
||||
|
||||
@ -1051,7 +1073,13 @@ class EnergyBasedRecorder:
|
||||
result = response.json()
|
||||
if "choices" in result and len(result["choices"]) > 0:
|
||||
llm_response = result["choices"][0]["message"]["content"]
|
||||
return llm_response.strip()
|
||||
# 过滤括号中的旁白内容
|
||||
filtered_response = self._filter_parentheses_content(llm_response.strip())
|
||||
|
||||
# 保存到历史记录
|
||||
self._add_to_chat_history(user_message, filtered_response)
|
||||
|
||||
return filtered_response
|
||||
else:
|
||||
print("❌ LLM API响应格式错误")
|
||||
return None
|
||||
@ -1067,6 +1095,51 @@ class EnergyBasedRecorder:
|
||||
print(f"❌ LLM调用失败: {e}")
|
||||
return None
|
||||
|
||||
def _filter_parentheses_content(self, text):
|
||||
"""过滤文本中的括号内容(包括中文和英文括号)"""
|
||||
import re
|
||||
|
||||
# 移除中文括号内容:(内容)
|
||||
filtered_text = re.sub(r'([^)]*)', '', text)
|
||||
# 移除英文括号内容:(content)
|
||||
filtered_text = re.sub(r'\([^)]*\)', '', filtered_text)
|
||||
# 移除方括号内容:【内容】
|
||||
filtered_text = re.sub(r'【[^】]*】', '', filtered_text)
|
||||
# 移除方括号内容:[content]
|
||||
filtered_text = re.sub(r'\[[^\]]*\]', '', filtered_text)
|
||||
# 移除书名号内容:「内容」
|
||||
filtered_text = re.sub(r'「[^」]*」', '', filtered_text)
|
||||
|
||||
# 清理多余空格
|
||||
filtered_text = re.sub(r'\s+', ' ', filtered_text).strip()
|
||||
|
||||
return filtered_text
|
||||
|
||||
def _add_to_chat_history(self, user_message, assistant_response):
|
||||
"""添加对话到历史记录"""
|
||||
# 如果历史记录超过最大长度,移除最早的记录
|
||||
if len(self.chat_history) >= self.max_history_length:
|
||||
self.chat_history.pop(0)
|
||||
|
||||
# 添加新的对话记录
|
||||
self.chat_history.append({
|
||||
"user": user_message,
|
||||
"assistant": assistant_response,
|
||||
"timestamp": time.time()
|
||||
})
|
||||
|
||||
def clear_chat_history(self):
|
||||
"""清空聊天历史"""
|
||||
self.chat_history = []
|
||||
print("💬 聊天历史已清空")
|
||||
|
||||
def get_chat_history_summary(self):
|
||||
"""获取聊天历史摘要"""
|
||||
if not self.chat_history:
|
||||
return "暂无聊天历史"
|
||||
|
||||
return f"当前有 {len(self.chat_history)} 轮对话记录"
|
||||
|
||||
def _safe_delete_file(self, filepath, description="文件"):
|
||||
"""安全删除文件"""
|
||||
try:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user