Skip to content

Instantly share code, notes, and snippets.

@mzbac
Created April 28, 2024 01:25
Show Gist options
  • Save mzbac/9846a963ea28723bd8d769049cf213be to your computer and use it in GitHub Desktop.
Save mzbac/9846a963ea28723bd8d769049cf213be to your computer and use it in GitHub Desktop.
tool use
def extract_arguments(json_str):
json_str = json_str.replace("'", '"')
start_index = json_str.find('"arguments":') + len('"arguments":')
start_of_json = json_str.find("{", start_index)
end_of_json = json_str.rfind("}")
if start_of_json != -1 and end_of_json != -1:
extracted = json_str[start_of_json:end_of_json]
if (extracted.startswith("'") and extracted.endswith("'")) or (
extracted.startswith('"') and extracted.endswith('"')
):
extracted = extracted[1:-1]
if extracted.endswith("'") or extracted.endswith('"'):
extracted = extracted[:-1]
if extracted.startswith("'") or extracted.startswith('"'):
extracted = extracted[1:]
logger.info("Extracted: %s", extracted)
return extracted
return None
try:
response = requests.post(
TOOL_API_URL,
headers=DEFAULT_HEADERS,
json=data,
)
response.raise_for_status()
response_data = response.json()
completion_text = response_data["choices"][0]["message"]["content"].strip()
if tool and "<functioncall>" in completion_text:
try:
json_str = extract_arguments(completion_text)
if json_str is None:
return None, completion_text
arguments = json.loads(json_str)
return arguments, None
except json.JSONDecodeError as e:
logger.error(f"Error parsing tool response: {json_str}")
return None, completion_text
else:
return None, completion_text
except requests.exceptions.RequestException as e:
raise Exception(f"Error calling the API: {e}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment