Skip to content

Commit

Permalink
Better error handling in openAi + limit to prompt length (#61)
Browse files Browse the repository at this point in the history
* Better error handling in openAi + limit to prompt length

* Fix missing space in join
  • Loading branch information
Lucieo authored Oct 26, 2023
1 parent 7d6438b commit f88cf84
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/components/teams/form/settings/SettingsField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default function SettingsField({
rightElement,
selectDefault,
selectOptions,
maxLength,
}: FieldData) {
const {
register,
Expand Down Expand Up @@ -72,6 +73,7 @@ export default function SettingsField({
className="sm:max-w-md"
defaultValue={defaultValue || ''}
placeholder={placeholder}
maxLength={maxLength}
{...register(id, {
...(!!registerOptions && registerOptions),
})}
Expand Down
2 changes: 2 additions & 0 deletions src/components/teams/form/settings/form-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface FieldData {
prefix?: string;
selectDefault?: string;
selectOptions?: SelectOptionProps[];
maxLength?: number;
}

export const fieldsData: FieldData[] = [
Expand Down Expand Up @@ -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,
},
];
21 changes: 16 additions & 5 deletions src/pages/api/teams/[teamId]/bookmark/summary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<AuthApiRequest, NextApiResponse>();

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)
Expand All @@ -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',
});
}
});
Expand Down

1 comment on commit f88cf84

@vercel
Copy link

@vercel vercel bot commented on f88cf84 Oct 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

digestclub – ./

digestclub-git-main-premieroctet.vercel.app
digestclub-premieroctet.vercel.app
digestclub.vercel.app

Please sign in to comment.