From ee0c3cf967cba421d3f80e93c3d5447d5e4306d1 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Wed, 14 Jun 2023 15:55:19 -0400 Subject: [PATCH] docs: add new feedback example --- scratch/examples/human-feedback-cli.ts | 39 +++++++++++++++++++++++ scratch/examples/human-feedback-select.ts | 32 ------------------- scratch/examples/human-feedback.ts | 33 ------------------- 3 files changed, 39 insertions(+), 65 deletions(-) create mode 100644 scratch/examples/human-feedback-cli.ts delete mode 100644 scratch/examples/human-feedback-select.ts delete mode 100644 scratch/examples/human-feedback.ts diff --git a/scratch/examples/human-feedback-cli.ts b/scratch/examples/human-feedback-cli.ts new file mode 100644 index 0000000..64c7434 --- /dev/null +++ b/scratch/examples/human-feedback-cli.ts @@ -0,0 +1,39 @@ +import 'dotenv/config' +import { OpenAIClient } from 'openai-fetch' +import { z } from 'zod' + +import { HumanFeedbackMechanismCLI } from '@/human-feedback' +import { Agentic, withHumanFeedback } from '@/index' + +async function main() { + const openai = new OpenAIClient({ apiKey: process.env.OPENAI_API_KEY! }) + const ai = new Agentic({ openai }) + + const topicFacts = ai + .gpt3(`Tell me {{num}} jokes about {{topic}}`) + .input( + z.object({ + topic: z.string(), + num: z.number().int().default(5).optional() + }) + ) + .output(z.array(z.string())) + .modelParams({ temperature: 0.9 }) + + const topicFactsFeedback = withHumanFeedback(topicFacts, { + type: 'selectN', + annotations: false, + bail: false, + editing: true, + mechanism: HumanFeedbackMechanismCLI + }) + + const out = await topicFactsFeedback.callWithMetadata({ + topic: 'politicians', + num: 5 + }) + const feedback = out.metadata.feedback + console.log(JSON.stringify(feedback, null, 2)) +} + +main() diff --git a/scratch/examples/human-feedback-select.ts b/scratch/examples/human-feedback-select.ts deleted file mode 100644 index 0e50ec0..0000000 --- a/scratch/examples/human-feedback-select.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { OpenAIClient } from '@agentic/openai-fetch' -import 'dotenv/config' -import { z } from 'zod' - -import { Agentic, HumanFeedbackSelect } from '@/index' - -async function main() { - const openai = new OpenAIClient({ apiKey: process.env.OPENAI_API_KEY! }) - const ai = new Agentic({ openai }) - - const jokes = ai - .gpt3(`Tell me {{num}} jokes about {{topic}}`) - .input( - z.object({ - topic: z.string(), - num: z.number().int().default(5).optional() - }) - ) - .output(z.array(z.string())) - .modelParams({ temperature: 0.9 }) - - const feedback = new HumanFeedbackSelect(z.string()) - let out = await jokes.call({ topic: 'statisticians' }) - let hf = await feedback.call(out) - while (!hf.accepted) { - out = await jokes.call({ topic: 'statisticians' }) - hf = await feedback.call(out) - } - console.log(hf.results) -} - -main() diff --git a/scratch/examples/human-feedback.ts b/scratch/examples/human-feedback.ts deleted file mode 100644 index c62f2b5..0000000 --- a/scratch/examples/human-feedback.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { OpenAIClient } from '@agentic/openai-fetch' -import 'dotenv/config' -import { z } from 'zod' - -import { Agentic, HumanFeedbackSingle } from '@/index' - -async function main() { - const openai = new OpenAIClient({ apiKey: process.env.OPENAI_API_KEY! }) - const ai = new Agentic({ openai }) - - const topicFacts = ai - .gpt3(`Give me {{numFacts}} random facts about {{topic}}`) - .input( - z.object({ - topic: z.string(), - numFacts: z.number().int().default(5).optional() - }) - ) - .output(z.object({ facts: z.array(z.string()) })) - .modelParams({ temperature: 0.9 }) - - const feedback = new HumanFeedbackSingle(topicFacts.outputSchema) - - let out = await topicFacts.call({ topic: 'cats' }) - let hf = await feedback.call(out) - while (!hf.accepted) { - out = await topicFacts.call({ topic: 'cats' }) - hf = await feedback.call(out) - } - console.log(hf.result) -} - -main()