mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-06 11:18:30 +00:00
Use Companion AI in llama.com by default
This commit is contained in:
parent
d9e27203d4
commit
3dac9f8999
8 changed files with 310 additions and 193 deletions
158
third_party/ggml/common.cc
vendored
158
third_party/ggml/common.cc
vendored
|
@ -2,7 +2,8 @@
|
|||
│vi: set net ft=c++ ts=4 sts=4 sw=4 fenc=utf-8 :vi│
|
||||
╚──────────────────────────────────────────────────────────────────────────────╝
|
||||
│ │
|
||||
│ llama.cpp │
|
||||
│ llama.com │
|
||||
│ Copyright (c) 2023 Justine Alexandra Roberts Tunney │
|
||||
│ Copyright (c) 2023 Georgi Gerganov │
|
||||
│ │
|
||||
│ Permission is hereby granted, free of charge, to any person obtaining │
|
||||
|
@ -25,6 +26,17 @@
|
|||
│ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │
|
||||
│ │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "third_party/ggml/common.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "third_party/libcxx/algorithm"
|
||||
#include "third_party/libcxx/cassert"
|
||||
#include "third_party/libcxx/cstring"
|
||||
#include "third_party/libcxx/fstream"
|
||||
#include "third_party/libcxx/iterator"
|
||||
#include "third_party/libcxx/string"
|
||||
|
||||
STATIC_YOINK("zipos");
|
||||
|
||||
asm(".ident\t\"\\n\\n\
|
||||
llama.cpp (MIT License)\\n\
|
||||
|
@ -32,51 +44,47 @@ Copyright (c) 2023 Georgi Gerganov\"");
|
|||
asm(".include \"libc/disclaimer.inc\"");
|
||||
// clang-format off
|
||||
|
||||
#include "third_party/ggml/common.h"
|
||||
static bool is_integer_str(const char *s) {
|
||||
if (*s == '-') ++s;
|
||||
if (!*s) return false;
|
||||
while (isdigit(*s)) ++s;
|
||||
return !*s;
|
||||
}
|
||||
|
||||
#include "third_party/libcxx/cassert"
|
||||
#include "third_party/libcxx/cstring"
|
||||
#include "third_party/libcxx/fstream"
|
||||
#include "third_party/libcxx/string"
|
||||
#include "third_party/libcxx/iterator"
|
||||
#include "third_party/libcxx/algorithm"
|
||||
static std::string replace_all(std::string const& original,
|
||||
std::string const& before,
|
||||
std::string const& after) {
|
||||
// https://stackoverflow.com/a/7724536/1653720
|
||||
std::string retval;
|
||||
std::string::const_iterator end = original.end();
|
||||
std::string::const_iterator current = original.begin();
|
||||
std::string::const_iterator next =
|
||||
std::search(current, end, before.begin(), before.end());
|
||||
while (next != end) {
|
||||
retval.append(current, next);
|
||||
retval.append(after);
|
||||
current = next + before.size();
|
||||
next = std::search(current, end, before.begin(), before.end());
|
||||
}
|
||||
retval.append(current, next);
|
||||
return retval;
|
||||
}
|
||||
|
||||
#if defined (_WIN32)
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/flock.h"
|
||||
#include "libc/calls/weirdtypes.h"
|
||||
#include "libc/sysv/consts/at.h"
|
||||
#include "libc/sysv/consts/f.h"
|
||||
#include "libc/sysv/consts/fd.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/posix.h"
|
||||
#include "libc/sysv/consts/s.h"
|
||||
// MISSING #include <io.h>
|
||||
#pragma comment(lib,"kernel32.lib")
|
||||
extern "C" __declspec(dllimport) void* __stdcall GetStdHandle(unsigned long nStdHandle);
|
||||
extern "C" __declspec(dllimport) int __stdcall GetConsoleMode(void* hConsoleHandle, unsigned long* lpMode);
|
||||
extern "C" __declspec(dllimport) int __stdcall SetConsoleMode(void* hConsoleHandle, unsigned long dwMode);
|
||||
extern "C" __declspec(dllimport) int __stdcall SetConsoleCP(unsigned int wCodePageID);
|
||||
extern "C" __declspec(dllimport) int __stdcall SetConsoleOutputCP(unsigned int wCodePageID);
|
||||
extern "C" __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int CodePage, unsigned long dwFlags,
|
||||
const wchar_t * lpWideCharStr, int cchWideChar,
|
||||
char * lpMultiByteStr, int cbMultiByte,
|
||||
const char * lpDefaultChar, bool * lpUsedDefaultChar);
|
||||
#define CP_UTF8 65001
|
||||
#endif
|
||||
static bool append_file_to_prompt(const char *path, gpt_params & params) {
|
||||
std::ifstream file(path);
|
||||
if (!file) {
|
||||
fprintf(stderr, "error: failed to open file '%s'\n", path);
|
||||
return false;
|
||||
}
|
||||
std::copy(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), back_inserter(params.prompt));
|
||||
if (params.prompt.back() == '\n') {
|
||||
params.prompt.pop_back();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
|
||||
// determine sensible default number of threads.
|
||||
// std::thread::hardware_concurrency may not be equal to the number of cores, or may return 0.
|
||||
#ifdef __linux__
|
||||
std::ifstream cpuinfo("/proc/cpuinfo");
|
||||
params.n_threads = std::count(std::istream_iterator<std::string>(cpuinfo),
|
||||
std::istream_iterator<std::string>(),
|
||||
std::string("processor"));
|
||||
#endif
|
||||
if (params.n_threads == 0) {
|
||||
params.n_threads = std::max(1, (int32_t) std::thread::hardware_concurrency());
|
||||
}
|
||||
params.n_threads = std::min(20, std::max(1, (int)(_getcpucount() * 0.75)));
|
||||
|
||||
bool invalid_param = false;
|
||||
std::string arg;
|
||||
|
@ -105,21 +113,21 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
|
|||
break;
|
||||
}
|
||||
params.prompt = argv[i];
|
||||
} else if (arg == "-C" || arg == "--prompt_cache") {
|
||||
if (++i >= argc) {
|
||||
invalid_param = true;
|
||||
break;
|
||||
}
|
||||
params.prompt_path = argv[i];
|
||||
} else if (arg == "-f" || arg == "--file") {
|
||||
if (++i >= argc) {
|
||||
invalid_param = true;
|
||||
break;
|
||||
}
|
||||
std::ifstream file(argv[i]);
|
||||
if (!file) {
|
||||
fprintf(stderr, "error: failed to open file '%s'\n", argv[i]);
|
||||
if (!append_file_to_prompt(argv[i], params)) {
|
||||
invalid_param = true;
|
||||
break;
|
||||
}
|
||||
std::copy(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), back_inserter(params.prompt));
|
||||
if (params.prompt.back() == '\n') {
|
||||
params.prompt.pop_back();
|
||||
}
|
||||
} else if (arg == "-n" || arg == "--n_predict") {
|
||||
if (++i >= argc) {
|
||||
invalid_param = true;
|
||||
|
@ -176,7 +184,13 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
|
|||
invalid_param = true;
|
||||
break;
|
||||
}
|
||||
params.n_keep = std::stoi(argv[i]);
|
||||
params.n_keep_str = argv[i];
|
||||
if (is_integer_str(argv[i])) {
|
||||
params.n_keep = std::stoi(params.n_keep_str);
|
||||
if (!params.n_keep) {
|
||||
params.n_keep_str = "";
|
||||
}
|
||||
}
|
||||
} else if (arg == "-m" || arg == "--model") {
|
||||
if (++i >= argc) {
|
||||
invalid_param = true;
|
||||
|
@ -253,6 +267,36 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
|
|||
exit(1);
|
||||
}
|
||||
|
||||
// if no prompt is specified, then use companion ai
|
||||
if (params.prompt.empty()) {
|
||||
if (params.verbose) {
|
||||
fprintf(stderr, "%s: No prompt specified\n", __func__);
|
||||
fprintf(stderr, "%s: Loading CompanionAI\n", __func__);
|
||||
}
|
||||
append_file_to_prompt("/zip/companionai.txt", params);
|
||||
const char *user;
|
||||
user = getenv("USER");
|
||||
if (!user || !*user) {
|
||||
user = "Cosmo";
|
||||
}
|
||||
params.prompt = replace_all(params.prompt, "USER_NAME", user);
|
||||
std::string user_prompt;
|
||||
user_prompt.append(user);
|
||||
user_prompt.append(":");
|
||||
params.antiprompt.push_back(user_prompt);
|
||||
params.repeat_penalty = 1.17647;
|
||||
params.repeat_last_n = 256;
|
||||
params.interactive = true;
|
||||
params.ignore_eos = true;
|
||||
params.n_predict = -1;
|
||||
params.n_ctx = 2048;
|
||||
params.n_keep = 0;
|
||||
params.n_keep_str = "\n\n\n";
|
||||
params.top_k = 40;
|
||||
params.top_p = .5;
|
||||
params.temp = 0.4;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -261,6 +305,7 @@ void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
|
|||
fprintf(stderr, "\n");
|
||||
fprintf(stderr, "options:\n");
|
||||
fprintf(stderr, " -h, --help show this help message and exit\n");
|
||||
fprintf(stderr, " -v, --verbose print plenty of helpful information, e.g. prompt\n");
|
||||
fprintf(stderr, " -i, --interactive run in interactive mode\n");
|
||||
fprintf(stderr, " --interactive-first run in interactive mode and wait for input right away\n");
|
||||
fprintf(stderr, " -ins, --instruct run in instruction mode (use with Alpaca models)\n");
|
||||
|
@ -271,11 +316,13 @@ void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
|
|||
fprintf(stderr, " -s SEED, --seed SEED RNG seed (default: -1, use random seed for <= 0)\n");
|
||||
fprintf(stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n", params.n_threads);
|
||||
fprintf(stderr, " -p PROMPT, --prompt PROMPT\n");
|
||||
fprintf(stderr, " prompt to start generation with (default: empty)\n");
|
||||
fprintf(stderr, " prompt to start generation with (default: Companion AI)\n");
|
||||
fprintf(stderr, " --random-prompt start with a randomized prompt.\n");
|
||||
fprintf(stderr, " --in-prefix STRING string to prefix user inputs with (default: empty)\n");
|
||||
fprintf(stderr, " -f FNAME, --file FNAME\n");
|
||||
fprintf(stderr, " prompt file to start generation.\n");
|
||||
fprintf(stderr, " text file containing prompt (default: Companion AI)\n");
|
||||
fprintf(stderr, " -C FNAME, --prompt_cache FNAME\n");
|
||||
fprintf(stderr, " path of cache for fast prompt reload (default: .prompt.jtlp)\n");
|
||||
fprintf(stderr, " -n N, --n_predict N number of tokens to predict (default: %d, -1 = infinity)\n", params.n_predict);
|
||||
fprintf(stderr, " --top_k N top-k sampling (default: %d)\n", params.top_k);
|
||||
fprintf(stderr, " --top_p N top-p sampling (default: %.1f)\n", (double)params.top_p);
|
||||
|
@ -288,7 +335,9 @@ void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
|
|||
fprintf(stderr, " --n_parts N number of model parts (default: -1 = determine from dimensions)\n");
|
||||
fprintf(stderr, " -b N, --batch_size N batch size for prompt processing (default: %d)\n", params.n_batch);
|
||||
fprintf(stderr, " --perplexity compute perplexity over the prompt\n");
|
||||
fprintf(stderr, " --keep number of tokens to keep from the initial prompt (default: %d, -1 = all)\n", params.n_keep);
|
||||
fprintf(stderr, " --keep NUM|STR number of tokens to keep from the initial prompt, or substring\n");
|
||||
fprintf(stderr, " to search for within prompt that divides the actual prompt from\n");
|
||||
fprintf(stderr, " its initial example text (default: %d, -1 = all)\n", params.n_keep);
|
||||
if (llama_mlock_supported()) {
|
||||
fprintf(stderr, " --mlock force system to keep model in RAM rather than swapping or compressing\n");
|
||||
}
|
||||
|
@ -319,7 +368,6 @@ std::string gpt_random_prompt(std::mt19937 & rng) {
|
|||
case 9: return "They";
|
||||
default: return "To";
|
||||
}
|
||||
|
||||
return "The";
|
||||
}
|
||||
|
||||
|
@ -330,7 +378,6 @@ std::vector<llama_token> llama_tokenize(struct llama_context * ctx, const std::s
|
|||
int n = llama_tokenize(ctx, text.c_str(), res.data(), res.size(), add_bos);
|
||||
assert(n >= 0);
|
||||
res.resize(n);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
@ -350,6 +397,7 @@ void set_console_color(console_state & con_st, console_color_t color) {
|
|||
}
|
||||
con_st.color = color;
|
||||
}
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
#if defined (_WIN32)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue