From 3e3c5e57d736b9c9fa526d390bb1655791cc988a Mon Sep 17 00:00:00 2001 From: Michael Dausmann Date: Sat, 29 Apr 2023 00:00:52 +1000 Subject: [PATCH] OpenAI integration and Note generation from prompt --- .env_example | 4 +- README.md | 2 + lib/services/notes.service.ts | 17 +++++ lib/services/openai.client.ts | 9 +++ nuxt.config.ts | 1 + package-lock.json | 110 ++++++++++++++++++++++++++-- package.json | 1 + pages/dashboard.vue | 18 ++++- pages/index.vue | 7 +- server/trpc/routers/notes.router.ts | 9 +++ stores/notes.store.ts | 12 +-- 11 files changed, 171 insertions(+), 19 deletions(-) create mode 100644 lib/services/openai.client.ts diff --git a/.env_example b/.env_example index 6dd1777..353ce33 100644 --- a/.env_example +++ b/.env_example @@ -12,4 +12,6 @@ STRIPE_CALLBACK_URL=http://localhost:3000 # Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB. # See the documentation for all the connection string options: https://pris.ly/d/connection-strings -DATABASE_URL="postgresql://postgres:xxxxxxxxxxxxx@db.xxxxxxxxxxxxx.supabase.co:5432/postgres" \ No newline at end of file +DATABASE_URL="postgresql://postgres:xxxxxxxxxxxxx@db.xxxxxxxxxxxxx.supabase.co:5432/postgres" + +OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx \ No newline at end of file diff --git a/README.md b/README.md index 1ce31fd..8a0a0eb 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ Please don't hitch your wagon to this star just yet... I'm coding this in the op - Pinia (State Store. I liked vuex a lot, in particular explicit mutations but gotta go with the cool crowd) - Stripe (Payments including Webhook integration) - Tailwind + daisyUI (Tailwind because everybody is using it, daisyUI because I suck at tailwind) +- OpenAI (text completions with AI) ## Features ### User Management @@ -71,6 +72,7 @@ Please don't hitch your wagon to this star just yet... I'm coding this in the op - [x] Max Notes limit property on Plan - [x] Max Notes enforced - [x] Add, Delete notes on Dashboard +- [x] AI Note generation with OpenAI ### Testing - [x] Manual test scenario for auth and sub workflows passing diff --git a/lib/services/notes.service.ts b/lib/services/notes.service.ts index e63ff90..f572da2 100644 --- a/lib/services/notes.service.ts +++ b/lib/services/notes.service.ts @@ -1,4 +1,5 @@ import prisma_client from '~~/prisma/prisma.client'; +import { openai } from './openai.client'; export default class NotesService { async getAllNotes() { @@ -33,4 +34,20 @@ export default class NotesService { async deleteNote(id: number) { return prisma_client.note.delete({ where: { id } }); } + + async generateAINoteFromPrompt(userPrompt: string) { + const prompt = ` + Write an interesting short note about ${userPrompt}. + Restrict the note to a single paragraph. + ` + const completion = await openai.createCompletion({ + model: "text-davinci-003", + prompt, + temperature: 0.6, + stop: "\n\n", + max_tokens: 1000, + n: 1, + }); + return completion.data.choices[0].text; + } } diff --git a/lib/services/openai.client.ts b/lib/services/openai.client.ts new file mode 100644 index 0000000..8a5e085 --- /dev/null +++ b/lib/services/openai.client.ts @@ -0,0 +1,9 @@ +import { Configuration, OpenAIApi } from "openai"; + +const config = useRuntimeConfig(); + +const configuration = new Configuration({ + apiKey: config.openAIKey, +}); + +export const openai = new OpenAIApi(configuration); \ No newline at end of file diff --git a/nuxt.config.ts b/nuxt.config.ts index 8e31d33..81c46d2 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -18,6 +18,7 @@ export default defineNuxtConfig({ subscriptionGraceDays: 3, initialPlanName: 'Free Trial', initialPlanActiveMonths: 1, + openAIKey: process.env.OPENAI_API_KEY, public: { debugMode: true, siteRootUrl: 'http://localhost:3000', diff --git a/package-lock.json b/package-lock.json index 7603da7..b54157f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "@trpc/server": "^10.9.0", "daisyui": "^2.51.5", "generate-password-ts": "^1.6.3", + "openai": "^3.2.1", "pinia": "^2.0.30", "stripe": "^11.12.0", "superjson": "^1.12.2", @@ -2914,6 +2915,11 @@ "integrity": "sha512-tLRNUXati5MFePdAk8dw7Qt7DpxPB60ofAgn8WRhW6a2rcimZnYBP9oxHiv0OHy+Wz7kPMG+t4LGdt31+4EmGg==", "dev": true }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, "node_modules/at-least-node": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/at-least-node/-/at-least-node-1.0.0.tgz", @@ -2955,6 +2961,14 @@ "postcss": "^8.1.0" } }, + "node_modules/axios": { + "version": "0.26.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz", + "integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==", + "dependencies": { + "follow-redirects": "^1.14.8" + } + }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -3595,6 +3609,17 @@ "resolved": "https://registry.npmjs.org/colorette/-/colorette-2.0.19.tgz", "integrity": "sha512-3tlv/dIP7FWvj3BsbHrGLJ6l/oKh1O3TcgBqMn+yyCagOxc23fyzDS6HypQbgxWbkpDnf52p1LuR4eWDQ/K9WQ==" }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/commander": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/commander/-/commander-7.2.0.tgz", @@ -4097,6 +4122,14 @@ "resolved": "https://registry.npmjs.org/defu/-/defu-6.1.2.tgz", "integrity": "sha512-+uO4+qr7msjNNWKYPHqN/3+Dx3NFkmIzayk2L1MyZQlvgZb/J1A0fo410dpKrN2SnqFjt8n4JL8fDJE0wIgjFQ==" }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/delegates": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/delegates/-/delegates-1.0.0.tgz", @@ -4720,7 +4753,6 @@ "version": "1.15.2", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz", "integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==", - "dev": true, "funding": [ { "type": "individual", @@ -4736,6 +4768,19 @@ } } }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/formdata-polyfill": { "version": "4.0.10", "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz", @@ -6374,7 +6419,6 @@ "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", - "dev": true, "engines": { "node": ">= 0.6" } @@ -6383,7 +6427,6 @@ "version": "2.1.35", "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", - "dev": true, "dependencies": { "mime-db": "1.52.0" }, @@ -7161,6 +7204,15 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/openai": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/openai/-/openai-3.2.1.tgz", + "integrity": "sha512-762C9BNlJPbjjlWZi4WYK9iM2tAVAv0uUp1UmI34vb0CN5T2mjB/qM6RYBmNKMh/dN9fC+bxqPwWJZUTWW052A==", + "dependencies": { + "axios": "^0.26.0", + "form-data": "^4.0.0" + } + }, "node_modules/ora": { "version": "6.1.2", "resolved": "https://registry.npmjs.org/ora/-/ora-6.1.2.tgz", @@ -13417,6 +13469,11 @@ "integrity": "sha512-tLRNUXati5MFePdAk8dw7Qt7DpxPB60ofAgn8WRhW6a2rcimZnYBP9oxHiv0OHy+Wz7kPMG+t4LGdt31+4EmGg==", "dev": true }, + "asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, "at-least-node": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/at-least-node/-/at-least-node-1.0.0.tgz", @@ -13436,6 +13493,14 @@ "postcss-value-parser": "^4.2.0" } }, + "axios": { + "version": "0.26.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz", + "integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==", + "requires": { + "follow-redirects": "^1.14.8" + } + }, "balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -13874,6 +13939,14 @@ "resolved": "https://registry.npmjs.org/colorette/-/colorette-2.0.19.tgz", "integrity": "sha512-3tlv/dIP7FWvj3BsbHrGLJ6l/oKh1O3TcgBqMn+yyCagOxc23fyzDS6HypQbgxWbkpDnf52p1LuR4eWDQ/K9WQ==" }, + "combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "requires": { + "delayed-stream": "~1.0.0" + } + }, "commander": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/commander/-/commander-7.2.0.tgz", @@ -14245,6 +14318,11 @@ "resolved": "https://registry.npmjs.org/defu/-/defu-6.1.2.tgz", "integrity": "sha512-+uO4+qr7msjNNWKYPHqN/3+Dx3NFkmIzayk2L1MyZQlvgZb/J1A0fo410dpKrN2SnqFjt8n4JL8fDJE0wIgjFQ==" }, + "delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==" + }, "delegates": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/delegates/-/delegates-1.0.0.tgz", @@ -14731,8 +14809,17 @@ "follow-redirects": { "version": "1.15.2", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz", - "integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==", - "dev": true + "integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==" + }, + "form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + } }, "formdata-polyfill": { "version": "4.0.10", @@ -16003,14 +16090,12 @@ "mime-db": { "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", - "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", - "dev": true + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==" }, "mime-types": { "version": "2.1.35", "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", - "dev": true, "requires": { "mime-db": "1.52.0" } @@ -16633,6 +16718,15 @@ } } }, + "openai": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/openai/-/openai-3.2.1.tgz", + "integrity": "sha512-762C9BNlJPbjjlWZi4WYK9iM2tAVAv0uUp1UmI34vb0CN5T2mjB/qM6RYBmNKMh/dN9fC+bxqPwWJZUTWW052A==", + "requires": { + "axios": "^0.26.0", + "form-data": "^4.0.0" + } + }, "ora": { "version": "6.1.2", "resolved": "https://registry.npmjs.org/ora/-/ora-6.1.2.tgz", diff --git a/package.json b/package.json index 6ee5056..5d2064b 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "@trpc/server": "^10.9.0", "daisyui": "^2.51.5", "generate-password-ts": "^1.6.3", + "openai": "^3.2.1", "pinia": "^2.0.30", "stripe": "^11.12.0", "superjson": "^1.12.2", diff --git a/pages/dashboard.vue b/pages/dashboard.vue index 83d025e..2189530 100644 --- a/pages/dashboard.vue +++ b/pages/dashboard.vue @@ -17,6 +17,11 @@ newNoteText.value = ''; } + async function genNote(){ + const genNoteText = await notesStore.generateAINoteFromPrompt(newNoteText.value) + newNoteText.value = genNoteText; + } + onMounted(async () => { await accountStore.init(); await notesStore.fetchNotesForCurrentUser(); @@ -28,13 +33,18 @@

Notes Dashboard

-
-
- +
+