Completion
autogen.Completion #
Bases: Completion
(openai<1)
A class for OpenAI completion API.
It also supports: ChatCompletion, Azure OpenAI API.
chat_models class-attribute
instance-attribute
#
chat_models = {'gpt-3.5-turbo', 'gpt-3.5-turbo-0301', 'gpt-3.5-turbo-0613', 'gpt-3.5-turbo-16k', 'gpt-3.5-turbo-16k-0613', 'gpt-35-turbo', 'gpt-35-turbo-16k', 'gpt-4', 'gpt-4-32k', 'gpt-4-32k-0314', 'gpt-4-0314', 'gpt-4-0613', 'gpt-4-32k-0613'}
price1K class-attribute
instance-attribute
#
price1K = {'text-ada-001': 0.0004, 'text-babbage-001': 0.0005, 'text-curie-001': 0.002, 'code-cushman-001': 0.024, 'code-davinci-002': 0.1, 'text-davinci-002': 0.02, 'text-davinci-003': 0.02, 'gpt-3.5-turbo': (0.0015, 0.002), 'gpt-3.5-turbo-instruct': (0.0015, 0.002), 'gpt-3.5-turbo-0301': (0.0015, 0.002), 'gpt-3.5-turbo-0613': (0.0015, 0.002), 'gpt-3.5-turbo-16k': (0.003, 0.004), 'gpt-3.5-turbo-16k-0613': (0.003, 0.004), 'gpt-35-turbo': (0.0015, 0.002), 'gpt-35-turbo-16k': (0.003, 0.004), 'gpt-35-turbo-instruct': (0.0015, 0.002), 'gpt-4': (0.03, 0.06), 'gpt-4-32k': (0.06, 0.12), 'gpt-4-0314': (0.03, 0.06), 'gpt-4-32k-0314': (0.06, 0.12), 'gpt-4-0613': (0.03, 0.06), 'gpt-4-32k-0613': (0.06, 0.12)}
default_search_space class-attribute
instance-attribute
#
default_search_space = {'model': choice(['text-ada-001', 'text-babbage-001', 'text-davinci-003', 'gpt-3.5-turbo', 'gpt-4']), 'temperature_or_top_p': choice([{'temperature': uniform(0, 2)}, {'top_p': uniform(0, 1)}]), 'max_tokens': lograndint(50, 1000), 'n': randint(1, 100), 'prompt': '{prompt}'} if FLAML_INSTALLED else {}
openai_completion_class class-attribute
instance-attribute
#
set_cache classmethod
#
Set cache path.
PARAMETER | DESCRIPTION |
---|---|
seed | The integer identifier for the pseudo seed. Results corresponding to different seeds will be cached in different places. |
cache_path | The root path for the cache. The complete cache path will be {cache_path_root}/{seed}. |
Source code in autogen/oai/completion.py
clear_cache classmethod
#
Clear cache.
PARAMETER | DESCRIPTION |
---|---|
seed | The integer identifier for the pseudo seed. If omitted, all caches under cache_path_root will be cleared. |
cache_path | The root path for the cache. The complete cache path will be {cache_path_root}/{seed}. |
Source code in autogen/oai/completion.py
tune classmethod
#
tune(data, metric, mode, eval_func, log_file_name=None, inference_budget=None, optimization_budget=None, num_samples=1, logging_level=WARNING, **config)
Tune the parameters for the OpenAI API call.
TODO: support parallel tuning with ray or spark. TODO: support agg_method as in test
PARAMETER | DESCRIPTION |
---|---|
data | The list of data points. TYPE: |
metric | The metric to optimize. TYPE: |
mode | The optimization mode, "min" or "max. TYPE: |
eval_func | The evaluation function for responses. The function should take a list of responses and a data point as input, and return a dict of metrics. For example, TYPE: |
def eval_func(responses, **data):
solution = data["solution"]
success_list = []
n = len(responses)
for i in range(n):
response = responses[i]
succeed = is_equiv_chain_of_thought(response, solution)
success_list.append(succeed)
return {
"expected_success": 1 - pow(1 - sum(success_list) / n, n),
"success": any(s for s in success_list),
}
log_file_name (str, optional): The log file.
inference_budget (float, optional): The inference budget, dollar per instance.
optimization_budget (float, optional): The optimization budget, dollar in total.
num_samples (int, optional): The number of samples to evaluate.
-1 means no hard restriction in the number of trials
and the actual number is decided by optimization_budget. Defaults to 1.
logging_level (optional): logging level. Defaults to logging.WARNING.
**config (dict): The search space to update over the default search.
For prompt, please provide a string/Callable or a list of strings/Callables.
- If prompt is provided for chat models, it will be converted to messages under role "user".
- Do not provide both prompt and messages for chat models, but provide either of them.
- A string template will be used to generate a prompt for each data instance
using `prompt.format(**data)`.
- A callable template will be used to generate a prompt for each data instance
using `prompt(data)`.
For stop, please provide a string, a list of strings, or a list of lists of strings.
For messages (chat models only), please provide a list of messages (for a single chat prefix)
or a list of lists of messages (for multiple choices of chat prefix to choose from).
Each message should be a dict with keys "role" and "content". The value of "content" can be a string/Callable template.
RETURNS | DESCRIPTION |
---|---|
dict | The optimized hyperparameter setting. |
tune.ExperimentAnalysis: The tuning results. |
Source code in autogen/oai/completion.py
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 |
|
create classmethod
#
create(context=None, use_cache=True, config_list=None, filter_func=None, raise_on_ratelimit_or_timeout=True, allow_format_str_template=False, **config)
Make a completion for a given context.
PARAMETER | DESCRIPTION |
---|---|
context | The context to instantiate the prompt. It needs to contain keys that are used by the prompt template or the filter function. E.g., TYPE: |
use_cache | Whether to use cached responses. |
config_list | List of configurations for the completion to try. The first one that does not raise an error will be used. Only the differences from the default config need to be provided. E.g.,
TYPE: |
filter_func | A function that takes in the context and the response and returns a boolean to indicate whether the response is valid. E.g., |
raise_on_ratelimit_or_timeout | Whether to raise RateLimitError or Timeout when all configs fail. When set to False, -1 will be returned when all configs fail. |
allow_format_str_template | Whether to allow format string template in the config. |
**config | Configuration for the openai API call. This is used as parameters for calling openai API. The "prompt" or "messages" parameter can contain a template (str or Callable) which will be instantiated with the context. Besides the parameters for the openai API call, it can also contain: - DEFAULT: |
RETURNS | DESCRIPTION |
---|---|
Responses from OpenAI API, with additional fields. - | |
When |
Source code in autogen/oai/completion.py
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 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 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 |
|
instantiate classmethod
#
Source code in autogen/oai/completion.py
test classmethod
#
test(data, eval_func=None, use_cache=True, agg_method='avg', return_responses_and_per_instance_result=False, logging_level=WARNING, **config)
Evaluate the responses created with the config for the OpenAI API call.
PARAMETER | DESCRIPTION |
---|---|
data | The list of test data points. TYPE: |
eval_func | The evaluation function for responses per data instance. The function should take a list of responses and a data point as input, and return a dict of metrics. You need to either provide a valid callable eval_func; or do not provide one (set None) but call the test function after calling the tune function in which a eval_func is provided. In the latter case we will use the eval_func provided via tune function. Defaults to None. TYPE: |
def eval_func(responses, **data):
solution = data["solution"]
success_list = []
n = len(responses)
for i in range(n):
response = responses[i]
succeed = is_equiv_chain_of_thought(response, solution)
success_list.append(succeed)
return {
"expected_success": 1 - pow(1 - sum(success_list) / n, n),
"success": any(s for s in success_list),
}
An example agg_method in a dict of Callable:
return_responses_and_per_instance_result (bool): Whether to also return responses
and per instance results in addition to the aggregated results.
logging_level (optional): logging level. Defaults to logging.WARNING.
**config (dict): parameters passed to the openai api call `create()`.
RETURNS | DESCRIPTION |
---|---|
None when no valid eval_func is provided in either test or tune; | |
Otherwise, a dict of aggregated results, responses and per instance results if | |
Otherwise, a dict of aggregated results (responses and per instance results are not returned). |
Source code in autogen/oai/completion.py
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 |
|
cost classmethod
#
Compute the cost of an API call.
PARAMETER | DESCRIPTION |
---|---|
response | The response from OpenAI API. TYPE: |
RETURNS | DESCRIPTION |
---|---|
The cost in USD. 0 if the model is not supported. |
Source code in autogen/oai/completion.py
extract_text classmethod
#
Extract the text from a completion or chat response.
PARAMETER | DESCRIPTION |
---|---|
response | The response from OpenAI API. TYPE: |
RETURNS | DESCRIPTION |
---|---|
list[str] | A list of text in the responses. |
Source code in autogen/oai/completion.py
extract_text_or_function_call classmethod
#
Extract the text or function calls from a completion or chat response.
PARAMETER | DESCRIPTION |
---|---|
response | The response from OpenAI API. TYPE: |
RETURNS | DESCRIPTION |
---|---|
list[str] | A list of text or function calls in the responses. |
Source code in autogen/oai/completion.py
print_usage_summary classmethod
#
Return the usage summary.
Source code in autogen/oai/completion.py
start_logging classmethod
#
Start book keeping.
PARAMETER | DESCRIPTION |
---|---|
history_dict | A dictionary for book keeping. If no provided, a new one will be created. TYPE: |
compact | Whether to keep the history dictionary compact. Compact history contains one key per conversation, and the value is a dictionary like: TYPE: |
{
0: {
"request": request_dict_0,
"response": response_dict_0,
},
1: {
"request": request_dict_1,
"response": response_dict_1,
},