Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(embed): leave the Voyage input to Voyage's own truncation

AkitaOnRailsa1a056e9144b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1a731b27..199df84d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -223,11 +223,11 @@ version in [`docs/changelog/`](docs/changelog/):
relay and wiki sync companions take the same `rustls-webpki` bump.
### Fixed
-- Bounded the embedding input on the Gemini and Voyage embedders, which sent
- the caller's text verbatim while every other HTTP embedder head-truncated
- first. Gemini rejects an over-long `embedContent` input with 400
- INVALID_ARGUMENT, and page embedding is best-effort, so a page past the
- model's input limit silently ended up with no vector.
+- The Gemini embedder now cuts a long input to the same 8 KB head as the
+ OpenAI, Copilot and local embedders. It sent the whole page, while the
+ default `gemini-embedding-001` takes at most 2,048 input tokens, and a
+ refused page embedding is only logged, so a long page could end up with no
+ vector. Voyage is unchanged: its API shortens an over-long input itself.
([#1513](https://github.com/akitaonrails/ai-memory/pull/1513))
- Reserved local-embedding capacity for interactive search queries instead of
diff --git a/crates/ai-memory-llm/src/embedding.rs b/crates/ai-memory-llm/src/embedding.rs
index 134f5b45..b1403f3f 100644
--- a/crates/ai-memory-llm/src/embedding.rs
+++ b/crates/ai-memory-llm/src/embedding.rs
@@ -645,6 +645,10 @@ impl VoyageEmbedder {
}
}
+/// Sent without a client-side cut: Voyage's `truncation` defaults to true and
+/// shortens an over-long input to the model's own context (4k-32k tokens), so
+/// the shared 8 KB head cut would only discard text the model can embed.
+/// <https://docs.voyageai.com/docs/embeddings>
#[derive(Debug, Serialize)]
struct VoyageRequest<'a> {
input: [&'a str; 1],
@@ -677,9 +681,8 @@ impl Embedder for VoyageEmbedder {
async fn embed(&self, text: &str) -> LlmResult<Vec<f32>> {
let url = normalize_openai_base(&self.base_url, "embeddings");
- let input = truncate_for_embedding(text, OPENAI_EMBED_MAX_TOKENS);
let req = VoyageRequest {
- input: [input.as_str()],
+ input: [text],
model: &self.model,
};
let mut attempt = 0u32;
@@ -1128,6 +1131,36 @@ mod tests {
.expect("embed_query succeeds");
}
+ /// Voyage truncates server side by default, so a long page goes out whole
+ /// rather than through the 8 KB head cut the other HTTP embedders need.
+ #[tokio::test]
+ async fn voyage_embedder_sends_a_long_input_whole() {
+ use wiremock::matchers::{method, path};
+ use wiremock::{Mock, MockServer, Request, ResponseTemplate};
+
+ let page = "word ".repeat(8_000);
+ let expected = page.clone();
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .and(path("/v1/embeddings"))
+ .respond_with(move |req: &Request| {
+ let body: serde_json::Value = serde_json::from_slice(&req.body).unwrap();
+ assert_eq!(body["input"][0], expected.as_str());
+ ResponseTemplate::new(200).set_body_json(serde_json::json!({
+ "data": [{ "embedding": [0.6_f32, 0.8] }],
+ }))
+ })
+ .expect(1)
+ .mount(&server)
+ .await;
+
+ let e = VoyageEmbedder::new(SecretString::from("test-key"), "voyage-3-large", 2)
+ .unwrap()
+ .with_base_url(server.uri());
+
+ e.embed(&page).await.expect("voyage embed succeeds");
+ }
+
#[tokio::test]
async fn synthetic_embedder_produces_unit_vectors() {
let e = SyntheticEmbedder::new(64);
diff --git a/crates/ai-memory-llm/src/google.rs b/crates/ai-memory-llm/src/google.rs
index 87e1fdc2..c9133844 100644
--- a/crates/ai-memory-llm/src/google.rs
+++ b/crates/ai-memory-llm/src/google.rs
@@ -75,9 +75,12 @@ impl GoogleEmbedder {
} else {
text.to_string()
};
- // Gemini rejects an over-long `embedContent` input with 400
- // INVALID_ARGUMENT, and `embed_page_version` only logs that failure, so
- // an unbounded body would leave the page with no vector at all.
+ // `gemini-embedding-001`, the default, takes at most 2,048 input tokens
+ // and the API does not document whether a longer input is cut or
+ // refused; `embed_page_version` only logs a refusal, which would leave
+ // the page with no vector. The 8 KB head cut stays near that limit for
+ // prose. Applied after the embedding-2 prefix so it bounds the whole
+ // input. <https://ai.google.dev/gemini-api/docs/embeddings>
let prepared = truncate_for_embedding(&prepared, OPENAI_EMBED_MAX_TOKENS);
let url = embed_url(&self.base_url, &self.model);