docs: add expert question answering example

old-agentic-v1^2
Philipp Burckhardt 2023-06-16 15:22:46 -04:00
rodzic 5a32b6140b
commit 21a41e23d4
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: A2C3BCA4F31D1DDD
1 zmienionych plików z 74 dodań i 0 usunięć

Wyświetl plik

@ -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()