diff --git a/functions/api/chat.ts b/functions/api/chat.ts index bd326a1..5b60e21 100644 --- a/functions/api/chat.ts +++ b/functions/api/chat.ts @@ -21,22 +21,37 @@ const MAX_TOKENS = 100; * @returns - Response. */ export const onRequestPost: PagesFunction = 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; + } + } }; /** diff --git a/src/components/ChatError.tsx b/src/components/ChatError.tsx index a17018e..2df2c46 100644 --- a/src/components/ChatError.tsx +++ b/src/components/ChatError.tsx @@ -4,7 +4,9 @@ interface Props { } export default function ChatError(props: Props) { - if (!props.error) { + const { error, reload } = props; + + if (!error) { return null; } @@ -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" > - An error occurred. + {error.message || 'An error occurred.'}