fix(misc): restrict countries not supported by OpenAI (#26725)

Restrict countries that are not supported by OpenAI APIs:
https://platform.openai.com/docs/supported-countries


![Screenshot 2024-06-27 at 3 58
47 PM](https://github.com/nrwl/nx/assets/6603745/54a82c41-57b3-408e-a79e-c5907d8de193)
This commit is contained in:
Katerina Skroumpelou 2024-06-27 17:11:26 +03:00 committed by GitHub
parent 0cefa29eca
commit 88efb216a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -15,7 +15,6 @@ import {
getUserQuery,
initializeChat,
extractErrorMessage,
// moderateContent,
} from '@nx/nx-dev/util-ai';
import { SupabaseClient } from '@supabase/supabase-js';
import OpenAI from 'openai';
@ -36,7 +35,24 @@ export const config = {
};
export default async function handler(request: NextRequest) {
const country = request.geo.country;
const restrictedCountries: string[] = [
'BY', // Belarus
'CN', // China
'CU', // Cuba
'IR', // Iran
'KP', // North Korea
'RU', // Russia
'SY', // Syria
'VE', // Venezuela
];
try {
if (restrictedCountries.includes(country)) {
throw new CustomError(
'user_error',
'Service is not available in your region.'
);
}
const openai = getOpenAI(openAiKey);
const supabaseClient: SupabaseClient<any, 'public', any> =
getSupabaseClient(supabaseUrl, supabaseServiceKey);
@ -46,11 +62,6 @@ export default async function handler(request: NextRequest) {
const query: string | null = getUserQuery(messages);
const sanitizedQuery = query.trim();
// Moderate the content to comply with OpenAI T&C
// Removing the moderation for now
// to see if it's faster
// await moderateContent(sanitizedQuery, openai);
// We include the previous response,
// to make sure the embeddings (doc sections)
// we get back are relevant.