Skip to content

Commit

Permalink
🔧 chore(.env.example): add FINE_TUNED_ID variable to example environm…
Browse files Browse the repository at this point in the history
…ent file for fine-tuned model ID configuration

🔧 chore(environment.ts): add FINE_TUNED_ID constant to environment configuration for fine-tuned model ID

🔧 chore(createAnalysis.ts): use getFineTunedModel function to determine the model to use for analysis based on the provided model parameter

🔧 chore(openai.ts): add getFineTunedModel function to handle determining the model to use for fine-tuned analysis based on the provided model parameter
  • Loading branch information
romantech committed Aug 27, 2023
1 parent 96f8b9d commit 76a3515
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ REDIS_PORT=
REDIS_USERNAME=
REDIS_PASSWORD=
CORS_ORIGIN=
FINE_TUNED_ID=


2 changes: 2 additions & 0 deletions src/config/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}

/**
Expand All @@ -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];
7 changes: 6 additions & 1 deletion src/controllers/analyzer/createAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ANALYZER_REDIS_SCHEMA,
decrementRedisCounters,
fetchFromOpenAI,
getFineTunedModel,
redis,
} from '@/services';
import { checkModelField, checkSentenceField } from '@/validators';
Expand All @@ -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(
Expand Down
13 changes: 11 additions & 2 deletions src/services/openai.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down

0 comments on commit 76a3515

Please sign in to comment.