Skip to content

Commit

Permalink
feat(cf-worker): retry for 5 times before throwing an exception
Browse files Browse the repository at this point in the history
  • Loading branch information
sdip15fa committed Feb 28, 2024
1 parent 66f439f commit f3e63a1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
12 changes: 12 additions & 0 deletions cf-worker/src/exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ export class BadRequestException {
this.reason = reason;
}
}

export class FailedException {
status: number;
statusText: string;
reason: string;

constructor(reason: string) {
this.status = 500;
this.statusText = 'Internal Server Error';
this.reason = reason;
}
}
14 changes: 12 additions & 2 deletions cf-worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// @ts-ignore
import { Ai } from '@cloudflare/ai';
import { Env } from './types.js';
import { BadRequestException } from './exceptions.js';
import { BadRequestException, FailedException } from './exceptions.js';
import { basicAuthentication, verifyCredentials } from './auth.js';

export default {
Expand Down Expand Up @@ -72,7 +72,17 @@ export default {
]),
],
};
const response = await ai.run('@hf/thebloke/orca-2-13b-awq', chat);
let response: string;
for (let i = 5; i > 0; i--) {
try {
response = await ai.run('@hf/thebloke/openchat_3.5-awq', chat);
break;
} catch {
if (i === 1) {
return new FailedException('Failed to generate after five tries.');
}
}
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand Down

0 comments on commit f3e63a1

Please sign in to comment.