Skip to content

Commit

Permalink
Improve error response for generate-read-more workflow (#86)
Browse files Browse the repository at this point in the history
* Update workflow to fail on missing api key

* Improve error message
  • Loading branch information
jhcoll authored Oct 25, 2023
1 parent 9b9bb58 commit 6983596
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions scripts/generate-related/compute-embeddings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}
Expand All @@ -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(
Expand All @@ -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()
})
Expand Down

0 comments on commit 6983596

Please sign in to comment.