Back to all functions

Extract & Filter Chunks from a Knowledge Base Response

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 By
Daniel D'Souza
Voiceflow
download-icon
INPUT VARIABLES
{
VFapiKey
}
Add your Voiceflow API key
{
question
}
Insert the user's question
{
}
{
}
{
}
{
}
share-icon
OUTPUT VARIABLES
{
cleaned_chunks
}
The filtered KB chunks
{
}
{
}
{
}
{
}
{
}
paths-icon
PATHS
{
error
}
Error path for the function
{
success
}
Success path for the function
{
}
{
}
{
}
{
}

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

Function Walkthrough

Explore More Functions

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

ghraphic