-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
68eb3ca
commit 93b3fb3
Showing
5 changed files
with
110 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
...t/[brainId]/components/BrainManagementTabs/components/SettingsTab/utils/checkOpenAIKey.ts
This file was deleted.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
...brainId]/components/BrainManagementTabs/components/SettingsTab/utils/validateOpenAIKey.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import axios from "axios"; | ||
|
||
import { ToastData } from "@/lib/components/ui/Toast/domain/types"; | ||
import { getAxiosErrorParams } from "@/lib/helpers/getAxiosErrorParams"; | ||
|
||
export const getOpenAIKeyValidationStatusCode = async ( | ||
key: string | ||
): Promise<number> => { | ||
const url = "https://api.openai.com/v1/chat/completions"; | ||
const headers = { | ||
Authorization: `Bearer ${key}`, | ||
"Content-Type": "application/json", | ||
}; | ||
|
||
const data = JSON.stringify({ | ||
model: "gpt-3.5-turbo", | ||
messages: [ | ||
{ | ||
role: "user", | ||
content: "Hello!", | ||
}, | ||
], | ||
}); | ||
|
||
try { | ||
await axios.post(url, data, { headers }); | ||
|
||
return 200; | ||
} catch (error) { | ||
return getAxiosErrorParams(error)?.status ?? 400; | ||
} | ||
}; | ||
|
||
type ErrorMessages = { | ||
badApiKeyError: string; | ||
invalidApiKeyError: string; | ||
}; | ||
|
||
export const validateOpenAIKey = async ( | ||
openai_api_key: string | undefined, | ||
errorMessages: ErrorMessages, | ||
publish: (toast: ToastData) => void | ||
): Promise<boolean> => { | ||
if (openai_api_key !== undefined) { | ||
const keyValidationStatusCode = await getOpenAIKeyValidationStatusCode( | ||
openai_api_key | ||
); | ||
|
||
if (keyValidationStatusCode !== 200) { | ||
if (keyValidationStatusCode === 401) { | ||
publish({ | ||
variant: "danger", | ||
text: errorMessages.badApiKeyError, | ||
}); | ||
} | ||
|
||
if (keyValidationStatusCode === 429) { | ||
publish({ | ||
variant: "danger", | ||
text: errorMessages.invalidApiKeyError, | ||
}); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters