escape prompt by default

This commit is contained in:
ngxson 2024-06-13 13:29:58 +02:00
parent 25fb0a6e61
commit ca86d4fd33
3 changed files with 16 additions and 5 deletions

View file

@ -7,7 +7,7 @@ Related PRs:
- (Issue) [Generate control vector using llama.cpp](https://github.com/ggerganov/llama.cpp/issues/6880) - (Issue) [Generate control vector using llama.cpp](https://github.com/ggerganov/llama.cpp/issues/6880)
- [Add control-vector-generator example](https://github.com/ggerganov/llama.cpp/pull/7514) - [Add control-vector-generator example](https://github.com/ggerganov/llama.cpp/pull/7514)
Example: ## Examples
```sh ```sh
# CPU only # CPU only
@ -21,5 +21,14 @@ Example:
# To see help message # To see help message
./control-vector-generator -h ./control-vector-generator -h
# Then, have a look at "control-vector-generator" section # Then, have a look at "control-vector" section
```
## Tips and tricks
If your prompt have multiple lines (per prompt), convert the newline to escape sequence `\n`. For example:
```
<|im_start|>system\nAct like a person who is extremely happy.<|im_end|>
<|im_start|>system\nYou are in a very good mood today<|im_end|>
``` ```

View file

@ -289,7 +289,7 @@ static std::string to_string(const T & val) {
return ss.str(); return ss.str();
} }
static std::vector<std::string> ctrlvec_load_prompt_file(std::string path, bool skip_empty_lines = false) { static std::vector<std::string> ctrlvec_load_prompt_file(std::string path, bool skip_empty_lines) {
std::vector<std::string> output; std::vector<std::string> output;
std::ifstream file(path); std::ifstream file(path);
if (!file.is_open()) { if (!file.is_open()) {
@ -300,6 +300,7 @@ static std::vector<std::string> ctrlvec_load_prompt_file(std::string path, bool
while (std::getline(file, line)) { while (std::getline(file, line)) {
bool is_skip = skip_empty_lines && line.empty(); bool is_skip = skip_empty_lines && line.empty();
if (!is_skip) { if (!is_skip) {
string_process_escapes(line);
output.push_back(line); output.push_back(line);
} }
} }
@ -362,8 +363,8 @@ static void export_gguf(const std::vector<struct ggml_tensor *> & v_ctrl, const
*/ */
static int prepare_entries(gpt_params & params, train_context & ctx_train) { static int prepare_entries(gpt_params & params, train_context & ctx_train) {
// load prompts // load prompts
std::vector<std::string> positive_prompts = ctrlvec_load_prompt_file(params.cvector_positive_file); std::vector<std::string> positive_prompts = ctrlvec_load_prompt_file(params.cvector_positive_file, true);
std::vector<std::string> negative_prompts = ctrlvec_load_prompt_file(params.cvector_negative_file); std::vector<std::string> negative_prompts = ctrlvec_load_prompt_file(params.cvector_negative_file, true);
if (positive_prompts.size() != negative_prompts.size()) { if (positive_prompts.size() != negative_prompts.size()) {
fprintf(stderr, "number of positive and negative prompts must be equal\n"); fprintf(stderr, "number of positive and negative prompts must be equal\n");
return 1; return 1;

View file

@ -11,6 +11,7 @@
#endif #endif
#include <cstdio> #include <cstdio>
#include <ctime>
#include <string> #include <string>
#include <tuple> #include <tuple>
#include <vector> #include <vector>