Skip to content

Commit

Permalink
add check to garantee all components of a prompt template are static
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosFerLo committed Oct 29, 2023
1 parent ea6d0bb commit 26c2beb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions synthetic/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class ComponentConflictError (PromptTemplateError) :
""" This error raises if try to add two components with the same name to a template
"""

class DynamicComponentInPromptTemplateError (ProcessLookupError) :
""" This error raises if try to add a dynamic component to a PromptTemplate
"""

class InvalidSignatureError (Error) :
""" This error raises if the validations checks for a signature fail
"""
Expand Down
6 changes: 5 additions & 1 deletion synthetic/prompts/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,8 @@ def _validate_input_variables_appear_in_template (template: str, input_variables
def _validate_components (components: List[Type[Component]]) -> None :
component_names = [component.name for component in components]
if len(component_names) != len(set(component_names)):
raise synthetic.ComponentConflictError("Two components cannot have the same name.")
raise synthetic.ComponentConflictError("Two components cannot have the same name.")

dynamic_components = [ c for c in components if c.is_dynamic ]
if len(dynamic_components) > 0 :
raise synthetic.DynamicComponentInPromptTemplateError(f"Dynamic components are not allowed in PromptTemplate. Dynamic components: {dynamic_components}.")
10 changes: 9 additions & 1 deletion test/test_prompt_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,12 @@ def format(self, name: str, **kwargs) -> str :
return f"Hello {name}"

prompt_template = synthetic.PromptTemplate(template="<MyComponent/>", input_variables=[], components=[MyComponent])
self.assertRaises(synthetic.PromptTemplateError, prompt_template.format)
self.assertRaises(synthetic.PromptTemplateError, prompt_template.format)

def test_prompt_template_checks_all_components_are_static_and_raises_error_if_try_to_add_a_dynamic_component_to_it (self):
class MyComponent (synthetic.Component) :
name = "MyComponent"
is_dynamic = True

with self.assertRaises(synthetic.DynamicComponentInPromptTemplateError):
prompt_template = synthetic.PromptTemplate(template="<MyComponent/>", input_variables=[], components=[MyComponent])

0 comments on commit 26c2beb

Please sign in to comment.