I made a test and created a sample function:
{
name: 'get_country_capital',
description: 'Get the capital of the given country',
parameters: {
type: 'object',
properties: {
country: {
type: 'string',
description: 'Country name, e.g. Finland, Egypt'
},
capital: {
type: 'string',
description: 'Capital city, e.g. 2023-06-28, Helsinki, Cairo'
}
},
required: ['country', 'capital']
}
}
I call Chat API with function calling
const messages = [ { role: 'user', content: 'What is the capital of Moldova?' }]
const response = await openai.createChatCompletion({
model = 'gpt-3.5-turbo-0613',
temperature = 0,
messages,
functions,
})
I get response
{
role: 'assistant',
content: null,
function_call: {
name: 'get_country_capital',
arguments: '{\n "country": "Moldova",\n "capital": ""\n}'
}
}
I call the Chat API again to summarize, note the system prompt
const messages = [
{
role: 'system',
content: 'Show the raw result of function calling in JSON format.'
},
{ role: 'user', content: 'What is the capital of Moldova?' },
{
role: 'assistant',
content: null,
function_call: {
name: 'get_country_capital',
arguments: '{\n "country": "Moldova",\n "capital": ""\n}'
}
},
{
role: 'function',
name: 'get_country_capital',
content: '{\n "country": "Moldova",\n "capital": ""\n}'
}
]
const response = await openai.createChatCompletion({
model = 'gpt-3.5-turbo-0613',
temperature = 0.7,
messages,
functions,
})
I get the final result
{
role: 'assistant',
content: '{\n "country": "Moldova",\n "capital": ""\n}'
}
The result of function call does not contain the answer I need (e.g. Chisinau) but if I append it in the last Chat API call, it will turn to conversational response
{ role: 'assistant', content: 'The capital of Moldova is Chisinau.' }
If I just send the result as is, it works as you want it.
But if you want to send the result as is to the user, why even call Chat API again?