Skip to content

Commit cd96be7

Browse files
committed
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.
1 parent 41aac5c commit cd96be7

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

examples/embedding/embedding.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "llama.h"
55

66
#include <ctime>
7+
#include <cstdio>
78
#include <algorithm>
89

910
#if defined(_MSC_VER)
@@ -70,6 +71,29 @@ static void batch_decode(llama_context * ctx, llama_batch & batch, float * outpu
7071
}
7172
}
7273

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

@@ -259,6 +283,10 @@ int main(int argc, char ** argv) {
259283
float * out = emb + e * n_embd;
260284
batch_decode(ctx, batch, out, s, n_embd, params.embd_normalize);
261285

286+
if (params.embd_out == "raw") {
287+
print_raw_embeddings(emb, n_embd_count, n_embd, model, pooling_type, params.embd_normalize);
288+
}
289+
262290
if (params.embd_out.empty()) {
263291
LOG("\n");
264292

0 commit comments

Comments
 (0)