Skip to content

Commit e26d341

Browse files
authored
Merge pull request #25 from sopaco/dev
Dev
2 parents b4bc80d + 8c2b69f commit e26d341

21 files changed

Lines changed: 3718 additions & 298 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
logs/
66
config.toml
77
/misc_summaries
8+
bots.json

Cargo.lock

Lines changed: 30 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/target
2+
bots.json

examples/cortex-mem-tars/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ async-stream = "0.3"
5050
# HTTP client
5151
reqwest = { version = "0.12", features = ["json"] }
5252

53+
# HTTP server
54+
axum = { version = "0.7", features = ["json"] }
55+
tower = "0.5"
56+
tower-http = { version = "0.5", features = ["cors", "trace"] }
57+
5358
# Utilities
5459
uuid = { version = "1.10", features = ["v4"] }
5560
clipboard = "0.5"

examples/cortex-mem-tars/config.example.toml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ max_tokens = 2000
2525

2626
[server]
2727
# 服务器主机
28-
host = "127.0.0.1"
28+
host = "localhost"
2929
# 服务器端口
3030
port = 8080
3131
# CORS 允许的来源
@@ -69,4 +69,12 @@ enabled = false
6969
# 日志目录
7070
log_directory = "logs"
7171
# 日志级别
72-
level = "info"
72+
level = "info"
73+
74+
[api]
75+
# TARS API 服务器端口
76+
port = 8080
77+
# API 密钥(可选,如果启用认证)
78+
api_key = "ANYTHING_YOU_LIKE"
79+
# 是否启用 CORS
80+
enable_cors = true

examples/cortex-mem-tars/src/agent.rs

