-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
302 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/** | ||
* Parameter Registry and Model Configuration | ||
* | ||
* This module provides a type-safe parameter management system for LLM models. | ||
* It handles parameter definitions, validation, and runtime values while | ||
* maintaining strict type safety throughout the application. | ||
* | ||
* Key concepts: | ||
* - ParameterRegistry: Defines all possible parameters and their constraints | ||
* - ParameterSpec: Model-specific parameter configurations | ||
* - ParameterValues: Runtime parameter values (initial and user overrides) | ||
* | ||
* @module llms | ||
*/ | ||
|
||
|
||
// shared constants | ||
export const FALLBACK_LLM_PARAM_RESPONSE_TOKENS = 4096; | ||
export const FALLBACK_LLM_PARAM_TEMPERATURE = 0.5; | ||
// const FALLBACK_LLM_PARAM_REF_UNKNOWN = 'unknown_id'; | ||
|
||
|
||
/// Registry | ||
|
||
export const DModelParameterRegistry = { | ||
|
||
/// Common parameters, normally available in all models /// | ||
// Note: we still use pre-v2 names for compatibility and ease of migration | ||
|
||
llmRef: { | ||
label: 'Model ID', | ||
type: 'string' as const, | ||
description: 'Upstream model reference', | ||
hidden: true, | ||
} as const, | ||
|
||
llmResponseTokens: { | ||
label: 'Maximum Tokens', | ||
type: 'integer' as const, | ||
description: 'Maximum length of generated text', | ||
nullable: { | ||
meaning: 'Explicitly avoid sending max_tokens to upstream API', | ||
} as const, | ||
requiredFallback: FALLBACK_LLM_PARAM_RESPONSE_TOKENS, // if required and not specified/user overridden, use this value | ||
} as const, | ||
|
||
llmTemperature: { | ||
label: 'Temperature', | ||
type: 'float' as const, | ||
description: 'Controls randomness in the output', | ||
range: [0.0, 2.0] as const, | ||
requiredFallback: FALLBACK_LLM_PARAM_TEMPERATURE, | ||
} as const, | ||
|
||
/// Extended parameters, specific to certain models/vendors | ||
|
||
llmTopP: { | ||
label: 'Top P', | ||
type: 'float' as const, | ||
description: 'Nucleus sampling threshold', | ||
range: [0.0, 1.0] as const, | ||
requiredFallback: 1.0, | ||
incompatibleWith: ['temperature'] as const, | ||
} as const, | ||
|
||
'vnd.oai.reasoning_quantity': { | ||
label: 'Reasoning Quantity', | ||
type: 'enum' as const, | ||
description: 'Controls reasoning depth (OpenAI specific)', | ||
values: ['low', 'med', 'high'] as const, | ||
requiredFallback: 'med', | ||
} as const, | ||
|
||
} as const; | ||
|
||
|
||
/// Types | ||
|
||
export interface DModelParameterSpec<T extends DModelParameterId> { | ||
paramId: T; | ||
required?: boolean; | ||
hidden?: boolean; | ||
upstreamDefault?: DModelParameterValue<T>; | ||
} | ||
|
||
export type DModelParameterValues = { | ||
[K in DModelParameterId]?: DModelParameterValue<K>; | ||
} | ||
|
||
export type DModelParameterId = keyof typeof DModelParameterRegistry; // max_tokens, temperature, top_p, vnd.oai.reasoning_quantity, ... | ||
// type _ExtendedParameterId = keyof typeof _ExtendedParameterRegistry; | ||
|
||
type DModelParameterValue<T extends DModelParameterId> = | ||
typeof DModelParameterRegistry[T]['type'] extends 'integer' ? number | null : | ||
typeof DModelParameterRegistry[T]['type'] extends 'float' ? number : | ||
typeof DModelParameterRegistry[T]['type'] extends 'string' ? string : | ||
typeof DModelParameterRegistry[T]['type'] extends 'boolean' ? boolean : | ||
typeof DModelParameterRegistry[T]['type'] extends 'enum' | ||
? (T extends { type: 'enum', values: readonly (infer U)[] } ? U : never) | ||
: never; | ||
|
||
|
||
/// Utility Functions | ||
|
||
const _requiredParamId: DModelParameterId[] = ['llmRef', 'llmResponseTokens', 'llmTemperature'] as const; | ||
|
||
export function getAllModelParameterValues(initialParameters: undefined | DModelParameterValues, userParameters?: DModelParameterValues): DModelParameterValues { | ||
|
||
// fallback values | ||
const fallbackParameters: DModelParameterValues = {}; | ||
for (const requiredParamId of _requiredParamId) { | ||
if ('requiredFallback' in DModelParameterRegistry[requiredParamId]) | ||
fallbackParameters[requiredParamId] = DModelParameterRegistry[requiredParamId].requiredFallback as DModelParameterValue<typeof requiredParamId>; | ||
} | ||
|
||
// accumulate initial and user values | ||
return { | ||
...fallbackParameters, | ||
...initialParameters, | ||
...userParameters, | ||
}; | ||
} | ||
|
||
|
||
export function getModelParameterValueOrThrow<T extends DModelParameterId>( | ||
paramId: T, | ||
initialValues: undefined | DModelParameterValues, | ||
userValues: undefined | DModelParameterValues, | ||
fallbackValue: undefined | DModelParameterValue<T>, | ||
): DModelParameterValue<T> { | ||
|
||
// check user values first | ||
if (userValues && paramId in userValues) { | ||
const value = userValues[paramId]; | ||
if (value !== undefined) return value; | ||
} | ||
|
||
// then check initial values | ||
if (initialValues && paramId in initialValues) { | ||
const value = initialValues[paramId]; | ||
if (value !== undefined) return value; | ||
} | ||
|
||
// then try provided fallback | ||
if (fallbackValue !== undefined) return fallbackValue; | ||
|
||
// finally the global registry fallback | ||
const paramDef = DModelParameterRegistry[paramId]; | ||
if ('requiredFallback' in paramDef && paramDef.requiredFallback !== undefined) | ||
return paramDef.requiredFallback as DModelParameterValue<T>; | ||
|
||
// if we're here, we couldn't find a value | ||
// [DANGER] VERY DANGEROUS, but shall NEVER happen | ||
throw new Error(`getModelParameterValue: missing required parameter '${paramId}'`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.