Skip to content

Instantly share code, notes, and snippets.

@donpdonp
Last active June 1, 2023 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donpdonp/db9f8d7c4380e4f6842463af57f42598 to your computer and use it in GitHub Desktop.
Save donpdonp/db9f8d7c4380e4f6842463af57f42598 to your computer and use it in GitHub Desktop.
neuron chatGPT
(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