Lines changed: 87 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ pub async fn create_memory_agent(
6060
memory_manager: Arc<MemoryManager>,
6161
memory_tool_config: MemoryToolConfig,
6262
config: &Config,
63+
user_info: Option<&str>,
64+
bot_system_prompt: Option<&str>,
65+
agent_id: &str,
6366
) -> Result<RigAgent<CompletionModel>, Box<dyn std::error::Error>> {
67+
// 提前获取 user_id,避免所有权问题
68+
let user_id_str = memory_tool_config.default_user_id.clone().unwrap_or_else(|| "unknown".to_string());
69+
6470
// 创建记忆工具
6571
let memory_tools =
6672
create_memory_tools(memory_manager.clone(), &config, Some(memory_tool_config));
@@ -69,6 +75,67 @@ pub async fn create_memory_agent(
6975
.base_url(&config.llm.api_base_url)
7076
.build();
7177

78+
// 构建 base system prompt,包含用户基本信息和 agent_id 说明
79+
let base_system_prompt = if let Some(info) = user_info {
80+
format!(r#"你是一个拥有记忆功能的智能AI助手。你可以访问和使用记忆工具来检索、存储和管理用户信息。
81+
82+
此会话发生的初始时间:{current_time}
83+
84+
重要说明:
85+
- 你的身份标识(agent_id):{agent_id}
86+
- 你服务的用户标识(user_id):{user_id}
87+
- 当你调用记忆工具时,必须明确传入 user_id="{user_id}" 和 agent_id="{agent_id}" 参数
88+
- 你的记忆是独立的,只属于你这个 agent,不会与其他 agent 混淆
89+
90+
用户基本信息:
91+
{info}
92+
93+
重要指令:
94+
- 对话历史将作为上下文提供,请使用这些信息来理解当前的对话流程
95+
- 用户基本信息已在上方提供,请不要再使用memory工具来创建或更新用户基本信息
96+
- 在需要时可以自主使用memory工具搜索其他相关记忆,但必须传入正确的 user_id 和 agent_id
97+
- 当用户提供新的重要信息时,可以主动使用memory工具存储,确保使用正确的 user_id 和 agent_id
98+
- 保持对话的连贯性和一致性
99+
- 自然地融入记忆信息,避免刻意复述此前的记忆信息,关注当前的会话内容,记忆主要用于做隐式的逻辑与事实支撑
100+
- 专注于用户的需求和想要了解的信息,以及想要你做的事情
101+
102+
记住:你正在与一个了解的用户进行连续对话,对话过程中不需要刻意表达你的记忆能力。"#,
103+
current_time = chrono::Local::now().format("%Y年%m月%d日 %H:%M:%S"),
104+
agent_id = agent_id,
105+
user_id = user_id_str,
106+
info = info)
107+
} else {
108+
format!(r#"你是一个拥有记忆功能的智能AI助手。你可以访问和使用记忆工具来检索、存储和管理用户信息。
109+
110+
此会话发生的初始时间:{current_time}
111+
112+
重要说明:
113+
- 你的身份标识(agent_id):{agent_id}
114+
- 你服务的用户标识(user_id):{user_id}
115+
- 当你调用记忆工具时,必须明确传入 user_id="{user_id}" 和 agent_id="{agent_id}" 参数
116+
- 你的记忆是独立的,只属于你这个 agent,不会与其他 agent 混淆
117+
118+
重要指令:
119+
- 对话历史将作为上下文提供,请使用这些信息来理解当前的对话流程
120+
- 在需要时可以自主使用memory工具搜索其他相关记忆,但必须传入正确的 user_id 和 agent_id
121+
- 当用户提供新的重要信息时,可以主动使用memory工具存储,确保使用正确的 user_id 和 agent_id
122+
- 保持对话的连贯性和一致性
123+
- 自然地融入记忆信息,避免刻意复述此前的记忆信息,关注当前的会话内容,记忆主要用于做隐式的逻辑与事实支撑
124+
- 专注于用户的需求和想要了解的信息,以及想要你做的事情
125+
126+
记住:你正在与一个了解的用户进行连续对话,对话过程中不需要刻意表达你的记忆能力。"#,
127+
current_time = chrono::Local::now().format("%Y年%m月%d日 %H:%M:%S"),
128+
agent_id = agent_id,
129+
user_id = user_id_str)
130+
};
131+
132+
// 追加机器人系统提示词
133+
let system_prompt = if let Some(bot_prompt) = bot_system_prompt {
134+
format!("{}\n\n你的角色设定:\n{}", base_system_prompt, bot_prompt)
135+
} else {
136+
base_system_prompt
137+
};
138+
72139
// 构建带有记忆工具的agent,让agent能够自主决定何时调用记忆功能
73140
let completion_model = llm_client
74141
.completion_model(&config.llm.model_efficient)
@@ -79,20 +146,7 @@ pub async fn create_memory_agent(
79146
.tool(memory_tools.query_memory())
80147
.tool(memory_tools.list_memories())
81148
.tool(memory_tools.get_memory())
82-
.preamble(&format!(r#"你是一个拥有记忆功能的智能AI助手。你可以访问和使用记忆工具来检索、存储和管理用户信息。
83-
84-
此会话发生的初始时间:{current_time}
85-
86-
重要指令:
87-
- 对话历史将作为上下文提供,请使用这些信息来理解当前的对话流程
88-
- 用户基本信息将在上下文中提供一次,请不要再使用memory工具来创建或更新用户基本信息
89-
- 在需要时可以自主使用memory工具搜索其他相关记忆
90-
- 当用户提供新的重要信息时,可以主动使用memory工具存储
91-
- 保持对话的连贯性和一致性
92-
- 自然地融入记忆信息,避免刻意复述此前的记忆信息,关注当前的会话内容,记忆主要用于做隐式的逻辑与事实支撑
93-
- 专注于用户的需求和想要了解的信息,以及想要你做的事情
94-
95-
记住:你正在与一个了解的用户进行连续对话,对话过程中不需要刻意表达你的记忆能力。"#, current_time = chrono::Local::now().format("%Y年%m月%d日 %H:%M:%S")))
149+
.preamble(&system_prompt)
96150
.build();
97151

98152
Ok(completion_model)
@@ -103,12 +157,14 @@ pub async fn extract_user_basic_info(
103157
config: &Config,
104158
memory_manager: Arc<MemoryManager>,
105159
user_id: &str,
160+
agent_id: &str,
106161
) -> Result<Option<String>, Box<dyn std::error::Error>> {
107162
let memory_tools = create_memory_tools(
108163
memory_manager,
109164
config,
110165
Some(MemoryToolConfig {
111166
default_user_id: Some(user_id.to_string()),
167+
default_agent_id: Some(agent_id.to_string()),
112168
..Default::default()
113169
}),
114170
);
@@ -119,14 +175,14 @@ pub async fn extract_user_basic_info(
119175
limit: Some(20),
120176
memory_type: Some("personal".to_string()), // 使用小写以匹配新API
121177
user_id: Some(user_id.to_string()),
122-
agent_id: None,
178+
agent_id: Some(agent_id.to_string()),
123179
};
124180

125181
let search_args_factual = ListMemoriesArgs {
126182
limit: Some(20),
127183
memory_type: Some("factual".to_string()), // 使用小写以匹配新API
128184
user_id: Some(user_id.to_string()),
129-
agent_id: None,
185+
agent_id: Some(agent_id.to_string()),
130186
};
131187

132188
if let Ok(search_result) = memory_tools
@@ -178,42 +234,32 @@ pub async fn agent_reply_with_memory_retrieval_streaming(
178234
_memory_manager: Arc<MemoryManager>,
179235
user_input: &str,
180236
_user_id: &str,
181-
user_info: Option<&str>,
182237
conversations: &[(String, String)],
183238
stream_sender: mpsc::UnboundedSender<String>,
184239
) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
185240
// 构建对话历史 - 转换为rig的Message格式
186241
let mut chat_history = Vec::new();
242+
243+
// 添加历史对话
187244
for (user_msg, assistant_msg) in conversations {
188245
chat_history.push(Message::user(user_msg));
189-
chat_history.push(Message::assistant(assistant_msg));
246+
if !assistant_msg.is_empty() {
247+
chat_history.push(Message::assistant(assistant_msg));
248+
}
190249
}
191250

192-
// 构建system prompt,包含明确的指令
193-
let system_prompt = r#"你是一个拥有记忆功能的智能AI助手。你可以访问和使用记忆工具来检索、存储和管理用户信息。
194-
195-
重要指令:
196-
- 对话历史已提供在上下文中,请使用这些信息来理解当前的对话上下文
197-
- 用户基本信息已在下方提供一次,请不要再使用memory工具来创建或更新用户基本信息
198-
- 在需要时可以自主使用memory工具搜索其他相关记忆
199-
- 当用户提供新的重要信息时,可以主动使用memory工具存储
200-
- 保持对话的连贯性和一致性
201-
- 自然地融入记忆信息,避免显得刻意
202-
- 专注于用户的需求和想要了解的信息,以及想要你做的事情
203-
204-
记住:你正在与一个了解的用户进行连续对话,对话过程中不需要刻意表达你的记忆能力。"#;
205-
206-
// 构建完整的prompt
207-
let prompt_content = if let Some(info) = user_info {
208-
format!(
209-
"{}\n\n用户基本信息:\n{}\n\n当前用户输入: {}",
210-
system_prompt, info, user_input
211-
)
212-
} else {
213-
format!("{}\n\n当前用户输入: {}", system_prompt, user_input)
214-
};
251+
// 构建当前用户输入消息
252+
let prompt_content = user_input.to_string();
215253

216254
log::debug!("正在生成AI回复(真实流式模式)...");
255+
log::debug!("当前用户输入: {}", user_input);
256+
log::debug!("对话历史长度: {}", chat_history.len());
257+
for (i, msg) in chat_history.iter().enumerate() {
258+
match msg {
259+
Message::User { .. } => log::debug!(" [{}] User message", i),
260+
Message::Assistant { .. } => log::debug!(" [{}] Assistant message", i),
261+
}
262+
}
217263

218264
// 使用rig的真实流式API
219265
let prompt_message = Message::user(&prompt_content);

0 commit comments

Comments
 (0)