From 2f1a0f6e41fa47cdf110bf14cd88ae7ecb0da97d Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Wed, 6 Nov 2024 04:17:56 +0900 Subject: [PATCH] Add support the Groq LLM API --- .gitignore | 1 + lib/anthropic.js | 6 +++--- lib/groq.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 lib/groq.js diff --git a/.gitignore b/.gitignore index 710d2e8..f012b4b 100644 --- a/.gitignore +++ b/.gitignore @@ -107,6 +107,7 @@ dist bin data/chatgpt-apikey.txt data/anthropic-apikey.txt +data/groq-apikey.txt app/assets/img/_templates app/assets/img/_captured settings.ini diff --git a/lib/anthropic.js b/lib/anthropic.js index d45c34c..2ca4875 100644 --- a/lib/anthropic.js +++ b/lib/anthropic.js @@ -1,4 +1,4 @@ -// Anthropic.js +// anthropic.js // https://github.com/gnh1201/welsonjs // Anthropic API documentation: https://docs.anthropic.com/en/api/getting-started var FILE = require("lib/file"); @@ -19,8 +19,8 @@ function chat(content) { .setVariables({ "ANTHROPIC_API_KEY": apikey }) + .setContentType("application/json") .setHeaders({ - "Content-Type": "application/json", "x-api-key": "{ANTHROPIC_API_KEY}", "anthropic-version": "2023-06-01" }) @@ -53,7 +53,7 @@ function chat(content) { exports.chat = chat; -exports.VERSIONINFO = "Anthropic (Claude) interface (anthropic.js) version 0.1"; +exports.VERSIONINFO = "Anthropic (Claude) interface (anthropic.js) version 0.1.1"; exports.AUTHOR = "abuse@catswords.net"; exports.global = global; exports.require = global.require; diff --git a/lib/groq.js b/lib/groq.js new file mode 100644 index 0000000..b88fa58 --- /dev/null +++ b/lib/groq.js @@ -0,0 +1,46 @@ +// groq.js +// https://github.com/gnh1201/welsonjs +// Groq API documentation: https://console.groq.com/docs/api-reference +var FILE = require("lib/file"); +var HTTP = require("lib/http"); + +function loadApiKey() { + var s = FILE.readFile("data/groq-apikey.txt", FILE.CdoCharset.CdoUTF_8); + return s.trim(); +} + +function chat(content) { + var answers = []; + + var apikey = loadApiKey(); + console.log("Groq (GroqCloud) API KEY:", apikey); + + var response = HTTP.create("MSXML") + .setContentType("application/json") + .setBearerAuth(apikey) + .setRequestBody({ + "model": "llama3-8b-8192", + "messages": [{ + "role": "user", + "content": content + }] + }) + .open("post", "https://api.groq.com/openai/v1/chat/completions") + .send() + .responseBody; + + if ("error" in response) { + answers.push("Error: " + response.error.message); + } else if (response.choices.length > 0) { + answers.push(response.choices[0].message.content); + } + + return answers.join(' '); +} + +exports.chat = chat; + +exports.VERSIONINFO = "Groq (GroqCloud) interface (groq.js) version 0.1"; +exports.AUTHOR = "abuse@catswords.net"; +exports.global = global; +exports.require = global.require;