Reference¶
whyhow.embedding
module¶
whyhow_rbr.embedding.generate_embeddings(openai_api_key, chunks, model='text-embedding-3-small')
¶
Generate embeddings for a list of chunks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
openai_api_key |
str
|
OpenAI API key. |
required |
chunks |
list[str]
|
List of chunks to generate embeddings for. |
required |
model |
str
|
OpenAI model to use for generating embeddings. |
'text-embedding-3-small'
|
Returns:
Type | Description |
---|---|
list[list[float]]
|
List of embeddings for each chunk. |
Source code in whyhow_rbr/embedding.py
whyhow.exceptions
module¶
whyhow_rbr.exceptions.IndexAlreadyExistsException
¶
whyhow_rbr.exceptions.IndexNotFoundException
¶
whyhow_rbr.exceptions.OpenAIException
¶
whyhow.processing
module¶
whyhow_rbr.processing.parse_and_split(path, chunk_size=512, chunk_overlap=100)
¶
Parse a PDF and split it into chunks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str or Path
|
Path to the document to process. |
required |
chunk_size |
int
|
Size of the chunks. |
512
|
chunk_overlap |
int
|
Overlap between chunks. |
100
|
Returns:
Type | Description |
---|---|
list[Document]
|
The chunks of the pdf. |
Source code in whyhow_rbr/processing.py
whyhow_rbr.processing.clean_chunks(chunks)
¶
Clean the chunks of a pdf.
No modifications in-place.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
chunks |
list[Document]
|
The chunks of the pdf. |
required |
Returns:
Type | Description |
---|---|
list[Document]
|
The cleaned chunks. |
Source code in whyhow_rbr/processing.py
whyhow.rag
module¶
whyhow_rbr.rag.Client
¶
Synchronous client.
Source code in whyhow_rbr/rag.py
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 533 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 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 |
|
clean_text(text)
¶
Return a lower case version of text with punctuation removed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
The raw text to be cleaned. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
The cleaned text string.
|
|
Source code in whyhow_rbr/rag.py
create_index(name, dimension=1536, metric='cosine', spec=None)
¶
Create a new index.
If the index does not exist, it creates a new index with the specified.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The name of the index. |
required |
dimension |
int
|
The dimension of the index. |
1536
|
metric |
Metric
|
The metric of the index. |
'cosine'
|
spec |
ServerlessSpec | PodSpec | None
|
The spec of the index. If None, it uses the default spec. |
None
|
Raises:
Type | Description |
---|---|
IndexAlreadyExistsException
|
If the index already exists. |
Source code in whyhow_rbr/rag.py
create_prompt(question, match_texts)
¶
Create the prompt for the OpenAI chat completion.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
question |
str
|
The question to ask. |
required |
match_texts |
list[str]
|
The list of context strings to include in the prompt. |
required |
Returns:
Type | Description |
---|---|
str
|
The generated prompt. |
Source code in whyhow_rbr/rag.py
get_index(name)
¶
Get an existing index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The name of the index. |
required |
Returns:
Type | Description |
---|---|
Index
|
The index. |
Raises:
Type | Description |
---|---|
IndexNotFoundException
|
If the index does not exist. |
Source code in whyhow_rbr/rag.py
process_response(response)
¶
Process the OpenAI chat completion response.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response |
Any
|
The OpenAI chat completion response. |
required |
Returns:
Type | Description |
---|---|
Output
|
The processed output. |
Raises:
Type | Description |
---|---|
OpenAIException
|
If the chat model did not finish or the response is not valid JSON. |
Source code in whyhow_rbr/rag.py
query(question, index, namespace, rules=None, top_k=5, chat_model='gpt-4-1106-preview', chat_temperature=0.0, chat_max_tokens=1000, chat_seed=2, embedding_model='text-embedding-3-small', process_rules_separately=False, keyword_trigger=False)
¶
Query the index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
question |
str
|
The question to ask. |
required |
index |
Index
|
The index to query. |
required |
namespace |
str
|
The namespace within the index to use. |
required |
rules |
list[Rule] | None
|
The rules to use for filtering the documents. |
None
|
top_k |
int
|
The number of matches to return per rule. |
5
|
chat_model |
str
|
The OpenAI chat model to use. |
'gpt-4-1106-preview'
|
chat_temperature |
float
|
The temperature for the chat model. |
0.0
|
chat_max_tokens |
int
|
The maximum number of tokens for the chat model. |
1000
|
chat_seed |
int
|
The seed for the chat model. |
2
|
embedding_model |
str
|
The OpenAI embedding model to use. |
'text-embedding-3-small'
|
process_rules_separately |
bool
|
Whether to process each rule individually and combine the results at the end. When set to True, each rule will be run independently, ensuring that every rule returns results. When set to False (default), all rules will be run as one joined query, potentially allowing one rule to dominate the others. Default is False. |
False
|
keyword_trigger |
bool
|
Whether to trigger rules based on keyword matches in the question. Default is False. |
False
|
Returns:
Type | Description |
---|---|
QueryReturnType
|
Dictionary with keys "answer", "matches", and "used_contexts". The "answer" is the answer to the question. The "matches" are the "top_k" matches from the index. The "used_contexts" are the indices of the matches that were actually used to answer the question. |
Raises:
Type | Description |
---|---|
OpenAIException
|
If there is an error with the OpenAI API. Some possible reasons include the chat model not finishing or the response not being valid JSON. |
Source code in whyhow_rbr/rag.py
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 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 |
|
upload_documents(index, documents, namespace, embedding_model='text-embedding-3-small', batch_size=100)
¶
Upload documents to the index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index |
Index
|
The index. |
required |
documents |
list[str | Path]
|
The documents to upload. |
required |
namespace |
str
|
The namespace within the index to use. |
required |
batch_size |
int
|
The number of documents to upload at a time. |
100
|
embedding_model |
str
|
The OpenAI embedding model to use. |
'text-embedding-3-small'
|
Source code in whyhow_rbr/rag.py
whyhow_rbr.rag.Rule
¶
Bases: BaseModel
Retrieval rule.
The rule is used to filter the documents in the index.
Attributes:
Name | Type | Description |
---|---|---|
filename |
str | None
|
The filename of the document. |
uuid |
str | None
|
The UUID of the document. |
page_numbers |
list[int] | None
|
The page numbers of the document. |
keywords |
list[str] | None
|
The keywords to trigger a rule. |
Source code in whyhow_rbr/rag.py
convert_empty_str_to_none(s)
¶
convert_empty_to_none(v)
classmethod
¶
Convert empty list to None.
to_filter()
¶
Convert rule to Pinecone filter format.
Source code in whyhow_rbr/rag.py
whyhow_rbr.rag.PineconeMetadata
¶
Bases: BaseModel
The metadata to be stored in Pinecone.
Attributes:
Name | Type | Description |
---|---|---|
text |
str
|
The text of the document. |
page_number |
int
|
The page number of the document. |
chunk_number |
int
|
The chunk number of the document. |
filename |
str
|
The filename of the document. |
uuid |
str
|
The UUID of the document. Note that this is not required to be provided when creating the metadata. It is generated automatically when creating the PineconeDocument. |
Source code in whyhow_rbr/rag.py
whyhow_rbr.rag.PineconeDocument
¶
Bases: BaseModel
The actual document to be stored in Pinecone.
Attributes:
Name | Type | Description |
---|---|---|
metadata |
PineconeMetadata
|
The metadata of the document. |
values |
list[float] | None
|
The embedding of the document. The None is used when querying the index since the values are not needed. At upsert time, the values are required. |
id |
str | None
|
The human-readable identifier of the document. This is generated automatically when creating the PineconeDocument unless it is provided. |
Source code in whyhow_rbr/rag.py
generate_human_readable_id()
¶
Generate a human-readable identifier for the document.
Source code in whyhow_rbr/rag.py
whyhow_rbr.rag.PineconeMatch
¶
Bases: BaseModel
The match returned from Pinecone.
Attributes:
Name | Type | Description |
---|---|---|
id |
str
|
The ID of the document. |
score |
float
|
The score of the match. Its meaning depends on the metric used for the index. |
metadata |
PineconeMetadata
|
The metadata of the document. |
Source code in whyhow_rbr/rag.py
whyhow_rbr.rag.Input
¶
Bases: BaseModel
Example input for the prompt.
Attributes:
Name | Type | Description |
---|---|---|
question |
str
|
The question to ask. |
contexts |
list[str]
|
The contexts to use for answering the question. |
Source code in whyhow_rbr/rag.py
whyhow_rbr.rag.Output
¶
Bases: BaseModel
Example output for the prompt.
Attributes:
Name | Type | Description |
---|---|---|
answer |
str
|
The answer to the question. |
contexts |
list[int]
|
The indices of the contexts that were used to answer the question. |
Source code in whyhow_rbr/rag.py
whyhow_rbr.rag.Client.query(question, index, namespace, rules=None, top_k=5, chat_model='gpt-4-1106-preview', chat_temperature=0.0, chat_max_tokens=1000, chat_seed=2, embedding_model='text-embedding-3-small', process_rules_separately=False, keyword_trigger=False)
¶
Query the index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
question |
str
|
The question to ask. |
required |
index |
Index
|
The index to query. |
required |
namespace |
str
|
The namespace within the index to use. |
required |
rules |
list[Rule] | None
|
The rules to use for filtering the documents. |
None
|
top_k |
int
|
The number of matches to return per rule. |
5
|
chat_model |
str
|
The OpenAI chat model to use. |
'gpt-4-1106-preview'
|
chat_temperature |
float
|
The temperature for the chat model. |
0.0
|
chat_max_tokens |
int
|
The maximum number of tokens for the chat model. |
1000
|
chat_seed |
int
|
The seed for the chat model. |
2
|
embedding_model |
str
|
The OpenAI embedding model to use. |
'text-embedding-3-small'
|
process_rules_separately |
bool
|
Whether to process each rule individually and combine the results at the end. When set to True, each rule will be run independently, ensuring that every rule returns results. When set to False (default), all rules will be run as one joined query, potentially allowing one rule to dominate the others. Default is False. |
False
|
keyword_trigger |
bool
|
Whether to trigger rules based on keyword matches in the question. Default is False. |
False
|
Returns:
Type | Description |
---|---|
QueryReturnType
|
Dictionary with keys "answer", "matches", and "used_contexts". The "answer" is the answer to the question. The "matches" are the "top_k" matches from the index. The "used_contexts" are the indices of the matches that were actually used to answer the question. |
Raises:
Type | Description |
---|---|
OpenAIException
|
If there is an error with the OpenAI API. Some possible reasons include the chat model not finishing or the response not being valid JSON. |
Source code in whyhow_rbr/rag.py
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 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 |
|
:docstring:
:members:
:undoc-members:
:show-inheritance:
:special-members: __init__