Forum Academy Marketplace Showcase Pricing Features

ChatGPT API - New feature Function calling

Hello everyone

I’m currently messing around with ChatGPT’s API and I saw there is this new feature called under roles called “Functions” which is really cool!

What I’m struggling with is setting it up.
I got the normal API working just fine, where I’m stumbling across an issue I can’t seem to figure out is when I try to introduce the function itself. I can’t for the life of me figure out where I need to store it so the API can reach it.

I hope someone can help me.

Best regards

1 Like

The function call itself in the api is essentially fictitious. What happens is, the api will decide what function it needs to call, or whichever you tell it, then you’ll get an assistant response with a ‘role’ of ‘function’ where you’ll conditionally run some code (outside of the api) appending the results of the function to the array of messages to go back into the api.

But you could also define a function to give the api which won’t execute any code anywhere and you can use this to steer the dialect or persona of the assistant.

Hey Doug

Thanks so much for taking your time to respond and help me out.
What you are saying makes sense, I’m just unsure of where to put the funtion itself.

So for example I give ChatGPT some text that I want it to extract certain information out of (which I can get to work with just telling it normally in the “message” that is being sent).
What I would like to do is instead of using tokens I would like for it to call a function.

So let’s say I give it some text and want to extract a date, this is what I have been doing so far (without using “Role:function”):

{
“model”: “gpt-3.5-turbo-0613”,
“messages”: [
{“role”: “system”, “content”: “Take the content of and extract the date”}

]
}

How do I set it up so the “Take the content of and extract the date” part runs in a function and the API call only handles the prompt?

Really curious if there is anyone who already implemented function calling in an Bubble app. Does anyone want to show an example so fellow Bubblers can learn? Much appreciated!

1 Like

I found this video:

I’m currently working on a project that include such a function call. I’m struggling a bit atm but I will get there :slight_smile:

1 Like

Awesome, thanks for sharing. Hopefully more sources with information about this subject will be available.

1 Like

This was a fantastic video. I’m pretty close to getting my own app to work. However, I’m stuck in one area. The app I have has a set of form fields that the user provides inputs. The inputs are then passed through the prompt via the openAI api. Everything seems to return properly and I’m able to view the JSON response in a group. However, the issue I’m having is getting the regular text (converted from JSON) to appear in another group. I used the repeating group like in the video example, with only one row, but multiple text fields mapped to the selectable items available via the Bubble API call and I can’t get anything to show up and not sure why. Would love a little guidance here. Thanks in advance!

Hey, You can check out this video - https://youtu.be/rvnTtWQxoR0

Here I have explained how you can leverage OpenAI function calling to get response in JSON format.

2 Likes

Hey @ankur1,

I’ve followed your video tutorial step by step exactly as you did just to get it to work but I don’t get any response.

The prompt I’m giving is: list exercise for chest.

Here is my JSON:
{
“model”:“gpt-4”,
“messages”:[
{
“role”:“system”,
“content”:""
},
{
“role”:“user”,
“content”:"<User_Prompt>"
}
],
“functions”:[
{
“name”:“exercise_list”,
“description”:“List of exercise.”,
“parameters”:{
“type”:“object”,
“properties”:{
“exercise_list”:{
“type”:“array”,
“description”:“list of exercise for chest.”,
“items”:{
“type”:“object”,
“Properties”:{
“name_of_exercise”:{
“type”:“string”,
“description”:“Exercise name”
},
“number_of_sets”:{
“type”:“integer”,
“description”:“Number of sets recomandation.”
}
}
}
}
},
“required”:[
“name_of_exercise”, “number_of_sets”
]
}
}
]
}

The response I’m getting when I initialise the call:
{
“id”: “chatcmpl-7qOU7HyEZ1ENlOi5f2Rphgr3X6G0W”,
“object”: “chat.completion”,
“created”: 1692721531,
“model”: “gpt-4-0613”,
“choices”: [
{
“index”: 0,
“message”: {
“role”: “assistant”,
“content”: null,
“function_call”: {
“name”: “exercise_list”,
“arguments”: “{}”
}
},
“finish_reason”: “function_call”
}
],
“usage”: {
“prompt_tokens”: 46,
“completion_tokens”: 8,
“total_tokens”: 54
}
}

The call its initialised but there are no arguments. Any idea of why is that?
Thank you for your video. Is awesome BTW.

Hey can you paste this curl into Bubble API connector-

curl --request POST \
  --url https://api.openai.com/v1/chat/completions \
  --header 'Authorization: Bearer openai_token' \
  --header 'Content-Type: application/json' \
  --data '{
  "model": "gpt-3.5-turbo-0613",
  "messages": [
    {"role": "user", "content": "List the exersise for Legs. I am 30 yrs old and can give 1 hr in gym."}
  ],
  "functions": [
  {
        "name": "exercise_list",
        "description": "List of exercises.",
        "parameters": {
            "type": "object",
            "properties": {
                  "exercise_list": {
                            "type": "array",
                            "description": "List of exercise",
                            "items": {
                                 "type": "object",
                                 "properties": {
                                            "name": {
                                                    "type": "string",
                                                    "description": "Exercise Name"
                                                },
																	 "num_of_sets": {
                                                    "type": "integer",
                                                    "description": "Number of sets recommandation."
                                                },
																	  "number_of_reps": {
                                                    "type": "integer",
                                                    "description": "Number of reps recommandation."
                                                }
														
                                             
                                         }
                            }
                        }
            },
            "required": ["exercise_list"]
        }
    }
  ]
}'

@dfduqu01

Hey @ankur1, Thank you! it worked.

I’m going to tinker it a little bit for me use case. I’ll let you know if I have any issues.

Cheers!


I am getting this error , is it just for me ? Did I do any mistake while following the video. Please reply :blush:

Do you happen to be using :format as text anywhere?

I got it working reliably, but the issue is parsing the JSON body back into bubble datatypes (for me)

Hello is this for me ? If it is then , i didn’t use : format as text anywhere, maybe it’s default settings somewhere, is it ?

Well it’s impossible to know without seeing your API connector call and the relevant workflow.

I have uploaded on video on function calling - May be this can solve your queries-

Thank you Ankur , it worked for me

1 Like