Implement early AI integration #18
- Added new event to catch messageCreate events - @ messages to the server will use ChatGPT to respond to the message with an indepth prompt about the server - Implement module to interact with chatGPT repeatably - Add linkcop with GPT integration #12 - Added environment variable for inital prompt for GPT integration
This commit is contained in:
39
discordBot/modules/gptHandler.mjs
Normal file
39
discordBot/modules/gptHandler.mjs
Normal file
@@ -0,0 +1,39 @@
|
||||
import { DebugBuilder } from "../../modules/debugger.mjs";
|
||||
const log = new DebugBuilder("server", "discordBot.modules.gptHandler");
|
||||
import dotenv from 'dotenv';
|
||||
dotenv.config();
|
||||
|
||||
import { OpenAI } from 'openai';
|
||||
|
||||
const openai = new OpenAI(process.env.OPENAI_API_KEY);
|
||||
|
||||
let conversation = [];
|
||||
|
||||
conversation.push({
|
||||
role: 'system',
|
||||
content: process.env.DRB_SERVER_INITIAL_PROMPT
|
||||
});
|
||||
|
||||
export const gptHandler = async (additionalMessages) => {
|
||||
// Add the additional messages to the conversation
|
||||
conversation = conversation.concat(additionalMessages);
|
||||
log.DEBUG("AI Conversation:", conversation);
|
||||
try {
|
||||
const response = await openai.chat.completions.create({
|
||||
model: 'gpt-3.5-turbo',
|
||||
messages: conversation,
|
||||
}).catch((error) => log.ERROR("OpenAI Error: ", error));
|
||||
|
||||
log.DEBUG("AI Response:", response);
|
||||
|
||||
if (!response) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return response
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error generating response:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user