enzostvs HF Staff commited on
Commit
530fd3f
·
1 Parent(s): b709981

use stream to avoid timeout

Browse files
app/api/ask/route.ts CHANGED
@@ -331,7 +331,7 @@ export async function PUT(request: NextRequest) {
331
  const dynamicMaxTokens = calculateMaxTokens(selectedProvider, estimatedInputTokens, false);
332
  const providerConfig = getProviderSpecificConfig(selectedProvider, dynamicMaxTokens);
333
 
334
- const response = await client.chatCompletion(
335
  {
336
  model: selectedModel.value,
337
  provider: selectedProvider.provider,
@@ -358,7 +358,18 @@ export async function PUT(request: NextRequest) {
358
  billTo ? { billTo } : {}
359
  );
360
 
361
- const chunk = response.choices[0]?.message?.content;
 
 
 
 
 
 
 
 
 
 
 
362
  if (!chunk) {
363
  return NextResponse.json(
364
  { ok: false, message: "No content returned from the model" },
 
331
  const dynamicMaxTokens = calculateMaxTokens(selectedProvider, estimatedInputTokens, false);
332
  const providerConfig = getProviderSpecificConfig(selectedProvider, dynamicMaxTokens);
333
 
334
+ const chatCompletion = client.chatCompletionStream(
335
  {
336
  model: selectedModel.value,
337
  provider: selectedProvider.provider,
 
358
  billTo ? { billTo } : {}
359
  );
360
 
361
+ let chunk = "";
362
+ while (true) {
363
+ const { done, value } = await chatCompletion.next();
364
+ if (done) {
365
+ break;
366
+ }
367
+
368
+ const deltaContent = value.choices[0]?.delta?.content;
369
+ if (deltaContent) {
370
+ chunk += deltaContent;
371
+ }
372
+ }
373
  if (!chunk) {
374
  return NextResponse.json(
375
  { ok: false, message: "No content returned from the model" },
components/editor/ask-ai/settings.tsx CHANGED
@@ -198,12 +198,12 @@ export function Settings({
198
  </SelectContent>
199
  </Select>
200
  </label>
201
- {isFollowUp && (
202
  <div className="bg-amber-500/10 border-amber-500/10 p-3 text-xs text-amber-500 border rounded-lg">
203
  Note: You can&apos;t use a Thinker model for follow-up requests.
204
  We automatically switch to the default model for you.
205
  </div>
206
- )}
207
  <div className="flex flex-col gap-3">
208
  <div className="flex items-center justify-between">
209
  <div>
 
198
  </SelectContent>
199
  </Select>
200
  </label>
201
+ {/* {isFollowUp && (
202
  <div className="bg-amber-500/10 border-amber-500/10 p-3 text-xs text-amber-500 border rounded-lg">
203
  Note: You can&apos;t use a Thinker model for follow-up requests.
204
  We automatically switch to the default model for you.
205
  </div>
206
+ )} */}
207
  <div className="flex flex-col gap-3">
208
  <div className="flex items-center justify-between">
209
  <div>
lib/best-provider.ts CHANGED
@@ -12,7 +12,7 @@ export const getBestProvider = async (model: string, provider?: string) => {
12
  } else {
13
  const providerData = data.providers.find((p: any) => p.provider === provider)
14
  if (providerData?.status === "live") {
15
- bestProvider = providerData.provider
16
  } else {
17
  bestProvider = data.providers?.find((p: any) => p.status === "live")
18
  }
 
12
  } else {
13
  const providerData = data.providers.find((p: any) => p.provider === provider)
14
  if (providerData?.status === "live") {
15
+ bestProvider = providerData
16
  } else {
17
  bestProvider = data.providers?.find((p: any) => p.status === "live")
18
  }
lib/prompts.ts CHANGED
@@ -26,6 +26,7 @@ Try to create the best UI possible. Important: Make the website responsive by us
26
  Also try to elaborate as much as you can, to create something unique, with a great design.
27
  If you want to use ICONS import Feather Icons (Make sure to add <script src="https://unpkg.com/feather-icons"></script> and <script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script> in the head., and <script>feather.replace();</script> in the body. Ex : <i data-feather="user"></i>).
28
  For interactive animations you can use: Vanta.js (Make sure to add <script src="https://cdn.jsdelivr.net/npm/vanta@latest/dist/vanta.globe.min.js"></script> and <script>VANTA.GLOBE({...</script> in the body.).
 
29
  You can create multiple pages website at once (following the format rules below) or a Single Page Application. But make sure to create multiple pages if the user asks for different pages.
30
  ${PROMPT_FOR_IMAGE_GENERATION}
31
  ${PROMPT_FOR_PROJECT_NAME}
@@ -70,6 +71,7 @@ IMPORTANT: The first file should be always named index.html.`
70
  export const FOLLOW_UP_SYSTEM_PROMPT = `You are an expert UI/UX and Front-End Developer modifying an existing HTML files.
71
  The user wants to apply changes and probably add new features/pages to the website, based on their request.
72
  You MUST output ONLY the changes required using the following UPDATE_PAGE_START and SEARCH/REPLACE format. Do NOT output the entire file.
 
73
  If it's a new page, you MUST applied the following NEW_PAGE_START and UPDATE_PAGE_END format.
74
  ${PROMPT_FOR_IMAGE_GENERATION}
75
  Do NOT explain the changes or what you did, just return the expected results.
 
26
  Also try to elaborate as much as you can, to create something unique, with a great design.
27
  If you want to use ICONS import Feather Icons (Make sure to add <script src="https://unpkg.com/feather-icons"></script> and <script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script> in the head., and <script>feather.replace();</script> in the body. Ex : <i data-feather="user"></i>).
28
  For interactive animations you can use: Vanta.js (Make sure to add <script src="https://cdn.jsdelivr.net/npm/vanta@latest/dist/vanta.globe.min.js"></script> and <script>VANTA.GLOBE({...</script> in the body.).
29
+ Don't hesitate to use real public API for the datas, you can find good ones here https://github.com/public-apis/public-apis depending on what the user asks for.
30
  You can create multiple pages website at once (following the format rules below) or a Single Page Application. But make sure to create multiple pages if the user asks for different pages.
31
  ${PROMPT_FOR_IMAGE_GENERATION}
32
  ${PROMPT_FOR_PROJECT_NAME}
 
71
  export const FOLLOW_UP_SYSTEM_PROMPT = `You are an expert UI/UX and Front-End Developer modifying an existing HTML files.
72
  The user wants to apply changes and probably add new features/pages to the website, based on their request.
73
  You MUST output ONLY the changes required using the following UPDATE_PAGE_START and SEARCH/REPLACE format. Do NOT output the entire file.
74
+ Don't hesitate to use real public API for the datas, you can find good ones here https://github.com/public-apis/public-apis depending on what the user asks for.
75
  If it's a new page, you MUST applied the following NEW_PAGE_START and UPDATE_PAGE_END format.
76
  ${PROMPT_FOR_IMAGE_GENERATION}
77
  Do NOT explain the changes or what you did, just return the expected results.