refactor(Action): Update property names to allow automatic doc gen

This commit is contained in:
Michael Poutre
2023-11-19 00:26:19 -08:00
parent b5a728a997
commit e002d0ad64
16 changed files with 128 additions and 22 deletions
+16 -2
View File
@@ -39,9 +39,23 @@ class Action(ABC):
@property
@abstractmethod
def name(self) -> str:
def display_name(self) -> str:
pass
@property
@abstractmethod
def action_name(self) -> str:
pass
@property
@abstractmethod
def description(self) -> str:
pass
@property
def usage_examples(self) -> List[str]:
return []
@property
@abstractmethod
def grammar(self) -> str:
@@ -116,7 +130,7 @@ class SingleArgAction(Action, ABC):
self.arg = arg
def __repr__(self) -> str:
return f"{self.name}({self.arg})"
return f"{self.display_name}({self.arg})"
class MultiArgAction(Action, ABC):
arity = ActionArity.MULTI
+9 -1
View File
@@ -12,9 +12,17 @@ from custom_nodes.KepPromptLang.lib.parser.prompt_segment import PromptSegment
class AverageAction(MultiArgAction):
grammar = 'avg(" arg "|" arg "|" arg ")"'
name = "avg"
chars = ["+", "+"]
display_name = "Average"
action_name = "avg"
description = "Performs a weighted average between two segments or actions. The recommended weight is 0 - 1."
usage_examples = [
"avg(The cat is|The dog is|0.5)",
"avg(Cat|Dog|0.5)",
]
def __init__(self, args: List[List[Union[PromptSegment, Action]]]) -> None:
super().__init__(args)
+9 -1
View File
@@ -11,9 +11,17 @@ from custom_nodes.KepPromptLang.lib.parser.registration import register_action
class DiffAction(MultiArgAction):
grammar = 'diff(" arg ("|" arg)* ")"'
name = "diff"
chars = ["-", "-"]
display_name = "Difference"
action_name = "diff"
description = "Subtracts the segments in the order they are given. The first segment is subtracted from the second, then the third from the result, and so on."
usage_examples = [
"diff(The cat is|The dog is)",
"diff(Cat|Dog)",
"sum(diff(king|man)|woman)",
]
def __init__(self, args: List[List[Union[PromptSegment, Action]]]):
super().__init__(args)
self.base_arg = args[0]
+8 -1
View File
@@ -13,9 +13,16 @@ from custom_nodes.KepPromptLang.lib.parser.registration import register_action
class MultiplyAction(MultiArgAction):
grammar = 'mult(" arg+ ")"'
name = "mult"
chars = ["[", "]"]
display_name = "Multiply"
action_name = "mult"
description = "Multiplies the provided segments or actions by the multiplier."
usage_examples = [
"mult(The cat is|2.5)",
"mult(Cat|-1)",
]
def __init__(self, args: List[List[SegOrAction]]) -> None:
super().__init__(args)
if len(args) != 2:
+8 -1
View File
@@ -7,9 +7,16 @@ from custom_nodes.KepPromptLang.lib.parser.registration import register_action
class NegAction(SingleArgAction):
grammar = 'neg(" arg+ ")"'
name = "neg"
chars = ["[", "]"]
display_name = "Negate"
action_name = "neg"
description = "Negates the provided segments or actions."
usage_examples = [
"neg(cat)",
"sum(king|neg(man)|women)",
]
def token_length(self) -> int:
"""
Neg negates the embeddings of the base segment, so the length is the length of the base segment
+8 -1
View File
@@ -10,9 +10,16 @@ from custom_nodes.KepPromptLang.lib.parser.registration import register_action
class NormAction(SingleArgAction):
grammar = 'norm(" arg+ ")"'
name = "norm"
chars = None
display_name = "Normalize"
action_name = "norm"
description = "Normalizes the provided segments or actions."
usage_examples = [
"norm(cat)",
"sum(cat|norm(sum(tiger|fish)))",
]
def token_length(self) -> int:
"""
Norm normalizes the embeddings of the base segment, so the length is the length of the base segment
+8 -1
View File
@@ -14,9 +14,16 @@ from custom_nodes.KepPromptLang.lib.parser.prompt_segment import PromptSegment
class PooledAvgAction(SingleArgAction):
grammar = 'pooledAvg(" arg+ ")"'
name = "_exp-pooledAvg"
chars = ["[", "]"]
display_name = "Pooled Average(Experimental)"
action_name = "_exp-pooledAvg"
description = "Processes the provided segments or actions fully through CLIP and creates a pooled average of the last hidden state by averaging the last hidden state of each token."
usage_examples = [
"A cat on a _exp-pooledAvg(beautiful sunny day)",
"A _exp-pooledAvg(broken glass) bottle",
]
def __init__(self, args):
super().__init__(args)
self.result = None
+9 -1
View File
@@ -14,9 +14,17 @@ from custom_nodes.KepPromptLang.lib.parser.prompt_segment import PromptSegment
class PoolerAction(SingleArgAction):
grammar = 'pooler(" arg+ ")"'
name = "_exp-pooler"
chars = ["[", "]"]
display_name = "Pooler Output(Experimental)"
action_name = "_exp-pooler"
description = "Processes the provided segments or actions fully through CLIP and returns the pooler_output from the transformer"
usage_examples = [
"A cat on a _exp-pooler(beautiful sunny day)",
"A _exp-pooler(broken glass) bottle",
]
def __init__(self, args):
super().__init__(args)
self.result = None
+7 -1
View File
@@ -13,9 +13,15 @@ from custom_nodes.KepPromptLang.lib.actions.types import SegOrAction
class PosScaleAction(MultiArgAction):
grammar = 'posScale(" arg+ ")"'
name = "posScale"
chars = ["[", "]"]
display_name = "Positional Embedding Scale"
action_name = "posScale"
description = "Scales(Multiplies) the positional embeddings of the provided segments or actions by the multiplier."
usage_examples = [
"A posScale(cat|1.5) on a rainy day",
]
def __init__(self, args: List[List[SegOrAction]]) -> None:
super().__init__(args)
if len(args) != 2:
+7 -1
View File
@@ -13,9 +13,15 @@ from custom_nodes.KepPromptLang.lib.action.base import (
class PostPosAction(SingleArgAction):
grammar = 'postPos(" arg+ ")"'
name = "postPos"
chars = ["[", "]"]
display_name = "Ignore Positional Embeddings"
action_name = "postPos"
description = "Prevents positional embeddings from being applied to the provided segments or actions."
usage_examples = [
"A postPos(cat) on a rainy day",
]
def __init__(self, args):
super().__init__(args)
self.result = None
+8
View File
@@ -17,6 +17,14 @@ class RandAction(MultiArgAction):
name = "rand"
chars = None
display_name = "Random Embedding"
action_name = "rand"
description = "Returns a random embedding of the specified token length, with the values optionally bounded by the second and third arguments."
usage_examples = [
"A rand(1) cat",
"A rand(1|-1|1) cat",
]
parsed_token_length = 0
range_min = 0
range_max = 1
+7 -3
View File
@@ -10,11 +10,15 @@ from custom_nodes.KepPromptLang.lib.parser.prompt_segment import PromptSegment
class ScaleDims(MultiArgAction):
grammar = 'scaleDims(" arg ("|" arg)* ")"'
name = "scaleDims"
description = "Scales the specified dimensions of the input embeddings by the specified amount"
example = "'The scaleDims(cat|4,1.5|76,1.2) is happy' scales the 4th dimension by 1.5 and the 76th dimension by 1.2 for the word 'cat'"
chars = ["-", "-"]
display_name = "Scale Dimensions"
action_name = "scaleDims"
description = "Scales the specified dimensions of the input embeddings by the specified amount"
usage_examples = [
"The scaleDims(cat|4,1.5|76,1.2) is happy",
]
def __init__(self, args: List[List[Union[PromptSegment, Action]]]):
super().__init__(args)
+7 -3
View File
@@ -10,11 +10,15 @@ from custom_nodes.KepPromptLang.lib.parser.prompt_segment import PromptSegment
class SetDims(MultiArgAction):
grammar = 'setDims(" arg ("|" arg)* ")"'
name = "setDims"
description = "Sets the specified dimensions of the input embeddings to the specified value"
example = "'The scaleDims(cat|4,1.5|76,1.2) is happy' scales the 4th dimension by 1.5 and the 76th dimension by 1.2 for the word 'cat'"
chars = ["-", "-"]
display_name = "Set Dimensions"
action_name = "setDims"
description = "Sets the specified dimensions of the input embeddings to the specified value"
usage_examples = [
"The setDims(cat|4, -0.01253|76, 1.2) is happy"
]
def __init__(self, args: List[List[Union[PromptSegment, Action]]]):
super().__init__(args)
+7 -1
View File
@@ -12,9 +12,15 @@ from custom_nodes.KepPromptLang.lib.parser.prompt_segment import PromptSegment
class SlerpAction(MultiArgAction):
grammar = 'slerp(" arg "|" arg "|" arg ")"'
name = "slerp"
chars = ["+", "+"]
display_name = "Slerp"
action_name = "slerp"
description = "Performs a slerp(Interpolation) between two segments or actions, with the given weight. The recommended weight is 0 - 1"
usage_examples = [
"The slerp(cat|dog|0.5) is happy",
]
def __init__(self, args: List[List[Union[PromptSegment, Action]]]) -> None:
super().__init__(args)
+7 -1
View File
@@ -11,9 +11,15 @@ from custom_nodes.KepPromptLang.lib.parser.registration import register_action
class SumAction(MultiArgAction):
grammar = 'sum(" arg ("|" arg)+ ")"'
name = "sum"
chars = ["+", "+"]
display_name = "Sum"
action_name = "sum"
description = "Adds the embeddings of the provided segments or actions."
usage_examples = [
"A happy sum(cat|dog|shark)",
]
def __init__(self, args: List[List[Union[PromptSegment, Action]]]) -> None:
super().__init__(args)
+3 -3
View File
@@ -9,9 +9,9 @@ def register_action(action: Type[Action]) -> None:
:rtype: object
"""
if action.name in action_registry:
raise ValueError(f"Action {action.name} already registered")
action_registry[str(action.name)] = action
if action.action_name in action_registry:
raise ValueError(f"Action {action.action_name} already registered")
action_registry[str(action.action_name)] = action
def get_action_by_name(name: str) -> Type[Action]:
if name not in action_registry: