Spaces:
Running
Running
rewrite first prompt
Browse files- app/actions/rewrite-prompt.ts +35 -0
- app/api/ask-ai/route.ts +9 -1
app/actions/rewrite-prompt.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { InferenceClient } from "@huggingface/inference";
|
| 2 |
+
|
| 3 |
+
const START_REWRITE_PROMPT = ">>>>>>> START PROMPT >>>>>>";
|
| 4 |
+
const END_REWRITE_PROMPT = ">>>>>>> END PROMPT >>>>>>";
|
| 5 |
+
|
| 6 |
+
export const callAiRewritePrompt = async (prompt: string, { token, billTo }: { token: string, billTo?: string | null }) => {
|
| 7 |
+
const client = new InferenceClient(token);
|
| 8 |
+
const response = await client.chatCompletion(
|
| 9 |
+
{
|
| 10 |
+
model: "deepseek-ai/DeepSeek-V3.1",
|
| 11 |
+
provider: "novita",
|
| 12 |
+
messages: [{
|
| 13 |
+
role: "system",
|
| 14 |
+
content: `You are a helpful assistant that rewrites prompts to make them better. All the prompts will be about creating a website or app.
|
| 15 |
+
Try to make the prompt more detailed and specific to create a good UI/UX Design and good code.
|
| 16 |
+
Format the result by following this format:
|
| 17 |
+
${START_REWRITE_PROMPT}
|
| 18 |
+
new prompt here
|
| 19 |
+
${END_REWRITE_PROMPT}
|
| 20 |
+
If you don't rewrite the prompt, return the original prompt.
|
| 21 |
+
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 it needed, but don't change the original intent.
|
| 22 |
+
`
|
| 23 |
+
},{ role: "user", content: prompt }],
|
| 24 |
+
},
|
| 25 |
+
billTo ? { billTo } : {}
|
| 26 |
+
);
|
| 27 |
+
|
| 28 |
+
const responseContent = response.choices[0]?.message?.content;
|
| 29 |
+
if (!responseContent) {
|
| 30 |
+
return prompt;
|
| 31 |
+
}
|
| 32 |
+
const startIndex = responseContent.indexOf(START_REWRITE_PROMPT);
|
| 33 |
+
const endIndex = responseContent.indexOf(END_REWRITE_PROMPT);
|
| 34 |
+
return responseContent.substring(startIndex + START_REWRITE_PROMPT.length, endIndex);
|
| 35 |
+
};
|
app/api/ask-ai/route.ts
CHANGED
|
@@ -19,6 +19,7 @@ import {
|
|
| 19 |
} from "@/lib/prompts";
|
| 20 |
import MY_TOKEN_KEY from "@/lib/get-cookie-name";
|
| 21 |
import { Page } from "@/types";
|
|
|
|
| 22 |
|
| 23 |
const ipAddresses = new Map();
|
| 24 |
|
|
@@ -97,6 +98,13 @@ export async function POST(request: NextRequest) {
|
|
| 97 |
? PROVIDERS[selectedModel.autoProvider as keyof typeof PROVIDERS]
|
| 98 |
: PROVIDERS[provider as keyof typeof PROVIDERS] ?? DEFAULT_PROVIDER;
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
try {
|
| 101 |
const encoder = new TextEncoder();
|
| 102 |
const stream = new TransformStream();
|
|
@@ -131,7 +139,7 @@ export async function POST(request: NextRequest) {
|
|
| 131 |
role: "user",
|
| 132 |
content: redesignMarkdown
|
| 133 |
? `Here is my current design as a markdown:\n\n${redesignMarkdown}\n\nNow, please create a new design based on this markdown.`
|
| 134 |
-
:
|
| 135 |
},
|
| 136 |
],
|
| 137 |
max_tokens: selectedProvider.max_tokens,
|
|
|
|
| 19 |
} from "@/lib/prompts";
|
| 20 |
import MY_TOKEN_KEY from "@/lib/get-cookie-name";
|
| 21 |
import { Page } from "@/types";
|
| 22 |
+
import { callAiRewritePrompt } from "@/app/actions/rewrite-prompt";
|
| 23 |
|
| 24 |
const ipAddresses = new Map();
|
| 25 |
|
|
|
|
| 98 |
? PROVIDERS[selectedModel.autoProvider as keyof typeof PROVIDERS]
|
| 99 |
: PROVIDERS[provider as keyof typeof PROVIDERS] ?? DEFAULT_PROVIDER;
|
| 100 |
|
| 101 |
+
let rewrittenPrompt = prompt;
|
| 102 |
+
|
| 103 |
+
if (prompt?.length < 240) {
|
| 104 |
+
|
| 105 |
+
rewrittenPrompt = await callAiRewritePrompt(prompt, { token, billTo });
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
try {
|
| 109 |
const encoder = new TextEncoder();
|
| 110 |
const stream = new TransformStream();
|
|
|
|
| 139 |
role: "user",
|
| 140 |
content: redesignMarkdown
|
| 141 |
? `Here is my current design as a markdown:\n\n${redesignMarkdown}\n\nNow, please create a new design based on this markdown.`
|
| 142 |
+
: rewrittenPrompt,
|
| 143 |
},
|
| 144 |
],
|
| 145 |
max_tokens: selectedProvider.max_tokens,
|