Function calling - How to know when it is happening

Hello!!

I hope the someone can give some light to me :slight_smile:
I am trying to create an assistant. In order to do that, i have created a prompt that detects when a concrete topic is detected, and it launchs another prompt with a function on it (for example, create a support ticket).

The problem is that i am not able to check if a function is running, just i am able to know when the function is finished.
I want to know when the function is running or not, because i want to come back to the main thread of the process that detect the different topics.

Exist any way to detect when is running a function?
I have read all the topics without luck:

How to call functions with chat models | OpenAI Cookbook

Function Calling - OpenAI API

Thanks in advance and best regards

It will stop and return with finish_reason: tool_calls

I’ve handled it with auto call function, check it out, it may inspire you

Thanks for the answer!!

But the finish_reason: tool_calls will change once the function is finished right?

This is the behaviour that i want to solve:.

  1. Someone try to create a ticket: the assistant detects it and the prompt with the correspond function is handled.
  2. The main program is flagged waiting for the support ticket to be created
  3. During the creation of the ticket, the user decide that the issue is not longer neccesary, so the function will stop.
  4. The main program is still flagged for the support ticket (Waiting for finish_reason: tool_calls)

Thanks in advance again

No. The finish_reason stops before function call. Then you have to append function return content to messsages.

See docs: https://platform.openai.com/docs/api-reference/chat/create

i do not get it sorry :smiling_face_with_tear:

i always see 'finish_reason=stop ’ Before and During the main process.

Regards

Heya, welcome to the forums.

Depending on how you’re using the models, Python or Node, you can get the run steps after each interaction. It will show which tools were used and why.

This code snippet should grab it, then you can print it out however you want.

# When the run is over, list and print the run steps for troubleshooting. 
run_steps = client.beta.threads.runs.steps.list(
    thread_id=thread.id,
    run_id=run.id
    
)

Thanks for the answer
I am using python.
Please, could you give a little bit more of code or address me in some point?

1 Like

Thanks for the suggestion, i will check

1 Like

Once the Function is called, it will give you JSON output as parameters. When you finish using these parameters of function, you’ll need to ‘Submit Tool Output’ to let your assistant know that function call is finished.

1 Like

Thanks for your answer

I am not able to get the thread_id neither run_id. I did not find anything like “client.beta.threads.list” in the API.

I am using the chat completion API part…

Threads and Runs don’t work for ChatCompletion. Its only for Assistants.

If you use function calling in ChatCompletion, it will give you JSON output parameters and that’s it. You can use these parameters however you like.

But if you are using Assistant APIs, it means there’s a thread of communication between you and the OpenAI Assistant. Now if you use Function Calling in Assistant, it will also give you JSON output but as this is a back and fourth communication, it will pause the communication between you and assistant until you tell the assistant that you’ve processed/used the function calling parameters and now its okay to resume the communication again. That’s where ‘Submit Tool Output’ comes which i mentioned in my previous comment.

If you are still confused, I am available to do a 1on1 to explain you these things.

2 Likes