🐛 fix(types.py): refactor _built_object_repr method in PromptVertex to improve readability and handle edge cases

This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-07-10 17:48:21 -03:00
commit 2cb8452968

View file

@ -213,10 +213,11 @@ class PromptVertex(Vertex):
def _built_object_repr(self): def _built_object_repr(self):
if ( if (
self.artifacts not self.artifacts
and self._built_object is not None or self._built_object is None
and hasattr(self._built_object, "format") or not hasattr(self._built_object, "format")
): ):
return super()._built_object_repr()
# We'll build the prompt with the artifacts # We'll build the prompt with the artifacts
# to show the user what the prompt looks like # to show the user what the prompt looks like
# with the variables filled in # with the variables filled in
@ -225,11 +226,14 @@ class PromptVertex(Vertex):
# so the prompt format doesn't break # so the prompt format doesn't break
artifacts.pop("handle_keys", None) artifacts.pop("handle_keys", None)
try: try:
return self._built_object.format(**artifacts) template = self._built_object.format(**artifacts)
return (
template
if isinstance(template, str)
else f"{self.vertex_type}({template})"
)
except KeyError: except KeyError:
return super()._built_object_repr() return str(self._built_object)
else:
return super()._built_object_repr()
class OutputParserVertex(Vertex): class OutputParserVertex(Vertex):