Skip to content

Commit 5c10f77

Browse files
committed
openAI to GenerativeAi fix
1 parent 39164c1 commit 5c10f77

File tree

5 files changed

+91
-37
lines changed

5 files changed

+91
-37
lines changed

actions/upload-actions.ts

+23-34
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ import getDbConnection from "@/lib/db";
33
import { revalidatePath } from "next/cache";
44
import { redirect } from "next/navigation";
55
import OpenAI from "openai";
6+
import { AssemblyAI } from 'assemblyai'
7+
import { GoogleGenerativeAI } from "@google/generative-ai"
68

7-
const openai = new OpenAI({
8-
apiKey: process.env.OPENAI_API_KEY,
9-
});
9+
const client = new AssemblyAI({
10+
apiKey: process.env.ASSEMBLY_API_KEY!
11+
})
12+
13+
// const openai = new OpenAI({
14+
// apiKey: process.env.OPENAI_API_KEY,
15+
// });
1016

1117
export async function transcribeUploadedFile(
1218
resp: {
@@ -36,19 +42,16 @@ export async function transcribeUploadedFile(
3642
};
3743
}
3844

39-
const response = await fetch(fileUrl);
45+
const config = {
46+
audio_url: fileUrl
47+
}
4048

4149
try {
42-
const transcriptions = await openai.audio.transcriptions.create({
43-
model: "whisper-1",
44-
file: response,
45-
});
46-
47-
console.log({ transcriptions });
50+
const transcriptions = await client.transcripts.transcribe(config)
4851
return {
4952
success: true,
5053
message: "File uploaded successfully!",
51-
data: { transcriptions, userId },
54+
data: { transcriptions:transcriptions.text, userId },
5255
};
5356
} catch (error) {
5457
console.error("Error processing file", error);
@@ -107,20 +110,10 @@ async function generateBlogPost({
107110
transcriptions: string;
108111
userPosts: string;
109112
}) {
110-
const completion = await openai.chat.completions.create({
111-
messages: [
112-
{
113-
role: "system",
114-
content:
115-
"You are a skilled content writer that converts audio transcriptions into well-structured, engaging blog posts in Markdown format. Create a comprehensive blog post with a catchy title, introduction, main body with multiple sections, and a conclusion. Analyze the user's writing style from their previous posts and emulate their tone and style in the new post. Keep the tone casual and professional.",
116-
},
117-
{
118-
role: "user",
119-
content: `Here are some of my previous blog posts for reference:
120-
121-
${userPosts}
122-
123-
Please convert the following transcription into a well-structured blog post using Markdown formatting. Follow this structure:
113+
114+
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY!);
115+
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
116+
const prompt = `Please convert the following transcription :- ${transcriptions} into a well-structured blog post using Markdown formatting. Follow this structure:
124117
125118
1. Start with a SEO friendly catchy title on the first line.
126119
2. Add two newlines after the title.
@@ -132,16 +125,12 @@ Please convert the following transcription into a well-structured blog post usin
132125
8. Ensure the content is informative, well-organized, and easy to read.
133126
9. Emulate my writing style, tone, and any recurring patterns you notice from my previous posts.
134127
135-
Here's the transcription to convert: ${transcriptions}`,
136-
},
137-
],
138-
model: "gpt-4o-mini",
139-
temperature: 0.7,
140-
max_tokens: 1000,
141-
});
142-
143-
return completion.choices[0].message.content;
128+
You can also look out for similiar posts :- ${userPosts}`;
129+
130+
const result = await model.generateContent(prompt);
131+
return result.response.text()
144132
}
133+
145134
export async function generateBlogPostAction({
146135
transcriptions,
147136
userId,

app/api/testconnection/route.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// app/api/testconnection/route.ts
2+
import { NextResponse } from "next/server";
3+
import getDbConnection from "../../../lib/db";
4+
5+
export async function GET() {
6+
try {
7+
await getDbConnection();
8+
return NextResponse.json({ message: "Database connection established successfully!" });
9+
} catch (error:any) {
10+
console.error("Error in database connection:", error);
11+
return NextResponse.json(
12+
{ message: "Database connection failed!", error: error.message },
13+
{ status: 500 }
14+
);
15+
}
16+
}

lib/db.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ export default async function getDbConnection() {
44
if (!process.env.DATABASE_URL) {
55
throw new Error("Neon Database URL is not defined");
66
}
7-
const sql = neon(process.env.DATABASE_URL);
8-
return sql;
9-
}
7+
try {
8+
const sql = neon(process.env.DATABASE_URL);
9+
console.log("Database connection established");
10+
return sql;
11+
} catch (error) {
12+
console.error("Database connection error:", error);
13+
throw error;
14+
}
15+
}

package-lock.json

+41
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
},
1111
"dependencies": {
1212
"@clerk/nextjs": "^5.7.2",
13+
"@google/generative-ai": "^0.21.0",
1314
"@neondatabase/serverless": "^0.10.1",
1415
"@radix-ui/react-slot": "^1.1.0",
1516
"@radix-ui/react-toast": "^1.2.2",
1617
"@uploadthing/react": "^7.0.3",
18+
"assemblyai": "^4.7.1",
1719
"class-variance-authority": "^0.7.0",
1820
"clsx": "^2.1.1",
1921
"lucide-react": "^0.452.0",

0 commit comments

Comments
 (0)