paperfoot-engram-embed 0.1.4

Embedding providers (Gemini Embed 2, local fallback) for engram v2
Documentation
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! Gemini Embedding 2 client.
//!
//! Uses Google's `:embedContent` REST endpoint with the v1beta API.
//! Reads `GEMINI_API_KEY` or accepts an explicit key.
//!
//! Model override: set `GEMINI_EMBED_MODEL` to force a specific model name
//! (e.g. "gemini-embedding-001" to pin to the legacy stable version).

use crate::{EmbedError, Embedder, TaskMode};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};

pub const DEFAULT_MODEL: &str = "gemini-embedding-2";
pub const DEFAULT_DIMS: usize = 1536;
pub const PROMPT_FORMAT: &str = "gemini-embedding-2-v1";
const ENDPOINT: &str = "https://generativelanguage.googleapis.com/v1beta/models";

pub struct GeminiEmbedder {
    client: reqwest::Client,
    api_key: String,
    model: String,
    dimensions: usize,
}

impl GeminiEmbedder {
    pub fn new(api_key: impl Into<String>) -> Self {
        Self {
            client: reqwest::Client::builder()
                .timeout(std::time::Duration::from_secs(180))
                .connect_timeout(std::time::Duration::from_secs(15))
                .user_agent(concat!("engram/", env!("CARGO_PKG_VERSION")))
                .pool_idle_timeout(std::time::Duration::from_secs(60))
                .build()
                .expect("failed to build reqwest client"),
            api_key: api_key.into(),
            model: DEFAULT_MODEL.to_string(),
            dimensions: DEFAULT_DIMS,
        }
    }

    pub fn from_env() -> Result<Self, EmbedError> {
        let key = std::env::var("GEMINI_API_KEY")
            .map_err(|_| EmbedError::MissingKey { provider: "gemini" })?;
        let mut e = Self::new(key);
        if let Ok(model) = std::env::var("GEMINI_EMBED_MODEL") {
            e = e.with_model(model);
        }
        Ok(e)
    }

    pub fn with_model(mut self, model: impl Into<String>) -> Self {
        self.model = model.into();
        self
    }

    pub fn with_dimensions(mut self, dims: usize) -> Self {
        self.dimensions = dims;
        self
    }

    pub fn model_name(&self) -> &str {
        &self.model
    }
}

#[derive(Serialize)]
struct EmbedRequest {
    model: String,
    content: Content,
    #[serde(rename = "taskType", skip_serializing_if = "Option::is_none")]
    task_type: Option<&'static str>,
    #[serde(
        rename = "outputDimensionality",
        skip_serializing_if = "Option::is_none"
    )]
    output_dimensionality: Option<usize>,
}

#[derive(Serialize)]
struct BatchEmbedRequest {
    requests: Vec<EmbedRequest>,
}

#[derive(Serialize)]
struct Content {
    parts: Vec<Part>,
}

#[derive(Serialize)]
struct Part {
    text: String,
}

#[derive(Deserialize)]
struct EmbedResponse {
    embedding: Embedding,
}

#[derive(Deserialize)]
struct BatchEmbedResponse {
    embeddings: Vec<Embedding>,
}

#[derive(Deserialize)]
struct Embedding {
    values: Vec<f32>,
}

fn task_type_str(mode: TaskMode) -> &'static str {
    match mode {
        TaskMode::RetrievalQuery => "RETRIEVAL_QUERY",
        TaskMode::RetrievalDocument => "RETRIEVAL_DOCUMENT",
    }
}

fn supports_task_type(model: &str) -> bool {
    !model.starts_with("gemini-embedding-2")
}

fn format_input(text: &str, mode: TaskMode) -> String {
    match mode {
        TaskMode::RetrievalQuery => format!("query: {}", text.trim()),
        TaskMode::RetrievalDocument => format!("document: {}", text.trim()),
    }
}

