Skip to content

Commit 1c1409e

Browse files
authored
embedding: add raw option for --embd-output-format (#16541)
* Add --embd-output-format raw for plain numeric embedding output This new option outputs embeddings as raw space-separated floats, without JSON or 'embedding N:' prefixes. Useful for downstream vector pipelines and scripting. * Move raw output handling into format handling section * Move raw output handling into else-if block with other format handlers * Use LOG instead of printf for raw embedding output * docs: document 'raw' embedding output format in arg.cpp and README
1 parent 7a0e900 commit 1c1409e

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

common/arg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3248,7 +3248,7 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
32483248
).set_examples({LLAMA_EXAMPLE_EMBEDDING}));
32493249
add_opt(common_arg(
32503250
{"--embd-output-format"}, "FORMAT",
3251-
"empty = default, \"array\" = [[],[]...], \"json\" = openai style, \"json+\" = same \"json\" + cosine similarity matrix",
3251+
"empty = default, \"array\" = [[],[]...], \"json\" = openai style, \"json+\" = same \"json\" + cosine similarity matrix, \"raw\" = plain whitespace-delimited output (one embedding per line)",
32523252
[](common_params & params, const std::string & value) {
32533253
params.embd_out = value;
32543254
}

examples/embedding/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ The above command will output space-separated float values.
3838
| | multiple embeddings | $[[x_1,...,x_n],[x_1,...,x_n],...,[x_1,...,x_n]]$
3939
| 'json' | openai style |
4040
| 'json+' | add cosine similarity matrix |
41+
| 'raw' | plain text output |
4142

4243
### --embd-separator $"string"$
4344
| $"string"$ | |

examples/embedding/embedding.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,29 @@ static void batch_decode(llama_context * ctx, llama_batch & batch, float * outpu
7070
}
7171
}
7272

73+
// plain, pipe-friendly output: one embedding per line
74+
static void print_raw_embeddings(const float * emb,
75+
int n_embd_count,
76+
int n_embd,
77+
const llama_model * model,
78+
enum llama_pooling_type pooling_type,
79+
int embd_normalize) {
80+
const uint32_t n_cls_out = llama_model_n_cls_out(model);
81+
const bool is_rank = (pooling_type == LLAMA_POOLING_TYPE_RANK);
82+
const int cols = is_rank ? std::min<int>(n_embd, (int) n_cls_out) : n_embd;
83+
84+
for (int j = 0; j < n_embd_count; ++j) {
85+
for (int i = 0; i < cols; ++i) {
86+
if (embd_normalize == 0) {
87+
LOG("%1.0f%s", emb[j * n_embd + i], (i + 1 < cols ? " " : ""));
88+
} else {
89+
LOG("%1.7f%s", emb[j * n_embd + i], (i + 1 < cols ? " " : ""));
90+
}
91+
}
92+
LOG("\n");
93+
}
94+
}
95+
7396
int main(int argc, char ** argv) {
7497
common_params params;
7598

@@ -372,6 +395,8 @@ int main(int argc, char ** argv) {
372395
}
373396

374397
if (notArray) LOG("\n}\n");
398+
} else if (params.embd_out == "raw") {
399+
print_raw_embeddings(emb, n_embd_count, n_embd, model, pooling_type, params.embd_normalize);
375400
}
376401

377402
LOG("\n");

0 commit comments

Comments
 (0)