Embedders
embedders ¶
Assembly: Chonky
Filename: embedders.py
Author: Terry D. Eppler
Created: 05-31-2022
Last Modified By: Terry D. Eppler
Last Modified On: 05-01-2025
embedders.py
Copyright © 2022 Terry Eppler
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
You can contact me at: terryeppler@gmail.com or eppler.terry@epa.gov
GPT ¶
OpenAI embedding provider wrapper.
Purpose
Creates single and batch text embeddings through OpenAI embedding models for Chonky's semantic-analysis and vector-database pipeline. The class stores the active API key, client, model, response, and generated vectors on the instance so the Streamlit workflow can inspect provider state after an embedding operation.
Attributes:
| Name | Type | Description |
|---|---|---|
web_options |
Optional[List[str]]
|
Optional web option values retained for provider compatibility. |
input |
Optional[List[str]]
|
Current text input or batch input sent to the provider. |
client |
Optional[OpenAI]
|
Initialized OpenAI client used for embedding requests. |
prompt |
Optional[str]
|
Prompt state retained for provider compatibility. |
response_format |
Optional[str]
|
Provider response-format state retained for compatibility. |
response |
Optional[Response]
|
Raw OpenAI embedding response from the last request. |
embedding |
Optional[List[float]]
|
Single embedding vector returned by the last single-text request. |
encoding_format |
Optional[str]
|
Output encoding format requested from the provider. |
dimensions |
Optional[int]
|
Optional vector dimensionality value retained for provider compatibility. |
Source code in embedders.py
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 | |
model_options
property
¶
Return supported OpenAI embedding models.
Purpose
Exposes the OpenAI embedding model names used by Chonky UI controls and provider selection logic.
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: Supported OpenAI embedding model names. |
encoding_options
property
¶
Return supported OpenAI embedding encoding formats.
Purpose
Exposes provider-supported encoding format options for embedding responses so the UI and calling code can request float vectors or base64-encoded vectors.
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: Supported encoding format names. |
create ¶
Create a single OpenAI embedding vector.
Purpose
Validates a single text input, stores the request model and encoding format on the instance, calls the OpenAI embeddings endpoint, captures the raw response, and returns the first embedding vector for downstream similarity search and vector persistence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text value to embed. |
required |
model
|
str
|
OpenAI embedding model name. |
'text-embedding-3-small'
|
format
|
str
|
Embedding response encoding format. |
'float'
|
Returns:
| Type | Description |
|---|---|
List[float]
|
List[float]: Embedding vector for the supplied text. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised when validation or OpenAI embedding generation fails. |
Source code in embedders.py
embed ¶
Create batch OpenAI embedding vectors.
Purpose
Validates and cleans a batch of text inputs, sends non-empty strings to the OpenAI embeddings endpoint in provider-safe batches, and returns vectors aligned to the cleaned batch for Chonky's embedding diagnostics and vector-database persistence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
List[str]
|
Text values to embed. |
required |
model
|
str
|
OpenAI embedding model name. |
'text-embedding-3-small'
|
Returns:
| Type | Description |
|---|---|
List[List[float]]
|
List[List[float]]: Embedding vectors for the cleaned text inputs. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised when validation or OpenAI batch embedding generation fails. |
Source code in embedders.py
count_tokens ¶
Count tokens for text using a tiktoken encoding.
Purpose
Measures provider-tokenized text length for embedding readiness diagnostics and UI metrics. The method validates the text and encoding name, resolves the tiktoken encoding, and returns the number of token identifiers produced from the input text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text value to tokenize. |
required |
coding
|
str
|
tiktoken encoding name. |
required |
Returns:
| Type | Description |
|---|---|
int | None
|
int | None: Token count for the supplied text. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised when validation or token encoding fails. |
Source code in embedders.py
Gemini ¶
Google GenAI embedding provider wrapper.
Purpose
Creates single and batch embeddings through the Google GenAI SDK for Chonky's hosted embedding workflow. The class stores Google project, location, client, task type, configuration, response, and vector state on the instance so embedding generation and diagnostics can share a consistent provider contract.
Attributes:
| Name | Type | Description |
|---|---|---|
number |
Optional[int]
|
Optional numeric state retained for provider compatibility. |
api_key |
Optional[str]
|
Google or Vertex API key from Chonky configuration. |
project |
Optional[str]
|
Google Cloud project identifier from configuration. |
location |
Optional[str]
|
Google Cloud location from configuration. |
prompt |
Optional[str]
|
Prompt state retained for provider compatibility. |
credentials |
Optional[str]
|
Google application credentials path from configuration. |
model |
Optional[str]
|
Active Gemini embedding model name. |
response_format |
Optional[str]
|
Response format state retained for provider compatibility. |
dimensions |
Optional[int]
|
Requested embedding dimensionality. |
client |
Optional[Client]
|
Initialized Google GenAI client. |
response |
Optional[Any]
|
Raw provider response from the last embedding request. |
embedding |
Optional[List[float]]
|
Single embedding vector from the last single-text request. |
embeddings |
Optional[List[List[float]]]
|
Batch embedding vectors from the last batch request. |
encoding_format |
Optional[str]
|
Encoding format state retained for provider compatibility. |
use_vertex |
Optional[bool]
|
Vertex AI client flag. |
task_type |
Optional[str]
|
Gemini embedding task type. |
http_options |
Optional[HttpOptions]
|
Google GenAI HTTP options. |
embedding_config |
Optional[EmbedContentConfig]
|
Google embedding request configuration. |
contents |
Optional[List[str]]
|
Provider content values retained for compatibility. |
input_text |
Optional[List[str]]
|
Current text input or batch input sent to the provider. |
file_path |
Optional[str]
|
Source file path state retained for compatibility. |
response_modalities |
Optional[str]
|
Response modality state retained for compatibility. |
Source code in embedders.py
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 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 520 521 522 523 524 525 526 527 528 529 530 531 532 | |
model_options
property
¶
Return supported Gemini embedding models.
Purpose
Exposes Gemini embedding model options for Chonky UI controls and provider selection.
Returns:
| Type | Description |
|---|---|
List[str] | None
|
List[str] | None: Supported Gemini embedding model names. |
task_options
property
¶
Return supported Gemini embedding task types.
Purpose
Exposes Gemini task-type values used to configure retrieval, document, and semantic similarity embedding requests.
Returns:
| Type | Description |
|---|---|
List[str] | None
|
List[str] | None: Supported Gemini embedding task types. |
generate ¶
generate(
text: str,
model: str = "gemini-embedding-001",
dimensions: int = 768,
) -> Optional[List[float]]
Create a single Gemini embedding vector.
Purpose
Validates a single text input, configures the active Gemini model and dimensionality, calls the Google GenAI embedding endpoint, stores the provider response, and returns the first embedding vector for downstream Chonky semantic retrieval workflows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text value to embed. |
required |
model
|
str
|
Gemini embedding model name. |
'gemini-embedding-001'
|
dimensions
|
int
|
Requested output dimensionality. |
768
|
Returns:
| Type | Description |
|---|---|
Optional[List[float]]
|
Optional[List[float]]: Embedding vector for the supplied text. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised when validation or Gemini embedding generation fails. |
Source code in embedders.py
embed ¶
embed(
texts: List[str],
task: str = "RETRIEVAL_DOCUMENT",
model: str = "gemini-embedding-001",
dimensions: int = 768,
) -> List[List[float]]
Create batch Gemini embedding vectors.
Purpose
Validates and cleans a batch of text values, configures Gemini task and dimensionality settings, submits provider-safe batches to Google GenAI, and returns vectors for Chonky embedding diagnostics and vector persistence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
List[str]
|
Text values to embed. |
required |
task
|
str
|
Gemini embedding task type. |
'RETRIEVAL_DOCUMENT'
|
model
|
str
|
Gemini embedding model name. |
'gemini-embedding-001'
|
dimensions
|
int
|
Requested output dimensionality. |
768
|
Returns:
| Type | Description |
|---|---|
List[List[float]]
|
List[List[float]]: Embedding vectors for the cleaned text inputs. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised when validation or Gemini batch embedding generation fails. |
Source code in embedders.py
Grok ¶
Grok-compatible embedding provider wrapper.
Purpose
Provides the embedding-provider contract used by Chonky for Grok/Groq-labeled hosted embedding workflows. The class prepares authorization headers, request payloads, model state, raw responses, and returned vector lists for single-text and batch embedding requests.
Attributes:
| Name | Type | Description |
|---|---|---|
content_type |
Optional[str]
|
HTTP content type used for embedding requests. |
api_key |
Optional[str]
|
Groq API key from Chonky configuration. |
authorization |
Optional[str]
|
Bearer authorization header value. |
prompt |
Optional[str]
|
Prompt state retained for provider compatibility. |
model |
Optional[str]
|
Active embedding model name. |
response_format |
Optional[Union[str, Dict[str, str]]]
|
Response-format state retained for provider compatibility. |
contents |
Optional[List[str]]
|
Text contents retained for batch provider compatibility. |
http_options |
Optional[Dict[str, Any]]
|
HTTP option state retained for provider compatibility. |
embedding |
Optional[float]
|
Embedding vector returned by the last single-text request. |
endpoint |
Optional[str]
|
Embedding endpoint URL used for requests. |
Source code in embedders.py
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 | |
model_options
property
¶
Return supported Grok-compatible embedding models.
Purpose
Exposes embedding model names for Chonky UI controls and provider selection where Grok/Groq-compatible embedding requests are available.
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: Supported embedding model names. |
encoding_options
property
¶
Return supported Grok-compatible encoding formats.
Purpose
Exposes embedding response encoding formats for Chonky UI controls and request configuration.
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: Supported encoding format names. |
create ¶
create(
text: str,
model: str = "text-embedding-3-small",
format: str = "float",
) -> List[float] | None
Create a single Grok-compatible embedding vector.
Purpose
Validates a single text input, prepares an embedding request payload, submits the request to the configured embedding endpoint, stores the raw JSON response, and returns the first vector when the provider reports success.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text value to embed. |
required |
model
|
str
|
Embedding model name. |
'text-embedding-3-small'
|
format
|
str
|
Embedding response encoding format. |
'float'
|
Returns:
| Type | Description |
|---|---|
List[float] | None
|
List[float] | None: Embedding vector when the provider succeeds; otherwise |
Raises:
| Type | Description |
|---|---|
Error
|
Raised when validation, request construction, or provider execution fails. |
Source code in embedders.py
embed ¶
Create batch Grok-compatible embedding vectors.
Purpose
Validates and cleans a batch of text inputs, prepares provider authorization headers, submits provider-safe embedding batches to the configured endpoint, and returns vector lists for Chonky embedding diagnostics and vector-database persistence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
List[str]
|
Text values to embed. |
required |
model
|
str
|
Embedding model name. |
'text-embedding-3-small'
|
Returns:
| Type | Description |
|---|---|
List[List[float]]
|
List[List[float]]: Embedding vectors for the cleaned text inputs. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised when validation or batch provider execution fails. |
Source code in embedders.py
throw_if ¶
Validate a required runtime argument.
Purpose
Provides a lightweight guard for provider wrapper methods that require non-empty
inputs before calling hosted embedding APIs. The function raises a deterministic
ValueError when a required value is missing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the argument being validated. |
required |
value
|
object
|
Runtime value to validate. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
Raised when |