Last active
April 29, 2024 23:13
-
-
Save kindgracekind/bd286ba8f5174376284fcdffe66ca8c7 to your computer and use it in GitHub Desktop.
Claude Completions
This file contains hidden or 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
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on code by @socketteer: https://github.com/cosmicoptima/loom/pull/15/files