diff --git a/.env.example b/.env.example index 2e7262f..c079b2c 100644 --- a/.env.example +++ b/.env.example @@ -6,3 +6,6 @@ REDIS_PORT= REDIS_USERNAME= REDIS_PASSWORD= CORS_ORIGIN= +FINE_TUNED_ID= + + diff --git a/src/config/environment.ts b/src/config/environment.ts index ff03fa1..69519b1 100644 --- a/src/config/environment.ts +++ b/src/config/environment.ts @@ -9,6 +9,7 @@ export enum EnvVars { REDIS_PASSWORD = 'REDIS_PASSWORD', REDIS_USERNAME = 'REDIS_USERNAME', CORS_ORIGIN = 'CORS_ORIGIN', + FINE_TUNED_ID = 'FINE_TUNED_ID', } /** @@ -24,3 +25,4 @@ export const REDIS_PASSWORD = env[EnvVars.REDIS_PASSWORD]; export const REDIS_USERNAME = env[EnvVars.REDIS_USERNAME]; export const OPENAI_API_KEY = env[EnvVars.OPENAI_API_KEY]; export const CORS_ORIGIN = env[EnvVars.CORS_ORIGIN]?.split(',') ?? []; +export const FINE_TUNED_ID = env[EnvVars.FINE_TUNED_ID]; diff --git a/src/controllers/analyzer/createAnalysis.ts b/src/controllers/analyzer/createAnalysis.ts index 861853d..b6002f8 100644 --- a/src/controllers/analyzer/createAnalysis.ts +++ b/src/controllers/analyzer/createAnalysis.ts @@ -4,6 +4,7 @@ import { ANALYZER_REDIS_SCHEMA, decrementRedisCounters, fetchFromOpenAI, + getFineTunedModel, redis, } from '@/services'; import { checkModelField, checkSentenceField } from '@/validators'; @@ -27,7 +28,11 @@ export const createAnalysis = [ const prompt = await redis.hget(KEYS.PROMPT, FIELDS.ANALYSIS); if (!prompt) return throwCustomError(RETRIEVE_FAILED('prompt'), 500); - const analysis = await fetchAnalysisFromOpenAI(sentence, model, prompt); + const analysis = await fetchAnalysisFromOpenAI( + sentence, + getFineTunedModel(model), + prompt, + ); if (!analysis) return throwCustomError(GENERATE_FAILED('analysis'), 500); await decrementRedisCounters( diff --git a/src/services/openai.ts b/src/services/openai.ts index f2f7427..2c2fe84 100644 --- a/src/services/openai.ts +++ b/src/services/openai.ts @@ -1,11 +1,20 @@ import { Configuration, CreateChatCompletionRequest, OpenAIApi } from 'openai'; -import { OPENAI_API_KEY } from '@/config'; +import { FINE_TUNED_ID, OPENAI_API_KEY } from '@/config'; import { throwCustomError } from '@/utils'; -import { ERROR_MESSAGES } from '@/constants'; +import { ERROR_MESSAGES, GPTModels } from '@/constants'; const configuration = new Configuration({ apiKey: OPENAI_API_KEY }); export const openai = new OpenAIApi(configuration); +export const getFineTunedModel = (model: GPTModels) => { + switch (model) { + case GPTModels.GPT_3: + return FINE_TUNED_ID ?? model; + default: + return model; + } +}; + export const fetchFromOpenAI = async (request: CreateChatCompletionRequest) => { const { SERVICE_ERROR } = ERROR_MESSAGES;