restores trial key detection via workaround

This commit is contained in:
nai-degen
2023-07-24 14:06:55 -05:00
parent e5b4c7bc9e
commit addfa7c57b
2 changed files with 15 additions and 12 deletions
+3 -3
View File
@@ -77,7 +77,7 @@ function cacheInfoPageHtml(baseUrl: string) {
type ServiceInfo = {
activeKeys: number;
// trialKeys?: number;
trialKeys?: number;
// activeLimit: string;
revokedKeys?: number;
overQuotaKeys?: number;
@@ -120,7 +120,7 @@ function getOpenAIInfo() {
info.turbo = {
activeKeys: turboKeys.filter((k) => !k.isDisabled).length,
// trialKeys: turboKeys.filter((k) => k.isTrial).length,
trialKeys: turboKeys.filter((k) => k.isTrial).length,
// activeLimit: quota.turbo,
revokedKeys: turboKeys.filter((k) => k.isRevoked).length,
overQuotaKeys: turboKeys.filter((k) => k.isOverQuota).length,
@@ -129,7 +129,7 @@ function getOpenAIInfo() {
if (hasGpt4) {
info.gpt4 = {
activeKeys: gpt4Keys.filter((k) => !k.isDisabled).length,
// trialKeys: gpt4Keys.filter((k) => k.isTrial).length,
trialKeys: gpt4Keys.filter((k) => k.isTrial).length,
// activeLimit: quota.gpt4,
revokedKeys: gpt4Keys.filter((k) => k.isRevoked).length,
overQuotaKeys: gpt4Keys.filter((k) => k.isOverQuota).length,
+12 -9
View File
@@ -131,7 +131,7 @@ export class OpenAIKeyChecker {
try {
// We only need to check for provisioned models on the initial check.
if (isInitialCheck) {
const [/* subscription,*/ provisionedModels, _livenessTest] =
const [/* subscription,*/ provisionedModels, livenessTest] =
await Promise.all([
// this.getSubscription(key),
this.getProvisionedModels(key),
@@ -139,11 +139,10 @@ export class OpenAIKeyChecker {
]);
const updates = {
isGpt4: provisionedModels.gpt4,
// isTrial: !subscription.has_payment_method,
// softLimit: subscription.soft_limit_usd,
// hardLimit: subscription.hard_limit_usd,
// systemHardLimit: subscription.system_hard_limit_usd,
isTrial: false,
isTrial: livenessTest.rateLimit <= 250,
softLimit: 0,
hardLimit: 0,
systemHardLimit: 0,
@@ -309,14 +308,16 @@ export class OpenAIKeyChecker {
* is actually not valid, but keys which are revoked or out of quota will fail
* with a 401 or 429 error instead of the expected 400 Bad Request error.
* This lets us avoid test keys without spending any quota.
*
* We use the rate limit header to determine whether it's a trial key.
*/
private async testLiveness(key: OpenAIKey): Promise<void> {
private async testLiveness(key: OpenAIKey): Promise<{ rateLimit: number }> {
const payload = {
model: "gpt-3.5-turbo",
max_tokens: -1,
messages: [{ role: "user", content: "" }],
};
const { data } = await axios.post<OpenAIError>(
const { headers, data } = await axios.post<OpenAIError>(
POST_CHAT_COMPLETIONS_URL,
payload,
{
@@ -324,15 +325,17 @@ export class OpenAIKeyChecker {
validateStatus: (status) => status === 400,
}
);
if (data.error.type === "invalid_request_error") {
// This is the expected error type for our bad prompt, so key is valid.
return;
} else {
const rateLimitHeader = headers["x-ratelimit-limit-requests"];
const rateLimit = parseInt(rateLimitHeader) || 3500; // trials have 200
// invalid_request_error is the expected error
if (data.error.type !== "invalid_request_error") {
this.log.warn(
{ key: key.hash, error: data },
"Unexpected 400 error class while checking key; assuming key is valid, but this may indicate a change in the API."
);
}
return { rateLimit };
}
static errorIsOpenAIError(