Coverage for src/appl/role_changer.py: 94%
31 statements
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-22 15:39 -0800
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-22 15:39 -0800
1from typing import Any, Optional
3from typing_extensions import override
5from .core.context import PromptContext
6from .core.modifiers import PrinterModifier, PrinterPush
7from .core.types import MessageRole, MessageRoleType
10class RoleChanger(PrinterModifier):
11 """The contextual role changer of the prompts."""
13 _new_role: Optional[MessageRole] = None
15 def __init__(
16 self, role: Optional[MessageRole] = None, _ctx: Optional[PromptContext] = None
17 ):
18 """Initialize the RoleChanger object.
20 Args:
21 role: The new role of the prompts. Defaults to None.
22 _ctx: The prompt context filled automatically by the APPL function.
23 """
24 super().__init__(_ctx)
25 if role is not None:
26 self._new_role = role
28 @override
29 @property
30 def push_args(self) -> PrinterPush:
31 return PrinterPush(new_role=self._new_role)
34class SystemRole(RoleChanger):
35 """Change the role of the prompts to system."""
37 def __init__(self, name: Optional[str] = None, **kwargs: Any):
38 """Initialize the SystemRole object.
40 Args:
41 name: The name of the system role. Defaults to None.
42 **kwargs: The keyword arguments to pass to the RoleChanger constructor.
43 """
44 role = MessageRole(MessageRoleType.SYSTEM, name=name)
45 super().__init__(role=role, **kwargs)
48class UserRole(RoleChanger):
49 """Change the role of the prompts to user."""
51 def __init__(self, name: Optional[str] = None, **kwargs: Any):
52 """Initialize the UserRole object.
54 Args:
55 name: The name of the user role. Defaults to None.
56 **kwargs: The keyword arguments to pass to the RoleChanger constructor.
57 """
58 role = MessageRole(MessageRoleType.USER, name=name)
59 super().__init__(role=role, **kwargs)
62class AIRole(RoleChanger):
63 """Change the role of the prompts to assistant."""
65 def __init__(self, name: Optional[str] = None, **kwargs: Any):
66 """Initialize the AIRole object.
68 Args:
69 name: The name of the assistant role. Defaults to None.
70 **kwargs: The keyword arguments to pass to the Role
71 """
72 role = MessageRole(MessageRoleType.ASSISTANT, name=name)
73 super().__init__(role=role, **kwargs)
76class ToolRole(RoleChanger):
77 """Change the role of the prompts to tool."""
79 def __init__(self, name: Optional[str] = None, **kwargs: Any):
80 """Initialize the ToolRole object.
82 Args:
83 name: The name of the tool role. Defaults to None.
84 **kwargs: The keyword arguments to pass to the RoleChanger constructor.
85 """
86 role = MessageRole(MessageRoleType.TOOL, name=name)
87 super().__init__(role=role, **kwargs)