I am trying to Implement function calling using ChatCompletion using Apex Programming Language. But for some reason I am getting Null message[0].content.
Here’s my function in Request:
{
"tool_choice": "auto",
"tools": [
{
"type": "function",
"function": {
"description": "Gets the Indian States and Their Captitals",
"name": "get_states",
"parameters": {
"type": "object",
"properties": {
"states": {
"items": {
"description": "List of states and their capitals.",
"type": "array",
"properties": {
"State_capital_name": {
"description": "Name of the state capital",
"type": "string"
},
"state_name": {
"description": "Name of the state",
"type": "string"
}
},
"type": "object"
}
}
}
}
}
}
],
"model": "gpt-4",
"messages": [
{
"content": "Give me all States of India with their capitals",
"role": "user"
},
{
"content": "",
"role": "system"
}
]
}
Here’s what am getting the response:
{
"choices": [
{
"finish_reason": "tool_calls",
"index": 0,
"logprobs": null,
"message": {
"content": null,
"role": "assistant",
"tool_calls": [
{
"function": {
"arguments": {},
"name": "get_states"
},
"id": "call_CV3CjBqVDcJkD2l1FSpPtYdq",
"type": "function"
}
]
}
}
],
"created": 1716831365,
"id": "chatcmpl-9TYYPrwqRzaLu336DRRZlUS5tqGUN",
"model": "gpt-4-0613",
"object": "chat.completion",
"system_fingerprint": null,
"usage": {
"completion_tokens": 7,
"prompt_tokens": 84,
"total_tokens": 91
}
}
If anyone Interested, here’s the apex code am using:
Map<String, Object> stateCustomFunction = new Map<String, Object>{
'type' => 'function',
'function' => new Map<String, Object>{
'name' => 'get_states',
'description' => 'Gets the Indian States and Their Captitals',
'parameters' => new Map<String, Object>{
'type' => 'object',
'properties' => new Map<String, Object>{
'states' => new Map<String, Object>{
'type' => 'array',
'description' => 'List of states and their capitals.',
'items' => new Map<String, Object>{
'type' => 'object',
'properties' => new Map<String, Object>{
'state_name' => new Map<String, Object>{
'type' => 'string',
'description' => 'Name of the state'
},
'State_capital_name' => new Map<String, Object>{
'type' => 'string',
'description' => 'Name of the state capital'
}
}
}
}
}
}
}
};
What am I doing wrong here?
