From 3989eeba45a3d6b1e57da1b2b5ddd657f906c4e6 Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Mon, 4 Nov 2024 00:32:25 +0900 Subject: [PATCH] Add Anthropic API integration --- .gitignore | 1 + lib/anthropic.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 lib/anthropic.js diff --git a/.gitignore b/.gitignore index d8796b1..710d2e8 100644 --- a/.gitignore +++ b/.gitignore @@ -106,6 +106,7 @@ dist # user defined assets bin data/chatgpt-apikey.txt +data/anthropic-apikey.txt app/assets/img/_templates app/assets/img/_captured settings.ini diff --git a/lib/anthropic.js b/lib/anthropic.js new file mode 100644 index 0000000..d45c34c --- /dev/null +++ b/lib/anthropic.js @@ -0,0 +1,59 @@ +// Anthropic.js +// https://github.com/gnh1201/welsonjs +// Anthropic API documentation: https://docs.anthropic.com/en/api/getting-started +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 + }) + .setHeaders({ + "Content-Type": "application/json", + "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; + +exports.VERSIONINFO = "Anthropic (Claude) interface (anthropic.js) version 0.1"; +exports.AUTHOR = "abuse@catswords.net"; +exports.global = global; +exports.require = global.require;