From 37f3465a0d304caa9d2c98ba77453e4061eb0187 Mon Sep 17 00:00:00 2001 From: Carson Date: Fri, 7 Mar 2025 09:50:43 -0600 Subject: [PATCH] Follow up to #30: consistently send the str() of tool results to the LLM --- chatlas/_content.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/chatlas/_content.py b/chatlas/_content.py index 56e313c2..e453a91b 100644 --- a/chatlas/_content.py +++ b/chatlas/_content.py @@ -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__() @@ -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