Connect to GPT-4o

Template
Function
1
Template
Function
by
Denys Linkov

Use Open AI’s latest model with a Voiceflow function. GPT-4o 50% cheaper, twice as fast and does better on a variety of tasks. Start building today!

Created:

Heading

Voiceflow APIs used:

Channels
No items found.
Created By
Denys Linkov
This is some text inside of a div block.
Overview
This is some text inside of a div block.
by
This is some text inside of a div block.

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Block quote

Ordered list

  1. Item 1
  2. Item 2
  3. Item 3

Unordered list

  • Item A
  • Item B
  • Item C

Text link

Bold text

Emphasis

Superscript

Subscript

Heading
Based in
This is some text inside of a div block.
Heading

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: "gpt-4o",
messages: [
{
"role": "system",
"content": "You are an assistant. Please respond thoughtfully and informatively."
},
{
"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 GPT-4: ${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.