Spaces:
Running
Running
Update app/actions/rewrite-prompt.ts
Browse files- app/actions/rewrite-prompt.ts +25 -26
app/actions/rewrite-prompt.ts
CHANGED
|
@@ -3,33 +3,35 @@ import { GoogleGenerativeAI } from "@google/generative-ai";
|
|
| 3 |
const START_REWRITE_PROMPT = ">>>>>>> START PROMPT >>>>>>";
|
| 4 |
const END_REWRITE_PROMPT = ">>>>>>> END PROMPT >>>>>>";
|
| 5 |
|
| 6 |
-
export const callAiRewritePrompt = async (
|
|
|
|
|
|
|
|
|
|
| 7 |
try {
|
| 8 |
-
//
|
| 9 |
-
const geminiApiKey = process.env.GEMINI_API_KEY;
|
|
|
|
| 10 |
if (!geminiApiKey) {
|
| 11 |
-
console.warn("Gemini API key not found
|
| 12 |
return prompt;
|
| 13 |
}
|
| 14 |
|
| 15 |
-
// Initialize Google Gemini AI
|
| 16 |
const genAI = new GoogleGenerativeAI(geminiApiKey);
|
| 17 |
const model = genAI.getGenerativeModel({ model: "gemini-2.5-flash" });
|
| 18 |
|
| 19 |
-
|
| 20 |
-
const systemMessage = `You are a helpful assistant that rewrites prompts to make them better. All the prompts will be about creating a website or app.
|
| 21 |
Try to make the prompt more detailed and specific to create a good UI/UX Design and good code.
|
| 22 |
Format the result by following this format:
|
| 23 |
${START_REWRITE_PROMPT}
|
| 24 |
new prompt here
|
| 25 |
${END_REWRITE_PROMPT}
|
| 26 |
If you don't rewrite the prompt, return the original prompt.
|
| 27 |
-
Make sure to return the prompt in the same language as the prompt you are given. Also IMPORTANT: Make sure to keep the original intent of the prompt. Improve it
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
|
| 31 |
-
|
| 32 |
-
const result = await model.generateContent(systemMessage);
|
| 33 |
const response = result.response;
|
| 34 |
const responseContent = response.text();
|
| 35 |
|
|
@@ -37,26 +39,23 @@ User prompt to rewrite: ${prompt}`;
|
|
| 37 |
return prompt;
|
| 38 |
}
|
| 39 |
|
| 40 |
-
// Extract the rewritten prompt between markers
|
| 41 |
const startIndex = responseContent.indexOf(START_REWRITE_PROMPT);
|
| 42 |
const endIndex = responseContent.indexOf(END_REWRITE_PROMPT);
|
| 43 |
-
|
| 44 |
-
if (startIndex === -1 || endIndex === -1) {
|
| 45 |
-
// If markers not found, return original prompt
|
| 46 |
-
return prompt;
|
| 47 |
-
}
|
| 48 |
-
|
| 49 |
-
const rewrittenPrompt = responseContent.substring(
|
| 50 |
-
startIndex + START_REWRITE_PROMPT.length,
|
| 51 |
-
endIndex
|
| 52 |
-
).trim();
|
| 53 |
-
|
| 54 |
-
// Return rewritten prompt or fallback to original
|
| 55 |
-
return rewrittenPrompt || prompt;
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
} catch (error) {
|
| 58 |
console.error("Error rewriting prompt with Gemini:", error);
|
| 59 |
-
//
|
| 60 |
return prompt;
|
| 61 |
}
|
| 62 |
};
|
|
|
|
| 3 |
const START_REWRITE_PROMPT = ">>>>>>> START PROMPT >>>>>>";
|
| 4 |
const END_REWRITE_PROMPT = ">>>>>>> END PROMPT >>>>>>";
|
| 5 |
|
| 6 |
+
export const callAiRewritePrompt = async (
|
| 7 |
+
prompt: string,
|
| 8 |
+
{ token, billTo }: { token: string; billTo?: string | null }
|
| 9 |
+
) => {
|
| 10 |
try {
|
| 11 |
+
// Use the provided token as the API key, or fall back to environment variable
|
| 12 |
+
const geminiApiKey = token || process.env.GEMINI_API_KEY;
|
| 13 |
+
|
| 14 |
if (!geminiApiKey) {
|
| 15 |
+
console.warn("Gemini API key not found. Returning original prompt.");
|
| 16 |
return prompt;
|
| 17 |
}
|
| 18 |
|
|
|
|
| 19 |
const genAI = new GoogleGenerativeAI(geminiApiKey);
|
| 20 |
const model = genAI.getGenerativeModel({ model: "gemini-2.5-flash" });
|
| 21 |
|
| 22 |
+
const systemPrompt = `You are a helpful assistant that rewrites prompts to make them better. All the prompts will be about creating a website or app.
|
|
|
|
| 23 |
Try to make the prompt more detailed and specific to create a good UI/UX Design and good code.
|
| 24 |
Format the result by following this format:
|
| 25 |
${START_REWRITE_PROMPT}
|
| 26 |
new prompt here
|
| 27 |
${END_REWRITE_PROMPT}
|
| 28 |
If you don't rewrite the prompt, return the original prompt.
|
| 29 |
+
Make sure to return the prompt in the same language as the prompt you are given. Also IMPORTANT: Make sure to keep the original intent of the prompt. Improve it if needed, but don't change the original intent.
|
| 30 |
+
`;
|
| 31 |
|
| 32 |
+
const fullPrompt = `${systemPrompt}\n\nUser prompt to rewrite: ${prompt}`;
|
| 33 |
|
| 34 |
+
const result = await model.generateContent(fullPrompt);
|
|
|
|
| 35 |
const response = result.response;
|
| 36 |
const responseContent = response.text();
|
| 37 |
|
|
|
|
| 39 |
return prompt;
|
| 40 |
}
|
| 41 |
|
|
|
|
| 42 |
const startIndex = responseContent.indexOf(START_REWRITE_PROMPT);
|
| 43 |
const endIndex = responseContent.indexOf(END_REWRITE_PROMPT);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
if (startIndex !== -1 && endIndex !== -1) {
|
| 46 |
+
const rewrittenPrompt = responseContent
|
| 47 |
+
.substring(startIndex + START_REWRITE_PROMPT.length, endIndex)
|
| 48 |
+
.trim();
|
| 49 |
+
|
| 50 |
+
return rewrittenPrompt || prompt;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
// If markers are not found, return the original prompt as a fallback
|
| 54 |
+
return prompt;
|
| 55 |
+
|
| 56 |
} catch (error) {
|
| 57 |
console.error("Error rewriting prompt with Gemini:", error);
|
| 58 |
+
// In case of any API error, return the original prompt
|
| 59 |
return prompt;
|
| 60 |
}
|
| 61 |
};
|