Extract & Filter Chunks from a KB Response

Template
Function
1
Template
Function
by
Daniel D'Souza

Use the Voiceflow Knowledge Base with a 3rd party AI model. This function takes a users question and extracts the relevant chunks found in the knowledge base.

Created:

Heading

Voiceflow APIs used:

Channels
No items found.
Created By
Daniel D'Souza
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 the API key and Question from inputVars
const { question, VFapiKey } = args.inputVars;

//Define the API data
const url = 'https://general-runtime.voiceflow.com/knowledge-base/query';
const data = {
"chunkLimit": 5,
"synthesis": false,
"settings": {
"model": "claude-instant-v1",
"temperature": 0.1,
"system": "You are an AI FAQ assistant. Information will be provided to help answer the user's questions. Always summarize your response to be as brief as possible and be extremely concise. Your responses should be fewer than a couple of sentences. Do not reference the material provided in your response."
},
"question": question
};

// Wrap the API call in a try block so it can detect errors
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'accept': 'application/json',
'content-type': 'application/json',
'Authorization': VFapiKey,
'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
const responseBody = await response.json;

// Access the chunks from the responseBody
let chunks = responseBody.chunks;

// Filter the chunks to only include ones with a score higher than 0.80
let filteredChunks = chunks.filter(chunk => chunk.score > 0.8);

// Remove anything from the chunks except the text and source information
let cleanedChunks = filteredChunks.map(({ score, chunkID, documentID, tags, ...rest }) => rest);

//turn this into a string so we can pass it to an API later
cleanedChunks = JSON.stringify(cleanedChunks);

// Return the desired output structure
return {
outputVars: {
cleaned_chunks: cleanedChunks
},
next: {
path: 'success'
},
trace: [
{
type: 'debug',
payload: {
message: `Chunks recieved: ${cleanedChunks}`
}
}
],
};
} 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}`
}
}
],
};
}
}
copy-icon

Explore More Templates

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

ghraphic
No items found.
No items found.