[PR #2398] [MERGED] Batch lorebook entry embedding requests #5579

Closed
opened 2026-10-22 04:17:32 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/2398
Author: @Romuromylus
Created: 6/6/2026
Status: ✅ Merged
Merged: 6/6/2026
Merged by: @Romuromylus

Base: refactor ← Head: fix/batch-lorebook-vectorize


📝 Commits (2)

  • 2ba9a76 Batch lorebook entry embedding requests
  • ec55e9b Reject malformed embedding batch indices instead of mis-mapping by position

📊 Changes

1 file changed (+170 additions, -21 deletions)

View changed files

📝 src-tauri/src/commands/storage/prompts.rs (+170 -21)

📄 Description

Linked issue

Closes #2285

Why this change

  • vectorize_lorebook fired one /embeddings HTTP round-trip per entry, so a large lorebook meant N serial requests (slow and rate-limit-prone). OpenAI-compatible embedding endpoints accept an input array, so the same work can be done in batches.

What changed

  • src-tauri/src/commands/storage/prompts.rs: added embed_texts() which, for OpenAI-compatible providers, POSTs input as an array and parses every data[].embedding ordered strictly by each item's index (array-position fallback when index is absent; fails closed if any slot is unfilled, so a misordered/truncated response can never write the wrong vector). Google/Ollama remain single-input per protocol (per-item fallback).
  • vectorize_lorebook now collects entries needing embedding and embeds in chunks of EMBEDDING_BATCH_SIZE = 50 (mirrors the web build's BATCH_SIZE). An N-entry lorebook drops from N round-trips to ceil(N/50).
  • embed_text/embed_openai_compatible are preserved as thin wrappers over the batch path, so single-call sites (chat memory, query embedding), the openai_chatgpt rejection, and the SSRF ensure_embedding_url_allowed guard are unchanged.

Refactor impact

Primary owner: Rust storage

Impact areas reviewed:

  • src-tauri/src/commands/storage/prompts.rs (vectorize_lorebook, embedding request/parse path)

Boundary notes:

  • None. Confined to the embeddings helpers in one Rust module. Failure semantics unchanged except now at chunk granularity (entries patched in prior chunks stay vectorized — same partial-progress behavior as before).

Pressure points touched:

  • None.

Validation

  • Matching validation command passes locally
  • Full pnpm check passes before PR push/handoff
  • Human/manual validation completed by contributor or reviewer

Manual verification notes

  • cargo test --manifest-path src-tauri/Cargo.toml -p marinara-engine (embed/vectorize filters) — pass; the 5 existing vectorize_lorebook_* tests still green. Order-by-index and batch-size behavior were verified with local inline tests (kept local per repo convention).
  • cargo clippy --manifest-path src-tauri/Cargo.toml --workspace --all-targets -- -D warnings — clean.
  • (cargo nextest is the CI runner; cargo test used locally as nextest is not installed here.)

Feature Discoverability

  • Updated src/features/shell/discovery/
  • N/A because this PR is a performance bugfix with no user-facing surface change.

Reason:

  • Same vectorization result, fewer requests.

Docs and release impact

  • No docs changes needed

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/2398 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 6/6/2026 **Status:** ✅ Merged **Merged:** 6/6/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `refactor` ← **Head:** `fix/batch-lorebook-vectorize` --- ### 📝 Commits (2) - [`2ba9a76`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2ba9a7684f55083aebb7916bbd4a49f174174b11) Batch lorebook entry embedding requests - [`ec55e9b`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ec55e9bc5b72106b032c84a19cbe8e78a3f6d2cc) Reject malformed embedding batch indices instead of mis-mapping by position ### 📊 Changes **1 file changed** (+170 additions, -21 deletions) <details> <summary>View changed files</summary> 📝 `src-tauri/src/commands/storage/prompts.rs` (+170 -21) </details> ### 📄 Description ## Linked issue Closes #2285 ## Why this change - `vectorize_lorebook` fired one `/embeddings` HTTP round-trip per entry, so a large lorebook meant N serial requests (slow and rate-limit-prone). OpenAI-compatible embedding endpoints accept an `input` array, so the same work can be done in batches. ## What changed - `src-tauri/src/commands/storage/prompts.rs`: added `embed_texts()` which, for OpenAI-compatible providers, POSTs `input` as an array and parses every `data[].embedding` ordered strictly by each item's `index` (array-position fallback when `index` is absent; fails closed if any slot is unfilled, so a misordered/truncated response can never write the wrong vector). Google/Ollama remain single-input per protocol (per-item fallback). - `vectorize_lorebook` now collects entries needing embedding and embeds in chunks of `EMBEDDING_BATCH_SIZE = 50` (mirrors the web build's BATCH_SIZE). An N-entry lorebook drops from N round-trips to ceil(N/50). - `embed_text`/`embed_openai_compatible` are preserved as thin wrappers over the batch path, so single-call sites (chat memory, query embedding), the `openai_chatgpt` rejection, and the SSRF `ensure_embedding_url_allowed` guard are unchanged. ## Refactor impact Primary owner: Rust storage Impact areas reviewed: - `src-tauri/src/commands/storage/prompts.rs` (`vectorize_lorebook`, embedding request/parse path) Boundary notes: - None. Confined to the embeddings helpers in one Rust module. Failure semantics unchanged except now at chunk granularity (entries patched in prior chunks stay vectorized — same partial-progress behavior as before). Pressure points touched: - None. ## Validation - [x] Matching validation command passes locally - [ ] Full `pnpm check` passes before PR push/handoff - [ ] Human/manual validation completed by contributor or reviewer ### Manual verification notes - `cargo test --manifest-path src-tauri/Cargo.toml -p marinara-engine` (embed/vectorize filters) — pass; the 5 existing `vectorize_lorebook_*` tests still green. Order-by-index and batch-size behavior were verified with local inline tests (kept local per repo convention). - `cargo clippy --manifest-path src-tauri/Cargo.toml --workspace --all-targets -- -D warnings` — clean. - (`cargo nextest` is the CI runner; `cargo test` used locally as nextest is not installed here.) ## Feature Discoverability - [ ] Updated `src/features/shell/discovery/` - [x] N/A because this PR is a performance bugfix with no user-facing surface change. Reason: - Same vectorization result, fewer requests. ## Docs and release impact - [x] No docs changes needed --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:17:32 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#5579
No description provided.