OpenAI integration and Note generation from prompt
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
9
lib/services/openai.client.ts
Normal file
9
lib/services/openai.client.ts
Normal file
@@ -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);
|
||||
Reference in New Issue
Block a user