Skip to content

Instantly share code, notes, and snippets.

@zmxv
Created May 13, 2023 17:29
Show Gist options
  • Select an option

  • Save zmxv/8d7d85b464930f5c17d652d027b95944 to your computer and use it in GitHub Desktop.

Select an option

Save zmxv/8d7d85b464930f5c17d652d027b95944 to your computer and use it in GitHub Desktop.
const fs = require("fs");
const claudeAPIKey = process.env.CLAUDE_APIKEY;
const claudeAPIEndpoint = "https://api.anthropic.com/v1/complete";
const claudeModel = "claude-v1-100k";
const claudeMaxTokensToSample = 100000;
const claudeStopSequences = ["\n\nHuman:"];
const claudeTemperature = 1;
const claudePromptPrefix = "\n\nHuman: Continue writing the following article. Do not repeat what has been written. Wrap your output in a markdown code block.\n\n```";
const claudePromptSuffix = "```\n\nAssistant: ";
function callClaude(prompt) {
return fetch(claudeAPIEndpoint, {
method: "POST",
headers: {
"X-API-Key": claudeAPIKey,
"Content-Type": "application/json",
},
body: JSON.stringify({
prompt,
model: claudeModel,
max_tokens_to_sample: claudeMaxTokensToSample,
stop_sequences: claudeStopSequences,
temperature: claudeTemperature,
}),
}).then(res => res.json());
}
async function main() {
if (!claudeAPIKey) {
console.log("Missing the environment variable CLAUDE_APIKEY");
process.exit(1);
}
if (process.argv.length !== 3) {
console.log("Usage: node main.js <prompt-file>");
process.exit(2);
}
let prompt = fs.readFileSync(process.argv[2], "utf8");
while(true) {
const r = await callClaude(claudePromptPrefix + prompt + claudePromptSuffix);
console.log(JSON.stringify(r, undefined, 2));
if (r.exception) {
throw new Error(r.exception);
}
const res = r.completion.replace(/^[^`]*```/, "").replace(/```[^`]*$/, "");
prompt += res;
fs.writeFileSync(process.argv[2], prompt);
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment