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

Template
Function
1
Template
Function
by
Daniel D'Souza

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:

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, 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}`
}
}
],
};
}
}
copy-icon

Explore More Templates

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

ghraphic
No items found.
No items found.