Skip to content

Commit e9ef088

Browse files
authored
validate template (#865)
1 parent 364b771 commit e9ef088

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

langchain/prompts/few_shot.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class FewShotPromptTemplate(BasePromptTemplate, BaseModel):
4141
template_format: str = "f-string"
4242
"""The format of the prompt template. Options are: 'f-string', 'jinja2'."""
4343

44+
validate_template: bool = True
45+
"""Whether or not to try validating the template."""
46+
4447
@root_validator(pre=True)
4548
def check_examples_and_selector(cls, values: Dict) -> Dict:
4649
"""Check that one and only one of examples/example_selector are provided."""
@@ -61,11 +64,12 @@ def check_examples_and_selector(cls, values: Dict) -> Dict:
6164
@root_validator()
6265
def template_is_valid(cls, values: Dict) -> Dict:
6366
"""Check that prefix, suffix and input variables are consistent."""
64-
check_valid_template(
65-
values["prefix"] + values["suffix"],
66-
values["template_format"],
67-
values["input_variables"],
68-
)
67+
if values["validate_template"]:
68+
check_valid_template(
69+
values["prefix"] + values["suffix"],
70+
values["template_format"],
71+
values["input_variables"],
72+
)
6973
return values
7074

7175
class Config:

langchain/prompts/prompt.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class PromptTemplate(BasePromptTemplate, BaseModel):
3131
template_format: str = "f-string"
3232
"""The format of the prompt template. Options are: 'f-string', 'jinja2'."""
3333

34+
validate_template: bool = True
35+
"""Whether or not to try validating the template."""
36+
3437
@property
3538
def _prompt_type(self) -> str:
3639
"""Return the prompt type key."""
@@ -61,9 +64,10 @@ def format(self, **kwargs: Any) -> str:
6164
@root_validator()
6265
def template_is_valid(cls, values: Dict) -> Dict:
6366
"""Check that template and input variables are consistent."""
64-
check_valid_template(
65-
values["template"], values["template_format"], values["input_variables"]
66-
)
67+
if values["validate_template"]:
68+
check_valid_template(
69+
values["template"], values["template_format"], values["input_variables"]
70+
)
6771
return values
6872

6973
@classmethod

0 commit comments

Comments
 (0)