diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-06-15 05:19:05 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-06-15 05:19:05 +0200 |
| commit | 6214651cc06845417e6546a6df23a3da1a821327 (patch) | |
| tree | 350a73cd77aa74c69ff3c1e0b7dc92c9771c2102 | |
| parent | 48f16a27a1771bbaf324095e5da51d3eeda94ee6 (diff) | |
| download | ffwd-6214651cc06845417e6546a6df23a3da1a821327.tar.gz ffwd-6214651cc06845417e6546a6df23a3da1a821327.zip | |
cli: add --json output, plain matrix, and version flags
| -rw-r--r-- | README.md | 23 | ||||
| -rw-r--r-- | embed_cli.c | 101 | ||||
| -rw-r--r-- | tests/test_cli.c | 36 |
3 files changed, 122 insertions, 38 deletions
@@ -10,8 +10,11 @@ OpenAI/Perplexity-compatible HTTP API. **Prerequisites**: -- On Linux, install `libcjson-dev`, and `libopenblas-dev`. -- On macOS, install `cjson`, `mlx` and `mlx-c` via Homebrew. +- On Linux, install `libopenblas-dev`. +- On macOS, install `mlx` and `mlx-c` via Homebrew. + +You also need `libcjson-dev`/`cjson` to build the server; the CLI and the shared +library can be built without it. **Makefile targets**: @@ -25,10 +28,16 @@ Pass one text to get its embedding, two or more to get a cosine similarity matrix: ```bash +# one text: the embedding as a line of space-separated floats ./embed -d ./pplx-embed-v1-0.6b "What is the capital of France?" + +# two or more: a cosine similarity matrix, one row per line, no labels ./embed -d ./pplx-embed-v1-0.6b "What is the capital of France?" \ "Paris is the capital of France." \ "Berlin is the capital of Germany." +# 1.000000 0.719822 0.422054 +# 0.719822 1.000000 0.601722 +# 0.422054 0.601722 1.000000 ``` With Qwen3, prefix the retrieval query with a task instruction and embed @@ -58,8 +67,18 @@ cat texts.txt | ./embed -d ./model --stream -b 8 Without `--stream`, reading from stdin accumulates all lines then prints the similarity matrix. +Add `--json` to emit JSON. One text prints an array of floats; two or more print +an array of rows (the matrix): + +```bash +./embed -d ./model --json "cat" "dog" +# [[1.000000,0.380861],[0.380861,1.000000]] +``` + Use `--mlx` or `--cuda` to select a GPU backend (build-dependent). Default is CPU. `-t` sets thread count, `-b` sets batch size, `-v` enables verbose output. +`-V`/`--version` prints the version; `--build-info` also reports the commit, +build date, target, and compiler. ## embed-server diff --git a/embed_cli.c b/embed_cli.c index 74071f0..caa586a 100644 --- a/embed_cli.c +++ b/embed_cli.c @@ -1,6 +1,7 @@ /* embed_cli.c - embed command-line tool */ #include "embed.h" +#include "embed_build.h" #include "qwen_kernels.h" #include "qwen_tokenizer.h" @@ -14,7 +15,6 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <math.h> #include <time.h> #include <limits.h> @@ -39,6 +39,7 @@ static void print_usage(const char *prog) " --cuda Use CUDA/cuBLAS GPU backend\n" #endif " --stream Read lines from stdin, write one JSON embedding per line\n" + " --json Emit JSON instead of plain text (embedding or matrix)\n" #ifdef USE_MLX " --mlx-quant-bits N\n" " Quantize MLX linear weights to 8 bits at load time\n" @@ -60,16 +61,19 @@ static void print_usage(const char *prog) " Print raw embeddings (with multiple texts)\n" " -v, --verbose\n" " Verbose (-vv for debug)\n" + " -V, --version\n" + " Print version and exit\n" + " --build-info Print build details and exit\n" " -h Show this help\n" "\n" "Modes:\n" - " 1 text arg Print embedding as space-separated floats\n" - " 2+ text args Print cosine similarity matrix\n" + " 1 text arg Embedding as space-separated floats (JSON with --json)\n" + " 2+ text args Cosine similarity matrix, one row per line (JSON with --json)\n" " no args Batch: read all stdin lines, then similarity matrix\n" " --stream Streaming: read stdin lines, write JSON per line\n" "\n" "Examples:\n" - " %s -d ./model \"query: what is AI?\"\n" + " %s -d ./model \"what is AI?\"\n" #ifdef USE_MLX " %s -d ./model --mlx --stream < texts.txt\n", prog, prog, prog); @@ -178,6 +182,17 @@ static void print_embedding_raw(const float *emb, int dim) putchar('\n'); } +/* Single embedding as a JSON array of floats. */ +static void print_embedding_array(const float *emb, int dim) +{ + putchar('['); + for (int i = 0; i < dim; i++) { + if (i) putchar(','); + printf("%.8f", (double)emb[i]); + } + puts("]"); +} + static size_t engine_workspace_nbytes(const engine_t *e) { return (e && e->workspace) ? embed_workspace_nbytes(e->workspace) : 0; @@ -346,8 +361,45 @@ static int append_text(char ***texts, int *n_texts, int *cap, const char *s) return 0; } +/* Cosine similarity matrix. --json emits a JSON array of rows; otherwise one + * row of space-separated values per line. With print_embs the raw embeddings + * follow, one per line after a blank line (plain-text output only). */ +static void print_matrix(const float *embs, int n, int dim, + int json, int print_embs) +{ + if (json) { + putchar('['); + for (int i = 0; i < n; i++) { + if (i) putchar(','); + putchar('['); + for (int j = 0; j < n; j++) { + if (j) putchar(','); + printf("%.6f", (double)embed_cosine_similarity( + embs + (size_t)i * dim, embs + (size_t)j * dim, dim)); + } + putchar(']'); + } + puts("]"); + return; + } + + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (j) putchar(' '); + printf("%.6f", (double)embed_cosine_similarity( + embs + (size_t)i * dim, embs + (size_t)j * dim, dim)); + } + putchar('\n'); + } + if (print_embs) { + putchar('\n'); + for (int i = 0; i < n; i++) + print_embedding_raw(embs + (size_t)i * dim, dim); + } +} + static int run_batch(engine_t *e, int argc, char **argv, int arg_start, - int print_embs, int batch_size) + int print_embs, int batch_size, int json) { int n_texts = 0, cap = 0; char **texts = NULL; @@ -417,26 +469,10 @@ static int run_batch(engine_t *e, int argc, char **argv, int arg_start, } if (n_texts == 1) { - print_embedding_raw(embs, dim); + if (json) print_embedding_array(embs, dim); + else print_embedding_raw(embs, dim); } else { - printf("Cosine similarity matrix (%d texts):\n", n_texts); - printf("%-4s", ""); - for (int j = 0; j < n_texts; j++) printf(" [%d] ", j); - printf("\n"); - for (int i = 0; i < n_texts; i++) { - printf("[%d] ", i); - for (int j = 0; j < n_texts; j++) - printf(" %.4f", (double)embed_cosine_similarity( - embs + (size_t)i * dim, embs + (size_t)j * dim, dim)); - printf(" \"%s\"\n", texts[i]); - } - if (print_embs) { - printf("\nEmbeddings:\n"); - for (int i = 0; i < n_texts; i++) { - printf("[%d] ", i); - print_embedding_raw(embs + (size_t)i * dim, dim); - } - } + print_matrix(embs, n_texts, dim, json, print_embs); } done: @@ -464,6 +500,8 @@ int main(int argc, char *argv[]) int verbose = 0; int stdin_mode = 0; int batch_size = 0; + int json = 0; + const char *prog = embed_prog_name(argv[0]); #ifdef USE_MLX int use_mlx = 0; int mlx_quantize_bits = 0; @@ -479,12 +517,21 @@ int main(int argc, char *argv[]) while (arg_start < argc && argv[arg_start][0] == '-') { const char *f = argv[arg_start]; if (!strcmp(f, "-d")) { model_dir = argv[++arg_start]; } + else if (!strcmp(f, "-V") || !strcmp(f, "--version")) { + embed_print_version(prog); + return 0; + } + else if (!strcmp(f, "--build-info")) { + embed_print_build_info(prog); + return 0; + } else if (!strcmp(f, "-t") || !strcmp(f, "--threads")) { n_threads = atoi(argv[++arg_start]); } else if (!strcmp(f, "-b") || !strcmp(f, "--batch-size")) { batch_size = atoi(argv[++arg_start]); } + else if (!strcmp(f, "--json")) { json = 1; } #ifdef USE_MLX else if (!strcmp(f, "--mlx-quant-bits")) { mlx_quantize_bits = atoi(argv[++arg_start]); @@ -522,7 +569,7 @@ int main(int argc, char *argv[]) else if (!strcmp(f, "--cuda")) { use_cuda = 1; } #endif else if (!strcmp(f, "-h") || !strcmp(f, "--help")) { - print_usage(argv[0]); + print_usage(prog); return 0; } else break; @@ -567,7 +614,7 @@ int main(int argc, char *argv[]) if (!model_dir || !model_dir[0]) { fprintf(stderr, "model directory required (-d <dir>)\n"); - print_usage(argv[0]); + print_usage(prog); return 1; } @@ -686,7 +733,7 @@ int main(int argc, char *argv[]) if (stdin_mode) rc = run_stdin(&e, batch_size > 0 ? batch_size : 1); else - rc = run_batch(&e, argc, argv, arg_start, print_embs, batch_size); + rc = run_batch(&e, argc, argv, arg_start, print_embs, batch_size, json); /* Cleanup */ if (e.workspace) embed_workspace_free(e.workspace); diff --git a/tests/test_cli.c b/tests/test_cli.c index 76a3f14..58cbad3 100644 --- a/tests/test_cli.c +++ b/tests/test_cli.c @@ -112,6 +112,11 @@ int main(int argc, char **argv) expect(run_cli("-d /nonexistent-embed-dir hi", NULL) == 1, "missing model dir exits 1"); + /* -V reports the program's base name, not the path it was invoked by. */ + expect(run_cli("-V", NULL) == 0, "-V exits 0"); + expect(!file_contains(g_out, "tests/"), + "-V strips the directory from the program name"); + /* Mode 1: one text prints one embedding line. */ snprintf(args, sizeof(args), "-d '%s' 'hello world'", g_dir); expect(run_cli(args, NULL) == 0, "single text exits 0"); @@ -124,25 +129,38 @@ int main(int argc, char **argv) expect(file_contains(g_err, "Using 2 CPU thread(s)"), "-v reports the thread count"); - /* Mode 2+: several texts print a similarity matrix. */ + /* Mode 2+: several texts print a bare numeric matrix, one row per line, + * with no titles or labels. Two texts give a 2x2 with a 1.0 diagonal. */ snprintf(args, sizeof(args), "-d '%s' 'hello world' 'held'", g_dir); expect(run_cli(args, NULL) == 0, "two texts exit 0"); - expect(file_contains(g_out, "Cosine similarity matrix (2 texts):"), - "two texts print a similarity matrix"); - expect(file_contains(g_out, "1.0000"), "matrix diagonal is 1.0000"); + expect(count_lines(g_out) == 2, "two texts print a 2-row matrix"); + expect(file_contains(g_out, "1.000000"), "matrix diagonal is 1.000000"); + expect(!file_contains(g_out, "Cosine"), "matrix has no title line"); + + /* --json on a matrix emits a bare JSON array of rows, no labels. */ + snprintf(args, sizeof(args), "-d '%s' --json 'hello world' 'held'", g_dir); + expect(run_cli(args, NULL) == 0, "two texts --json exits 0"); + expect(file_contains(g_out, "[[") && file_contains(g_out, "1.000000"), + "--json matrix is a bare array of rows"); + expect(!file_contains(g_out, "{"), "--json matrix has no object or labels"); + + /* --json on a single text emits a bare JSON array of floats. */ + snprintf(args, sizeof(args), "-d '%s' --json 'hello world'", g_dir); + expect(run_cli(args, NULL) == 0, "single text --json exits 0"); + expect(file_contains(g_out, "[") && !file_contains(g_out, "{"), + "--json single text is a bare array"); + /* -e appends the raw embeddings (label-free) after the matrix: three + * matrix rows, a blank separator, then three embedding rows. */ snprintf(args, sizeof(args), "-d '%s' -e -b 1 'hello' 'world' 'held'", g_dir); expect(run_cli(args, NULL) == 0, "-e -b 1 with three texts exits 0"); - expect(file_contains(g_out, "Cosine similarity matrix (3 texts):"), - "-b 1 still prints the full matrix"); - expect(file_contains(g_out, "Embeddings:"), "-e prints raw embeddings"); + expect(count_lines(g_out) == 7, "-e prints matrix then raw embeddings"); /* Batch mode reads stdin when no texts are given. */ snprintf(args, sizeof(args), "-d '%s'", g_dir); expect(run_cli(args, "hello world\nheld\n") == 0, "stdin batch exits 0"); - expect(file_contains(g_out, "Cosine similarity matrix (2 texts):"), - "stdin batch prints a similarity matrix"); + expect(count_lines(g_out) == 2, "stdin batch prints a 2-row matrix"); expect(run_cli(args, "\n\n") == 1, "blank stdin exits 1"); /* Streaming mode writes one JSON object per input line. */ |
