Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exemplar Selection (in context learning) API #1055

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions flaml/autogen/icl/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from flaml.autogen.icl.exemplar_selector import ExemplarSelector

__all__ = ["ExemplarSelector"]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from functools import partial
import random
import numpy as np
from flaml.autogen.oai.selection_methods import RandomSelect
from flaml.autogen.icl.selection_methods import RandomSelect

class ExemplarSelector:
METHOD_MAPPING = {
Expand Down Expand Up @@ -42,10 +42,10 @@ def construct_template(context, train_data, method=None, few_shot_template=None,
@staticmethod
def default_template(context, exemplars, key_order):
few_shot_prompt = ""
for examplar in exemplars:
for exemplar in exemplars:
few_shot_prompt += "\n".join(
[
key + ": " + str(examplar[key]) for key in key_order
key + ": " + str(exemplar[key]) for key in key_order
]
) + "\n"
few_shot_prompt += "\n".join(
Expand Down
3 changes: 1 addition & 2 deletions flaml/autogen/oai/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from flaml.autogen.oai.completion import Completion, ChatCompletion
from flaml.autogen.oai.openai_utils import get_config_list, config_list_gpt4_gpt35, config_list_openai_aoai
from flaml.autogen.oai.exemplar_selector import ExemplarSelector

__all__ = ["Completion", "ChatCompletion", "get_config_list", "config_list_gpt4_gpt35", "config_list_openai_aoai", "ExemplarSelector"]
__all__ = ["Completion", "ChatCompletion", "get_config_list", "config_list_gpt4_gpt35", "config_list_openai_aoai"]
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest
import datasets
from flaml import oai
from flaml import icl
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from flaml import icl
from flaml.autogen.icl import ExemplarSelector


class TestExemplarSelector(unittest.TestCase):
'''
Expand All @@ -19,7 +19,7 @@ def setUpClass(cls):

def test_case_existing_method_default_template(self):
# Most cases should use the default template and existing methods
prompt_fn = oai.ExemplarSelector.get_few_shot_template(self.exemplars, method="random",
prompt_fn = icl.ExemplarSelector.get_few_shot_template(self.exemplars, method="random",
method_params={"k": 3}, template_params={"key_order": self.key_order})
output= prompt_fn(self.context)
#print("Existing method + default template: prompt = ", output)
Expand All @@ -45,7 +45,7 @@ def few_shot_template(context, exemplars=None):
)
few_shot_prompt += "\n" + key_order[-1] + ": " + "\n"
return few_shot_prompt
prompt_fn = oai.ExemplarSelector.get_few_shot_template(self.exemplars,
prompt_fn = icl.ExemplarSelector.get_few_shot_template(self.exemplars,
few_shot_template=few_shot_template,
method_params={"k": 3})
output= prompt_fn(self.context)
Expand All @@ -65,7 +65,7 @@ def test_case_user_method_no_k(self):
def user_method(context):
return self.exemplars[3:5]
# key_order should be provided if we use the default template
prompt_fn = oai.ExemplarSelector.get_few_shot_template(self.exemplars,
prompt_fn = icl.ExemplarSelector.get_few_shot_template(self.exemplars,
method = user_method,
template_params={"key_order": self.key_order})
output= prompt_fn(self.context)
Expand All @@ -82,7 +82,7 @@ def user_method(context):

def test_invalid_method(self):
with self.assertRaises(ValueError):
oai.ExemplarSelector.get_few_shot_template(self.exemplars, method="nonexistent")
icl.ExemplarSelector.get_few_shot_template(self.exemplars, method="nonexistent")



Expand Down