From 4f4b708ba5aea74f6629bbc1cbe7d15d77169db6 Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Fri, 24 Mar 2023 15:44:22 +0100 Subject: [PATCH] move config to a typescript file, add option for chatCompletionConfig Now we can write code inside config, which allows us to send current time to the OpenAI api inside system message! Example config updated accordingly --- .gitignore | 2 +- src/config_example.json | 8 -------- src/config_example.ts | 34 ++++++++++++++++++++++++++++++++++ src/index.ts | 5 ++--- src/toOpenAIMessages.ts | 4 ++-- 5 files changed, 39 insertions(+), 14 deletions(-) delete mode 100644 src/config_example.json create mode 100644 src/config_example.ts diff --git a/.gitignore b/.gitignore index c98720f..2c1b07f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ dist node_modules -src/config.json +src/config.* diff --git a/src/config_example.json b/src/config_example.json deleted file mode 100644 index 26cf23d..0000000 --- a/src/config_example.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "__comment": "Don't forget to rename the file to config.json", - "tokens": { - "Discord": "Discord token here", - "OpenAI": "OpenAI token here" - }, - "systemPrompt": "You are GPTcord, an AI built on top of ChatGPT (a large language model trained by OpenAI) for Discord. Answer as concisely as possible." -} diff --git a/src/config_example.ts b/src/config_example.ts new file mode 100644 index 0000000..7d3701e --- /dev/null +++ b/src/config_example.ts @@ -0,0 +1,34 @@ +import { ChatCompletionRequestMessage as OpenAIMessage , CreateChatCompletionRequest as ChatCompletionRequestData } from "openai"; + +// Don't forget to rename the file to config.ts + +const calendarConfig: Intl.DateTimeFormatOptions = { + weekday: "short", + year: "numeric", + month: "short", + day: "numeric", + hour: "2-digit", + minute: "2-digit", + hour12: false, +}; + +export default class config { + /** Tokens to authenticate with */ + static readonly tokens = { + Discord: "Discord token here", + OpenAI: "OpenAI token here", + }; + + /** Messages to append at the start of every chat when sending to API */ + static systemPrompt(): OpenAIMessage[] { + return [ + { role: "system", content: `You are GPTcord, an AI built on top of ChatGPT (a large language model trained by OpenAI) for Discord. Answer as concisely as possible. Current time (${Intl.DateTimeFormat().resolvedOptions().timeZone}): ${new Date().toLocaleString("en-US", calendarConfig)}` } + ]; + } + + /** OpenAI model config */ + static readonly chatCompletionConfig: Omit = { + model: "gpt-3.5-turbo", + max_tokens: 384, + }; +} diff --git a/src/index.ts b/src/index.ts index 380063b..45115d1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import DiscordApi from "discord.js"; import { Configuration as OpenAIApiConfiguration, OpenAIApi } from "openai"; -import config from "./config.json"; +import config from "./config"; import toOpenAIMessages from "./toOpenAIMessages"; import Moderation from "./moderation"; @@ -34,9 +34,8 @@ discord.on("messageCreate", async message => { message.channel.sendTyping(); const answer = await openai.createChatCompletion({ - model: "gpt-3.5-turbo", + ...config.chatCompletionConfig, messages: toOpenAIMessages(messages), - max_tokens: 168 }); const usage = answer.data.usage; diff --git a/src/toOpenAIMessages.ts b/src/toOpenAIMessages.ts index 38c7cd1..9a8f99e 100644 --- a/src/toOpenAIMessages.ts +++ b/src/toOpenAIMessages.ts @@ -2,7 +2,7 @@ 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"; +import config from "./config"; import countTokens from "./tokenCounter"; /** @@ -97,7 +97,7 @@ export default function toOpenAIMessages(messages: Collection