Check to make sure that a function used by Mirix returned a valid response. Truncates to return_char_limit if necessary. Responses need to be strings (or None) that fall under a certain text count limit.
(
function_response_string: any,
return_char_limit: int,
strict: bool = False,
truncate: bool = True,
)
| 982 | |
| 983 | |
| 984 | def validate_function_response( |
| 985 | function_response_string: any, |
| 986 | return_char_limit: int, |
| 987 | strict: bool = False, |
| 988 | truncate: bool = True, |
| 989 | ) -> str: |
| 990 | """Check to make sure that a function used by Mirix returned a valid response. Truncates to return_char_limit if necessary. |
| 991 | |
| 992 | Responses need to be strings (or None) that fall under a certain text count limit. |
| 993 | """ |
| 994 | if not isinstance(function_response_string, str): |
| 995 | # Soft correction for a few basic types |
| 996 | |
| 997 | if function_response_string is None: |
| 998 | # function_response_string = "Empty (no function output)" |
| 999 | function_response_string = "None" # backcompat |
| 1000 | |
| 1001 | elif isinstance(function_response_string, dict): |
| 1002 | if strict: |
| 1003 | # TODO add better error message |
| 1004 | raise ValueError(function_response_string) |
| 1005 | |
| 1006 | # Allow dict through since it will be cast to json.dumps() |
| 1007 | try: |
| 1008 | # TODO find a better way to do this that won't result in double escapes |
| 1009 | function_response_string = json_dumps(function_response_string) |
| 1010 | except Exception: |
| 1011 | raise ValueError(function_response_string) |
| 1012 | |
| 1013 | else: |
| 1014 | if strict: |
| 1015 | # TODO add better error message |
| 1016 | raise ValueError(function_response_string) |
| 1017 | |
| 1018 | # Try to convert to a string, but throw a warning to alert the user |
| 1019 | try: |
| 1020 | function_response_string = str(function_response_string) |
| 1021 | except Exception: |
| 1022 | raise ValueError(function_response_string) |
| 1023 | |
| 1024 | # Now check the length and make sure it doesn't go over the limit |
| 1025 | # TODO we should change this to a max token limit that's variable based on tokens remaining (or context-window) |
| 1026 | if truncate and len(function_response_string) > return_char_limit: |
| 1027 | logger.debug( |
| 1028 | f"{CLI_WARNING_PREFIX}function return was over limit ({len(function_response_string)} > {return_char_limit}) and was truncated" |
| 1029 | ) |
| 1030 | function_response_string = f"{function_response_string[:return_char_limit]}... [NOTE: function output was truncated since it exceeded the character limit ({len(function_response_string)} > {return_char_limit})]" |
| 1031 | |
| 1032 | return function_response_string |
| 1033 | |
| 1034 | |
| 1035 | def list_agent_config_files(sort="last_modified"): |
no test coverage detected