welsonjs/lib/anthropic.js

65 lines
1.9 KiB
JavaScript
Raw Normal View History

2024-11-05 19:17:56 +00:00
// anthropic.js
// Namhyeon Go <abuse@catswords.net>
2024-11-03 15:32:25 +00:00
// https://github.com/gnh1201/welsonjs
//
// ***SECURITY NOTICE***
// Anthropic/Claude requires an internet connection. Data may be transmitted externally. Users must also comply with the terms of use and the privacy policy.
// - Privacy Policy: https://www.anthropic.com/legal/privacy
//
2024-11-03 15:32:25 +00:00
var FILE = require("lib/file");
var HTTP = require("lib/http");
function loadApiKey() {
var s = FILE.readFile("data/anthropic-apikey.txt", FILE.CdoCharset.CdoUTF_8);
return s.trim();
}
function chat(content) {
var answers = [];
var apikey = loadApiKey();
console.log("Anthropic (Claude) API KEY:", apikey);
var response = HTTP.create("MSXML")
.setVariables({
"ANTHROPIC_API_KEY": apikey
})
2024-11-05 19:17:56 +00:00
.setContentType("application/json")
2024-11-03 15:32:25 +00:00
.setHeaders({
"x-api-key": "{ANTHROPIC_API_KEY}",
"anthropic-version": "2023-06-01"
})
.setRequestBody({
"model": "claude-3-5-sonnet-20241022",
"max_tokens": 1024,
"messages": [{
"role": "user",
"content": content
}]
})
.open("post", "https://api.anthropic.com/v1/messages")
.send()
.responseBody;
if ("error" in response) {
answers.push("Error: " + response.error.message);
} else if ("content" in response && response.content.length > 0) {
response.content.forEach(function(x) {
if (x.type == "text") {
answers.push(x.text);
} else {
answers.push("Not supported type: " + x.type);
}
});
}
return answers.join(' ');
}
exports.chat = chat;
2024-11-05 19:17:56 +00:00
exports.VERSIONINFO = "Anthropic (Claude) interface (anthropic.js) version 0.1.1";
2024-11-03 15:32:25 +00:00
exports.AUTHOR = "abuse@catswords.net";
exports.global = global;
exports.require = global.require;