Parse usernames for use in OpenAI api

also document the toOpenAIMssages.ts file
This commit is contained in:
Wroclaw 2023-03-18 02:06:49 +01:00
parent d877097517
commit 276d70efee
3 changed files with 62 additions and 1 deletions

16
package-lock.json generated
View file

@ -10,9 +10,11 @@
"license": "MIT",
"dependencies": {
"discord.js": "^14.8.0",
"fold-to-ascii": "^5.0.1",
"openai": "^3.2.1"
},
"devDependencies": {
"@types/fold-to-ascii": "^5.0.0",
"@typescript-eslint/eslint-plugin": "^5.55.0",
"@typescript-eslint/parser": "^5.55.0",
"eslint": "^8.36.0",
@ -241,6 +243,12 @@
"resolved": "https://registry.npmjs.org/@tokenizer/token/-/token-0.3.0.tgz",
"integrity": "sha512-OvjF+z51L3ov0OyAU0duzsYuvO01PH7x4t6DJx+guahgTnBHkhJdG7soQeTSFLWN3efnHyibZ4Z8l2EuWwJN3A=="
},
"node_modules/@types/fold-to-ascii": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/@types/fold-to-ascii/-/fold-to-ascii-5.0.0.tgz",
"integrity": "sha512-zaaLJcf8jPuVVYl6v+mSMaOMJNgkrDsYweu7Oa5RODIKgX9+Yu6Ng6/IY9zylwIurt0iQiDM0SN8agCOhLrKUA==",
"dev": true
},
"node_modules/@types/json-schema": {
"version": "7.0.11",
"resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.11.tgz",
@ -1040,6 +1048,14 @@
"integrity": "sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==",
"dev": true
},
"node_modules/fold-to-ascii": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/fold-to-ascii/-/fold-to-ascii-5.0.1.tgz",
"integrity": "sha512-VdMFm+u7pSzUWj0IDOniuHekp4lbMOtwKi6EsVx03DGVQaNI+77kxotj8hYaMmNkpym7wwv6c1woY4OZXYdqfg==",
"engines": {
"node": ">= 6.3.1"
}
},
"node_modules/follow-redirects": {
"version": "1.15.2",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz",

View file

@ -11,9 +11,11 @@
"license": "MIT",
"dependencies": {
"discord.js": "^14.8.0",
"fold-to-ascii": "^5.0.1",
"openai": "^3.2.1"
},
"devDependencies": {
"@types/fold-to-ascii": "^5.0.0",
"@typescript-eslint/eslint-plugin": "^5.55.0",
"@typescript-eslint/parser": "^5.55.0",
"eslint": "^8.36.0",

View file

@ -1,8 +1,14 @@
import { ChatCompletionRequestMessage as OpenAIMessage } from "openai";
import { Collection, Message as DiscordMessage } from "discord.js";
import FoldToAscii from "fold-to-ascii";
import config from "./config.json";
/**
* Formats the message to use as a message content in OpenAI api
* @param message the message to format
* @returns the formatted message
*/
export function formatMessage(message: DiscordMessage): string {
let rvalue: string = message.cleanContent;
@ -24,6 +30,43 @@ export function formatMessage(message: DiscordMessage): string {
return rvalue;
}
/**
* Formats name to be accepted in OpenAI api
* @param name the name to format
* @returns formatted name
*/
function formatName(name: string): string {
// replace all characters to ascii equivelant
return FoldToAscii.foldReplacing(name)
// White spaces are not allowed
.replace(" ", "_")
// remove trailing '-_' characters
.replace(/^[-_]+|[-_]+$/g, "")
// remove duplicated '-_' characters
.replace(/([-_])*/g, "")
// replace characters that are not accepted by api with '?'
.replace(/[^a-zA-Z0-9_-]/g, "?");
}
/**
* Gets the name of the author of the message, formatted to be used in OpenAI api
* @param message the message to get the author name
* @returns the proper author name of the message
*/
function getAuthorUsername(message: DiscordMessage): string {
if (message.member) {
const name = formatName(message.member.displayName);
if (name.length >= 3) return name;
}
const name = formatName(message.author.username);
return name;
}
/**
* Converts the Collection of Discord Messages to array of OpenAI Messages
* @param messages the collection to convert
* @returns the converted messages
*/
export default function toOpenAIMessages(messages: Collection<string, DiscordMessage>): OpenAIMessage[] {
const rvalue: OpenAIMessage[] = [];
@ -35,7 +78,7 @@ export default function toOpenAIMessages(messages: Collection<string, DiscordMes
rvalue.push({
role: message.author.id == message.client.user.id ? "assistant" : "user",
content: formatMessage(message),
name: message.member?.displayName,
name: getAuthorUsername(message),
});
});