In your provided response the model choose to call function. You must check finish_reason
key. In your response finish_reason='tool_calls'
. It means model choose to call function.
The older, and deprecated, function_call
key is equal to None
, but only because is deprecated.
So, first you must check that finish_reason='tool_calls'
. If finish_reason
is equal to tool_calls
, then you can extract function arguments.
I take it for granted that you put response in a variable, such as 'chat_response'
.
First extract assistant message:
message = chat_response.json()["choices"][0]["message"]['tool_calls']
Now message
must contain:
[{'id': 'call_BEvTzOZ8V4jTA3cT3qd3o2Lz', 'type': 'function', 'function': {'name': 'print_test', 'arguments': '{\n "text": "Hello, world!"\n}'}}]
Then extract "text"
argument:
text = json.loads(message[0]["function"]["arguments"])["text"]
print(text)
# 'Hello, world!'
Calling your application function:
def print_text(body):
os.system(f’echo “{body}” | lp’)
print(“printed”)
print_text(text)
Hope it help.