Share feedback, ideas and get community help

F
M
a
g
E

ChatGPT Function Calling

Hi all, I'm trying to use a chatGPT to function call an API to get data when a user enters a postcode - I've set up the function in the AssistantsAPI and in the prompt so its using the function when the postcode is inputted.

I'm having trouble with calling the API using the function and returning the response, I've tested the API keys and such and they are working - however when in typebot it returns:

"status": 500, "body": { "message": "400 1 validation error for Request\nbody -> tool_outputs -> 0 -> output\n field required (type=value_error.missing)", "code": "INTERNAL_SERVER_ERROR"

This is my code in the function:

' const baseUrl = https://epc.opendatacommunities.org/api/v1/domestic/search?postcode=BH10;
const encodedAPI = "BASE64 Encode"; // Pre-encoded API key

const requestOptions = {
method: "GET",
headers: {
Accept: "application/json",
Authorization: "Basic" + encodedAPI
}
};

try {
const response = await fetch(baseUrl, requestOptions);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
console.log(data); // Log the response data
return data; // Return the parsed JSON data
} catch (error) {
console.error("Error fetching data:", error);
throw error; // Re-throw the error for handling outside the function
} '
B
J
V
29 comments
You need to return a string
here it seems you return a JSON object?
Would it better to use the option for csv/text? or convert it to a string with json:

const data = await response.json(); const dataString = JSON.stringify(data); // Convert JSON data to a string return dataString
that returned the error of {
"status": 500,
"body": {
"message": "400 1 validation error for Request\nbody -> tool_outputs -> 0 -> output\n field required (type=value_error.missing)",
"code": "INTERNAL_SERVER_ERROR"
}
}
@Baptiste I have fixed this in that PR.
But you didn't include this and other fixes.
Ok, like I said I will publish it first thing in the morning
Sorry, my English is not great, and I don't think I was clear.
I meant that many bug fixes that were included in that pull request were not accepted by you.
You are referring to https://github.com/baptisteArno/typebot.io/pull/1181, right? I told you in DM that you don't have to bother, I will push the changes manually.
Oh, thank you. I apologize for my poor English getting in the way. 🙂
Hmmm so I'm not getting an error anymore but the response isn't from using the function, chatgpt responds by telling me it will use the function getAddress to check for addresses rather than running the function - I've tested the process of calling the API using the webhook block which works in getting the data but would like to use the function calling to understand it for the future
and strange responses such as -
Sorry, I am not able to access external APIs in this environment. However, I can continue the conversation without knowing the specific address. Is there anything else I can assist you with?
The error persists if you do
Plain Text
return JSON.stringify(dataString)
I believe it is calling the function and Typebot is executing it.
However, when passing the value, it throws an error because Typebot is expecting a string.
ChatGPT receives the error when the function returns and responds as you're seeing.
P.S.: The OpenAI function calling is a "days-old" feature. We're still fine-tuning it.
Hmmm I've added that to my script but no luck unfortunately

try { const response = await fetch(baseUrl, requestOptions); if (!response.ok) { throw new Error('Network response was not ok'); } const data = await response.json(); console.log(data); // Log the response data return JSON.stringify(data); } catch (error) { // Moved catch block to the correct position console.error("Error fetching data:", error); throw error; // Re-throw the error for handling outside the function }
Unfortunetly, as it requires an API key, I won't be able to reproduce here.
Hmm I can DM you it?
I prefer to discuss it here. But sure, you can DM if you must.
Just for the API Key, don't want to post it publically
Tried it with the header Accept as application/json and text/csv
1) You missed the quotes in the url.
2) I'm getting 401 (Unauthorized). Guess your problem is your API key.
Attachment
image.png
Ohhhh... just fixed. 😆
You missed an white space after "Basic ".
Plain Text
const requestOptions = {
    method: "GET",
    headers: {
        Accept: "application/json",
        Authorization: "Basic " + encodedAPI,
    },
}
Booom thank you so much 🙂 Simple over looked quote 🙂
Recomend to code and run your code outside the bot, then just copy / paste.
Yeah that approach makes more sense, will use it in the future 🙂
Add a reply
Sign up and join the conversation on Discord
Join