chatgpt-api/src/human-feedback/cli.ts

75 wiersze
1.9 KiB
TypeScript
Czysty Zwykły widok Historia

import checkbox from '@inquirer/checkbox'
import editor from '@inquirer/editor'
import input from '@inquirer/input'
import select from '@inquirer/select'
import {
HumanFeedbackMechanism,
2023-06-13 22:14:12 +00:00
HumanFeedbackType,
2023-06-13 22:16:29 +00:00
HumanFeedbackUserActionMessages,
HumanFeedbackUserActions
} from './feedback'
2023-06-13 22:14:12 +00:00
export class HumanFeedbackMechanismCLI<
2023-06-14 01:25:40 +00:00
T extends HumanFeedbackType,
TOutput = any
> extends HumanFeedbackMechanism<T, TOutput> {
2023-06-13 20:32:58 +00:00
/**
* Prompt the user to select one of a list of options.
*/
2023-06-15 01:22:06 +00:00
protected async _askUser(
2023-06-13 20:32:58 +00:00
message: string,
2023-06-13 22:16:29 +00:00
choices: HumanFeedbackUserActions[]
): Promise<HumanFeedbackUserActions> {
2023-06-13 20:32:58 +00:00
return select({
message,
choices: choices.map((choice) => ({
2023-06-13 22:16:29 +00:00
name: HumanFeedbackUserActionMessages[choice],
2023-06-13 20:32:58 +00:00
value: choice
}))
2023-06-13 19:52:45 +00:00
})
}
2023-06-15 01:22:06 +00:00
protected async _edit(output: string): Promise<string> {
2023-06-13 20:32:58 +00:00
return editor({
message: 'Edit the output:',
default: output
})
}
2023-06-15 01:22:06 +00:00
protected async _annotate(): Promise<string> {
2023-06-13 20:32:58 +00:00
return input({
message:
'Please leave an annotation (leave blank to skip; press enter to submit):'
})
}
2023-06-15 01:22:06 +00:00
protected async _selectOne(
2023-06-14 01:25:40 +00:00
response: TOutput
): Promise<TOutput extends (infer U)[] ? U : never> {
if (!Array.isArray(response)) {
throw new Error('selectOne called on non-array response')
}
2023-06-13 20:32:58 +00:00
const choices = response.map((option) => ({
2023-06-14 01:25:40 +00:00
name: String(option),
2023-06-13 20:32:58 +00:00
value: option
}))
return select({ message: 'Pick one output:', choices })
}
2023-06-15 01:22:06 +00:00
protected async _selectN(
2023-06-14 01:25:40 +00:00
response: TOutput
): Promise<TOutput extends any[] ? TOutput : never> {
if (!Array.isArray(response)) {
throw new Error('selectN called on non-array response')
}
2023-06-13 20:32:58 +00:00
const choices = response.map((option) => ({
2023-06-14 01:25:40 +00:00
name: String(option),
2023-06-13 20:32:58 +00:00
value: option
}))
2023-06-14 01:25:40 +00:00
return checkbox({ message: 'Select outputs:', choices }) as any
}
}