-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from flacial/gpt-initial
feat: Add OpenAI GPT model integration
- Loading branch information
Showing
8 changed files
with
287 additions
and
81 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,25 @@ | ||
import { SlashCommandBuilder } from "@discordjs/builders"; | ||
import { SlashCommandBuilder } from '@discordjs/builders' | ||
|
||
const userSlashCommand = | ||
new SlashCommandBuilder() | ||
.setName("lookup") | ||
.setDescription('Get the user Discord username by their C0D3 username') | ||
.addStringOption(option => | ||
option.setName('username') | ||
.setDescription("The user's username on C0D3") | ||
.setRequired(true)) | ||
new SlashCommandBuilder() | ||
.setName("lookup") | ||
.setDescription('Get the user Discord username by their C0D3 username') | ||
.addStringOption(option => | ||
option.setName('username') | ||
.setDescription("The user's username on C0D3") | ||
.setRequired(true)) | ||
|
||
|
||
|
||
export const commands = [userSlashCommand].map((command) => command.toJSON()); | ||
|
||
const gptSlashCommand = new SlashCommandBuilder() | ||
.setName('ask') | ||
.setDescription('C0D3 assistant — Disclaimer: May occasionally generate incorrect information') | ||
.addStringOption((option) => | ||
option | ||
.setName('question') | ||
.setDescription('The question you want to ask the assistant') | ||
.setRequired(true) | ||
) | ||
|
||
|
||
export const commands = [userSlashCommand, gptSlashCommand].map((command) => command.toJSON()) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Configuration, OpenAIApi } from 'openai' | ||
import { config } from '../../../../config' | ||
|
||
const configuration = new Configuration({ | ||
apiKey: config.openaiApiKey, | ||
}) | ||
const openai = new OpenAIApi(configuration) | ||
|
||
export const sendPrompt = async (question: string) => { | ||
const system = "You're the best coding assistant in the universe. You help students and explain things to them in a simple, clear, and super-friendly way, and you only respond to questions related to coding or programming.\n\n##EXAMPLE\n\nuser: What's const in JavaScript?\ncoding assistant: In JavaScript, const is a keyword used to declare a constant variable. A constant variable, as the name suggests, is a variable whose value cannot be changed once it has been assigned.\n\nTo declare a constant variable in JavaScript, you can use the const keyword followed by the variable name and its initial value. For example, the following code declares a constant variable named PI and assigns it the value of the mathematical constant pi:\n\n```js\nconst PI = 3.141592653589793;\n```\nOnce a constant variable has been declared, any attempt to reassign a new value to it will result in a TypeError. For example, the following code will produce an error:\n\n```js\nconst PI = 3.141592653589793;\nPI = 3.14; // TypeError: Assignment to constant variable.\n```\n\nNote that while a constant variable's value cannot be changed, its properties can still be modified if it is an object or array. In that case, the object or array itself is still considered constant, but its properties can be changed." | ||
const response = await openai.createChatCompletion({ | ||
model: 'gpt-3.5-turbo', | ||
messages: [ | ||
{ role: 'system', content: system }, | ||
{ role: 'user', content: question } | ||
], | ||
temperature: 0.7, | ||
max_tokens: 756, | ||
top_p: 1, | ||
frequency_penalty: 0, | ||
presence_penalty: 0, | ||
}) | ||
|
||
if (response.status !== 200) { | ||
throw new Error('OpenAI API request failed') | ||
} | ||
|
||
const completion = response.data.choices[0].message?.content | ||
|
||
return { | ||
statusText: response.statusText, | ||
status: response.status, | ||
completion | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,19 @@ | ||
import { Awaitable, Interaction } from "discord.js"; | ||
import { lookupReply } from "./commandsReplies"; | ||
import { assistantAskReply, lookupReply } from "./commandsReplies"; | ||
|
||
export const onInteractionCreate = (interaction: Interaction): Awaitable<void> => { | ||
if (!interaction.isCommand()) return; | ||
|
||
const { commandName } = interaction; | ||
|
||
if (commandName === "lookup") lookupReply(interaction) | ||
switch (commandName) { | ||
case "lookup": | ||
lookupReply(interaction); | ||
break; | ||
case "ask": | ||
assistantAskReply(interaction); | ||
break; | ||
default: | ||
break; | ||
} | ||
} |
Oops, something went wrong.