MCPcopy
hub / github.com/llmware-ai/llmware / remediate_function_call_string

Method remediate_function_call_string

llmware/models.py:1997–2150  ·  view source on GitHub ↗

This method attempts to remediate a function call output string that can not be automatically converted into a programmatic object. The method supports both DICT and LIST outputs. It is designed to address the most common source of automatic failing, which is a premature termin

(self,input_string, dedupe_values=True)

Source from the content-addressed store, hash-verified

1995 return output_keys
1996
1997 def remediate_function_call_string(self,input_string, dedupe_values=True):
1998
1999 """ This method attempts to remediate a function call output string that can not be automatically
2000 converted into a programmatic object. The method supports both DICT and LIST outputs. It is designed
2001 to address the most common source of automatic failing, which is a premature termination at the end of the
2002 string, usually due to a max_len cap, e.g., {'key': ['value1', value2', ..., 'val """
2003
2004 starter = 3
2005 keys = []
2006 values = []
2007
2008 # if very short output, then can not remediate - assume that a bigger problem happened with the inference
2009 if len(input_string) < starter:
2010 # llm response very short - could not remediate and convert to dict or list
2011 return "string", input_string
2012
2013 start = -1
2014 list_start = -1
2015
2016 # will scan the start of the string for either a dictionary start '{' or list start '['
2017 # if neither found, will return the original string
2018
2019 for x in range(0, starter):
2020
2021 if input_string[x] == "{":
2022 # found dict starter
2023 start = x
2024
2025 if input_string[x] == "[":
2026 # found list starter
2027 list_start = x
2028
2029 if start < 0 and list_start < 0:
2030 # remediation not successful - could not find a start marker for dictionary or list
2031 return "string", input_string
2032
2033 # based on the start marker, determine the target output type
2034 if start < 0 and list_start >= 0:
2035 # try to build the string as a list output
2036 list_type = True
2037 key_or_value = "value"
2038 response_type = "list"
2039 start = list_start-1
2040 else:
2041 # try to build the string as a dictionary output
2042 list_type = False
2043 key_or_value = "key"
2044 response_type = "dict"
2045
2046 string_on = False
2047 key_tmp = ""
2048 counter = 0
2049 output_dict = {}
2050 output_list = []
2051 current_key = ""
2052
2053 logger.debug(f"***test*** - remediation - input string - {input_string}")
2054

Callers 4

function_callMethod · 0.80
function_callMethod · 0.80
function_callMethod · 0.80
function_callMethod · 0.80

Calls 1

itemsMethod · 0.80

Tested by

no test coverage detected