kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
feat: refactor human feedback to change results
rodzic
b61caf629e
commit
c20ce31e00
|
@ -10,7 +10,7 @@ async function main() {
|
||||||
|
|
||||||
const question = 'How do I build a product that people will love?'
|
const question = 'How do I build a product that people will love?'
|
||||||
|
|
||||||
const { metadata } = await agentic
|
const result = await agentic
|
||||||
.gpt4(
|
.gpt4(
|
||||||
`Generate a list of {n} prominent experts that can answer the following question: {{question}}.`
|
`Generate a list of {n} prominent experts that can answer the following question: {{question}}.`
|
||||||
)
|
)
|
||||||
|
@ -32,15 +32,10 @@ async function main() {
|
||||||
.withHumanFeedback({
|
.withHumanFeedback({
|
||||||
type: 'multiselect'
|
type: 'multiselect'
|
||||||
})
|
})
|
||||||
.callWithMetadata({
|
.call({
|
||||||
question
|
question
|
||||||
})
|
})
|
||||||
|
|
||||||
if (
|
|
||||||
metadata.feedback &&
|
|
||||||
metadata.feedback.type === 'multiselect' &&
|
|
||||||
metadata.feedback.selected
|
|
||||||
) {
|
|
||||||
const answer = await agentic
|
const answer = await agentic
|
||||||
.gpt4(
|
.gpt4(
|
||||||
`Generate an answer to the following question: "{{question}}" from each of the following experts: {{#each experts}}
|
`Generate an answer to the following question: "{{question}}" from each of the following experts: {{#each experts}}
|
||||||
|
@ -63,7 +58,7 @@ async function main() {
|
||||||
)
|
)
|
||||||
.call({
|
.call({
|
||||||
question,
|
question,
|
||||||
experts: metadata.feedback.selected
|
experts: result
|
||||||
})
|
})
|
||||||
|
|
||||||
const message = answer.reduce((acc, { expert, answer }) => {
|
const message = answer.reduce((acc, { expert, answer }) => {
|
||||||
|
@ -81,7 +76,6 @@ async function main() {
|
||||||
},
|
},
|
||||||
to: [{ subscriberId: '123' }]
|
to: [{ subscriberId: '123' }]
|
||||||
})
|
})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -16,8 +16,12 @@ async function main() {
|
||||||
numFacts: z.number().int().default(5)
|
numFacts: z.number().int().default(5)
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
.output(z.object({ facts: z.array(z.string()) }))
|
.output(z.array(z.string()))
|
||||||
.modelParams({ temperature: 0.9 })
|
.modelParams({ temperature: 0.9 })
|
||||||
|
.withHumanFeedback({
|
||||||
|
type: 'confirm',
|
||||||
|
editing: true
|
||||||
|
})
|
||||||
.call({ topic: 'cats' })
|
.call({ topic: 'cats' })
|
||||||
|
|
||||||
console.log(out)
|
console.log(out)
|
||||||
|
|
|
@ -253,7 +253,7 @@ export abstract class HumanFeedbackMechanism<
|
||||||
throw new Error('Expected output to be an array')
|
throw new Error('Expected output to be an array')
|
||||||
}
|
}
|
||||||
|
|
||||||
feedback.chosen = await this._select(output)
|
feedback.chosen = [await this._select(output)]
|
||||||
}
|
}
|
||||||
|
|
||||||
break
|
break
|
||||||
|
|
16
src/task.ts
16
src/task.ts
|
@ -172,6 +172,18 @@ export abstract class BaseTask<
|
||||||
this.addAfterCallHook(async (output, ctx) => {
|
this.addAfterCallHook(async (output, ctx) => {
|
||||||
const feedback = await feedbackMechanism.interact(output)
|
const feedback = await feedbackMechanism.interact(output)
|
||||||
ctx.metadata = { ...ctx.metadata, feedback }
|
ctx.metadata = { ...ctx.metadata, feedback }
|
||||||
|
if (feedback.editedOutput) {
|
||||||
|
return feedback.editedOutput
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (feedback.type) {
|
||||||
|
case 'confirm':
|
||||||
|
return output
|
||||||
|
case 'select':
|
||||||
|
return feedback.chosen
|
||||||
|
case 'multiselect':
|
||||||
|
return feedback.selected
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return this
|
return this
|
||||||
|
@ -250,12 +262,14 @@ export abstract class BaseTask<
|
||||||
|
|
||||||
const result = await pRetry(
|
const result = await pRetry(
|
||||||
async () => {
|
async () => {
|
||||||
const result = await this._call(ctx)
|
let result = await this._call(ctx)
|
||||||
|
|
||||||
for (const { hook: postHook } of this._postHooks) {
|
for (const { hook: postHook } of this._postHooks) {
|
||||||
const postHookResult = await postHook(result, ctx)
|
const postHookResult = await postHook(result, ctx)
|
||||||
if (postHookResult === SKIP_HOOKS) {
|
if (postHookResult === SKIP_HOOKS) {
|
||||||
break
|
break
|
||||||
|
} else if (postHookResult !== undefined) {
|
||||||
|
result = postHookResult
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -191,4 +191,8 @@ export type TaskAfterCallHook<
|
||||||
> = (
|
> = (
|
||||||
output: TOutput,
|
output: TOutput,
|
||||||
ctx: TaskCallContext<TInput>
|
ctx: TaskCallContext<TInput>
|
||||||
) => void | typeof SKIP_HOOKS | Promise<void | typeof SKIP_HOOKS>
|
) =>
|
||||||
|
| void
|
||||||
|
| TOutput
|
||||||
|
| typeof SKIP_HOOKS
|
||||||
|
| Promise<void | typeof SKIP_HOOKS | TOutput>
|
||||||
|
|
Ładowanie…
Reference in New Issue