Skip to content

Commit

Permalink
fix(functions): catch and show daily quota exceeded error
Browse files Browse the repository at this point in the history
```
✘ [ERROR]   Error: 3036: you have used up your daily free allocation of
10,000 neurons, please upgrade to Cloudflare's Workers Paid plan if you
would like to continue usage
```
  • Loading branch information
remarkablemark committed Nov 15, 2024
1 parent 99ddc5b commit bf0d53f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
39 changes: 27 additions & 12 deletions functions/api/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,37 @@ const MAX_TOKENS = 100;
* @returns - Response.
*/
export const onRequestPost: PagesFunction<Env> = async (context) => {
const { messages } = await context.request.json<{
messages: RoleScopedChatInput[];
}>();
try {
const { messages } = await context.request.json<{
messages: RoleScopedChatInput[];
}>();

messages.unshift(PROMPT);
messages.unshift(PROMPT);

const workersai = createWorkersAI({ binding: context.env.AI });
const workersai = createWorkersAI({ binding: context.env.AI });

// https://sdk.vercel.ai/providers/community-providers/cloudflare-workers-ai#generatetext
const result = await generateText({
model: workersai('@cf/meta/llama-3.1-8b-instruct'),
maxTokens: MAX_TOKENS,
messages,
});
// https://sdk.vercel.ai/providers/community-providers/cloudflare-workers-ai#generatetext
const result = await generateText({
model: workersai('@cf/meta/llama-3.1-8b-instruct'),
maxTokens: MAX_TOKENS,
messages,
});

return new Response(result.text, getResponseInit(context));
return new Response(result.text, getResponseInit(context));
} catch (error) {
// InferenceUpstreamError: you have used up your daily free allocation of 10,000 neurons, please upgrade to Cloudflare's Workers Paid plan if you would like to continue usage.
if (
error instanceof Error &&
/you have used up your daily free allocation/.test(error.message)
) {
return new Response('Daily quota exceeded.', {
...getResponseInit(context),
status: 429,
});
} else {
throw error;
}
}
};

/**
Expand Down
8 changes: 5 additions & 3 deletions src/components/ChatError.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ interface Props {
}

export default function ChatError(props: Props) {
if (!props.error) {
const { error, reload } = props;

if (!error) {
return null;
}

Expand All @@ -13,12 +15,12 @@ export default function ChatError(props: Props) {
class="mb-4 flex items-center justify-between rounded border border-red-200 bg-red-100 px-4 py-2 text-red-700"
role="alert"
>
<span>An error occurred.</span>
<span>{error.message || 'An error occurred.'}</span>

<button
class="w-16 rounded-md bg-white px-4 py-2 text-sm font-medium text-black shadow hover:bg-gray-50 focus-visible:outline-none focus-visible:ring-1"
type="button"
onClick={props.reload}
onClick={reload}
>
Retry
</button>
Expand Down

0 comments on commit bf0d53f

Please sign in to comment.