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
This commit is contained in:
parent
960c340760
commit
4f4b708ba5
5 changed files with 39 additions and 14 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,3 +1,3 @@
|
|||
dist
|
||||
node_modules
|
||||
src/config.json
|
||||
src/config.*
|
||||
|
|
|
@ -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."
|
||||
}
|
34
src/config_example.ts
Normal file
34
src/config_example.ts
Normal file
|
@ -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<ChatCompletionRequestData, "messages"> = {
|
||||
model: "gpt-3.5-turbo",
|
||||
max_tokens: 384,
|
||||
};
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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<string, DiscordMes
|
|||
});
|
||||
}
|
||||
|
||||
rvalue.push({ role: "system", content: config.systemPrompt});
|
||||
rvalue.push(...config.systemPrompt().reverse());
|
||||
|
||||
return rvalue.reverse();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue