1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
#include "server_internal.h"
#include "server_util.h"
#include "sbuf.h"
#include "base64.h"
#include "embed.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
/* ========================================================================
* Embedding output encoding
*
* Turns a pooled float vector into the wire form the model's API family wants:
* base64 of int8 (tanh-quantized, Perplexity default), base64 of a binary sign
* bitmap, base64 of little-endian float32, or a plain JSON float array. The
* render_* functions assemble the full OpenAI-style response object from a
* prepared request and its computed embeddings.
* ======================================================================== */
signed char quantize_int8_tanh(float x) {
int v = (int)lrintf(tanhf(x) * 127.0f);
if (v > 127)
v = 127;
if (v < -128)
v = -128;
return (signed char)v;
}
char *encode_embedding(const float *emb, int dims, const char *encoding) {
if (!strcmp(encoding, "base64")) {
/* OpenAI/DashScope (Qwen3): base64 of the raw little-endian float32
* vector. Copy through a byte buffer instead of reinterpret-casting the
* float pointer; x86_64 and aarch64 are little-endian, so the bytes
* match the wire format with no swap. */
size_t nbytes = (size_t)dims * sizeof(float);
unsigned char *buf = xmalloc(nbytes);
memcpy(buf, emb, nbytes);
char *out = base64_encode(buf, nbytes);
free(buf);
return out;
}
if (!strcmp(encoding, "base64_binary")) {
size_t bytes = (size_t)dims / 8;
unsigned char *bits = xcalloc(bytes, 1);
for (int i = 0; i < dims; i++) {
if (emb[i] >= 0.0f)
bits[(size_t)i >> 3] |= (unsigned char)(1u << (i & 7));
}
char *out = base64_encode(bits, bytes);
free(bits);
return out;
}
signed char *q = xmalloc((size_t)dims);
for (int i = 0; i < dims; i++)
q[i] = quantize_int8_tanh(emb[i]);
char *out = base64_encode((const unsigned char *)q, (size_t)dims);
free(q);
return out;
}
static void append_embedding_object(sbuf *b, int index, const char *embedding) {
sbuf_printf(b, "{\"object\":\"embedding\",\"index\":%d,\"embedding\":\"", index);
sbuf_puts(b, embedding);
sbuf_puts(b, "\"}");
}
/* Emit one embedding into the response, following the model's API family.
* "float" renders a JSON array: the true float32 vector for OpenAI/DashScope
* (Qwen3), or the int8-decoded view (int8/128) for Perplexity (pplx). Every
* other encoding renders a base64 string via encode_embedding(). */
void append_embedding_value(
sbuf *b, int index, const float *emb, int dims, const char *encoding, embedding_api_t api) {
if (!strcmp(encoding, "float")) {
sbuf_printf(b, "{\"object\":\"embedding\",\"index\":%d,\"embedding\":[", index);
for (int i = 0; i < dims; i++) {
if (i)
sbuf_putc(b, ',');
float v = api == EMBED_API_OPENAI ? emb[i] : (float)quantize_int8_tanh(emb[i]) / 128.0f;
sbuf_printf(b, "%.9g", (double)v);
}
sbuf_puts(b, "]}");
return;
}
char *encoded = encode_embedding(emb, dims, encoding);
append_embedding_object(b, index, encoded);
free(encoded);
}
void set_response_from_buf(job *j, sbuf *b) {
j->status = 200;
j->content_type = "application/json";
j->response = b->ptr;
j->response_len = b->len;
memset(b, 0, sizeof(*b));
}
int render_embedding_response(embedding_request *r, const float *embs) {
const int full_dim = r->model->info->dim;
const float *render_embs = embs;
float *truncated = NULL;
if (r->model->renormalize_truncated && r->dims < full_dim) {
truncated = xmalloc((size_t)r->n_inputs * r->dims * sizeof(*truncated));
for (int i = 0; i < r->n_inputs; i++) {
float *dst = truncated + (size_t)i * r->dims;
memcpy(dst, embs + (size_t)i * full_dim, (size_t)r->dims * sizeof(*dst));
if (embed_l2_normalize(dst, r->dims) != 0) {
free(truncated);
return -1;
}
}
render_embs = truncated;
}
int render_stride = truncated ? r->dims : full_dim;
embedding_api_t api = r->model->info->api;
sbuf b = {0};
sbuf_puts(&b, "{\"object\":\"list\",\"data\":[");
for (int i = 0; i < r->n_inputs; i++) {
if (i)
sbuf_putc(&b, ',');
append_embedding_value(&b, i, render_embs + (size_t)i * render_stride, r->dims, r->encoding,
api);
}
sbuf_printf(&b,
"],\"model\":\"%s\",\"usage\":{\"prompt_tokens\":%d,"
"\"total_tokens\":%d}}",
r->model->info->id, r->total_tokens, r->total_tokens);
set_response_from_buf(r->j, &b);
free(truncated);
return 0;
}
void render_contextual_response(contextual_request *r, const float *embs) {
sbuf b = {0};
sbuf_puts(&b, "{\"object\":\"list\",\"data\":[");
int span_offset = 0;
for (int di = 0; di < r->n_docs; di++) {
if (di)
sbuf_putc(&b, ',');
sbuf_printf(&b, "{\"object\":\"list\",\"index\":%d,\"data\":[", di);
for (int ci = 0; ci < r->docs[di].n_spans; ci++) {
if (ci)
sbuf_putc(&b, ',');
const float *emb = embs + (size_t)span_offset * r->model->info->dim;
append_embedding_value(&b, ci, emb, r->dims, r->encoding, r->model->info->api);
span_offset++;
}
sbuf_puts(&b, "]}");
}
sbuf_printf(&b, "],\"model\":\"%s\",\"usage\":{\"prompt_tokens\":%d,\"total_tokens\":%d}}",
r->model->info->id, r->total_tokens, r->total_tokens);
set_response_from_buf(r->j, &b);
}
|