okand
okand2mo ago

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 []; // Extra security for empty hit if (!chunks.length) { return { outputVars: { error: "No product found" }, next: { path: "not_found" } }; } const cleaned = chunks.map(c => ({ text: c.content, metadata: c.metadata })); return { outputVars: { cleaned_chunks: JSON.stringify(cleaned), first_match_text: chunks[0]?.content "", first_match_id: chunks[0]?.metadata?.item_number || "" }, next: { path: "success" } }; } catch (error) { return { outputVars: { error: error.message }, next: { path: "error" } }; } }
No description
1 Reply
exotic-emerald
exotic-emerald2mo ago

Did you find this page helpful?