Stopping the chat completion when using function calling and no answer found

Hi everyone,

I have read in many places that the benefit of using function calling is that it always returns an structured output, whereas simple prompting can sometimes return more verbose output that sometimes can not be converted to json.

Other feature of function calling is that when the function isn’t called (because there are no arguments in the user query that satisfy the call of the function) it outputs the response that the common chat would give.

The thing here is that I don’t want this last feature. I just want to use the function calling, and in case that my query doesn’t have an output for my function, just return an empty json/key.

For example, imagine I have a function for extracting the names of the cities that appear in the user query and my query is “What is the time in Sidney?”. The function will return something like {"cities": ["Sidney"]}. What I want is that for the query “What was the time yesterday” it just outputs {"cities": []}, and not the completion.

I know I can solve this with a bit of wrapping, but I want this because the kind of query I am using for my application tends to give very long answers, and I see the completion like a waste of tokens when it can do it cheaper.

Thank you very much!!

Hi and welcome to the Developer Forum!

You can specify that the function must be called no matter what, the retuned values may be undefined though, depending on the data in the prompt.

Use the function_call="your_fucntion_name" option

1 Like

こんにちは。

私も同じ悩みを抱えています。
function_callで複数の関数名を指定することはできますか?
ドキュメントを確認しましたが、1つの関数名を指定する方法しか見つけられませんでした。
function_call={“name”: “get_n_day_weather_forecast”}

I think both @Foxabilo and @sugiura have gave the correct answer. There’s a function_call parameter in the chat completion api, which allows you to control the model behavior.

The reference: OpenAI Platform

2 Likes

Thank you!! The @sugiura solution seems to work on my case :slight_smile:

1 Like