From ebed7de626bbd99b1f86b29a72b089144298d196 Mon Sep 17 00:00:00 2001 From: Darina Rustamova Date: Tue, 28 Jan 2025 13:09:14 +0300 Subject: [PATCH 1/8] fix: change location of prompts --- autointent/generation/{utterances => intents}/prompts.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename autointent/generation/{utterances => intents}/prompts.py (100%) diff --git a/autointent/generation/utterances/prompts.py b/autointent/generation/intents/prompts.py similarity index 100% rename from autointent/generation/utterances/prompts.py rename to autointent/generation/intents/prompts.py From f8a7564cd270c339120436f8bd38d8de29176c19 Mon Sep 17 00:00:00 2001 From: Darina Rustamova Date: Tue, 28 Jan 2025 16:33:31 +0300 Subject: [PATCH 2/8] feat: update evolution class --- .../evolution/chat_templates/formal.yaml | 33 ++++++++++++++++++ .../evolution/chat_templates/funny.yaml | 33 ++++++++++++++++++ .../evolution/chat_templates/goofy.yaml | 33 ++++++++++++++++++ .../evolution/chat_templates/informal.yaml | 33 ++++++++++++++++++ .../generation/utterances/evolution/cli.py | 34 +++++-------------- .../utterances/evolution/evolver.py | 10 +++--- 6 files changed, 146 insertions(+), 30 deletions(-) create mode 100644 autointent/generation/utterances/evolution/chat_templates/formal.yaml create mode 100644 autointent/generation/utterances/evolution/chat_templates/funny.yaml create mode 100644 autointent/generation/utterances/evolution/chat_templates/goofy.yaml create mode 100644 autointent/generation/utterances/evolution/chat_templates/informal.yaml diff --git a/autointent/generation/utterances/evolution/chat_templates/formal.yaml b/autointent/generation/utterances/evolution/chat_templates/formal.yaml new file mode 100644 index 000000000..7c5e6263c --- /dev/null +++ b/autointent/generation/utterances/evolution/chat_templates/formal.yaml @@ -0,0 +1,33 @@ +- role: user + content: | + {base_instruction} + 1. Rewrite the utterance in a more formal tone. + 2. Use polite and professional language while maintaining clarity. + 3. The rewritten utterance should be grammatically correct and complete. + 4. Keep the rewritten utterance within 15 words. + + Intent Class: + Reserve Restaurant + + Utterance: + I want to reserve a table for 4 persons at 9 pm. +- role: assistant + content: | + I would like to make a reservation for four guests at 9 pm. +- role: user + content: | + Intent Class: + requesting technical support + + Utterance: + My Lenovo laptop is constantly rebooting and overheating. +- role: assistant + content: | + My Lenovo laptop frequently restarts and experiences overheating issues. Kindly assist. +- role: user + content: | + Intent Class: + {intent_name} + + Utterance: + {utterance} diff --git a/autointent/generation/utterances/evolution/chat_templates/funny.yaml b/autointent/generation/utterances/evolution/chat_templates/funny.yaml new file mode 100644 index 000000000..cde6216b3 --- /dev/null +++ b/autointent/generation/utterances/evolution/chat_templates/funny.yaml @@ -0,0 +1,33 @@ +- role: user + content: | + {base_instruction} + 1. Rewrite the utterance in a humorous way while maintaining its original meaning. + 2. Use wordplay, exaggeration, or lighthearted phrasing. + 3. The rewritten utterance should still be understandable and relevant. + 4. Keep it within 15 words. + + Intent Class: + Reserve Restaurant + + Utterance: + I want to reserve a table for 4 persons at 9 pm. +- role: assistant + content: | + Gotta feed my squad at 9 pm. Got a table for us? +- role: user + content: | + Intent Class: + requesting technical support + + Utterance: + My Lenovo laptop is constantly rebooting and overheating. +- role: assistant + content: | + My Lenovo thinks it's a phoenix—keeps dying and rising in flames. +- role: user + content: | + Intent Class: + {intent_name} + + Utterance: + {utterance} diff --git a/autointent/generation/utterances/evolution/chat_templates/goofy.yaml b/autointent/generation/utterances/evolution/chat_templates/goofy.yaml new file mode 100644 index 000000000..a692eef6d --- /dev/null +++ b/autointent/generation/utterances/evolution/chat_templates/goofy.yaml @@ -0,0 +1,33 @@ +- role: user + content: | + {base_instruction} + 1. Rewrite the utterance in a silly, exaggerated, or nonsensical way while keeping the intent clear. + 2. Use playful words, randomness, or exaggeration. + 3. The rewritten utterance should still be answerable. + 4. Keep it under 15 words. + + Intent Class: + Reserve Restaurant + + Utterance: + I want to reserve a table for 4 persons at 9 pm. +- role: assistant + content: | + Need a feast for my hungry goblins at 9. Got room? +- role: user + content: | + Intent Class: + requesting technical support + + Utterance: + My Lenovo laptop is constantly rebooting and overheating. +- role: assistant + content: | + My laptop's having an existential crisis—keeps rebooting and melting. Help! +- role: user + content: | + Intent Class: + {intent_name} + + Utterance: + {utterance} diff --git a/autointent/generation/utterances/evolution/chat_templates/informal.yaml b/autointent/generation/utterances/evolution/chat_templates/informal.yaml new file mode 100644 index 000000000..1bf482d9c --- /dev/null +++ b/autointent/generation/utterances/evolution/chat_templates/informal.yaml @@ -0,0 +1,33 @@ +- role: user + content: | + {base_instruction} + 1. Rewrite the utterance in a more casual and relaxed tone. + 2. Use contractions, friendly language, and a conversational style. + 3. The rewritten utterance should feel natural in an informal conversation. + 4. Keep it under 15 words. + + Intent Class: + Reserve Restaurant + + Utterance: + I want to reserve a table for 4 persons at 9 pm. +- role: assistant + content: | + Hey, can I book a table for 4 at 9? +- role: user + content: | + Intent Class: + requesting technical support + + Utterance: + My Lenovo laptop is constantly rebooting and overheating. +- role: assistant + content: | + My Lenovo keeps crashing and getting super hot. Any ideas? +- role: user + content: | + Intent Class: + {intent_name} + + Utterance: + {utterance} diff --git a/autointent/generation/utterances/evolution/cli.py b/autointent/generation/utterances/evolution/cli.py index be04e11e7..f92153d5d 100644 --- a/autointent/generation/utterances/evolution/cli.py +++ b/autointent/generation/utterances/evolution/cli.py @@ -1,14 +1,15 @@ """CLI for evolutionary augmenter.""" from argparse import ArgumentParser -from typing import TYPE_CHECKING +from typing import get_args from autointent import load_dataset from autointent.generation.utterances.evolution.evolver import UtteranceEvolver from autointent.generation.utterances.generator import Generator -if TYPE_CHECKING: - from .evolver import EvolutionType +from .evolver import EvolutionType + +EVOLUTION_TYPES: list[EvolutionType] = list(get_args(EvolutionType)) def main() -> None: @@ -34,32 +35,13 @@ def main() -> None: ) parser.add_argument("--private", action="store_true", help="Publish privately if --output-repo option is used") parser.add_argument("--n-evolutions", type=int, default=1, help="Number of utterances to generate for each intent") - parser.add_argument("--reasoning", action="store_true", help="Whether to use `Reasoning` evolution") - parser.add_argument("--concretizing", action="store_true", help="Whether to use `Concretizing` evolution") - parser.add_argument("--abstract", action="store_true", help="Whether to use `Abstract` evolution") - parser.add_argument("--formal", action="store_true", help="Whether to use `Formal` evolution") - parser.add_argument("--informal", action="store_true", help="Whether to use `Informal` evolution") - parser.add_argument("--funny", action="store_true", help="Whether to use `Funny` evolution") - parser.add_argument("--goofy", action="store_true", help="Whether to use `Goofy` evolution") parser.add_argument("--seed", type=int, default=0) + parser.add_argument( + "--evolutions", nargs="+", choices=EVOLUTION_TYPES, required=True, help="Evolution types to apply." + ) args = parser.parse_args() - evolutions: list[EvolutionType] = [] - if args.reasoning: - evolutions.append("reasoning") - if args.concretizing: - evolutions.append("concretizing") - if args.abstract: - evolutions.append("abstract") - if args.formal: - evolutions.append("formal") - if args.informal: - evolutions.append("informal") - if args.funny: - evolutions.append("funny") - if args.goofy: - evolutions.append("goofy") - + evolutions: list[EvolutionType] = args.evolutions dataset = load_dataset(args.input_path) generator = UtteranceEvolver(Generator(), evolutions, args.seed) diff --git a/autointent/generation/utterances/evolution/evolver.py b/autointent/generation/utterances/evolution/evolver.py index 690afd234..1e1feb95b 100644 --- a/autointent/generation/utterances/evolution/evolver.py +++ b/autointent/generation/utterances/evolution/evolver.py @@ -83,10 +83,12 @@ def augment( def _load_prompts() -> dict[str, str]: files = ires.files("autointent.generation.utterances.evolution.chat_templates") - res = {} - for file_name in ["reasoning.yaml", "concretizing.yaml", "abstract.yaml", "base_instruction.txt"]: - with files.joinpath(file_name).open() as file: - res[file_name.split(".")[0]] = file.read() + + for file_path in files.iterdir(): + if (file_path.suffix == ".yaml" and file_path.is_file()) or (file_path.name == "base_instruction.txt"): + key = file_path.stem + with file_path.open() as file: + res[key] = file.read() return res From ad57a2ad3d3be48a875292a5465069a497f560d1 Mon Sep 17 00:00:00 2001 From: Darina Rustamova Date: Sun, 2 Feb 2025 13:36:57 +0300 Subject: [PATCH 3/8] feat: update chat templates --- .../evolution/chat_templates/formal.py | 54 +++++++++++++++++++ .../evolution/chat_templates/formal.yaml | 33 ------------ .../evolution/chat_templates/funny.py | 51 ++++++++++++++++++ .../evolution/chat_templates/funny.yaml | 33 ------------ .../evolution/chat_templates/goofy.py | 53 ++++++++++++++++++ .../evolution/chat_templates/goofy.yaml | 33 ------------ .../evolution/chat_templates/informal.py | 51 ++++++++++++++++++ .../evolution/chat_templates/informal.yaml | 33 ------------ 8 files changed, 209 insertions(+), 132 deletions(-) create mode 100644 autointent/generation/utterances/evolution/chat_templates/formal.py delete mode 100644 autointent/generation/utterances/evolution/chat_templates/formal.yaml create mode 100644 autointent/generation/utterances/evolution/chat_templates/funny.py delete mode 100644 autointent/generation/utterances/evolution/chat_templates/funny.yaml create mode 100644 autointent/generation/utterances/evolution/chat_templates/goofy.py delete mode 100644 autointent/generation/utterances/evolution/chat_templates/goofy.yaml create mode 100644 autointent/generation/utterances/evolution/chat_templates/informal.py delete mode 100644 autointent/generation/utterances/evolution/chat_templates/informal.yaml diff --git a/autointent/generation/utterances/evolution/chat_templates/formal.py b/autointent/generation/utterances/evolution/chat_templates/formal.py new file mode 100644 index 000000000..9172fd056 --- /dev/null +++ b/autointent/generation/utterances/evolution/chat_templates/formal.py @@ -0,0 +1,54 @@ +"""Chat template for formal tone augmentation.""" + +from typing import ClassVar + +from autointent.generation.utterances.schemas import Message, Role +from autointent.schemas import Intent + +from .base import EvolutionChatTemplate + + +class FormalEvolution(EvolutionChatTemplate): + """Chat template for formal tone augmentation.""" + + _messages: ClassVar[list[Message]] = [ + Message( + role=Role.USER, + content=( + "{base_instruction}\n" + "1. Rewrite the utterance in a more formal tone.\n" + "2. Use polite and professional language while maintaining clarity.\n" + "3. The rewritten utterance should be grammatically correct and complete.\n" + "4. Keep the rewritten utterance within 15 words.\n\n" + "Intent Class:\n" + "Reserve Restaurant\n\n" + "Utterance:\n" + "I want to reserve a table for 4 persons at 9 pm." + ), + ), + Message(role=Role.ASSISTANT, content="I would like to make a reservation for four guests at 9 pm."), + Message( + role=Role.USER, + content=( + "Intent Class:\n" + "requesting technical support\n\n" + "Utterance:\n" + "My Lenovo laptop is constantly rebooting and overheating." + ), + ), + Message( + role=Role.ASSISTANT, + content="My Lenovo laptop frequently restarts and experiences overheating issues. Kindly assist.", + ), + Message( + role=Role.USER, + content=("Intent Class:\n" "{intent_name}\n\n" "Utterance:\n" "{utterance}"), + ), + ] + + def __call__(self, utterance: str, intent_data: Intent) -> list[Message]: + """Generate chat for formal tone adaptation.""" + return [ + *self._messages, + Message(role=Role.USER, content=f"Intent Class:\n{intent_data.name or ''}\n\nUtterance:\n{utterance}"), + ] diff --git a/autointent/generation/utterances/evolution/chat_templates/formal.yaml b/autointent/generation/utterances/evolution/chat_templates/formal.yaml deleted file mode 100644 index 7c5e6263c..000000000 --- a/autointent/generation/utterances/evolution/chat_templates/formal.yaml +++ /dev/null @@ -1,33 +0,0 @@ -- role: user - content: | - {base_instruction} - 1. Rewrite the utterance in a more formal tone. - 2. Use polite and professional language while maintaining clarity. - 3. The rewritten utterance should be grammatically correct and complete. - 4. Keep the rewritten utterance within 15 words. - - Intent Class: - Reserve Restaurant - - Utterance: - I want to reserve a table for 4 persons at 9 pm. -- role: assistant - content: | - I would like to make a reservation for four guests at 9 pm. -- role: user - content: | - Intent Class: - requesting technical support - - Utterance: - My Lenovo laptop is constantly rebooting and overheating. -- role: assistant - content: | - My Lenovo laptop frequently restarts and experiences overheating issues. Kindly assist. -- role: user - content: | - Intent Class: - {intent_name} - - Utterance: - {utterance} diff --git a/autointent/generation/utterances/evolution/chat_templates/funny.py b/autointent/generation/utterances/evolution/chat_templates/funny.py new file mode 100644 index 000000000..6d36d8740 --- /dev/null +++ b/autointent/generation/utterances/evolution/chat_templates/funny.py @@ -0,0 +1,51 @@ +"""Chat template for humorous tone augmentation.""" + +from typing import ClassVar + +from autointent.generation.utterances.schemas import Message, Role +from autointent.schemas import Intent + +from .base import EvolutionChatTemplate + + +class FunnyEvolution(EvolutionChatTemplate): + """Chat template for humorous tone augmentation.""" + + _messages: ClassVar[list[Message]] = [ + Message( + role=Role.USER, + content=( + "{base_instruction}\n" + "1. Rewrite the utterance in a humorous way while maintaining its original meaning.\n" + "2. Use wordplay, exaggeration, or lighthearted phrasing.\n" + "3. The rewritten utterance should still be understandable and relevant.\n" + "4. Keep it within 15 words.\n\n" + "Intent Class:\n" + "Reserve Restaurant\n\n" + "Utterance:\n" + "I want to reserve a table for 4 persons at 9 pm." + ), + ), + Message(role=Role.ASSISTANT, content="Gotta feed my squad at 9 pm. Got a table for us?"), + Message( + role=Role.USER, + content=( + "Intent Class:\n" + "requesting technical support\n\n" + "Utterance:\n" + "My Lenovo laptop is constantly rebooting and overheating." + ), + ), + Message(role=Role.ASSISTANT, content="My Lenovo thinks it's a phoenix—keeps dying and rising in flames."), + Message( + role=Role.USER, + content=("Intent Class:\n" "{intent_name}\n\n" "Utterance:\n" "{utterance}"), + ), + ] + + def __call__(self, utterance: str, intent_data: Intent) -> list[Message]: + """Generate chat for humorous tone adaptation.""" + return [ + *self._messages, + Message(role=Role.USER, content=f"Intent Class:\n{intent_data.name or ''}\n\nUtterance:\n{utterance}"), + ] diff --git a/autointent/generation/utterances/evolution/chat_templates/funny.yaml b/autointent/generation/utterances/evolution/chat_templates/funny.yaml deleted file mode 100644 index cde6216b3..000000000 --- a/autointent/generation/utterances/evolution/chat_templates/funny.yaml +++ /dev/null @@ -1,33 +0,0 @@ -- role: user - content: | - {base_instruction} - 1. Rewrite the utterance in a humorous way while maintaining its original meaning. - 2. Use wordplay, exaggeration, or lighthearted phrasing. - 3. The rewritten utterance should still be understandable and relevant. - 4. Keep it within 15 words. - - Intent Class: - Reserve Restaurant - - Utterance: - I want to reserve a table for 4 persons at 9 pm. -- role: assistant - content: | - Gotta feed my squad at 9 pm. Got a table for us? -- role: user - content: | - Intent Class: - requesting technical support - - Utterance: - My Lenovo laptop is constantly rebooting and overheating. -- role: assistant - content: | - My Lenovo thinks it's a phoenix—keeps dying and rising in flames. -- role: user - content: | - Intent Class: - {intent_name} - - Utterance: - {utterance} diff --git a/autointent/generation/utterances/evolution/chat_templates/goofy.py b/autointent/generation/utterances/evolution/chat_templates/goofy.py new file mode 100644 index 000000000..702a815f7 --- /dev/null +++ b/autointent/generation/utterances/evolution/chat_templates/goofy.py @@ -0,0 +1,53 @@ +"""Chat template for goofy tone augmentation.""" + +from typing import ClassVar + +from autointent.generation.utterances.schemas import Message, Role +from autointent.schemas import Intent + +from .base import EvolutionChatTemplate + + +class GoofyEvolution(EvolutionChatTemplate): + """Chat template for goofy tone augmentation.""" + + _messages: ClassVar[list[Message]] = [ + Message( + role=Role.USER, + content=( + "{base_instruction}\n" + "1. Rewrite the utterance in a silly, exaggerated, or nonsensical way while keeping the intent clear.\n" + "2. Use playful words, randomness, or exaggeration.\n" + "3. The rewritten utterance should still be answerable.\n" + "4. Keep it under 15 words.\n\n" + "Intent Class:\n" + "Reserve Restaurant\n\n" + "Utterance:\n" + "I want to reserve a table for 4 persons at 9 pm." + ), + ), + Message(role=Role.ASSISTANT, content="Need a feast for my hungry goblins at 9. Got room?"), + Message( + role=Role.USER, + content=( + "Intent Class:\n" + "requesting technical support\n\n" + "Utterance:\n" + "My Lenovo laptop is constantly rebooting and overheating." + ), + ), + Message( + role=Role.ASSISTANT, content="My laptop's having an existential crisis—keeps rebooting and melting. Help!" + ), + Message( + role=Role.USER, + content=("Intent Class:\n" "{intent_name}\n\n" "Utterance:\n" "{utterance}"), + ), + ] + + def __call__(self, utterance: str, intent_data: Intent) -> list[Message]: + """Generate chat for goofy tone adaptation.""" + return [ + *self._messages, + Message(role=Role.USER, content=f"Intent Class:\n{intent_data.name or ''}\n\nUtterance:\n{utterance}"), + ] diff --git a/autointent/generation/utterances/evolution/chat_templates/goofy.yaml b/autointent/generation/utterances/evolution/chat_templates/goofy.yaml deleted file mode 100644 index a692eef6d..000000000 --- a/autointent/generation/utterances/evolution/chat_templates/goofy.yaml +++ /dev/null @@ -1,33 +0,0 @@ -- role: user - content: | - {base_instruction} - 1. Rewrite the utterance in a silly, exaggerated, or nonsensical way while keeping the intent clear. - 2. Use playful words, randomness, or exaggeration. - 3. The rewritten utterance should still be answerable. - 4. Keep it under 15 words. - - Intent Class: - Reserve Restaurant - - Utterance: - I want to reserve a table for 4 persons at 9 pm. -- role: assistant - content: | - Need a feast for my hungry goblins at 9. Got room? -- role: user - content: | - Intent Class: - requesting technical support - - Utterance: - My Lenovo laptop is constantly rebooting and overheating. -- role: assistant - content: | - My laptop's having an existential crisis—keeps rebooting and melting. Help! -- role: user - content: | - Intent Class: - {intent_name} - - Utterance: - {utterance} diff --git a/autointent/generation/utterances/evolution/chat_templates/informal.py b/autointent/generation/utterances/evolution/chat_templates/informal.py new file mode 100644 index 000000000..721281089 --- /dev/null +++ b/autointent/generation/utterances/evolution/chat_templates/informal.py @@ -0,0 +1,51 @@ +"""Chat template for informal tone augmentation.""" + +from typing import ClassVar + +from autointent.generation.utterances.schemas import Message, Role +from autointent.schemas import Intent + +from .base import EvolutionChatTemplate + + +class InformalEvolution(EvolutionChatTemplate): + """Chat template for informal tone augmentation.""" + + _messages: ClassVar[list[Message]] = [ + Message( + role=Role.USER, + content=( + "{base_instruction}\n" + "1. Rewrite the utterance in a more casual and relaxed tone.\n" + "2. Use contractions, friendly language, and a conversational style.\n" + "3. The rewritten utterance should feel natural in an informal conversation.\n" + "4. Keep it under 15 words.\n\n" + "Intent Class:\n" + "Reserve Restaurant\n\n" + "Utterance:\n" + "I want to reserve a table for 4 persons at 9 pm." + ), + ), + Message(role=Role.ASSISTANT, content="Hey, can I book a table for 4 at 9?"), + Message( + role=Role.USER, + content=( + "Intent Class:\n" + "requesting technical support\n\n" + "Utterance:\n" + "My Lenovo laptop is constantly rebooting and overheating." + ), + ), + Message(role=Role.ASSISTANT, content="My Lenovo keeps crashing and getting super hot. Any ideas?"), + Message( + role=Role.USER, + content=("Intent Class:\n" "{intent_name}\n\n" "Utterance:\n" "{utterance}"), + ), + ] + + def __call__(self, utterance: str, intent_data: Intent) -> list[Message]: + """Generate chat for informal tone adaptation.""" + return [ + *self._messages, + Message(role=Role.USER, content=f"Intent Class:\n{intent_data.name or ''}\n\nUtterance:\n{utterance}"), + ] diff --git a/autointent/generation/utterances/evolution/chat_templates/informal.yaml b/autointent/generation/utterances/evolution/chat_templates/informal.yaml deleted file mode 100644 index 1bf482d9c..000000000 --- a/autointent/generation/utterances/evolution/chat_templates/informal.yaml +++ /dev/null @@ -1,33 +0,0 @@ -- role: user - content: | - {base_instruction} - 1. Rewrite the utterance in a more casual and relaxed tone. - 2. Use contractions, friendly language, and a conversational style. - 3. The rewritten utterance should feel natural in an informal conversation. - 4. Keep it under 15 words. - - Intent Class: - Reserve Restaurant - - Utterance: - I want to reserve a table for 4 persons at 9 pm. -- role: assistant - content: | - Hey, can I book a table for 4 at 9? -- role: user - content: | - Intent Class: - requesting technical support - - Utterance: - My Lenovo laptop is constantly rebooting and overheating. -- role: assistant - content: | - My Lenovo keeps crashing and getting super hot. Any ideas? -- role: user - content: | - Intent Class: - {intent_name} - - Utterance: - {utterance} From 659e457dac4b1de7b4b087107e91534b08fe5174 Mon Sep 17 00:00:00 2001 From: Darina Rustamova Date: Sun, 2 Feb 2025 14:00:00 +0300 Subject: [PATCH 4/8] feat: added new evolutions --- .../evolution/chat_templates/__init__.py | 15 +++++++++- .../generation/utterances/evolution/cli.py | 28 ++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/autointent/generation/utterances/evolution/chat_templates/__init__.py b/autointent/generation/utterances/evolution/chat_templates/__init__.py index 8417ff1a6..748f69077 100644 --- a/autointent/generation/utterances/evolution/chat_templates/__init__.py +++ b/autointent/generation/utterances/evolution/chat_templates/__init__.py @@ -1,6 +1,19 @@ from .abstract import AbstractEvolution from .base import EvolutionChatTemplate from .concrete import ConcreteEvolution +from .formal import FormalEvolution +from .funny import FunnyEvolution +from .goofy import GoofyEvolution +from .informal import InformalEvolution from .reasoning import ReasoningEvolution -__all__ = ["AbstractEvolution", "ConcreteEvolution", "EvolutionChatTemplate", "ReasoningEvolution"] +__all__ = [ + "AbstractEvolution", + "ConcreteEvolution", + "EvolutionChatTemplate", + "ReasoningEvolution", + "FormalEvolution", + "FunnyEvolution", + "GoofyEvolution", + "InformalEvolution", +] diff --git a/autointent/generation/utterances/evolution/cli.py b/autointent/generation/utterances/evolution/cli.py index f05991ed1..fc271b45a 100644 --- a/autointent/generation/utterances/evolution/cli.py +++ b/autointent/generation/utterances/evolution/cli.py @@ -7,7 +7,16 @@ from autointent.generation.utterances.evolution.evolver import UtteranceEvolver from autointent.generation.utterances.generator import Generator -from .chat_templates import AbstractEvolution, ConcreteEvolution, EvolutionChatTemplate, ReasoningEvolution +from .chat_templates import ( + AbstractEvolution, + ConcreteEvolution, + EvolutionChatTemplate, + FormalEvolution, + FunnyEvolution, + GoofyEvolution, + InformalEvolution, + ReasoningEvolution, +) logging.basicConfig(level="INFO") logger = logging.getLogger(__name__) @@ -40,7 +49,12 @@ def main() -> None: parser.add_argument("--reasoning", action="store_true", help="Whether to use `Reasoning` evolution") parser.add_argument("--concretizing", action="store_true", help="Whether to use `Concretizing` evolution") parser.add_argument("--abstract", action="store_true", help="Whether to use `Abstract` evolution") + parser.add_argument("--formal", action="store_true", help="Whether to use `Formal` evolution") + parser.add_argument("--funny", action="store_true", help="Whether to use `Funny` evolution") + parser.add_argument("--goofy", action="store_true", help="Whether to use `Goofy` evolution") + parser.add_argument("--informal", action="store_true", help="Whether to use `Informal` evolution") parser.add_argument("--seed", type=int, default=0) + args = parser.parse_args() evolutions: list[EvolutionChatTemplate] = [] @@ -50,6 +64,18 @@ def main() -> None: evolutions.append(ConcreteEvolution()) if args.abstract: evolutions.append(AbstractEvolution()) + if args.formal: + evolutions.append(FormalEvolution()) + if args.funny: + evolutions.append(FunnyEvolution()) + if args.goofy: + evolutions.append(GoofyEvolution()) + if args.informal: + evolutions.append(InformalEvolution()) + + if not evolutions: + logger.warning("No evolutions selected. Exiting.") + return dataset = load_dataset(args.input_path) n_before = len(dataset[args.split]) From 90f8dd7e78c9dba591c1e39bbb55541bab86b56b Mon Sep 17 00:00:00 2001 From: Darina Rustamova Date: Sun, 2 Feb 2025 14:28:24 +0300 Subject: [PATCH 5/8] feat: change test structure & add test_generator --- tests/generation/intents/__init__.py | 0 .../test_description_generation.py | 0 tests/generation/utterances/__init__.py | 0 .../test_basic_synthesizer.py | 0 .../{ => utterances}/test_evolver.py | 0 tests/generation/utterances/test_generator.py | 38 +++++++++++++++++++ 6 files changed, 38 insertions(+) create mode 100644 tests/generation/intents/__init__.py rename tests/generation/{ => intents}/test_description_generation.py (100%) create mode 100644 tests/generation/utterances/__init__.py rename tests/generation/{ => utterances}/test_basic_synthesizer.py (100%) rename tests/generation/{ => utterances}/test_evolver.py (100%) create mode 100644 tests/generation/utterances/test_generator.py diff --git a/tests/generation/intents/__init__.py b/tests/generation/intents/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/generation/test_description_generation.py b/tests/generation/intents/test_description_generation.py similarity index 100% rename from tests/generation/test_description_generation.py rename to tests/generation/intents/test_description_generation.py diff --git a/tests/generation/utterances/__init__.py b/tests/generation/utterances/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/generation/test_basic_synthesizer.py b/tests/generation/utterances/test_basic_synthesizer.py similarity index 100% rename from tests/generation/test_basic_synthesizer.py rename to tests/generation/utterances/test_basic_synthesizer.py diff --git a/tests/generation/test_evolver.py b/tests/generation/utterances/test_evolver.py similarity index 100% rename from tests/generation/test_evolver.py rename to tests/generation/utterances/test_evolver.py diff --git a/tests/generation/utterances/test_generator.py b/tests/generation/utterances/test_generator.py new file mode 100644 index 000000000..667f3dc9c --- /dev/null +++ b/tests/generation/utterances/test_generator.py @@ -0,0 +1,38 @@ +from unittest.mock import MagicMock, patch + +import pytest + +from autointent.generation.utterances.generator import Generator +from autointent.generation.utterances.schemas import Message + + +@pytest.fixture(autouse=True) +def set_env_vars(monkeypatch): + monkeypatch.setenv("OPENAI_BASE_URL", "https://api.openai.com/v1") + monkeypatch.setenv("OPENAI_API_KEY", "fake-api-key") + monkeypatch.setenv("OPENAI_MODEL_NAME", "gpt-3.5-turbo") + + +@pytest.fixture +def mock_openai_client(): + with patch("openai.OpenAI") as mock_client: + yield mock_client + + +def test_generator_initialization(mock_openai_client): + generator = Generator() + assert generator.client == mock_openai_client.return_value + assert generator.model_name == "gpt-3.5-turbo" + + +def test_get_chat_completion(mock_openai_client): + mock_response = MagicMock() + mock_response.choices = [MagicMock(message=MagicMock(content="Test response"))] + mock_openai_client.return_value.chat.completions.create.return_value = mock_response + + generator = Generator() + messages = [Message(role="user", content="Hello")] + response = generator.get_chat_completion(messages) + + assert response == "Test response" + mock_openai_client.return_value.chat.completions.create.assert_called_once() From 7a054f1ae1845780c90a969a83cc1342909e8a7f Mon Sep 17 00:00:00 2001 From: Darina Rustamova Date: Sun, 2 Feb 2025 14:57:36 +0300 Subject: [PATCH 6/8] fix: fixex ruff --- autointent/generation/utterances/__init__.py | 32 +++++++++++++------ .../utterances/evolution/__init__.py | 23 +++++++++++-- .../evolution/chat_templates/__init__.py | 2 +- 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/autointent/generation/utterances/__init__.py b/autointent/generation/utterances/__init__.py index db54310ee..7534eb1a0 100644 --- a/autointent/generation/utterances/__init__.py +++ b/autointent/generation/utterances/__init__.py @@ -1,14 +1,28 @@ from .basic import SynthesizerChatTemplate, UtteranceGenerator -from .evolution import AbstractEvolution, ConcreteEvolution, EvolutionChatTemplate, ReasoningEvolution, UtteranceEvolver +from .evolution import ( + AbstractEvolution, + ConcreteEvolution, + EvolutionChatTemplate, + FormalEvolution, + FunnyEvolution, + GoofyEvolution, + InformalEvolution, + ReasoningEvolution, + UtteranceEvolver, +) from .generator import Generator __all__ = [ - "AbstractEvolution", - "ConcreteEvolution", - "EvolutionChatTemplate", - "Generator", - "ReasoningEvolution", - "SynthesizerChatTemplate", - "UtteranceEvolver", - "UtteranceGenerator", + "AbstractEvolution", + "ConcreteEvolution", + "EvolutionChatTemplate", + "FormalEvolution", + "FunnyEvolution", + "Generator", + "GoofyEvolution", + "InformalEvolution", + "ReasoningEvolution", + "SynthesizerChatTemplate", + "UtteranceEvolver", + "UtteranceGenerator", ] diff --git a/autointent/generation/utterances/evolution/__init__.py b/autointent/generation/utterances/evolution/__init__.py index 27007e7f1..7e352bd86 100644 --- a/autointent/generation/utterances/evolution/__init__.py +++ b/autointent/generation/utterances/evolution/__init__.py @@ -1,4 +1,23 @@ -from .chat_templates import AbstractEvolution, ConcreteEvolution, EvolutionChatTemplate, ReasoningEvolution +from .chat_templates import ( + AbstractEvolution, + ConcreteEvolution, + EvolutionChatTemplate, + FormalEvolution, + FunnyEvolution, + GoofyEvolution, + InformalEvolution, + ReasoningEvolution, +) from .evolver import UtteranceEvolver -__all__ = ["AbstractEvolution", "ConcreteEvolution", "EvolutionChatTemplate", "ReasoningEvolution", "UtteranceEvolver"] +__all__ = [ + "AbstractEvolution", + "ConcreteEvolution", + "EvolutionChatTemplate", + "FormalEvolution", + "FunnyEvolution", + "GoofyEvolution", + "InformalEvolution", + "ReasoningEvolution", + "UtteranceEvolver", +] diff --git a/autointent/generation/utterances/evolution/chat_templates/__init__.py b/autointent/generation/utterances/evolution/chat_templates/__init__.py index 748f69077..f6c4e2902 100644 --- a/autointent/generation/utterances/evolution/chat_templates/__init__.py +++ b/autointent/generation/utterances/evolution/chat_templates/__init__.py @@ -11,9 +11,9 @@ "AbstractEvolution", "ConcreteEvolution", "EvolutionChatTemplate", - "ReasoningEvolution", "FormalEvolution", "FunnyEvolution", "GoofyEvolution", "InformalEvolution", + "ReasoningEvolution", ] From 438ced18eb1bc604566c5af2d2749cfebf645685 Mon Sep 17 00:00:00 2001 From: Darina Rustamova Date: Sun, 2 Feb 2025 16:24:00 +0300 Subject: [PATCH 7/8] fix: fixed import --- autointent/generation/intents/prompt_scheme.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autointent/generation/intents/prompt_scheme.py b/autointent/generation/intents/prompt_scheme.py index b0883cff1..026e3ec83 100644 --- a/autointent/generation/intents/prompt_scheme.py +++ b/autointent/generation/intents/prompt_scheme.py @@ -2,7 +2,7 @@ from pydantic import BaseModel, field_validator -from autointent.generation.utterances.prompts import PROMPT_DESCRIPTION +from autointent.generation.intents.prompts import PROMPT_DESCRIPTION class PromptDescription(BaseModel): From fff039ce31ac6925f933aa3474ae6889b34c2063 Mon Sep 17 00:00:00 2001 From: Darina Rustamova Date: Sun, 2 Feb 2025 17:11:50 +0300 Subject: [PATCH 8/8] fix: fixed chat_templates --- .../evolution/chat_templates/abstract.py | 4 +-- .../evolution/chat_templates/concrete.py | 5 +++- .../evolution/chat_templates/formal.py | 24 +++++++---------- .../evolution/chat_templates/funny.py | 23 +++++++--------- .../evolution/chat_templates/goofy.py | 26 ++++++++----------- .../evolution/chat_templates/informal.py | 22 ++++++---------- 6 files changed, 43 insertions(+), 61 deletions(-) diff --git a/autointent/generation/utterances/evolution/chat_templates/abstract.py b/autointent/generation/utterances/evolution/chat_templates/abstract.py index 4ab88c359..20c2da5b2 100644 --- a/autointent/generation/utterances/evolution/chat_templates/abstract.py +++ b/autointent/generation/utterances/evolution/chat_templates/abstract.py @@ -28,9 +28,9 @@ class AbstractEvolution(EvolutionChatTemplate): ), Message(role=Role.ASSISTANT, content="Please, reserve a table for me."), Message( - role=Role.ASSISTANT, + role=Role.USER, content=( - "Intent name: requesting technical support" + "Intent name: requesting technical support\n" "Utterance: My Lenovo laptop is constantly rebooting and overheating." ), ), diff --git a/autointent/generation/utterances/evolution/chat_templates/concrete.py b/autointent/generation/utterances/evolution/chat_templates/concrete.py index dcca78bac..4a7ab52f2 100644 --- a/autointent/generation/utterances/evolution/chat_templates/concrete.py +++ b/autointent/generation/utterances/evolution/chat_templates/concrete.py @@ -29,7 +29,10 @@ class ConcreteEvolution(EvolutionChatTemplate): Message(role=Role.ASSISTANT, content="I want to reserve a table for 4 persons at 9 pm."), Message( role=Role.USER, - content=("Intent name: requesting technical support\n" "Utterance: I'm having trouble with my laptop."), + content=( + "Intent name: requesting technical support\n" + "Utterance: I'm having trouble with my laptop." + ), ), Message(role=Role.ASSISTANT, content="My laptop is constantly rebooting and overheating."), ] diff --git a/autointent/generation/utterances/evolution/chat_templates/formal.py b/autointent/generation/utterances/evolution/chat_templates/formal.py index 9172fd056..a527826c9 100644 --- a/autointent/generation/utterances/evolution/chat_templates/formal.py +++ b/autointent/generation/utterances/evolution/chat_templates/formal.py @@ -15,40 +15,34 @@ class FormalEvolution(EvolutionChatTemplate): Message( role=Role.USER, content=( - "{base_instruction}\n" + "I want you to act as a rewriter. " + "You will be provided with an utterance and the topic (name of intent class) of the utterance. " + "You need to rewrite the utterance in a more formal tone using the following method:\n" "1. Rewrite the utterance in a more formal tone.\n" "2. Use polite and professional language while maintaining clarity.\n" "3. The rewritten utterance should be grammatically correct and complete.\n" "4. Keep the rewritten utterance within 15 words.\n\n" - "Intent Class:\n" - "Reserve Restaurant\n\n" - "Utterance:\n" - "I want to reserve a table for 4 persons at 9 pm." + "Intent name: Reserve Restaurant" + "Utterance: I want to reserve a table for 4 persons at 9 pm." ), ), Message(role=Role.ASSISTANT, content="I would like to make a reservation for four guests at 9 pm."), Message( - role=Role.USER, + role=Role.ASSISTANT, content=( - "Intent Class:\n" - "requesting technical support\n\n" - "Utterance:\n" - "My Lenovo laptop is constantly rebooting and overheating." + "Intent name: requesting technical support\n" + "Utterance: My Lenovo laptop is constantly rebooting and overheating." ), ), Message( role=Role.ASSISTANT, content="My Lenovo laptop frequently restarts and experiences overheating issues. Kindly assist.", ), - Message( - role=Role.USER, - content=("Intent Class:\n" "{intent_name}\n\n" "Utterance:\n" "{utterance}"), - ), ] def __call__(self, utterance: str, intent_data: Intent) -> list[Message]: """Generate chat for formal tone adaptation.""" return [ *self._messages, - Message(role=Role.USER, content=f"Intent Class:\n{intent_data.name or ''}\n\nUtterance:\n{utterance}"), + Message(role=Role.USER, content=f"Intent name: {intent_data.name or ''}\nUtterance: {utterance}"), ] diff --git a/autointent/generation/utterances/evolution/chat_templates/funny.py b/autointent/generation/utterances/evolution/chat_templates/funny.py index 6d36d8740..0f401b464 100644 --- a/autointent/generation/utterances/evolution/chat_templates/funny.py +++ b/autointent/generation/utterances/evolution/chat_templates/funny.py @@ -15,37 +15,32 @@ class FunnyEvolution(EvolutionChatTemplate): Message( role=Role.USER, content=( - "{base_instruction}\n" + "I want you to act as a rewriter. " + "You will be provided with an utterance and the topic (name of intent class) of the utterance. " + "You need to rewrite the utterance in a humorous way while maintaining its original meaning using " + "the following method:\n" "1. Rewrite the utterance in a humorous way while maintaining its original meaning.\n" "2. Use wordplay, exaggeration, or lighthearted phrasing.\n" "3. The rewritten utterance should still be understandable and relevant.\n" "4. Keep it within 15 words.\n\n" - "Intent Class:\n" - "Reserve Restaurant\n\n" - "Utterance:\n" - "I want to reserve a table for 4 persons at 9 pm." + "Intent name: Reserve Restaurant" + "Utterance: I want to reserve a table for 4 persons at 9 pm." ), ), Message(role=Role.ASSISTANT, content="Gotta feed my squad at 9 pm. Got a table for us?"), Message( role=Role.USER, content=( - "Intent Class:\n" - "requesting technical support\n\n" - "Utterance:\n" - "My Lenovo laptop is constantly rebooting and overheating." + "Intent name: requesting technical support\n" + "Utterance: My Lenovo laptop is constantly rebooting and overheating." ), ), Message(role=Role.ASSISTANT, content="My Lenovo thinks it's a phoenix—keeps dying and rising in flames."), - Message( - role=Role.USER, - content=("Intent Class:\n" "{intent_name}\n\n" "Utterance:\n" "{utterance}"), - ), ] def __call__(self, utterance: str, intent_data: Intent) -> list[Message]: """Generate chat for humorous tone adaptation.""" return [ *self._messages, - Message(role=Role.USER, content=f"Intent Class:\n{intent_data.name or ''}\n\nUtterance:\n{utterance}"), + Message(role=Role.USER, content=f"Intent name: {intent_data.name or ''}\nUtterance: {utterance}"), ] diff --git a/autointent/generation/utterances/evolution/chat_templates/goofy.py b/autointent/generation/utterances/evolution/chat_templates/goofy.py index 702a815f7..15a6fcb17 100644 --- a/autointent/generation/utterances/evolution/chat_templates/goofy.py +++ b/autointent/generation/utterances/evolution/chat_templates/goofy.py @@ -15,33 +15,29 @@ class GoofyEvolution(EvolutionChatTemplate): Message( role=Role.USER, content=( - "{base_instruction}\n" + "I want you to act as a rewriter. " + "You will be provided with an utterance and the topic (name of intent class) of the utterance. " + "You need to rewrite the utterance in a silly, exaggerated, or nonsensical way while keeping " + "the intent clear using the following method:\n" "1. Rewrite the utterance in a silly, exaggerated, or nonsensical way while keeping the intent clear.\n" "2. Use playful words, randomness, or exaggeration.\n" "3. The rewritten utterance should still be answerable.\n" "4. Keep it under 15 words.\n\n" - "Intent Class:\n" - "Reserve Restaurant\n\n" - "Utterance:\n" - "I want to reserve a table for 4 persons at 9 pm." + "Intent name: Reserve Restaurant" + "Utterance: I want to reserve a table for 4 persons at 9 pm." ), ), Message(role=Role.ASSISTANT, content="Need a feast for my hungry goblins at 9. Got room?"), Message( role=Role.USER, content=( - "Intent Class:\n" - "requesting technical support\n\n" - "Utterance:\n" - "My Lenovo laptop is constantly rebooting and overheating." + "Intent name: requesting technical support\n" + "Utterance: My Lenovo laptop is constantly rebooting and overheating." ), ), Message( - role=Role.ASSISTANT, content="My laptop's having an existential crisis—keeps rebooting and melting. Help!" - ), - Message( - role=Role.USER, - content=("Intent Class:\n" "{intent_name}\n\n" "Utterance:\n" "{utterance}"), + role=Role.ASSISTANT, + content="My laptop's having an existential crisis—keeps rebooting and melting. Help!" ), ] @@ -49,5 +45,5 @@ def __call__(self, utterance: str, intent_data: Intent) -> list[Message]: """Generate chat for goofy tone adaptation.""" return [ *self._messages, - Message(role=Role.USER, content=f"Intent Class:\n{intent_data.name or ''}\n\nUtterance:\n{utterance}"), + Message(role=Role.USER, content=f"Intent name: {intent_data.name or ''}\nUtterance: {utterance}"), ] diff --git a/autointent/generation/utterances/evolution/chat_templates/informal.py b/autointent/generation/utterances/evolution/chat_templates/informal.py index 721281089..7d9243f20 100644 --- a/autointent/generation/utterances/evolution/chat_templates/informal.py +++ b/autointent/generation/utterances/evolution/chat_templates/informal.py @@ -15,37 +15,31 @@ class InformalEvolution(EvolutionChatTemplate): Message( role=Role.USER, content=( - "{base_instruction}\n" + "I want you to act as a rewriter. " + "You will be provided with an utterance and the topic (name of intent class) of the utterance. " + "You need to rewrite the utterance in a more casual and relaxed tone using the following method:\n" "1. Rewrite the utterance in a more casual and relaxed tone.\n" "2. Use contractions, friendly language, and a conversational style.\n" "3. The rewritten utterance should feel natural in an informal conversation.\n" "4. Keep it under 15 words.\n\n" - "Intent Class:\n" - "Reserve Restaurant\n\n" - "Utterance:\n" - "I want to reserve a table for 4 persons at 9 pm." + "Intent name: Reserve Restaurant" + "Utterance: I want to reserve a table for 4 persons at 9 pm." ), ), Message(role=Role.ASSISTANT, content="Hey, can I book a table for 4 at 9?"), Message( role=Role.USER, content=( - "Intent Class:\n" - "requesting technical support\n\n" - "Utterance:\n" - "My Lenovo laptop is constantly rebooting and overheating." + "Intent name: requesting technical support\n" + "Utterance: My Lenovo laptop is constantly rebooting and overheating." ), ), Message(role=Role.ASSISTANT, content="My Lenovo keeps crashing and getting super hot. Any ideas?"), - Message( - role=Role.USER, - content=("Intent Class:\n" "{intent_name}\n\n" "Utterance:\n" "{utterance}"), - ), ] def __call__(self, utterance: str, intent_data: Intent) -> list[Message]: """Generate chat for informal tone adaptation.""" return [ *self._messages, - Message(role=Role.USER, content=f"Intent Class:\n{intent_data.name or ''}\n\nUtterance:\n{utterance}"), + Message(role=Role.USER, content=f"Intent name: {intent_data.name or ''}\nUtterance: {utterance}"), ]