DavidD
Voiceflow Partners8mo ago
1 reply
David

Search Knowledge base with Question and Filter

Hey, I need help creating a function that search for specific products using the filter. So the goal is basically to filter out "noise" so the question I use to search the knowledge base only retrieve the relevant chunks having the filter I applied, for example brand "sony" should make so all the chunks I receieve has the brand "sony" with my question I used to search for.

Here is the code:
export default async function main(args) {
const { item_number, VFapiKey, brand, question } = args.inputVars;

const url = 'https://general-runtime.voiceflow.com/knowledge-base/query';
const data = {
question: question, // <- must be empty to only filter
chunkLimit: 10,
synthesis: false,
filters: {
brand: {
$eq: brand // <- the exact metadata field from table-upload
}
};

try {
const response = await fetch(url, {
method: 'POST',
headers: {
'accept': 'application/json',
'content-type': 'application/json',
'Authorization': VFapiKey
},
body: JSON.stringify(data)
});

if (!response.ok) throw new Error(
HTTP error! status: ${response.status}
);
const responseBody = await response.json;

let chunks = responseBody.chunks "",
first_match_id: chunks[0]?.metadata?.item_number || ""
},
next: { path: "success" }
};
} catch (error) {
return {
outputVars: { error: error.message },
next: { path: "error" }
};
}
}
image.png
Was this page helpful?