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