Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions chatlas/_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,21 +199,24 @@ class ContentToolResult(Content):
name: Optional[str] = None
error: Optional[str] = None

def _get_value_and_language(self) -> tuple[str, str]:
def _get_value(self, pretty: bool = False) -> str:
if self.error:
return f"Tool calling failed with error: '{self.error}'", ""
return f"Tool calling failed with error: '{self.error}'"
if not pretty:
return str(self.value)
try:
json_val = json.loads(self.value)
return pformat(json_val, indent=2, sort_dicts=False), "python"
json_val = json.loads(self.value) # type: ignore
return pformat(json_val, indent=2, sort_dicts=False)
except: # noqa: E722
return str(self.value), ""
return str(self.value)

# Primarily used for `echo="all"`...
def __str__(self):
comment = f"# tool result ({self.id})"
value, language = self._get_value_and_language()

return f"""```{language}\n{comment}\n{value}\n```"""
value = self._get_value(pretty=True)
return f"""```python\n{comment}\n{value}\n```"""

# ... and for displaying in the notebook
def _repr_markdown_(self):
return self.__str__()

Expand All @@ -224,9 +227,9 @@ def __repr__(self, indent: int = 0):
res += f" error='{self.error}'"
return res + ">"

# The actual value to send to the model
def get_final_value(self) -> str:
value, _language = self._get_value_and_language()
return value
return self._get_value()


@dataclass
Expand Down