import {z} from 'zod' export type RLEncodedTextChunk = {tokens: Array}; export type RLModelInputChunk = {encoded_text: RLEncodedTextChunk}; export type RLModelInput = {chunks: Array}; export type RLTensorData = {data: Array, dtype: 'int64' | 'float32', shape?: Array | undefined, sparse_crow_indices?: Array | undefined, sparse_col_indices?: Array | undefined}; export type RLTrainingSample = {model_input: RLModelInput, loss_fn_inputs: {[key: string]: RLTensorData}, routed_experts_key?: string | undefined}; export type RLDType = 'D_TYPE_UNSPECIFIED' | 'D_TYPE_INT64' | 'D_TYPE_FLOAT32' | 'D_TYPE_BFLOAT16'; export type RLTargetLogprobGradients = {data: Array, dtype?: RLDType | undefined}; export type RLCustomForwardBackwardBody = {samples: Array, gradients: Array}; export type CreateApiV1RlTrainingSessionsSessionIdOperationsCustomForwardBackwardArgs = {session_id: string, 'Idempotency-Key': string, body: RLCustomForwardBackwardBody}; export type CreateApiV1RlTrainingSessionsSessionIdOperationsCustomForwardBackwardOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const rLTrainingOperationStatus = z.enum(["TRAINING_OPERATION_STATUS_UNSPECIFIED", "TRAINING_OPERATION_STATUS_PENDING", "TRAINING_OPERATION_STATUS_RUNNING", "TRAINING_OPERATION_STATUS_COMPLETED", "TRAINING_OPERATION_STATUS_FAILED"]); export const rLCustomForwardBackwardResult = z.object({}); export const rLTrainingOperationErrorCode = z.enum(["TRAINING_OPERATION_ERROR_CODE_UNSPECIFIED", "TRAINING_OPERATION_ERROR_CODE_RESOURCE_EXHAUSTED", "TRAINING_OPERATION_ERROR_CODE_TIMEOUT", "TRAINING_OPERATION_ERROR_CODE_INTERNAL_ERROR", "TRAINING_OPERATION_ERROR_CODE_SESSION_NOT_ACTIVE", "TRAINING_OPERATION_ERROR_CODE_INVALID_INPUT", "TRAINING_OPERATION_ERROR_CODE_NON_FINITE_LOSS"]); export const rLTrainingOperationError = z.object({code: rLTrainingOperationErrorCode.optional(), message: z.string().optional()}); export const rLCustomForwardBackwardOperation = z.object({id: z.string(), status: rLTrainingOperationStatus, output: rLCustomForwardBackwardResult.optional(), error: rLTrainingOperationError.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Custom forward-backward pass * * POST /v1/rl/training-sessions/{session_id}/operations/custom-forward-backward */ export const createApiV1RlTrainingSessionsSessionIdOperationsCustomForwardBackward = async (args: CreateApiV1RlTrainingSessionsSessionIdOperationsCustomForwardBackwardArgs, options?: CreateApiV1RlTrainingSessionsSessionIdOperationsCustomForwardBackwardOptions) => { const {baseUrl = "https://api.together.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/rl/training-sessions/${encodeURIComponent(String(args.session_id))}/operations/custom-forward-backward${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(rLCustomForwardBackwardOperation, await res.json()) };