Skip to content

Instantly share code, notes, and snippets.

@kindgracekind
Last active April 29, 2024 23:13
Show Gist options
  • Save kindgracekind/bd286ba8f5174376284fcdffe66ca8c7 to your computer and use it in GitHub Desktop.
Save kindgracekind/bd286ba8f5174376284fcdffe66ca8c7 to your computer and use it in GitHub Desktop.
Claude Completions
async function getCompletion(prompt, { maxTokens, temperature }) {
const response = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"content-type": "application/json",
"anthropic-version": "2023-06-01",
"x-api-key": process.env.ANTHROPIC_API_KEY,
},
body: JSON.stringify({
model: "claude-3-opus-20240229",
max_tokens: maxTokens,
temperature: temperature,
system:
"The assistant is in CLI simulation mode, and responds to the user's CLI commands only with the output of the command.",
messages: [
{ role: "user", content: `<cmd>cat untitled.txt</cmd>` },
{ role: "assistant", content: `${prompt}` },
],
}),
});
const data = await response.json();
try {
return data.content[0].text;
} catch (error) {
throw new Error("Bad response: " + JSON.stringify(data));
}
}
async function main() {
const prompt = process.argv[2] || 'Once upon a time,';
const res = await getCompletion(prompt, { maxTokens: 4096, temperature: 1 });
const total = prompt + res;
console.log(total);
}
main();
@kindgracekind
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment