From 74005364e2080ed7e1acbf673cdfdf5730000dc9 Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Fri, 17 Jan 2025 14:04:35 +0900 Subject: [PATCH] Add Grok (x.ai) interface --- data/apikey.json | 1 + lib/chatgpt.js | 6 +++--- lib/grok.js | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 lib/grok.js diff --git a/data/apikey.json b/data/apikey.json index 0593721..b2e294a 100644 --- a/data/apikey.json +++ b/data/apikey.json @@ -2,6 +2,7 @@ "chatgpt": "file:data/chatgpt_apikey.txt", "anthropic": "file:data/anthropic_apikey.txt", "groq": "file:data/groq_apikey.txt", + "grok": "file:data/grok_apikey.txt", "scrapeops": "file:data/scrapeops_apikey.txt", "searchapi": "file:data/searchapi_apikey.txt", "aviationstack": "file:data/aviationstack_apikey.txt", diff --git a/lib/chatgpt.js b/lib/chatgpt.js index d855dd6..4515c81 100644 --- a/lib/chatgpt.js +++ b/lib/chatgpt.js @@ -14,7 +14,7 @@ function chat(content) { var answers = []; var apikey = CRED.get("apikey", "chatgpt"); - console.log("ChatGPT API KEY:", apikey); + console.log("OpenAI (ChatGPT) API KEY:", apikey); var response = HTTP.create("MSXML") .setVariables({ @@ -38,7 +38,7 @@ function chat(content) { .responseBody ; - if (response.choices.length > 0) { + if ("choices" in response && response.choices.length > 0) { response.choices.forEach(function(x) { answers.push(x.message.content); }); @@ -49,7 +49,7 @@ function chat(content) { exports.chat = chat; -exports.VERSIONINFO = "ChatGPT interface (chatgpt.js) version 0.1.1"; +exports.VERSIONINFO = "OpenAI (ChatGPT) interface version 0.1.2"; exports.AUTHOR = "abuse@catswords.net"; exports.global = global; exports.require = global.require; diff --git a/lib/grok.js b/lib/grok.js new file mode 100644 index 0000000..8bcc39a --- /dev/null +++ b/lib/grok.js @@ -0,0 +1,53 @@ +// grok.js +// Namhyeon Go +// https://github.com/gnh1201/welsonjs +// +// ***SECURITY NOTICE*** +// Grok (x.ai) requires an internet connection, and data may be transmitted externally. Users must adhere to the terms of use and privacy policy. +// - Privacy Policy: https://x.ai/legal/privacy-policy +// +var FILE = require("lib/file"); +var HTTP = require("lib/http"); +var CRED = require("lib/credentials"); + +function chat(content) { + var answers = []; + + var apikey = CRED.get("apikey", "grok"); + console.log("Grok (x.ai) API KEY:", apikey); + + var response = HTTP.create("MSXML") + .setVariables({ + "GROK_API_KEY": apikey + }) + .setHeaders({ + "Content-Type": "application/json", + "Authorization": "Bearer {GROK_API_KEY}" + }) + .setRequestBody({ + "messages": [{ + "role": "user", + "content": content + }], + "model": "grok-2-latest" + }) + .open("post", "https://api.x.ai/v1/chat/completions") + .send() + .responseBody + ; + + if ("choices" in response && response.choices.length > 0) { + response.choices.forEach(function(x) { + answers.push(x.message.content); + }); + } + + return answers.join(' '); +} + +exports.chat = chat; + +exports.VERSIONINFO = "Grok (x.ai) interface version 0.1"; +exports.AUTHOR = "abuse@catswords.net"; +exports.global = global; +exports.require = global.require;