-
-
Save heyitsnoah/3f678ec2cb04c9699ca51d79ffeede26 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// First we need to pull our Input Variables into the script | |
const inputConfig = input.config(); | |
// We need to put our title and description into a prompt. | |
const prompt = `Using the following title and context, write a blog post.\n\nTitle: ${inputConfig.title}\nContext: ${inputConfig.additionalContext}\n\nBlog Post:`; | |
// Let's format the body of the API call we will make. There is a bunch of stuff in here that OpenAI needs. | |
const body = { | |
model: "text-davinci-003", | |
prompt: prompt, | |
temperature: 0.7, | |
max_tokens: 4000, | |
top_p: 1, | |
frequency_penalty: 0, | |
presence_penalty: 0, | |
}; | |
// This is where we call the actual API. Make sure you replace TOKEN with your OpenAI API Token. | |
let response = await fetch("https://api.openai.com/v1/completions", { | |
method: "POST", | |
body: JSON.stringify(body), | |
headers: { | |
"Content-Type": "application/json", | |
Authorization: "Bearer TOKEN", | |
}, | |
}); | |
// Make sure the call went through properly | |
if (response.ok) { | |
// Grab the JSON body response | |
const responseBody = await response.json(); | |
// Grab the text completion and trim the whitespace | |
const blogPost = responseBody.choices[0].text.trim(); | |
// Finally, we are going to insert the finished post back into our table. | |
const table = base.getTable("Blog Posts"); | |
await table.updateRecordAsync(inputConfig.recordId, { | |
"Blog Post": blogPost, | |
}); | |
console.log("Successfully Added Post"); | |
} else { | |
console.log("Error"); | |
console.log(await response.json()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment