other-emerald
other-emerald15mo ago

perflexity

Hi, I'm trying to utilize the Perplexity API with Voiceflow, but I keep receiving a 400 Bad Request error. Does anyone know how to resolve this? Thanks.
10 Replies
Tico | Voiceflow Assistant
Voiceflow provides a platform for creating voice and chat assistants. It offers various functionalities such as interacting with third-party APIs or your own backend services, making network requests, and processing responses. Here's an example of how to make a GET request and process the response:
export default async function main(args) {
const response = await fetch('https://cat-fact.herokuapp.com/facts');
const responseBody = response.json();
// Accessing the response body
// ... process responseBody
}
export default async function main(args) {
const response = await fetch('https://cat-fact.herokuapp.com/facts');
const responseBody = response.json();
// Accessing the response body
// ... process responseBody
}
You can also create traces for each item you want to include in the assistant's response:
return next({ path: 'success', trace: facts.map(text => ({ type: 'text', payload: { message: text } })) });
return next({ path: 'success', trace: facts.map(text => ({ type: 'text', payload: { message: text } })) });
Voiceflow also supports various trace types, including text, visual content, cards, and more. For example, to perform a POST request, you can use the following code:
await fetch('YOUR-NGROK-URL-HERE', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name, age })
});
await fetch('YOUR-NGROK-URL-HERE', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name, age })
});
For more detailed guidance on making API requests in Voiceflow, you can refer to our Voiceflow Functions Documentation.
W. Williams (SFT)
Send Question to Perplexity | Voiceflow Functions
Send a question to Perplexity's AI models. This supports pplx-7b-online and pplx-70b-online.
W. Williams (SFT)
There is a function to do it super easily
genetic-orange
genetic-orange15mo ago
still gets a bad request
W. Williams (SFT)
in the perplexity function?
genetic-orange
genetic-orange15mo ago
yeah it's a real mess it's very different from the other chat completion functions
W. Williams (SFT)
I haven't used it but it couldn't be that different. Let me have a look at the API docs and see if I can through something together real quick.
genetic-orange
genetic-orange15mo ago
the endpoint works exactly like together.ai or mistral or anyscale i think i'm working on it myself, just tweaking the function i used for anyscale yeah it does look like it's a function error i'm gonna try to get a test perplexity api key to confirm it
export default async function main(args) {
// Extract the API key and Question from inputVars
const {system, prompt, last_utterance, apiKey, model} = args.inputVars;

//Define the API data
const url = 'https://api.perplexity.ai/chat/completions';
const data = {
model: `${model}`,
max_tokens: 2048,
messages: [
{
role: 'system',
content: `${system}`
},
{
role: 'user',
content: ` ${prompt}. User's message: ${last_utterance}`
}
],
temperature: 0.6,
};

// Wrap the API call in a try block so it can detect errors
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'content-type': 'application/json',
'Authorization': `Bearer ${api_Key}`,
'OpenAI-Beta': 'assistants=v1'
},
body: JSON.stringify(data)
});

// Check if the response status is OK (status in the range 200-299)
if (!response.ok) {

// If not OK, throw an error to be caught by the catch block
throw new Error(`HTTP error! status: ${response.status}`);
}

// Await the response and save it to the responseBody variable
let responseBody = await response.json;

// Access the answer from the responseBody
const answer = responseBody.choices[0].message.content;


// Return the desired output structure
return {
outputVars: {
answer: answer
},
next: {
path: 'success'
},
trace: [
{
type: 'debug',
payload: {
message: `Information recieved: ${answer}`
}
}
],
};
} catch (error) {
// Catch block handles any errors thrown in the try block
return {
outputVars: {
error: `An error occurred: ${error.message}`
},
next: {
path: 'error'
},
trace: [
{
type: 'text',
payload: {
message: `An error occurred: ${error.message} body is ${responseBody}`
}
}
],
};
}
}
export default async function main(args) {
// Extract the API key and Question from inputVars
const {system, prompt, last_utterance, apiKey, model} = args.inputVars;

//Define the API data
const url = 'https://api.perplexity.ai/chat/completions';
const data = {
model: `${model}`,
max_tokens: 2048,
messages: [
{
role: 'system',
content: `${system}`
},
{
role: 'user',
content: ` ${prompt}. User's message: ${last_utterance}`
}
],
temperature: 0.6,
};

// Wrap the API call in a try block so it can detect errors
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'content-type': 'application/json',
'Authorization': `Bearer ${api_Key}`,
'OpenAI-Beta': 'assistants=v1'
},
body: JSON.stringify(data)
});

// Check if the response status is OK (status in the range 200-299)
if (!response.ok) {

// If not OK, throw an error to be caught by the catch block
throw new Error(`HTTP error! status: ${response.status}`);
}

// Await the response and save it to the responseBody variable
let responseBody = await response.json;

// Access the answer from the responseBody
const answer = responseBody.choices[0].message.content;


// Return the desired output structure
return {
outputVars: {
answer: answer
},
next: {
path: 'success'
},
trace: [
{
type: 'debug',
payload: {
message: `Information recieved: ${answer}`
}
}
],
};
} catch (error) {
// Catch block handles any errors thrown in the try block
return {
outputVars: {
error: `An error occurred: ${error.message}`
},
next: {
path: 'error'
},
trace: [
{
type: 'text',
payload: {
message: `An error occurred: ${error.message} body is ${responseBody}`
}
}
],
};
}
}
@sjst_0414 this might work? but i don't have an api key on me so i can't really test it
W. Williams (SFT)
@shen @sjst_0414 Re-wrote it from scratch. Works like a charm! Now supports online models and did a bit more error checking.
genetic-orange
genetic-orange15mo ago
nice man that looks perfect

Did you find this page helpful?