Skip to content

Commit

Permalink
modified: libs/core/langchain_core/output_parsers/base.py
Browse files Browse the repository at this point in the history
	modified:   libs/core/langchain_core/prompts/prompt.py
	modified:   libs/core/tests/unit_tests/output_parsers/test_prompt.py
  • Loading branch information
Ahonanhin committed Dec 3, 2024
1 parent 9da7a47 commit cfeb571
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 25 deletions.
5 changes: 2 additions & 3 deletions libs/core/langchain_core/output_parsers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,13 @@ def parse_result(self, result: list[Generation], *, partial: bool = False) -> T:
output_text = result[0].text.strip()

if self.update_and_check_repetition(output_text):
error_message = "Detected repetitive\
reasoning or circular logic."
error_message = "Detected repetitive reasoning or circular logic."
raise ValueError(error_message)

if "iteration limit exceeded" in output_text.lower()\
or"unable to proceed" in output_text.lower():
return {"error": "Agent terminated due to iteration\
limit or inability to continue."}
limit or inability to continue."}

try:
return output_text.strip()
Expand Down
10 changes: 0 additions & 10 deletions libs/core/langchain_core/prompts/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,6 @@ def pre_init_validation(cls, values: dict) -> Any:
values.setdefault("template_format", "f-string")
values.setdefault("partial_variables", {})

# if "do not generate additional user input" not in values["template"].lower():
# raise ValueError(
# "Prompt template must include constraints for
# avoiding additional user input generation."
# )
# if "avoid infinite loops" not in values["template"].lower():
# raise ValueError(
# "Prompt template must include instructions to avoid infinite loops."
# )

if values.get("validate_template"):
if values["template_format"] == "mustache":
msg = "Mustache templates cannot be validated."
Expand Down
15 changes: 3 additions & 12 deletions libs/core/tests/unit_tests/output_parsers/test_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,13 @@
class TestPromptTemplate(unittest.TestCase):
def test_constraints_appended(self):
template = "You are a helpful assistant."
constraints = (
"\nNote:\n"
"1. Do not generate additional user input requests during task execution.\n"
"2. Use only the available tools to perform actions.\n"
"3. If a solution cannot be determined with the given tools or information, respond:\n"
"\"I'm unable to complete this task with the current resources.\"\n"
"4. Avoid infinite loops by stopping reasoning and returning an appropriate message if progress cannot be made.\n"
)
prompt_template = PromptTemplate.from_template(template, constraints=constraints)
expected_template = template + constraints
prompt_template = PromptTemplate.from_template(template)
expected_template = template
self.assertEqual(prompt_template.template, expected_template)

def test_input_variables(self):
template = "Answer the following question: {input}"
constraints = "\nNote:\n1. Constraint 1.\n2. Constraint 2."
prompt_template = PromptTemplate.from_template(template, constraints=constraints)
prompt_template = PromptTemplate.from_template(template)
self.assertIn("input", prompt_template.input_variables)

if __name__ == "__main__":
Expand Down

0 comments on commit cfeb571

Please sign in to comment.