Send a Query to Mistral 7xB (via. together.ai)

Use Voiceflows Knowledge Base with Mistral 7xB as your AI model. This function works with the chunk extraction function to send a question and relevant chunks to Mistral 7xB (via. together.ai).
Created By
Daniel D'Souza
Voiceflow Built
INPUT VARIABLES
{
MistralAPIKey
}
This is your API key from together.ai to use their hosted Mistral AI model
{
question
}
The users question
{
chunks
}
(optional) The filtered chunks from the Knowledge Base Chunk retrieval function
{
}
{
}
{
}
OUTPUT VARIABLES
{
Answer
}
This is the extracted answer from the Mistral API
{
}
{
}
{
}
{
}
{
}
PATHS
{
Success
}
If the function was successful
{
Error
}
If there was an error in the function
{
}
{
}
{
}
{
}

Function Code Snippet


export default async function main(args) {
// Extract the API key and Question from inputVars
  const {question, MistralApiKey, chunks} = args.inputVars;

//Define the API data
  const url = 'https://api.together.xyz/v1/chat/completions';
  const data = {
      model: 'mistralai/Mixtral-8x7B-Instruct-v0.1',
    max_tokens: 1024,
    messages: [
    {
        role: 'system',
        content: 'Answer the question using only the information provided'
    },
    {
        role: 'user',
        content: `Question: ${question} ### Information: ${chunks}`
    }
    ]
  };

// 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 ${MistralApiKey}`,
          '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}`
          }
        }
      ],
    };
  }
}

Function Walkthrough