| 5 | * Represents an object that can translate natural language requests in JSON objects of the given type. |
| 6 | */ |
| 7 | export interface TypeChatJsonTranslator<T extends object> { |
| 8 | /** |
| 9 | * The associated `TypeChatLanguageModel`. |
| 10 | */ |
| 11 | model: TypeChatLanguageModel; |
| 12 | /** |
| 13 | * The associated `TypeChatJsonValidator<T>`. |
| 14 | */ |
| 15 | validator: TypeChatJsonValidator<T>; |
| 16 | /** |
| 17 | * A boolean indicating whether to attempt repairing JSON objects that fail to validate. The default is `true`, |
| 18 | * but an application can set the property to `false` to disable repair attempts. |
| 19 | */ |
| 20 | attemptRepair: boolean; |
| 21 | /** |
| 22 | * A boolean indicating whether to delete properties with null values from parsed JSON objects. Some language |
| 23 | * models (e.g. gpt-3.5-turbo) have a tendency to assign null values to optional properties instead of omitting |
| 24 | * them. The default for this property is `false`, but an application can set the property to `true` for schemas |
| 25 | * that don't permit null values. |
| 26 | */ |
| 27 | stripNulls: boolean; |
| 28 | /** |
| 29 | * Creates an AI language model prompt from the given request. This function is called by `completeAndValidate` |
| 30 | * to obtain the prompt. An application can assign a new function to provide a different prompt. |
| 31 | * @param request The natural language request. |
| 32 | * @returns A prompt that combines the request with the schema and type name of the underlying validator. |
| 33 | */ |
| 34 | createRequestPrompt(request: string): PromptContent; |
| 35 | /** |
| 36 | * Creates a repair prompt to append to an original prompt/response in order to repair a JSON object that |
| 37 | * failed to validate. This function is called by `completeAndValidate` when `attemptRepair` is true and the |
| 38 | * JSON object produced by the original prompt failed to validate. An application can assign a new function |
| 39 | * to provide a different repair prompt. |
| 40 | * @param validationError The error message returned by the validator. |
| 41 | * @returns A repair prompt constructed from the error message. |
| 42 | */ |
| 43 | createRepairPrompt(validationError: string): string; |
| 44 | /** |
| 45 | * Optionally implements additional validation logic beyond what is expressed in the schema. This function is |
| 46 | * called following successful schema validation of an instance. By default the function just returns a |
| 47 | * `Success<T>` for the given instance, but an application can assign a new function that implements any |
| 48 | * additional validation. |
| 49 | * @param instance The instance to validate. |
| 50 | * @returns A `Success<T>` with the final validated instance, or an `Error` explaining the validation failure. |
| 51 | */ |
| 52 | validateInstance(instance: T): Result<T>; |
| 53 | /** |
| 54 | * Translates a natural language request into an object of type `T`. If the JSON object returned by |
| 55 | * the language model fails to validate and the `attemptRepair` property is `true`, a second |
| 56 | * attempt to translate the request will be made. The prompt for the second attempt will include the |
| 57 | * diagnostics produced for the first attempt. This often helps produce a valid instance. |
| 58 | * @param request The natural language request. |
| 59 | * @param promptPreamble An optional string or array of prompt sections to prepend to the generated |
| 60 | * prompt. If a string is specified, it is converted into a single "user" role prompt section. |
| 61 | * @returns A promise for the resulting object. |
| 62 | */ |
| 63 | translate(request: string, promptPreamble?: string | PromptSection[]): Promise<Result<T>>; |
| 64 | } |
no outgoing calls
no test coverage detected
searching dependent graphs…