smalleyes/Bot-memory
收藏Hugging Face2023-06-19 更新2024-03-04 收录
下载链接:
https://hf-mirror.com/datasets/smalleyes/Bot-memory
下载链接
链接失效反馈官方服务:
资源简介:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <unistd.h>
using namespace std;
const string CHAT_HISTORY_FILE = "your-bot-model_chat_history.txt";
vector<string> load_chat_history() {
ifstream f(CHAT_HISTORY_FILE);
vector<string> chat_history;
string line;
while (getline(f, line)) {
chat_history.push_back(line);
}
return chat_history;
}
void save_chat_history(const vector<string>& chat_history) {
ofstream f(CHAT_HISTORY_FILE);
for (const string& line : chat_history) {
f << line << endl;
}
}
string call_your_bot_model(const string& prompt) {
string command = "./your-bot-model/chat " + prompt;
int status = system(command.c_str());
if (status != 0) {
cerr << "Error calling your bot model: " << status << endl;
return "";
}
string response;
ifstream f("/dev/stdout");
getline(f, response);
return response;
}
int main() {
vector<string> chat_history = load_chat_history();
while (true) {
cout << "You: ";
string user_input;
cin >> user_input;
if (user_input == "quit") {
break;
}
chat_history.push_back("You: " + user_input);
string prompt = " ";
for (int i = chat_history.size() - 1000; i < chat_history.size(); i++) {
prompt += chat_history[i] + " ";
}
string ai_response = call_your_bot_model(prompt);
cout << "Your bot model: " << ai_response << endl;
chat_history.push_back("Your bot model: " + ai_response);
save_chat_history(chat_history);
}
return 0;
}
以下为一段C++编写的交互式对话客户端程序代码,用于管理聊天历史记录并调用自定义bot模型完成对话:
### 引入依赖头文件
cpp
#include <iostream> // 标准输入输出流库
#include <fstream> // 文件操作流库
#include <string> // 字符串处理库
#include <vector> // 动态数组容器库
#include <unistd.h> // Unix系统调用标准库
采用标准命名空间:
cpp
using namespace std;
定义常量字符串变量`CHAT_HISTORY_FILE`,其值为`"your-bot-model_chat_history.txt"`,用于指定聊天历史记录的存储文件路径。
### 加载聊天历史记录函数`load_chat_history`
cpp
vector<string> load_chat_history() {
ifstream f(CHAT_HISTORY_FILE);
vector<string> chat_history;
string line;
while (getline(f, line)) {
chat_history.push_back(line);
}
return chat_history;
}
该函数通过输入文件流(ifstream)打开指定的聊天历史文件,逐行读取文件内容并存储至动态数组容器中,最终返回包含所有对话行的容器。
### 保存聊天历史记录函数`save_chat_history`
cpp
void save_chat_history(const vector<string>& chat_history) {
ofstream f(CHAT_HISTORY_FILE);
for (const string& line : chat_history) {
f << line << endl;
}
}
该函数接收常量引用类型的对话历史容器作为参数,通过输出文件流(ofstream)打开指定存储文件,将容器内的所有对话行逐行写入文件并自动换行。
### 调用自定义bot模型函数`call_your_bot_model`
cpp
string call_your_bot_model(const string& prompt) {
string command = "./your-bot-model/chat " + prompt;
int status = system(command.c_str());
if (status != 0) {
cerr << "Error calling your bot model: " << status << endl;
return "";
}
string response;
ifstream f("/dev/stdout");
getline(f, response);
return response;
}
该函数接收提示词`prompt`作为输入参数,拼接得到调用外部bot模型的命令行字符串,通过系统调用(system)执行该命令。若命令执行失败(返回状态非0),则向标准错误流(cerr)输出错误信息并返回空字符串;若执行成功,则读取标准输出的首行内容作为AI回复并返回。
### 主函数`main`
cpp
int main() {
vector<string> chat_history = load_chat_history();
while (true) {
cout << "You: ";
string user_input;
cin >> user_input;
if (user_input == "quit") {
break;
}
chat_history.push_back("You: " + user_input);
string prompt = " ";
for (int i = chat_history.size() - 1000; i < chat_history.size(); i++) {
prompt += chat_history[i] + " ";
}
string ai_response = call_your_bot_model(prompt);
cout << "Your bot model: " << ai_response << endl;
chat_history.push_back("Your bot model: " + ai_response);
save_chat_history(chat_history);
}
return 0;
}
主函数的具体执行流程如下:
1. 首先调用`load_chat_history()`加载已有的聊天历史记录;
2. 进入无限对话循环:
- 向标准输出流(cout)输出提示前缀`You: `,通过标准输入流(cin)读取用户输入的对话内容;
- 若用户输入为`quit`则终止循环;
- 将用户输入格式化为`You: [用户输入]`并添加至对话历史容器;
- 构造上下文提示词:取对话历史容器中最多最近1000条对话内容拼接为提示词(若对话总数不足1000则取全部内容);
- 调用`call_your_bot_model`函数获取AI回复,并向控制台输出格式为`Your bot model: [AI回复]`的内容;
- 将AI回复格式化为`Your bot model: [AI回复]`并添加至对话历史容器,随后调用`save_chat_history`函数保存更新后的对话历史。
提供机构:
smalleyes
原始信息汇总
数据集概述
数据集名称
- your-bot-model_chat_history.txt
数据集功能
- 加载和保存聊天历史记录
- 调用机器人模型进行对话
数据集操作
-
加载聊天历史
- 从文件
your-bot-model_chat_history.txt中读取聊天历史记录。 - 使用
ifstream打开文件,逐行读取并存储到vector<string>中。
- 从文件
-
保存聊天历史
- 将聊天历史记录写回文件
your-bot-model_chat_history.txt。 - 使用
ofstream打开文件,逐行写入聊天历史。
- 将聊天历史记录写回文件
-
调用机器人模型
- 根据用户输入构建命令,调用机器人模型进行响应。
- 使用
system函数执行命令,从标准输出读取机器人模型的响应。
-
主循环
- 持续接收用户输入,直到用户输入"quit"。
- 将用户输入和机器人模型的响应添加到聊天历史中,并保存。
数据集使用
- 用户通过命令行界面与机器人模型进行交互。
- 聊天历史限制在最近的1000条记录内。



