[Q&A] Function Call via API

i have been using function call in ChatGPT 3.5 ( in prompt )
now i switching to Official Method, function in API.

------ lets start the discussion with this sample :
{
“model”: “gpt-3.5-turbo-0613”,
“messages”: [{“role” : “user” , “content” : “i need you to send a email to zhihong, telling him we cancel meeting today” }],
“functions” : [{
“name” : “send_email”,
“description” : “Help drafting email and send the email”,
“parameters”: { “type”: “object”, “properties” : {
“email” : { “type” : “string” , “description” : “receipient email address” },
“subject” : { “type” : “string” , “description” : “Email Subject” },
“content” : { “type” : “string” , “description” : “Email content with html formatted” }
},
“required” : [“email”]
}
}],
“temperature” : 0.5,
“max_tokens” : 500
}


ChatGPT response :
“index”: 0,
“message”: {
“role”: “assistant”,
“content”: null,
“function_call”: {
“name”: “send_email”,
“arguments”: “{\n "email": "[my email]",\n "subject": "Meeting Cancellation",\n "content": "

Dear Zhihong,

I hope this email finds you well. I wanted to inform you that we have decided to cancel the meeting scheduled for today. I apologize for any inconvenience this may cause.

Best regards,

Your Name

"\n}”
}
},
“finish_reason”: “function_call”


My Question :
All work well EXCEPT :
In my prompt, i didnt give chatGPT the receipient email address.
and i did mentioned Required = Email

Need any forumer give some input how to improve this.

You can try to include in the description of the email parameter something like ‘receipient email address. Check for valid email before proceeding further.’

Maybe GPT will ask a follow-up question.

The main part about function calling is, that you usually use it in some kind of form, where the email is already provided. After pushing the ‘Send Email’- button you start the function call process with the API and the workflow starts.

So it is your responsibility to check the email field for a valid input. GPT answers you with a placeholder in your example.

Updated Prompt. Please Check

{
“model”: “gpt-3.5-turbo-0613”,
“messages”: [
{"role" : "system" , "content" : "1. If user do not provide receipient email address, please ask before you proceed 'send_email' function."},
{“role” : “user” , “content” : “i need you to send a email to zhihong, telling him we cancel meeting today” }
],
“functions” : [
{
"name" : "send_email", 
"description" : "draft and send email to user instructed emaill address.", 
"parameters": { "type": "object", "properties" : {
		"email"	: { "type" : "string" , "description" : "User provided receipient email address." },
		"subject" : { "type" : "string" , "description" : "Email Subject" },
		"content" : { "type" : "string" , "description" : "Email content with html formatted" }
 						},
"required" : ["email"]
}
},
{
"name" : "fallback", 
"description" : "If required parameters is not found to call a function", 
"parameters": { "type": "object", "properties" : {
		"error_message"	: { "type" : "string" , "description" : "describe the error, or any info you require to proceed the function" }}
}
}
],
“temperature” : 0.5,
“max_tokens” : 500
}

------ already added another fallback function, and system instruction to prevent ChatGPT to hallucinate email address. BUT result is here :

{
            "index": 0,
            "message": {
                "role": "assistant",
                "content": null,
                "function_call": {
                    "name": "send_email",
                    "arguments": "{\n  \"email\": \"peter@example.com\",\n  \"subject\": \"Meeting Postponed\",\n  \"content\": \"<p>Dear Peter,</p><p>We regret to inform you that the meeting scheduled for today has been postponed. We will notify you of the new date and time as soon as it is confirmed.</p><p>Thank you for your understanding.</p><p>Best regards,</p><p>Your Name</p>\"\n}"
                }
            },
            "finish_reason": "function_call"
        }

This solved the Hallucination Email Address :

{
"name" : "send_email", 
"description" : "draft and send email to user instructed emaill address. (do not use this function if user didnt provide receipient email address)", 
"parameters": { "type": "object", "properties" : {
		"email"	: { "type" : "string" , "description" : "User provided receipient email address." },
		"subject" : { "type" : "string" , "description" : "Email Subject" },
		"content" : { "type" : "string" , "description" : "Email content with html formatted" }
 						},
"required" : ["email"]
}
}

( added a CONSTRAINT in function description ) now see the result :

{
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "Sure, I can help you with that. Can you please provide me with Peter's email address?"
            },
            "finish_reason": "stop"
        }
3 Likes

Good that it works now. But what I meant with my reply before was to put it in the description field for the request in the email property. Maybe this makes the fallback function obsolete:

{
“model”: “gpt-3.5-turbo-0613”,
“messages”: [
{"role" : "system" , "content" : "1. If user do not provide receipient email address, please ask before you proceed 'send_email' function."},
{“role” : “user” , “content” : “i need you to send a email to zhihong, telling him we cancel meeting today” }
],
“functions” : [
{
"name" : "send_email", 
"description" : "draft and send email to user instructed emaill address.", 
"parameters": { "type": "object", "properties" : {
		"email"	: { "type" : "string" , "description" : "User provided receipient email address. Check for valid email before proceeding further." },
		"subject" : { "type" : "string" , "description" : "Email Subject" },
		"content" : { "type" : "string" , "description" : "Email content with html formatted" }
 						},
"required" : ["email"]
}
},
{
"name" : "fallback", 
"description" : "If required parameters is not found to call a function", 
"parameters": { "type": "object", "properties" : {
		"error_message"	: { "type" : "string" , "description" : "describe the error, or any info you require to proceed the function" }}
}
}
],
“temperature” : 0.5,
“max_tokens” : 500
}

The problem I’m facing is, that the arguments are always null and therefore no arguments are passed to the functions. Did you get it to work?

i tried, not working also, thats y i move your suggested prompt into function description.
Actually not working too.

which argument?

i did get it works, with latest constraint in Function Description.
the AI didnt call the function but ask the reciepient email

1 Like

what is function call

is is like prompt eng or what
how did u create this

You’ll get more millage by giving the model an “ask” function that it can use to ask the user questions. That function doesn’t need to do anything. This add is to make the model happy. Anytime you present the model with a choice like pick from a set of functions, it wants to make a choice.

The other suggestion I would make is to give the model very clear direction as to the steps you want it to perform. “Use the ask function to ask the user their email address and message. Then use send_email to send the user an email.”

1 Like

Yes, this makes sense overall.

A lot of prompt engineering is needed to steer the model into the right direction.
Also temperature 0.5 is much too high.

The problem I see here is, that if the user wants a usual conversation, that the temp needs to be higher for creative content creation. This is then the responsible of the user. In my opinion a fragile system at the moment.

1 Like

tried that also , together with a proper system message, but doesn’t work for me