From: hidenorly Date: Wed, 22 Mar 2023 19:43:22 +0000 (+0900) Subject: Add pipe input for prompt on gpt examples (#38) X-Git-Tag: upstream/0.0.1642~1575 X-Git-Url: https://git.djapps.eu/?a=commitdiff_plain;h=084ba0919bee971ddd3dd7455f5de50ad28bb6ad;p=pkg%2Fggml%2Fsources%2Fggml Add pipe input for prompt on gpt examples (#38) Enable prompt input through pipe, instead of using -p option. This makes easier to give longer and multiple lines for the prompt. Test: $ echo "This is an example" > prompt.txt $ cat prompt.txt | ./bin/gpt-j -m models/gpt-j-6B/ggml-model.bin $ cat promot.txt | ./bin/gpt-2 -m models/gpt-2-117M/ggml-model.bin Note that -p option and no -p specified case are kept. $ ./bin/gpt-j -m models/gpt-j-6B/ggml-model.bin -p "This is an example" $ ./bin/gpt-j -m models/gpt-j-6B/ggml-model.bin $ ./bin/gpt-2 -m models/gpt-2-117M/ggml-model.bin -p "This is an example" $ ./bin/gpt-2 -m models/gpt-2-117M/ggml-model.bin --- diff --git a/examples/gpt-2/main.cpp b/examples/gpt-2/main.cpp index 134a9304..5a0ab01f 100644 --- a/examples/gpt-2/main.cpp +++ b/examples/gpt-2/main.cpp @@ -10,6 +10,8 @@ #include #include #include +#include +#include // default hparams (GPT-2 117M) struct gpt2_hparams { @@ -673,7 +675,14 @@ int main(int argc, char ** argv) { std::mt19937 rng(params.seed); if (params.prompt.empty()) { - params.prompt = gpt_random_prompt(rng); + if( !isatty(STDIN_FILENO) ){ + std::string line; + while( std::getline(std::cin, line) ){ + params.prompt = params.prompt + "\n" + line; + } + } else { + params.prompt = gpt_random_prompt(rng); + } } int64_t t_load_us = 0; diff --git a/examples/gpt-j/README.md b/examples/gpt-j/README.md index 7cc78fc7..57a4293c 100644 --- a/examples/gpt-j/README.md +++ b/examples/gpt-j/README.md @@ -211,6 +211,10 @@ make -j4 gpt-j # Run the inference (requires 16GB of CPU RAM) ./bin/gpt-j -m models/gpt-j-6B/ggml-model.bin -p "This is an example" + +# Input prompt through pipe and run the inference. +echo "This is an example" > prompt.txt +cat prompt.txt | ./bin/gpt-j -m models/gpt-j-6B/ggml-model.bin ``` To run the `gpt-j` tool, you need the 12GB `ggml-model.bin` file which contains the GPT-J model in diff --git a/examples/gpt-j/main.cpp b/examples/gpt-j/main.cpp index 63248d7d..5c6ce93a 100644 --- a/examples/gpt-j/main.cpp +++ b/examples/gpt-j/main.cpp @@ -10,6 +10,8 @@ #include #include #include +#include +#include // default hparams (GPT-J 6B) struct gptj_hparams { @@ -605,7 +607,14 @@ int main(int argc, char ** argv) { std::mt19937 rng(params.seed); if (params.prompt.empty()) { - params.prompt = gpt_random_prompt(rng); + if( !isatty(STDIN_FILENO) ){ + std::string line; + while( std::getline(std::cin, line) ){ + params.prompt = params.prompt + "\n" + line; + } + } else { + params.prompt = gpt_random_prompt(rng); + } } int64_t t_load_us = 0;