my function json in open ai
{
"name": "do_web_search",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The search query"
}
},
"required": [
"query"
]
},
"description": "Allows for searching the internet"
}
getting
{
"status": 500,
"body": {
"message": "400 5 validation errors for Request\nbody -> tool_outputs -> 0 -> output\n field required (type=value_error.missing)\nbody -> tool_outputs -> 1 -> output\n field required (type=value_error.missing)\nbody -> tool_outputs -> 2 -> output\n field required (type=value_error.missing)\nbody -> tool_outputs -> 3 -> output\n field required (type=value_error.missing)\nbody -> tool_outputs -> 4 -> output\n field required (type=value_error.missing)",
"code": "INTERNAL_SERVER_ERROR"
}
}
This is the JS function I put in the block
async function do_web_search(query) {
const apiKey = 'my-key-here-not-this-string-i-no-dumb';
try {
const response = await fetch('https://api.tavily.com/search', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
api_key: apiKey,
query: query,
search_depth: 'advanced'
})
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('Error during Tavily search:', error);
throw error;
}
}
Maybe I am using this ability wrong, can anyone help here?
at the code field you should add just the function body.
remove the header (and the last })
@Vilela™ What if I were to have multiple functions to call, how would Typebot know which function to call?
i dont in this case and appreciate the help just thinking ahead as I know of some ideas I have that could involve multiple tools for the ai to use
Sounds like you're not adding it to the right place.
You have do add multiples functions on the Open AI Block.
@Vilela™ I went back and looked at the feature and I missed the add function button below the code block. Sorry about that. Thank you for the help!
@Vilela™ I am still getting an error
{
"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"
}
}
Code
var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://api.tavily.com/search', false); // 'false' makes the request synchronous
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({
api_key: 'my-api-key',
query: query,
search_depth: 'advanced'
}));
if (xhr.status === 200) {
return xhr.responseText;
} else {
return 'Tavily search error!';
}
I know this code works, tossed into a file
my ai assistant function json
typebot function in ask ai assistant block
The thing is that, for security reasons, this context must be limited and it's up to Baptiste to define how much.
At the moment. XMLHttpRequest
is not accessible in this context.
If you're self-hosting you may change the code to allow more stuff.
YA we are not self hosting at this time. Are you all able to construct a pre-defined function that typebot feels is safe for me to have my ai assistant target that is used for web searching?
All I do is provide an endpoint, an api key
I did not tried any type of web call yet.
If you all are able to figure out a way, without self hosting, for the function calling to make web search API calls, the power of ths ai assistant blocks become very very powerful within typebot
The abilities of ai assistants are then multipled imo
Use Case for us: Giving our AI assistant ability to look up local doctors offices by zip code
@Baptiste Adding you to the convo and see your thoughts on this
Agree with you. It's a must have.
XMLHttpRequest is a web entity, client-side
The function is executed on a Node.js server. So it doesn't know about XMLHttpRequest
You can do API calls using fetch
method, super easy 👍
I tried fetch and got similar errors. I will retry my fetch call
This works for me:
async function fetchData() {
try {
const response = await fetch('https://jsonplaceholder.typicode.com/todos/1');
const json = await response.json();
console.log(json);
return json;
} catch (error) {
console.error('Error fetching data:', error);
}
}
return fetchData();
You can directly insert
try {
const response = await fetch("https://jsonplaceholder.typicode.com/todos/1");
const json = await response.json();
console.log(json);
return json;
} catch (error) {
console.error("Error fetching data:", error);
}
I'm having a somewhat similar issue and I'm sure it's just something I'm doing wrong. I am able to get the fetch to work when I'm just trying to call a webhook, but I can't figure out what the Assistant Function would be sending in it's parameter, body, etc, to know what to stringify for the webhook...
My assistant is calling two functions:
{
"name": "get_folders_tool",
"description": "Get a list of categories and corresponding folders within each category. This allows you to determine which category and folders most likely address the user's query.",
"parameters": {
"type": "object",
"properties": {
"folder_list": {
"type": "string",
"description": "A request for a list of all available categories and their corresponding folders."
}
},
"required": [
"folder_list"
]
}
}
and this:
{
"name": "get_articles_tool",
"description": "Get a list of the titles of all articles in the three most relevant folders.",
"parameters": {
"type": "object",
"properties": {
"articles_list": {
"type": "string",
"description": "The titles of the available articles."
},
"folder_1_name": {
"type": "string",
"description": "The name of the first folder to get a list of available articles from."
},
"folder_2_name": {
"type": "string",
"description": "The name of the second folder to get a list of available articles from."
},
"folder_3_name": {
"type": "string",
"description": "The name of the third folder to get a list of available articles from."
}
},
"required": [
"articles_list"
]
}
}
For the code in the Ask Assistant mode in Typebot, I have this:
The first one works great as I'm just triggering the webhook and don't need to pass anything from the Assistant, but in the second one (get_articles_tool), I can't figure out how to pass the body, parameters, etc, from the Assistant to the webhook.
If I remove the 'body: JSON.stringify(...) part in the second one, the webhook receives the POST call just fine. I just can't figure out what to send with it in there.
You defined get_articles_tool
on OpenAI with these props: articles_list
, folder_1_name
, folder_2_name
, folder_3_name
These can be used in your code. But body
in your case does not exist
Got it figured out and damn it's slick! Love it!
Quick question about this topic. This is the code i've assigned to my function calling
try {
const response = await fetch("http://your-endpoint.here", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
input: input
}),
});
const json = await response.json();
console.log(json);
return json;
} catch (error) {
console.error("Error fetching data:", error);
}
While this is the function in my openai assistant:
{
"name": "agencyKnowledgeBase",
"description": "Useful for whenever you need information about the agency, including general information, working hours, website and social media.",
"strict": true,
"parameters": {
"type": "object",
"properties": {
"input": {
"type": "string",
"description": "User's query about the agency"
}
},
"additionalProperties": false,
"required": [
"input"
]
}
}
Now, when i ask my assistant
what are the agency working hours?
, I can see the POST request is triggered. However, even though the endpoint response is the following, the chatbot says
I don't know
.
Endpoint response:
{
"output": "The agency is open at 10am from Monday to Friday."
}
Nevermind, just found the problem. My request now looks like this and it worked.
try {
// Send the API request using fetch
const response = await fetch('https://your.n8n.site/webhook/cf8070ef-4023-43d9-0023-852475f88a4b', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
input: input,
agencyId: '{{agency_id}}'
})
});
// Check if the response is valid
if (!response) {
console.error('No response received or response is undefined.');
throw new Error('No response from the server.');
}
// Log the raw response object to inspect its structure
console.log('Raw Response Object:', response);
// Manually inspect all properties of the response object
for (let key in response) {
if (response.hasOwnProperty(key)) {
console.log(`Property: ${key} - Value:`, response[key]);
}
}
// Since .text() or .json() is not available, print the response as-is
console.log('Raw response object as string:', String(response));
// If possible, try to access the body directly
if (response.body) {
console.log('Response body available:', response.body);
}
// Return the raw response object for further debugging
return response;
} catch (error) {
console.error('Error during API request:', error);
throw error;
}
This could be useful for many people out there. In my case, I send a POST request to a n8n webhook node, which takes the
input
and the
agencyId
to look into a vector store db in order to get the answer (example: agency working hours, etc). The chatbot will use the endpoint when necessary and provide the right answer.