Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions autointent/_dataset/_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def _validate_classes(self, splits: list[list[Sample]]) -> int:
)
raise ValueError(message)
if not n_classes[0]:
message = "Number of classes is zero or undefined. " "Ensure at least one class is present in the splits."
message = "Number of classes is zero or undefined. Ensure at least one class is present in the splits."
raise ValueError(message)
return n_classes[0]

Expand All @@ -120,8 +120,7 @@ def _validate_intents(self, n_classes: int) -> "DatasetReader":
intent_ids = [intent.id for intent in self.intents]
if intent_ids != list(range(len(self.intents))):
message = (
f"Invalid intent IDs. Expected sequential IDs from 0 to {len(self.intents) - 1}, "
f"but got {intent_ids}."
f"Invalid intent IDs. Expected sequential IDs from 0 to {len(self.intents) - 1}, but got {intent_ids}."
)
raise ValueError(message)
return self
Expand Down
2 changes: 1 addition & 1 deletion autointent/context/data_handler/_data_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def _split_cv(self) -> None:
random_seed=self.random_seed,
allow_oos_in_train=True,
)
self.dataset[f"{Split.TRAIN}_{self.config.n_folds-1}"] = self.dataset.pop(Split.TRAIN)
self.dataset[f"{Split.TRAIN}_{self.config.n_folds - 1}"] = self.dataset.pop(Split.TRAIN)

def _split_validation_from_train(self, size: float) -> None:
if Split.TRAIN in self.dataset:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ def __call__(self, intent_data: Intent, n_examples: int) -> list[Message]:
]

def _create_final_message(self, intent_data: Intent, n_examples: int, sample_utterances: list[str]) -> Message:
content = f"{self._INTENT_NAME_LABEL}: {intent_data.name}\n\n" f"{self._EXAMPLE_UTTERANCES_LABEL}:\n"
content = f"{self._INTENT_NAME_LABEL}: {intent_data.name}\n\n{self._EXAMPLE_UTTERANCES_LABEL}:\n"

if sample_utterances:
numbered_utterances = "\n".join(f"{i+1}. {utt}" for i, utt in enumerate(sample_utterances))
numbered_utterances = "\n".join(f"{i + 1}. {utt}" for i, utt in enumerate(sample_utterances))
content += numbered_utterances + "\n\n"

