Track all requests to OpenAI in a database

this will be in future used to limit access to the bot
This commit is contained in:
Wroclaw 2023-05-02 17:55:48 +02:00
parent 05c50d25e4
commit a66115c3b8
5 changed files with 94 additions and 0 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
dist
node_modules
src/config.*
.env

51
package-lock.json generated
View file

@ -9,6 +9,7 @@
"version": "0.0.1",
"license": "MIT",
"dependencies": {
"@prisma/client": "^4.13.0",
"discord.js": "^14.8.0",
"fold-to-ascii": "^5.0.1",
"gpt-3-encoder": "^1.1.4",
@ -19,6 +20,7 @@
"@typescript-eslint/eslint-plugin": "^5.55.0",
"@typescript-eslint/parser": "^5.55.0",
"eslint": "^8.36.0",
"prisma": "^4.13.0",
"typescript": "^4.9.5"
}
},
@ -208,6 +210,38 @@
"node": ">= 8"
}
},
"node_modules/@prisma/client": {
"version": "4.13.0",
"resolved": "https://registry.npmjs.org/@prisma/client/-/client-4.13.0.tgz",
"integrity": "sha512-YaiiICcRB2hatxsbnfB66uWXjcRw3jsZdlAVxmx0cFcTc/Ad/sKdHCcWSnqyDX47vAewkjRFwiLwrOUjswVvmA==",
"hasInstallScript": true,
"dependencies": {
"@prisma/engines-version": "4.13.0-50.1e7af066ee9cb95cf3a403c78d9aab3e6b04f37a"
},
"engines": {
"node": ">=14.17"
},
"peerDependencies": {
"prisma": "*"
},
"peerDependenciesMeta": {
"prisma": {
"optional": true
}
}
},
"node_modules/@prisma/engines": {
"version": "4.13.0",
"resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-4.13.0.tgz",
"integrity": "sha512-HrniowHRZXHuGT9XRgoXEaP2gJLXM5RMoItaY2PkjvuZ+iHc0Zjbm/302MB8YsPdWozAPHHn+jpFEcEn71OgPw==",
"devOptional": true,
"hasInstallScript": true
},
"node_modules/@prisma/engines-version": {
"version": "4.13.0-50.1e7af066ee9cb95cf3a403c78d9aab3e6b04f37a",
"resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-4.13.0-50.1e7af066ee9cb95cf3a403c78d9aab3e6b04f37a.tgz",
"integrity": "sha512-fsQlbkhPJf08JOzKoyoD9atdUijuGBekwoOPZC3YOygXEml1MTtgXVpnUNchQlRSY82OQ6pSGQ9PxUe4arcSLQ=="
},
"node_modules/@sapphire/async-queue": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/@sapphire/async-queue/-/async-queue-1.5.0.tgz",
@ -1602,6 +1636,23 @@
"node": ">= 0.8.0"
}
},
"node_modules/prisma": {
"version": "4.13.0",
"resolved": "https://registry.npmjs.org/prisma/-/prisma-4.13.0.tgz",
"integrity": "sha512-L9mqjnSmvWIRCYJ9mQkwCtj4+JDYYTdhoyo8hlsHNDXaZLh/b4hR0IoKIBbTKxZuyHQzLopb/+0Rvb69uGV7uA==",
"devOptional": true,
"hasInstallScript": true,
"dependencies": {
"@prisma/engines": "4.13.0"
},
"bin": {
"prisma": "build/index.js",
"prisma2": "build/index.js"
},
"engines": {
"node": ">=14.17"
}
},
"node_modules/punycode": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.0.tgz",

View file

@ -10,6 +10,7 @@
"author": "Wroclaw",
"license": "MIT",
"dependencies": {
"@prisma/client": "^4.13.0",
"discord.js": "^14.8.0",
"fold-to-ascii": "^5.0.1",
"gpt-3-encoder": "^1.1.4",
@ -20,6 +21,7 @@
"@typescript-eslint/eslint-plugin": "^5.55.0",
"@typescript-eslint/parser": "^5.55.0",
"eslint": "^8.36.0",
"prisma": "^4.13.0",
"typescript": "^4.9.5"
}
}

23
schema.prisma Normal file
View file

@ -0,0 +1,23 @@
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}
model Usage {
timestamp DateTime @id
user BigInt
channel BigInt
guild BigInt?
usageReguest Int
usageResponse Int
}
model Limits {
user BigInt @id
limit Int?
vip Boolean @default(false)
}

View file

@ -1,5 +1,6 @@
import DiscordApi from "discord.js";
import { Configuration as OpenAIApiConfiguration, OpenAIApi } from "openai";
import { PrismaClient } from "@prisma/client";
import config from "./config";
import toOpenAIMessages from "./toOpenAIMessages";
@ -17,6 +18,8 @@ export const openai = new OpenAIApi(new OpenAIApiConfiguration({
apiKey: config.tokens.OpenAI
}));
export const database = new PrismaClient();
discord.on("ready", async event => {
console.log(`Connected to Discord as ${event.user.tag} (${event.user.id})`);
});
@ -55,6 +58,20 @@ async function onMessage(channel: string) {
if (usage != undefined) {
const channelName: string = message.inGuild() ? `${message.channel.name} (${message.guild.name})` : `@${message.author.tag}`;
console.log(`Used ${usage.total_tokens} (${usage.prompt_tokens} + ${usage.completion_tokens}) tokens for ${message.author.tag} (${message.author.id}) in #${channelName}`);
database.usage.create({
data: {
timestamp: message.createdAt,
user: BigInt(message.author.id),
channel: BigInt(message.channelId),
guild: message.guildId ? BigInt(message.guildId) : null,
usageReguest: usage.prompt_tokens,
usageResponse: usage.completion_tokens
}
}).catch((e => {
console.error("Failed to push to a database");
console.error(e);
}));
}
const answerContent = answer.data.choices[0].message?.content;