🐛 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,23 +213,27 @@ 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")
): ):
# We'll build the prompt with the artifacts
# to show the user what the prompt looks like
# with the variables filled in
artifacts = self.artifacts.copy()
# Remove the handle_keys from the artifacts
# so the prompt format doesn't break
artifacts.pop("handle_keys", None)
try:
return self._built_object.format(**artifacts)
except KeyError:
return super()._built_object_repr()
else:
return super()._built_object_repr() return super()._built_object_repr()
# We'll build the prompt with the artifacts
# to show the user what the prompt looks like
# with the variables filled in
artifacts = self.artifacts.copy()
# Remove the handle_keys from the artifacts
# so the prompt format doesn't break
artifacts.pop("handle_keys", None)
try:
template = self._built_object.format(**artifacts)
return (
template
if isinstance(template, str)
else f"{self.vertex_type}({template})"
)
except KeyError:
return str(self._built_object)
class OutputParserVertex(Vertex): class OutputParserVertex(Vertex):