content += self._GENERATE_INSTRUCTION.format(n_examples=n_examples)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class EnglishSynthesizerTemplate(BaseSynthesizerTemplate):
),
Message(
role=Role.ASSISTANT,
content=("1. Can you tell me the forecast for tomorrow?\n" "2. Is it going to rain this weekend?"),
content="1. Can you tell me the forecast for tomorrow?\n2. Is it going to rain this weekend?",
),
Message(
role=Role.USER,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class RussianSynthesizerTemplate(BaseSynthesizerTemplate):
),
Message(
role=Role.ASSISTANT,
content=("1. Забронируйте люкс в Санкт-Петербурге на выходные\n" "2. Ищу номер с видом на море в Сочи"),
content=("1. Забронируйте люкс в Санкт-Петербурге на выходные\n2. Ищу номер с видом на море в Сочи"),
),
Message(
role=Role.USER,
Expand All @@ -66,7 +66,7 @@ class RussianSynthesizerTemplate(BaseSynthesizerTemplate):
),
Message(
role=Role.ASSISTANT,
content=("1. Какой прогноз на завтра?\n" "2. Будет ли дождь в субботу?"),
content=("1. Какой прогноз на завтра?\n2. Будет ли дождь в субботу?"),
),
Message(
role=Role.USER,
Expand Down
7 changes: 2 additions & 5 deletions autointent/generation/utterances/basic/utterance_generator.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
"""Basic generation of new utterances from existing ones."""

import asyncio
from collections.abc import Callable

from datasets import Dataset as HFDataset
from datasets import concatenate_datasets

from autointent import Dataset
from autointent.custom_types import Split
from autointent.generation.utterances.basic.chat_templates import BaseSynthesizerTemplate
from autointent.generation.utterances.generator import Generator
from autointent.generation.utterances.schemas import Message
from autointent.schemas import Intent, Sample


Expand All @@ -22,9 +21,7 @@ class UtteranceGenerator:
punctuation, and length of the desired generations.
"""

def __init__(
self, generator: Generator, prompt_maker: Callable[[Intent, int], list[Message]], async_mode: bool = False
) -> None:
def __init__(self, generator: Generator, prompt_maker: BaseSynthesizerTemplate, async_mode: bool = False) -> None:
"""Initialize."""
self.generator = generator
self.prompt_maker = prompt_maker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
from .informal import InformalEvolution
from .reasoning import ReasoningEvolution

EVOLUTION_NAMES = [evolution.name for evolution in EvolutionChatTemplate.__subclasses__()]

EVOLUTION_MAPPING = {evolution.name: evolution() for evolution in EvolutionChatTemplate.__subclasses__()}

__all__ = [
"EVOLUTION_MAPPING",
"EVOLUTION_NAMES",
"AbstractEvolution",
"ConcreteEvolution",
"EvolutionChatTemplate",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
from typing import ClassVar

from autointent.generation.utterances.schemas import Message, Role
from autointent.schemas import Intent

from .base import EvolutionChatTemplate


class AbstractEvolution(EvolutionChatTemplate):
"""Chat template for evolution augmentation via abstraction."""

name = "abstract"
_messages: ClassVar[list[Message]] = [
Message(
role=Role.USER,
Expand All @@ -36,10 +36,3 @@ class AbstractEvolution(EvolutionChatTemplate):
),
Message(role=Role.ASSISTANT, content="I'm having trouble with my laptop."),
]

def __call__(self, utterance: str, intent_data: Intent) -> list[Message]:
"""Make chat to complete."""
return [
*self._messages,
Message(role=Role.USER, content=f"Intent name: {intent_data.name or ''}\nUtterance: {utterance}"),
]
15 changes: 11 additions & 4 deletions autointent/generation/utterances/evolution/chat_templates/base.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
"""Base class for chat templates for evolution augmentation."""

from abc import ABC, abstractmethod
from typing import ClassVar

from autointent.generation.utterances.schemas import Message
from autointent.generation.utterances.schemas import Message, Role
from autointent.schemas import Intent


class EvolutionChatTemplate(ABC):
class EvolutionChatTemplate:
"""Base class for chat templates for evolution augmentation."""

@abstractmethod
_messages: ClassVar[list[Message]]
name: str

def __call__(self, utterance: str, intent_data: Intent) -> list[Message]:
"""Make a chat to complete by LLM."""
invoke_message = Message(
role=Role.USER,
content=f"Intent name: {intent_data.name or ''}\nUtterance: {utterance}",
)
return [*self._messages, invoke_message]
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
from typing import ClassVar

from autointent.generation.utterances.schemas import Message, Role
from autointent.schemas import Intent

from .base import EvolutionChatTemplate


class ConcreteEvolution(EvolutionChatTemplate):
"""Chat template for evolution augmentation via concretizing."""

name = "concrete"

_messages: ClassVar[list[Message]] = [
Message(
role=Role.USER,
Expand All @@ -29,14 +30,7 @@ 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\nUtterance: I'm having trouble with my laptop.",
),
Message(role=Role.ASSISTANT, content="My laptop is constantly rebooting and overheating."),
]

def __call__(self, utterance: str, intent_data: Intent) -> list[Message]:
"""Make chat to complete."""
return [
*self._messages,
Message(role=Role.USER, content=f"Intent name: {intent_data.name or ''}\nUtterance: {utterance}"),
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
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."""

name: str = "formal"

_messages: ClassVar[list[Message]] = [
Message(
role=Role.USER,
Expand Down Expand Up @@ -39,10 +40,3 @@ class FormalEvolution(EvolutionChatTemplate):
content="My Lenovo laptop frequently restarts and experiences overheating issues. Kindly assist.",
),
]

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 name: {intent_data.name or ''}\nUtterance: {utterance}"),
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
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."""

name: str = "funny"
_messages: ClassVar[list[Message]] = [
Message(
role=Role.USER,
Expand All @@ -37,10 +37,3 @@ class FunnyEvolution(EvolutionChatTemplate):
),
Message(role=Role.ASSISTANT, content="My Lenovo thinks it's a phoenix—keeps dying and rising in flames."),
]

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 name: {intent_data.name or ''}\nUtterance: {utterance}"),
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
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."""

name: str = "goofy"
_messages: ClassVar[list[Message]] = [
Message(
role=Role.USER,
Expand Down Expand Up @@ -39,10 +39,3 @@ class GoofyEvolution(EvolutionChatTemplate):
role=Role.ASSISTANT, content="My laptop's having an existential crisis—keeps rebooting and melting. Help!"
),
]

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 name: {intent_data.name or ''}\nUtterance: {utterance}"),
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
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."""

name: str = "informal"
_messages: ClassVar[list[Message]] = [
Message(
role=Role.USER,
Expand All @@ -36,10 +36,3 @@ class InformalEvolution(EvolutionChatTemplate):
),
Message(role=Role.ASSISTANT, content="My Lenovo keeps crashing and getting super hot. Any ideas?"),
]

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 name: {intent_data.name or ''}\nUtterance: {utterance}"),
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
from typing import ClassVar

from autointent.generation.utterances.schemas import Message, Role
from autointent.schemas import Intent

from .base import EvolutionChatTemplate


class ReasoningEvolution(EvolutionChatTemplate):
"""Chat template for evolution augmentation via reasoning."""

name = "reasoning"

_messages: ClassVar[list[Message]] = [
Message(
role=Role.USER,
Expand All @@ -36,10 +37,3 @@ class ReasoningEvolution(EvolutionChatTemplate):
),
Message(role=Role.ASSISTANT, content="I don't know what's happening with my laptop."),
]

def __call__(self, utterance: str, intent_data: Intent) -> list[Message]:
"""Make chat to complete."""
return [
*self._messages,
Message(role=Role.USER, content=f"Intent name: {intent_data.name or ''}\nUtterance: {utterance}"),
]
Loading