Add Grok (x.ai) interface

This commit is contained in:
Namhyeon Go 2025-01-17 14:04:35 +09:00
parent 1cea720a70
commit 74005364e2
3 changed files with 57 additions and 3 deletions

View File

@ -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",

View File

@ -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;

53
lib/grok.js Normal file
View File

@ -0,0 +1,53 @@
// grok.js
// Namhyeon Go <abuse@catswords.net>
// 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;