Skip to content

Commit

Permalink
Merge pull request #135 from flacial/fix/discord-chars-limit
Browse files Browse the repository at this point in the history
fix: Discord does not accept messages that exceed 2000 characters.
  • Loading branch information
flacial authored Jan 5, 2024
2 parents e0a81dd + 2ffd02c commit ac4a0fd
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 54 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"express": "^4.18.2",
"graphql": "^16.6.0",
"graphql-request": "^6.1.0",
"openai": "^3.2.1",
"openai": "^4.24.1",
"zod": "^3.21.4"
},
"devDependencies": {
Expand Down
37 changes: 29 additions & 8 deletions src/Bot/commands/commandsReplies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ const graphQLClient = new GraphQLClient(config.graphqlAPI);

export const lookupReply = async (interaction: ChatInputCommandInteraction) => {
try {
const username = interaction.options.getString("username") ?? "";
await interaction.deferReply({ ephemeral: true });

const username = interaction.options.getString("username") ?? "Unknown";

const data = (await graphQLClient.request(USER_INFO, {
username: username,
username,
})) as UserInfoQuery;

const userInfo = data?.userInfo;
Expand Down Expand Up @@ -75,12 +76,32 @@ export const assistantAskReply = async (

const { completion } = await promptPromise;

await interaction.editReply({
content:
completion ||
"Sorry, I had an issue while responding. Please try again!",
});
return;
if (!completion) {
await interaction.editReply({
content: "Sorry, I had an issue while responding. Please try again!",
});
return;
}

// split the response into paragraphs
const paragraphs = completion.split("\n");

let message = "";
for (const paragraph of paragraphs) {
// add paragraphs to the message until it reaches the 2000 character limit
if ((message + paragraph).length <= 1900) {
message += `${paragraph}\n`;
} else {
// send a follow up if the message is > 2000 characters
await interaction.followUp({ content: message });
message = `${paragraph}\n`;
}
}

// send the rest of the message if it's not empty
if (message.trim() !== "") {
await interaction.followUp({ content: message });
}
} catch (e) {
console.error(e);

Expand Down
48 changes: 22 additions & 26 deletions src/Bot/commands/externals/gpt.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
import { Configuration, OpenAIApi } from "openai";
import OpenAI from "openai";
import { config } from "../../../../config";

const configuration = new Configuration({
const openai = new OpenAI({
apiKey: config.openaiApiKey,
});
const openai = new OpenAIApi(configuration);

export const sendPrompt = async (question: string) => {
const instructions =
"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.\n\n";
const response = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [
{ role: "user", content: instructions },
{ role: "user", content: question },
],
temperature: 0.7,
max_tokens: 756,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
});
try {
const instructions = `Your role is to be a coding assistant, offering clear and concise explanations on programming-related topics to students. Your responses should be friendly and easy to understand, and you should only address questions about coding, programming, and jobs.`;
const response = await openai.chat.completions.create({
model: "gpt-3.5-turbo-1106",
messages: [
{ role: "system", content: instructions },
{ role: "user", content: question },
],
temperature: 0.7,
max_tokens: 4000,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
});

if (response.status !== 200) {
const completion = response.choices[0].message?.content;

return {
completion,
};
} catch (error) {
throw new Error("OpenAI API request failed");
}

const completion = response.data.choices[0].message?.content;

return {
statusText: response.statusText,
status: response.status,
completion,
};
};
Loading

0 comments on commit ac4a0fd

Please sign in to comment.