#[async_trait]
impl Embedder for GeminiEmbedder {
    fn name(&self) -> &'static str {
        "gemini"
    }

    fn dimensions(&self) -> usize {
        self.dimensions
    }

    fn model(&self) -> String {
        self.model.clone()
    }

    fn prompt_format(&self) -> &'static str {
        PROMPT_FORMAT
    }

    async fn embed_one(&self, text: &str, mode: TaskMode) -> Result<Vec<f32>, EmbedError> {
        let formatted = format_input(text, mode);
        let req = EmbedRequest {
            model: format!("models/{}", self.model),
            content: Content {
                parts: vec![Part { text: formatted }],
            },
            task_type: supports_task_type(&self.model).then_some(task_type_str(mode)),
            output_dimensionality: Some(self.dimensions),
        };

        let url = format!(
            "{}/{}:embedContent?key={}",
            ENDPOINT, self.model, self.api_key
        );

        // Full-lifecycle retry: send + 429 + 5xx + body read + JSON parse.
        // Long benchmark runs (1500+ queries) hit transient drops; without
        // retry the whole bench dies on the first one.
        let delays_secs: [u64; 6] = [2, 4, 8, 16, 32, 64];
        let mut attempt: u32 = 0;
        loop {
            // 1. send
            let send_result = self.client.post(&url).json(&req).send().await;
            let resp = match send_result {
                Ok(r) => r,
                Err(e) => {
                    if (attempt as usize) < delays_secs.len() {
                        let wait = delays_secs[attempt as usize];
                        attempt += 1;
                        tracing::warn!(
                            "gemini embed_one network error, backing off {}s (attempt {}): {}",
                            wait,
                            attempt,
                            e
                        );
                        tokio::time::sleep(std::time::Duration::from_secs(wait)).await;
                        continue;
                    }
                    return Err(EmbedError::Http {
                        provider: "gemini",
                        source: e,
                    });
                }
            };

            // 2. status
            let status = resp.status();
            if status == reqwest::StatusCode::TOO_MANY_REQUESTS {
                if (attempt as usize) < delays_secs.len() {
                    let wait = delays_secs[attempt as usize];
                    attempt += 1;
                    tracing::info!(
                        "gemini embed_one 429, backing off {}s (attempt {})",
                        wait,
                        attempt
                    );
                    tokio::time::sleep(std::time::Duration::from_secs(wait)).await;
                    continue;
                }
                return Err(EmbedError::RateLimited { provider: "gemini" });
            }
            if status.is_server_error() {
                let body = resp.text().await.unwrap_or_default();
                if (attempt as usize) < delays_secs.len() {
                    let wait = delays_secs[attempt as usize];
                    attempt += 1;
                    tracing::warn!(
                        "gemini embed_one 5xx ({}), backing off {}s (attempt {}): {}",
                        status,
                        wait,
                        attempt,
                        body
                    );
                    tokio::time::sleep(std::time::Duration::from_secs(wait)).await;
                    continue;
                }
                return Err(EmbedError::Api {
                    provider: "gemini",
                    message: format!("status {}: {}", status, body),
                });
            }
            if !status.is_success() {
                // 4xx (non-429) — real client error, never retry.
                let body = resp.text().await.unwrap_or_default();
                return Err(EmbedError::Api {
                    provider: "gemini",
                    message: format!("status {}: {}", status, body),
                });
            }

            // 3. body read (TLS drop mid-stream lands here)
            let body_text =
                match resp.text().await {
                    Ok(t) => t,
                    Err(e) => {
                        if (attempt as usize) < delays_secs.len() {
                            let wait = delays_secs[attempt as usize];
                            attempt += 1;
                            tracing::warn!(
                            "gemini embed_one body-read error, backing off {}s (attempt {}): {}",
                            wait, attempt, e
                        );
                            tokio::time::sleep(std::time::Duration::from_secs(wait)).await;
                            continue;
                        }
                        return Err(EmbedError::Http {
                            provider: "gemini",
                            source: e,
                        });
                    }
                };

            // 4. JSON parse
            match serde_json::from_str::<EmbedResponse>(&body_text) {
                Ok(parsed) => return Ok(parsed.embedding.values),
                Err(e) => {
                    if (attempt as usize) < delays_secs.len() {
                        let wait = delays_secs[attempt as usize];
                        attempt += 1;
                        tracing::warn!(
                            "gemini embed_one parse error, backing off {}s (attempt {}): {}",
                            wait,
                            attempt,
                            e
                        );
                        tokio::time::sleep(std::time::Duration::from_secs(wait)).await;
                        continue;
                    }
                    return Err(EmbedError::Api {
                        provider: "gemini",
                        message: format!(
                            "parse failed: {}; body: {}",
                            e,
                            body_text.chars().take(500).collect::<String>()
                        ),
                    });
                }
            }
        }
    }

    /// Override default loop with a real batch call. Gemini's
    /// `:batchEmbedContents` accepts up to 250 texts BUT enforces both a
    /// 2,048 input-token-per-text cap and a 20,000 token-per-request cap.
    /// We approximate tokens as `chars / 4` and pack each batch under 16,000
    /// estimated tokens for safety. Inputs longer than ~8,000 chars are
    /// truncated client-side so we don't even ship the bytes.
    async fn embed_batch(
        &self,
        texts: &[&str],
        mode: TaskMode,
    ) -> Result<Vec<Vec<f32>>, EmbedError> {
        if texts.is_empty() {
            return Ok(Vec::new());
        }
        const MAX_CHARS_PER_TEXT: usize = 7800; // ~1950 tokens, under the 2048 cap
        const MAX_CHARS_PER_BATCH: usize = 60_000; // ~15000 tokens, under the 20K cap
        const MAX_TEXTS_PER_BATCH: usize = 100;

        let truncated: Vec<String> = texts
            .iter()
            .map(|t| {
                if t.len() <= MAX_CHARS_PER_TEXT {
                    format_input(t, mode)
                } else {
                    // Truncate at a char boundary.
                    let mut end = MAX_CHARS_PER_TEXT;
                    while !t.is_char_boundary(end) && end > 0 {
                        end -= 1;
                    }
                    format_input(&t[..end], mode)
                }
            })
            .collect();

        let mut out: Vec<Vec<f32>> = Vec::with_capacity(texts.len());
        let url = format!(
            "{}/{}:batchEmbedContents?key={}",
            ENDPOINT, self.model, self.api_key
        );

        let mut idx = 0usize;
        while idx < truncated.len() {
            // Pack the next batch under both limits.
            let mut end = idx;
            let mut batch_chars = 0usize;
            while end < truncated.len()
                && (end - idx) < MAX_TEXTS_PER_BATCH
                && batch_chars + truncated[end].len() <= MAX_CHARS_PER_BATCH
            {
                batch_chars += truncated[end].len();
                end += 1;
            }
            if end == idx {
                end = idx + 1;
            }

            let requests: Vec<EmbedRequest> = truncated[idx..end]
                .iter()
                .map(|t| EmbedRequest {
                    model: format!("models/{}", self.model),
                    content: Content {
                        parts: vec![Part { text: t.clone() }],
                    },
                    task_type: supports_task_type(&self.model).then_some(task_type_str(mode)),
                    output_dimensionality: Some(self.dimensions),
                })
                .collect();
            let body = BatchEmbedRequest { requests };

            // Retry with exponential backoff on 429 and transient network
            // errors. Gemini free tier hits the per-minute quota fast on
            // large corpora; we try up to 6 times with backoffs 4s, 8s,
            // 16s, 32s, 60s, 60s (~3 min total) before surfacing the error.
            // Transport errors (connection reset, DNS hiccup) get the same
            // backoff since they're usually transient.
            let mut attempt: u32 = 0;
            let delays_secs: [u64; 6] = [4, 8, 16, 32, 60, 60];
            let parsed: BatchEmbedResponse = loop {
                let send_result = self.client.post(&url).json(&body).send().await;
                let resp = match send_result {
                    Ok(r) => r,
                    Err(e) => {
                        if (attempt as usize) < delays_secs.len() {
                            let wait = delays_secs[attempt as usize];
                            attempt += 1;
                            tracing::info!(
                                "gemini network error ({}), backing off {}s (attempt {})",
                                e,
                                wait,
                                attempt
                            );
                            tokio::time::sleep(std::time::Duration::from_secs(wait)).await;
                            continue;
                        }
                        return Err(EmbedError::Http {
                            provider: "gemini",
                            source: e,
                        });
                    }
                };
                let status = resp.status();
                if status == reqwest::StatusCode::TOO_MANY_REQUESTS {
                    if (attempt as usize) < delays_secs.len() {
                        let wait = delays_secs[attempt as usize];
                        attempt += 1;
                        tracing::info!("gemini 429, backing off {}s (attempt {})", wait, attempt);
                        tokio::time::sleep(std::time::Duration::from_secs(wait)).await;
                        continue;
                    }
                    return Err(EmbedError::RateLimited { provider: "gemini" });
                }
                if !status.is_success() {
                    let body_text = resp.text().await.unwrap_or_default();
                    return Err(EmbedError::Api {
                        provider: "gemini",
                        message: format!("status {}: {}", status, body_text),
                    });
                }
                let parsed: BatchEmbedResponse =
                    resp.json().await.map_err(|e| EmbedError::Http {
                        provider: "gemini",
                        source: e,
                    })?;
                break parsed;
            };
            for emb in parsed.embeddings {
                out.push(emb.values);
            }
            idx = end;
        }
        Ok(out)
    }
}