From 69835967b31cab673608c6d0c31f81d8443008b8 Mon Sep 17 00:00:00 2001 From: Jack Collier <93914995+jhcoll@users.noreply.github.com> Date: Wed, 25 Oct 2023 10:45:02 +0100 Subject: [PATCH] Improve error response for generate-read-more workflow (#86) * Update workflow to fail on missing api key * Improve error message --- scripts/generate-related/compute-embeddings.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/scripts/generate-related/compute-embeddings.js b/scripts/generate-related/compute-embeddings.js index 6496d9505a..1d905e3a3e 100644 --- a/scripts/generate-related/compute-embeddings.js +++ b/scripts/generate-related/compute-embeddings.js @@ -17,11 +17,8 @@ const { markdownToTxt } = require("markdown-to-txt"); const filename = `${outputPath}/${file}`; if (!fs.existsSync(filename)) { - await summarisePost(formatContent(path)).then((embedding) => { + await summarisePost(formatContent(path), file).then((embedding) => { fs.writeFileSync(filename, JSON.stringify(embedding, null, 2)); - }).catch((err) => { - console.log("failed to embed: ", filename); - console.log(err); }); } } @@ -39,7 +36,7 @@ const formatContent = (post) => { return text.split(/[\s]+/).slice(0, 1000).join(" "); }; -const summarisePost = async (data) => { +const summarisePost = async (data, file) => { const OPENAI_API_KEY = process.env.npm_config_openai_api_key; return await fetch( @@ -57,7 +54,11 @@ const summarisePost = async (data) => { }) .then((res) => { if(res.status !== 200) { - throw new Error(res.statusText); + console.log("failed to embed: " + file) + if(res.status === 401) { + throw Error(res.statusText + " - check your OpenAI API key"); + } + throw Error(res.statusText); } return res.json() })