From 1ea3b7ccb58737dc13c773e93f79901979025c76 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Fri, 16 Jun 2023 15:22:46 -0400 Subject: [PATCH] docs: add expert question answering example --- legacy/examples/expert-question-answering.ts | 74 ++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 legacy/examples/expert-question-answering.ts diff --git a/legacy/examples/expert-question-answering.ts b/legacy/examples/expert-question-answering.ts new file mode 100644 index 00000000..b85b8641 --- /dev/null +++ b/legacy/examples/expert-question-answering.ts @@ -0,0 +1,74 @@ +import { OpenAIClient } from '@agentic/openai-fetch' +import 'dotenv/config' +import { z } from 'zod' + +import { Agentic, SerpAPITool, withHumanFeedback } from '@/index' + +async function main() { + const openai = new OpenAIClient({ apiKey: process.env.OPENAI_API_KEY! }) + const agentic = new Agentic({ openai }) + + const question = 'How do I build a product that people will love?' + + const task = withHumanFeedback( + agentic + .gpt4( + `Generate a list of {n} prominent experts that can answer the following question: {{question}}.` + ) + .tools([new SerpAPITool()]) + .output( + z.array( + z.object({ + name: z.string(), + bio: z.string() + }) + ) + ) + .input( + z.object({ + question: z.string(), + n: z.number().int().default(5) + }) + ), + { + type: 'selectN' + } + ) + const { metadata } = await task.callWithMetadata({ + question + }) + + if ( + metadata.feedback && + metadata.feedback.type === 'selectN' && + metadata.feedback.selected + ) { + const answer = agentic + .gpt4( + `Generate an answer to the following question: "{{question}}" from each of the following experts: {{#each experts}} + - {{this.name}}: {{this.bio}} + {{/each}}` + ) + .output( + z.array( + z.object({ + expert: z.string(), + answer: z.string() + }) + ) + ) + .input( + z.object({ + question: z.string(), + experts: z.array(z.object({ name: z.string(), bio: z.string() })) + }) + ) + .call({ + question, + experts: metadata.feedback.selected + }) + console.log(answer) + } +} + +main()