Skip to content

Instantly share code, notes, and snippets.

@heyitsnoah
Last active March 29, 2023 18:49
Show Gist options
  • Save heyitsnoah/3f678ec2cb04c9699ca51d79ffeede26 to your computer and use it in GitHub Desktop.
Save heyitsnoah/3f678ec2cb04c9699ca51d79ffeede26 to your computer and use it in GitHub Desktop.
// 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