neuron chatGPT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
setup() | |
return {name:"chatgpt"} | |
}) | |
var dbkey = "openai-key" | |
var api_key = "" | |
function setup() { | |
db.get(dbkey, function(key) { | |
var word = "missing" | |
if(key && key.length > 0) { | |
api_key = key | |
word = "loaded" | |
} | |
bot.say(bot.admin_channel, "openai api key "+word) | |
}) | |
} | |
function go(msg){ | |
if(msg.method == "irc.privmsg") { | |
var match = /^\!chatgpt(\s+(.+))?$/.exec(msg.params.message) | |
if(match) { | |
var response_str = api(match[2]) | |
var reply = "" | |
if (response_str.length > 0) { | |
try { | |
var data = JSON.parse(response_str) | |
if (data.error) { | |
reply = "openai: "+data.error.message | |
} else { | |
var gpt_response = data.choices[0].message.content.trim() | |
reply = "chatgpt: "+gpt_response | |
} | |
} catch (e) { | |
reply = "openai api: "+response_str | |
} | |
} else { | |
reply = "openai API returned 0 bytes" | |
} | |
bot.say(msg.params.channel, reply) | |
} | |
} | |
} | |
function api(msg) { | |
var url = 'https://api.openai.com/v1/chat/completions' | |
var model = 'gpt-3.5-turbo' | |
var params = { url: url, headers: {"Content-Type": "application/json", | |
"Authorization": "Bearer "+api_key}} | |
var body = {model: model, messages: [{role: "user", content: msg}]} | |
var data = http.post(params, body); | |
return data | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment