Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | import type {
AIProviderFunctionConfig,
ResponseTransformFunction,
} from '@shared/types/ai-providers/config';
import type { ParameterConfigDefaultFunction } from '@shared/types/api/response/body';
import type { CreateFineTuningJobRequestBody } from '@shared/types/api/routes/fine-tuning-api/request';
import {
type CreateFineTuningJobResponseBody,
FineTuningJobStatus,
} from '@shared/types/api/routes/fine-tuning-api/response';
import { bedrockErrorResponseTransform } from './chat-complete';
import { populateHyperParameters } from './utils';
export const BedrockCreateFinetuneConfig: AIProviderFunctionConfig = {
model: {
param: 'baseModelIdentifier',
required: true,
},
suffix: {
param: 'customModelName',
required: true,
},
hyperparameters: {
param: 'hyperParameters',
required: false,
transform: (saRequestBody: CreateFineTuningJobRequestBody) => {
const hyperParameters = populateHyperParameters(
saRequestBody as unknown as CreateFineTuningJobRequestBody,
);
const epochCount = hyperParameters.n_epochs;
const learningRateMultiplier = hyperParameters.learning_rate_multiplier;
const batchSize = hyperParameters.batch_size;
return {
epochCount: epochCount ? String(epochCount) : undefined,
learningRateMultiplier: learningRateMultiplier
? String(learningRateMultiplier)
: undefined,
batchSize: batchSize ? String(batchSize) : undefined,
};
},
},
training_file: {
param: 'trainingDataConfig',
required: true,
transform: (saRequestBody: CreateFineTuningJobRequestBody) => {
return {
s3Uri: decodeURIComponent(saRequestBody.training_file as string),
};
},
},
validation_file: {
param: 'validationDataConfig',
required: false,
transform: (saRequestBody: CreateFineTuningJobRequestBody) => {
if (!saRequestBody.validation_file) {
return undefined;
}
return {
s3Uri: decodeURIComponent(saRequestBody.validation_file as string),
};
},
},
output_file: {
param: 'outputDataConfig',
required: true,
default: (({ saRequestBody }): Record<string, unknown> => {
const finetuneRequestBody =
saRequestBody as CreateFineTuningJobRequestBody;
const trainingFile = decodeURIComponent(
finetuneRequestBody.training_file as string,
);
const uri =
trainingFile.substring(0, trainingFile.lastIndexOf('/') + 1) +
finetuneRequestBody.suffix;
return {
s3Uri: uri,
};
}) as ParameterConfigDefaultFunction,
},
// job_name: {
// param: 'jobName',
// required: true,
// default: (({ saRequestBody }): string => {
// const finetuneRequestBody =
// saRequestBody as CreateFineTuningJobRequestBody;
// return (
// finetuneRequestBody.job_name ?? `sa-finetune-${crypto.randomUUID()}`
// );
// }) as ParameterConfigDefaultFunction,
// }, // TODO: Fix this
role_arn: {
param: 'roleArn',
required: true,
},
customization_type: {
param: 'customizationType',
required: true,
default: 'FINE_TUNING',
},
};
const OK_STATUS = [200, 201];
export const bedrockCreateFinetuneResponseTransform: ResponseTransformFunction =
(response, responseStatus) => {
if (!OK_STATUS.includes(responseStatus) || 'error' in response) {
const errorResponse = bedrockErrorResponseTransform(response);
if (errorResponse) {
return errorResponse;
}
}
// AWS Bedrock CreateModelCustomizationJob returns a simple response with jobArn
// We need to construct the OpenAI-compatible response
const awsResponse = response as unknown as {
jobArn: string;
creationTime?: string;
};
const finetuneResponseBody: CreateFineTuningJobResponseBody = {
id: encodeURIComponent(awsResponse.jobArn) as string,
object: 'fine_tuning.job',
created_at: awsResponse.creationTime
? new Date(awsResponse.creationTime).getTime()
: Date.now(), // Use current time if creationTime not provided
status: FineTuningJobStatus.VALIDATING_FILES, // Initial status for newly created job
model: '', // Will be populated when job completes
hyperparameters: {
n_epochs: 'auto', // Default values as per OpenAI spec
batch_size: 'auto',
learning_rate_multiplier: 'auto',
},
training_file: '', // These will be populated from the original request context
fine_tuned_model: null, // Will be available when job completes
finished_at: null,
trained_tokens: null,
validation_file: null,
};
return finetuneResponseBody;
};
|