embeddings
zocrates.rag.embeddings ¶
Embedding functions for the vector store.
Supports two modes
- "local" → Jina v5 (jina-embeddings-v5-text-nano), runs entirely offline.
- "cloud" → OpenRouter-hosted embedding models (e.g. voyage-4, nemotron-3-embed-1b:free), requires an OpenRouter API key.
EMBEDDING_DIMENSION
module-attribute
¶
KNOWN_EMBEDDING_DIMENSIONS
module-attribute
¶
KNOWN_EMBEDDING_DIMENSIONS = {'baai/bge-m3': 1024, 'nvidia/nemotron-3-embed-1b:free': 1024, 'nvidia/nemotron-3-embed-1b': 1024, 'text-embedding-3-small': 1536, 'text-embedding-3-large': 3072, 'text-embedding-ada-002': 1536, 'jinaai/jina-embeddings-v5-text-nano': 768, 'text-embedding-004': 768, 'qwen/qwen-2.5-coder-32b-instruct': 1024}
LOCAL_EMBEDDING_MODEL
module-attribute
¶
EmbedFn ¶
Bases: Protocol
Callable protocol accepted by VectorStore.
LocalEmbedder ¶
Wraps Hugging Face jina-embeddings-v5-text-nano model with opportunistic GPU acceleration and graceful CPU fallback.
Initialize the local embedding model wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
Hugging Face model identifier string. |
LOCAL_EMBEDDING_MODEL
|
dimension
|
int
|
Target embedding output dimension. |
EMBEDDING_DIMENSION
|
Source code in zocrates/rag/embeddings.py
OpenRouterEmbedder ¶
Wraps OpenRouter embedding models via litellm.embedding with dynamic dimension auto-detection.
Initialize the OpenRouter cloud embedding model wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str | None
|
Optional OpenRouter embedding model identifier. |
None
|
dimension
|
int | None
|
Optional explicit embedding dimension override. |
None
|
api_key
|
str | None
|
Optional OpenRouter API key override. |
None
|
Source code in zocrates/rag/embeddings.py
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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 | |
_detect_best_device ¶
Return the optimal torch device to use for local embedding inference.
Opportunistically probes CUDA and Apple Silicon MPS with a lightweight tensor sanity probe. Gracefully falls back to CPU if probes fail or no GPU accelerator is available.
Returns:
| Type | Description |
|---|---|
DeviceLike
|
A |
Source code in zocrates/rag/hardware.py
_safe_to ¶
Recursively moves module parameters/buffers to device/dtype, skipping meta tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
Any
|
PyTorch module to move. |
required |
device
|
Any
|
Target torch device or device string. |
required |
dtype
|
Any
|
Target torch data type. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The moved module. |
Raises:
| Type | Description |
|---|---|
Exception
|
If moving to a non-CPU device fails due to hardware or driver errors. |
Source code in zocrates/rag/embeddings.py
get_embedder ¶
Return the configured embedding function.
Reads the active mode from the EMBEDDING_MODE environment variable,
falling back to the persisted config file value, then to "local".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
str | None
|
Override for the embedding mode ( |
None
|
Returns:
| Type | Description |
|---|---|
LocalEmbedder | OpenRouterEmbedder
|
A |
LocalEmbedder | OpenRouterEmbedder
|
|