Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 36 additions & 20 deletions app/api/generateCode/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import dedent from "dedent";
import { z } from "zod";
import { GoogleGenerativeAI } from "@google/generative-ai";

const apiKey = process.env.GOOGLE_AI_API_KEY || "";
const genAI = new GoogleGenerativeAI(apiKey);

export async function POST(req: Request) {
const apiKey = process.env.GOOGLE_AI_API_KEY;

if (!apiKey) {
return new Response("Missing Google AI API key.", { status: 500 });
}

const genAI = new GoogleGenerativeAI(apiKey);
let json = await req.json();
let result = z
.object({
Expand All @@ -28,30 +32,42 @@ export async function POST(req: Request) {
let { model, messages, shadcn } = result.data;
let systemPrompt = getSystemPrompt(shadcn);

const geminiModel = genAI.getGenerativeModel({model: model});
if (messages.length === 0) {
return new Response("No messages provided.", { status: 400 });
}

const geminiStream = await geminiModel.generateContentStream(
messages[0].content + systemPrompt + "\nPlease ONLY return code, NO backticks or language names. Don't start with \`\`\`typescript or \`\`\`javascript or \`\`\`tsx or \`\`\`."
);
const geminiModel = genAI.getGenerativeModel({ model: model });

console.log(messages[0].content + systemPrompt + "\nPlease ONLY return code, NO backticks or language names. Don't start with \`\`\`typescript or \`\`\`javascript or \`\`\`tsx or \`\`\`.")
try {
const fullPrompt = constructPrompt(messages[0].content, systemPrompt);
const geminiStream = await geminiModel.generateContentStream(fullPrompt);

const readableStream = new ReadableStream({
async start(controller) {
for await (const chunk of geminiStream.stream) {
const chunkText = chunk.text();
controller.enqueue(new TextEncoder().encode(chunkText));
}
controller.close();
},
});
console.log(fullPrompt)

const readableStream = new ReadableStream({
async start(controller) {
for await (const chunk of geminiStream.stream) {
const chunkText = chunk.text();
controller.enqueue(new TextEncoder().encode(chunkText));
}
controller.close();
},
});

return new Response(readableStream);
} catch (e) {
console.error(e);
return new Response("Error generating content.", { status: 500 });
}
}

return new Response(readableStream);
function constructPrompt(userPrompt: string, systemPrompt: string): string {
return `${userPrompt}\n${systemPrompt}\nPlease ONLY return code, NO backticks or language names. Don't start with \`\`\`typescript or \`\`\`javascript or \`\`\`tsx or \`\`\`.`;
}

function getSystemPrompt(shadcn: boolean) {
let systemPrompt =
`You are an expert frontend React engineer who is also a great UI/UX designer. Follow the instructions carefully, I will tip you $1 million if you do a good job:
let systemPrompt =
`You are an expert frontend React engineer who is also a great UI/UX designer. Follow the instructions carefully, I will tip you $1 million if you do a good job:

- Think carefully step by step.
- Create a React component for whatever the user asked you to create and make sure it can run by itself by using a default export
Expand Down
Loading