From f88cf845341c37c7958751640d9b99ac345bdf3f Mon Sep 17 00:00:00 2001 From: Lucieo Date: Thu, 26 Oct 2023 17:56:37 +0200 Subject: [PATCH] Better error handling in openAi + limit to prompt length (#61) * Better error handling in openAi + limit to prompt length * Fix missing space in join --- .../teams/form/settings/SettingsField.tsx | 2 ++ .../teams/form/settings/form-data.tsx | 2 ++ .../api/teams/[teamId]/bookmark/summary.tsx | 21 ++++++++++++++----- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/components/teams/form/settings/SettingsField.tsx b/src/components/teams/form/settings/SettingsField.tsx index f1d3c62..ba9c4a5 100644 --- a/src/components/teams/form/settings/SettingsField.tsx +++ b/src/components/teams/form/settings/SettingsField.tsx @@ -15,6 +15,7 @@ export default function SettingsField({ rightElement, selectDefault, selectOptions, + maxLength, }: FieldData) { const { register, @@ -72,6 +73,7 @@ export default function SettingsField({ className="sm:max-w-md" defaultValue={defaultValue || ''} placeholder={placeholder} + maxLength={maxLength} {...register(id, { ...(!!registerOptions && registerOptions), })} diff --git a/src/components/teams/form/settings/form-data.tsx b/src/components/teams/form/settings/form-data.tsx index a961f8c..598c42a 100644 --- a/src/components/teams/form/settings/form-data.tsx +++ b/src/components/teams/form/settings/form-data.tsx @@ -29,6 +29,7 @@ export interface FieldData { prefix?: string; selectDefault?: string; selectOptions?: SelectOptionProps[]; + maxLength?: number; } export const fieldsData: FieldData[] = [ @@ -88,5 +89,6 @@ export const fieldsData: FieldData[] = [ label: 'Summary generator prompt', placeholder: 'Add a custom prompt for bookmark summary generation. Your prompt will be followed by : + article content', + maxLength: 4000, }, ]; diff --git a/src/pages/api/teams/[teamId]/bookmark/summary.tsx b/src/pages/api/teams/[teamId]/bookmark/summary.tsx index 4e2c9e1..46c4ef4 100644 --- a/src/pages/api/teams/[teamId]/bookmark/summary.tsx +++ b/src/pages/api/teams/[teamId]/bookmark/summary.tsx @@ -5,9 +5,15 @@ import type { NextApiResponse } from 'next'; import { createRouter } from 'next-connect'; import Parser from '@postlight/parser'; import { getTeamById } from '@/lib/queries'; -const MAX_PROMPT_LENGTH = 4097; + export const router = createRouter(); +const OPENAI_ERRORS: { [key: string]: string } = { + rate_limit_exceeded: + 'Generation service is overloaded, Please try again later', + context_length_exceeded: 'Cannot generate summary, your prompt is too long !', +}; + router .use(checkTeam) .use(checkProAccount) @@ -22,19 +28,24 @@ router try { const team = await getTeamById(req.query.teamId as string); const linkInfo = await Parser.parse(url, { contentType: 'text' }); - const articleContent = linkInfo.content.substring(0, MAX_PROMPT_LENGTH); + // (so 130 tokens ~= 100 words). 8K => 6150 words for gpt4 including prompt + answer + const articleContent = linkInfo.content; const prompt = team?.prompt ? `${team?.prompt} : "${articleContent}"` - : `Write a summary of 2 to 3 sentences, using between 40 to 70 words maximum, with a journalistic tone for the following article web content ${articleContent}. Only send back the summary text, do not add quotes or any intro sentence. Make liaisons between sentences. Do not use ; characters.`; + : `Write a summary of 2 to 3 sentences, using between 40 to 70 words maximum, with a journalistic tone for the following article web content. Only send back the summary text, do not add quotes or any intro sentence. Make liaisons between sentences. Do not use ; characters. Here is the content : ${articleContent}. ` + .split(' ') + .slice(0, 5500) + .join(' '); const response = await openAiCompletion({ prompt, model: 'gpt-4' }); const summary = response[0]?.message?.content; return res.status(201).json(summary); - } catch (e) { + } catch (e: any) { + const errorCode = e?.error.code as string; return res.status(400).json({ - error: 'Something went wrong', + error: OPENAI_ERRORS[errorCode] || 'Something went wrong', }); } });