|
31 | 31 | #pragma warning(disable: 4244 4267) // possible loss of data |
32 | 32 | #endif |
33 | 33 |
|
| 34 | +static const std::string CMD_READFILE = "/readfile"; |
| 35 | +static const std::string CMD_SAVE_SESS = "/savesess"; |
| 36 | +static const std::string CMD_LOAD_SESS = "/loadsess"; |
| 37 | + |
34 | 38 | static llama_context ** g_ctx; |
35 | 39 | static llama_model ** g_model; |
36 | 40 | static common_sampler ** g_smpl; |
@@ -851,6 +855,43 @@ int main(int argc, char ** argv) { |
851 | 855 |
|
852 | 856 | LOG_DBG("buffer: '%s'\n", buffer.c_str()); |
853 | 857 |
|
| 858 | + // check for special commands |
| 859 | + if (buffer.rfind(CMD_READFILE, 0) == 0) { |
| 860 | + const std::string filename = string_strip(buffer.substr(CMD_READFILE.length())); |
| 861 | + LOG_DBG("reading file: '%s'\n", filename.c_str()); |
| 862 | + std::ifstream text_file(filename); |
| 863 | + if (!text_file) { |
| 864 | + LOG("failed to open file '%s'\n", filename.c_str()); |
| 865 | + continue; |
| 866 | + } |
| 867 | + std::stringstream tmp; |
| 868 | + tmp << text_file.rdbuf(); |
| 869 | + buffer = tmp.str(); |
| 870 | + LOG("%s\n", buffer.c_str()); |
| 871 | + } else if (buffer.rfind(CMD_SAVE_SESS, 0) == 0) { |
| 872 | + const std::string filename = string_strip(buffer.substr(CMD_SAVE_SESS.length())); |
| 873 | + LOG("save session file: '%s'\n", filename.c_str()); |
| 874 | + size_t res = llama_state_save_file(ctx, filename.c_str(), embd_inp.data(), n_past); |
| 875 | + if (res == 0) { |
| 876 | + LOG("failed to save session file '%s'\n", filename.c_str()); |
| 877 | + } |
| 878 | + continue; |
| 879 | + } else if (buffer.rfind(CMD_LOAD_SESS, 0) == 0) { |
| 880 | + const std::string filename = string_strip(buffer.substr(CMD_LOAD_SESS.length())); |
| 881 | + LOG("load session file: '%s'\n", filename.c_str()); |
| 882 | + std::vector<llama_token> sess_tokens; |
| 883 | + sess_tokens.resize(n_ctx); |
| 884 | + size_t n_loaded_tokens; |
| 885 | + size_t res = llama_state_load_file(ctx, filename.c_str(), sess_tokens.data(), sess_tokens.size(), &n_loaded_tokens); |
| 886 | + if (res == 0) { |
| 887 | + LOG("failed to load session file '%s'\n", filename.c_str()); |
| 888 | + } else { |
| 889 | + n_past = n_loaded_tokens; |
| 890 | + LOG("loaded %zu tokens from session file '%s'\n", n_loaded_tokens, filename.c_str()); |
| 891 | + } |
| 892 | + continue; |
| 893 | + } |
| 894 | + |
854 | 895 | const size_t original_size = embd_inp.size(); |
855 | 896 |
|
856 | 897 | if (params.escape) { |
|
0 commit comments