Hire 
Daniel D'Souza
Submit
Note: Voiceflow is not responsible for the exchange of services between you and the partner.
Thank you! An email will be sent connecting you with the expert.
Oops! Something went wrong while submitting the form.

Connect to GPT-o1

Template
Function
28
Template
Function
by
Daniel D'Souza

Use Open AI’s latest model with a Voiceflow function. GPT-o1 improved logic and reasoning. Start building today!

Created:

Heading

Voiceflow APIs used:

Channels
No items found.
Created By
Daniel D'Souza
This is some text inside of a div block.

Function Code Snippet

 
export default async function main(args) {
// Extract input variables from args
const { last_utterance, openaiApiKey } = args.inputVars;

// Validate that the required input variables are provided
if (!last_utterance || !openaiApiKey) {
return {
next: { path: 'error' },
trace: [{ type: "debug", payload: { message: "Missing required input variable: last_utterance or openaiApiKey" } }]
};
}

// Define the URL for the OpenAI API
const url = `https://api.openai.com/v1/chat/completions`;

// Configure the request payload for the OpenAI API
const data = {
model: "o1-preview",
messages: [
{
"role": "user",
"content": last_utterance
}
]
};

// Configure the fetch request headers and body
const config = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${openaiApiKey}`
},
body: JSON.stringify(data)
};

try {
// Make the API call
const response = await fetch(url, config);

// Check if the response status is OK
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

// Parse the JSON response
const responseBody = await response.json;

// Validate the responseBody structure as expected
if (!responseBody || typeof responseBody !== 'object') {
throw new Error("Invalid or missing response body from the API");
}

// Extract the response text from the completion
const completion = responseBody.choices[0].message.content;

// Create the success return object with extracted data
return {
outputVars: { completion },
next: { path: 'success' },
trace: [
{
type: "text",
payload: { message: `Received response from o1: ${completion}` }
}
]
};
} catch (error) {
return {
next: { path: 'error' },
trace: [{ type: "debug", payload: { message: "Error: " + error.message } }]
};
}
}
copy-icon

Explore More Templates

Build and submit a Template to have it featured in the community.

ghraphic
No items found.
No items found.