Methods
API
Bases: Application
Base API template. The API is an extended txtai application, adding the ability to cluster API instances together.
Downstream applications can extend this base template to add/modify functionality.
Source code in txtai/api/base.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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 | |
add(documents)
Adds a batch of documents for indexing.
Downstream applications can override this method to also store full documents in an external system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents
|
list of {id: value, text: value} |
required |
Returns:
| Type | Description |
|---|---|
|
unmodified input documents |
Source code in txtai/api/base.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
addobject(data, uid, field)
Helper method that builds a batch of object documents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
object content |
required | |
uid
|
optional list of corresponding uids |
required | |
field
|
optional field to set |
required |
Returns:
| Type | Description |
|---|---|
|
documents |
Source code in txtai/app/base.py
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 | |
agent(name, *args, **kwargs)
Executes an agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
agent name |
required | |
args
|
agent positional arguments |
()
|
|
kwargs
|
agent keyword arguments |
{}
|
Source code in txtai/app/base.py
796 797 798 799 800 801 802 803 804 805 806 807 808 809 | |
batchexplain(queries, texts=None, limit=10)
Explains the importance of each input token in text for a list of queries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
queries text |
required | |
texts
|
optional list of text, otherwise runs search queries |
None
|
|
limit
|
optional limit if texts is None |
10
|
Returns:
| Type | Description |
|---|---|
|
list of dict per input text per query where a higher token scores represents higher importance relative to the query |
Source code in txtai/app/base.py
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 | |
batchsimilarity(queries, texts)
Computes the similarity between list of queries and list of text. Returns a list of {id: value, score: value} sorted by highest score per query, where id is the index in texts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queries
|
queries text |
required | |
texts
|
list of text |
required |
Returns:
| Type | Description |
|---|---|
|
list of {id: value, score: value} per query |
Source code in txtai/app/base.py
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 | |
batchtransform(texts, category=None, index=None)
Transforms list of text into embeddings arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
list of text |
required | |
category
|
category for instruction-based embeddings |
None
|
|
index
|
index name, if applicable |
None
|
Returns:
| Type | Description |
|---|---|
|
embeddings arrays |
Source code in txtai/app/base.py
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 | |
count()
Total number of elements in this embeddings index.
Returns:
| Type | Description |
|---|---|
|
number of elements in embeddings index |
Source code in txtai/api/base.py
121 122 123 124 125 126 127 128 129 130 131 132 | |
createagents()
Create agents.
Source code in txtai/app/base.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
createpipelines()
Create pipelines.
Source code in txtai/app/base.py
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 | |
delete(ids)
Deletes from an embeddings index. Returns list of ids deleted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ids
|
list of ids to delete |
required |
Returns:
| Type | Description |
|---|---|
|
ids deleted |
Source code in txtai/api/base.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
explain(query, texts=None, limit=10)
Explains the importance of each input token in text for a query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
query text |
required | |
texts
|
optional list of text, otherwise runs search query |
None
|
|
limit
|
optional limit if texts is None |
10
|
Returns:
| Type | Description |
|---|---|
|
list of dict per input text where a higher token scores represents higher importance relative to the query |
Source code in txtai/app/base.py
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 | |
extract(queue, texts=None)
Extracts answers to input questions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queue
|
list of {name: value, query: value, question: value, snippet: value} |
required | |
texts
|
optional list of text |
None
|
Returns:
| Type | Description |
|---|---|
|
list of {name: value, answer: value} |
Source code in txtai/app/base.py
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 | |
index()
Builds an embeddings index for previously batched documents.
Source code in txtai/api/base.py
71 72 73 74 75 76 77 78 79 | |
label(text, labels)
Applies a zero shot classifier to text using a list of labels. Returns a list of {id: value, score: value} sorted by highest score, where id is the index in labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
text|list |
required | |
labels
|
list of labels |
required |
Returns:
| Type | Description |
|---|---|
|
list of {id: value, score: value} per text element |
Source code in txtai/app/base.py
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 | |
pipeline(name, *args, **kwargs)
Generic pipeline execution method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
pipeline name |
required | |
args
|
pipeline positional arguments |
()
|
|
kwargs
|
pipeline keyword arguments |
{}
|
Returns:
| Type | Description |
|---|---|
|
pipeline results |
Source code in txtai/app/base.py
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 | |
reindex(config, function=None)
Recreates this embeddings index using config. This method only works if document content storage is enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
new config |
required | |
function
|
optional function to prepare content for indexing |
None
|
Source code in txtai/api/base.py
107 108 109 110 111 112 113 114 115 116 117 118 119 | |
similarity(query, texts)
Computes the similarity between query and list of text. Returns a list of {id: value, score: value} sorted by highest score, where id is the index in texts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
query text |
required | |
texts
|
list of text |
required |
Returns:
| Type | Description |
|---|---|
|
list of {id: value, score: value} |
Source code in txtai/app/base.py
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 | |
transform(text, category=None, index=None)
Transforms text into embeddings arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
input text |
required | |
category
|
category for instruction-based embeddings |
None
|
|
index
|
index name, if applicable |
None
|
Returns:
| Type | Description |
|---|---|
|
embeddings array |
Source code in txtai/app/base.py
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 | |
upsert()
Runs an embeddings upsert operation for previously batched documents.
Source code in txtai/api/base.py
81 82 83 84 85 86 87 88 89 | |
wait()
Closes threadpool and waits for completion.
Source code in txtai/app/base.py
811 812 813 814 815 816 817 818 819 | |
workflow(name, elements)
Executes a workflow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
workflow name |
required | |
elements
|
elements to process |
required |
Returns:
| Type | Description |
|---|---|
|
processed elements |
Source code in txtai/app/base.py
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 | |