Feedback Functions¶
Feedback Functions¶
Initialize feedback function providers:
hugs = Huggingface()
openai = OpenAI()
Run feedback functions. See examples below on how to create them:
feedbacks = tru.run_feedback_functions(
chain=chain,
record=record,
feedback_functions=[f_lang_match, f_qs_relevance]
)
Examples:¶
Non-toxicity of response:
f_non_toxic = Feedback(hugs.not_toxic).on_response()
Language match feedback function:
f_lang_match = Feedback(hugs.language_match).on(text1="prompt", text2="response")
Feedback
¶
Source code in trulens_eval/trulens_eval/tru_feedback.py
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 304 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 |
|
name
property
¶
Name of the feedback function. Presently derived from the name of the function implementing it.
__init__(imp=None, selectors=None, feedback_id=None)
¶
A Feedback function container.
- imp: Optional[Callable] -- implementation of the feedback function.
- selectors: Optional[Dict[str, Selection]] -- mapping of implementation argument names to where to get them from a record.
Source code in trulens_eval/trulens_eval/tru_feedback.py
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 |
|
extract_selection(chain_json, record_json)
¶
Given the chain
that produced the given record
, extract from
record
the values that will be sent as arguments to the implementation
as specified by self.selectors
.
Source code in trulens_eval/trulens_eval/tru_feedback.py
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 |
|
on(**selectors)
¶
Create a variant of self
with the same implementation but the given selectors
.
Source code in trulens_eval/trulens_eval/tru_feedback.py
316 317 318 319 320 321 |
|
on_multiple(multiarg, each_query=None, agg=np.mean)
¶
Create a variant of self
whose implementation will accept multiple
values for argument multiarg
, aggregating feedback results for each.
Optionally each input element is further projected with each_query
.
- multiarg: str -- implementation argument that expects multiple values.
- each_query: Optional[Query] -- a query providing the path from each
input to
multiarg
to some inner value which will be sent toself.imp
.
Source code in trulens_eval/trulens_eval/tru_feedback.py
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 |
|
on_prompt(arg='text')
¶
Create a variant of self
that will take in the main chain input or
"prompt" as input, sending it as an argument arg
to implementation.
Source code in trulens_eval/trulens_eval/tru_feedback.py
296 297 298 299 300 301 302 |
|
on_response(arg='text')
¶
Create a variant of self
that will take in the main chain output or
"response" as input, sending it as an argument arg
to implementation.
Source code in trulens_eval/trulens_eval/tru_feedback.py
306 307 308 309 310 311 312 |
|
run_on_record(chain_json, record_json)
¶
Run the feedback function on the given record
. The chain
that
produced the record is also required to determine input/output argument
names.
Source code in trulens_eval/trulens_eval/tru_feedback.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 |
|
Huggingface
¶
Bases: Provider
Source code in trulens_eval/trulens_eval/tru_feedback.py
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 |
|
__init__()
¶
A set of Huggingface Feedback Functions. Utilizes huggingface api-inference
Source code in trulens_eval/trulens_eval/tru_feedback.py
810 811 812 813 814 815 |
|
language_match(text1, text2)
¶
Uses Huggingface's papluca/xlm-roberta-base-language-detection model. A
function that uses language detection on text1
and text2
and
calculates the probit difference on the language detected on text1. The
function is: 1.0 - (|probit_language_text1(text1) -
probit_language_text1(text2))
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text1 |
str
|
Text to evaluate. |
required |
text2 |
str
|
Comparative text to evaluate. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
A value between 0 and 1. 0 being "different languages" and 1 |
float
|
being "same languages". |
Source code in trulens_eval/trulens_eval/tru_feedback.py
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 |
|
not_toxic(text)
¶
Uses Huggingface's martin-ha/toxic-comment-model model. A function that
uses a toxic comment classifier on text
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
Text to evaluate. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
A value between 0 and 1. 0 being "toxic" and 1 being "not |
float
|
toxic". |
Source code in trulens_eval/trulens_eval/tru_feedback.py
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 |
|
positive_sentiment(text)
¶
Uses Huggingface's cardiffnlp/twitter-roberta-base-sentiment model. A
function that uses a sentiment classifier on text
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
Text to evaluate. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
A value between 0 and 1. 0 being "negative sentiment" and 1 |
float
|
being "positive sentiment". |
Source code in trulens_eval/trulens_eval/tru_feedback.py
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 |
|
OpenAI
¶
Bases: Provider
Source code in trulens_eval/trulens_eval/tru_feedback.py
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 773 774 |
|
__init__(model_engine='gpt-3.5-turbo')
¶
A set of OpenAI Feedback Functions.
- model_engine (str, optional): The specific model version. Defaults to "gpt-3.5-turbo".
Source code in trulens_eval/trulens_eval/tru_feedback.py
496 497 498 499 500 501 502 503 504 505 506 |
|
model_agreement(prompt, response)
¶
Uses OpenAI's Chat GPT Model. A function that gives Chat GPT the same prompt and gets a response, encouraging truthfulness. A second template is given to Chat GPT with a prompt that the original response is correct, and measures whether previous Chat GPT's response is similar.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt |
str
|
A text prompt to an agent. response (str): The agent's |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
A value between 0 and 1. 0 being "not in agreement" and 1 |
float
|
being "in agreement". |
Source code in trulens_eval/trulens_eval/tru_feedback.py
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 |
|
moderation_not_hate(text)
¶
Uses OpenAI's Moderation API. A function that checks if text is hate speech.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
Text to evaluate. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
A value between 0 and 1. 0 being "hate" and 1 being "not |
float
|
hate". |
Source code in trulens_eval/trulens_eval/tru_feedback.py
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
|
moderation_not_hatethreatening(text)
¶
Uses OpenAI's Moderation API. A function that checks if text is threatening speech.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
Text to evaluate. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
A value between 0 and 1. 0 being "threatening" and 1 being |
float
|
"not threatening". |
Source code in trulens_eval/trulens_eval/tru_feedback.py
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
|
moderation_not_selfharm(text)
¶
Uses OpenAI's Moderation API. A function that checks if text is about self harm.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
Text to evaluate. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
A value between 0 and 1. 0 being "self harm" and 1 being "not |
float
|
self harm". |
Source code in trulens_eval/trulens_eval/tru_feedback.py
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 |
|
moderation_not_sexual(text)
¶
Uses OpenAI's Moderation API. A function that checks if text is sexual speech.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
Text to evaluate. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
A value between 0 and 1. 0 being "sexual" and 1 being "not |
float
|
sexual". |
Source code in trulens_eval/trulens_eval/tru_feedback.py
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 |
|
moderation_not_sexualminors(text)
¶
Uses OpenAI's Moderation API. A function that checks if text is about sexual minors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
Text to evaluate. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
A value between 0 and 1. 0 being "sexual minors" and 1 being |
float
|
"not sexual minors". |
Source code in trulens_eval/trulens_eval/tru_feedback.py
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
|
moderation_not_violence(text)
¶
Uses OpenAI's Moderation API. A function that checks if text is about violence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
Text to evaluate. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
A value between 0 and 1. 0 being "violence" and 1 being "not |
float
|
violence". |
Source code in trulens_eval/trulens_eval/tru_feedback.py
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 |
|
moderation_not_violencegraphic(text)
¶
Uses OpenAI's Moderation API. A function that checks if text is about graphic violence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
Text to evaluate. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
A value between 0 and 1. 0 being "graphic violence" and 1 |
float
|
being "not graphic violence". |
Source code in trulens_eval/trulens_eval/tru_feedback.py
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 |
|
qs_relevance(question, statement)
¶
Uses OpenAI's Chat Completion Model. A function that completes a template to check the relevance of the statement to the question.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
question |
str
|
A question being asked. statement (str): A statement |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
A value between 0 and 1. 0 being "not relevant" and 1 being |
float
|
"relevant". |
Source code in trulens_eval/trulens_eval/tru_feedback.py
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 |
|
relevance(prompt, response)
¶
Uses OpenAI's Chat Completion Model. A function that completes a template to check the relevance of the response to a prompt.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt |
str
|
A text prompt to an agent. response (str): The agent's |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
A value between 0 and 1. 0 being "not relevant" and 1 being |
float
|
"relevant". |
Source code in trulens_eval/trulens_eval/tru_feedback.py
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 |
|
sentiment(text)
¶
Uses OpenAI's Chat Completion Model. A function that completes a template to check the sentiment of some text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
A prompt to an agent. response (str): The agent's |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
A value between 0 and 1. 0 being "negative sentiment" and 1 |
float
|
being "positive sentiment". |
Source code in trulens_eval/trulens_eval/tru_feedback.py
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 773 774 |
|