Views
No views yet
<functioncall> {\"name\": \"calculate_loan_payment\", \"arguments\": '{\"principal\": 50000, \"interest_rate\": 5, \"loan_term\": 10}'} </functioncall>import re
import json
input_str = "<functioncall> {\"name\": \"calculate_loan_payment\", \"arguments\": '{\"principal\": 50000, \"interest_rate\": 5, \"loan_term\": 10}'} </functioncall>"
# Define the pattern to match the JSON string within the functioncall tags
pattern = r'<functioncall>(.*?)</functioncall>'
# Use re.search to find the matched pattern
match = re.search(pattern, input_str, re.DOTALL)
if match:
json_str = match.group(1)
# Remove the single quotes surrounding the inner JSON string
json_str = json_str.replace("'", "")
# Load the JSON string into a Python dictionary
json_data = json.loads(json_str)
print(json_data)
else:
print("No match found.")arguments value (where openai does it like this, which makes json.loads fail shortly on the original json_str), use ast.literal_eval for the rescue.if match:
import ast
json_str = match.group(1)
json_str = json_str.strip()
"""
https://www.datasciencebyexample.com/2023/03/16/what-to-do-when-single-quotes-in-json-string/
"""
json_dict = ast.literal_eval(json_str)
print(json_dict['name'], json_dict['arguments'])
else:
print("No match found.")<functionresponse> in order to be identified as a function call result (which could be evaluted behind the scene, and its result in principle should be interpreted as part of the user input), which then will be processed by the assistant for form a conversational response.<functionresponse> jons_str </functionresponse>SYSTEM: You are a helpful assistant with access to the following functions. Use them if required -\n{\n \"name\": \"get_news_headlines\",\n \"description\": \"Get the latest news headlines\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"country\": {\n \"type\": \"string\",\n \"description\": \"The country for which to fetch news\"\n }\n },\n \"required\": [\n \"country\"\n ]\n }\n}\n\nWhat date is it today?"
SYSTEM: You are a helpful assistant with access to the following functions. Use them if required -\n{\n \"name\": \"calculate_median\",\n \"description\": \"Calculate the median of a list of numbers\",\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n \"numbers\": {\n \"type\": \"array\",\n \"items\": {\n \"type\": \"number\"\n },\n \"description\": \"A list of numbers\"\n }\n },\n \"required\": [\n \"numbers\"\n ]\n }\n}\n\nHi, I have a list of numbers and I need to find the median. Can you help me with that?<functionresponse> {"median": 5} </functionresponse>