chatgpt-api/examples/llm-with-search.ts

45 wiersze
1.1 KiB
TypeScript
Czysty Zwykły widok Historia

2023-06-01 01:53:09 +00:00
import 'dotenv/config'
2023-05-27 00:38:55 +00:00
import { OpenAIClient } from 'openai-fetch'
import { z } from 'zod'
2023-06-07 19:13:01 +00:00
import { Agentic, MetaphorSearchTool } from '@/index'
2023-05-27 00:38:55 +00:00
async function main() {
const openai = new OpenAIClient({ apiKey: process.env.OPENAI_API_KEY! })
const $ = new Agentic({ openai })
const metaphorSearch = new MetaphorSearchTool({ agentic: $ })
2023-05-27 00:38:55 +00:00
2023-05-27 00:47:59 +00:00
const { results: searchResults } = await metaphorSearch.call({
query: 'news from today, 2023',
numResults: 5
})
2023-05-27 00:38:55 +00:00
console.log('searchResults', searchResults)
2023-05-27 01:27:33 +00:00
const res = await $.gpt4(
2023-05-27 00:47:59 +00:00
`Give me a summary of today's news. Here is what I got back from a search engine: \n{{#searchResults}}{{title}}\n{{/searchResults}}`
2023-05-27 00:38:55 +00:00
)
.input(
z.object({
2023-05-27 00:47:59 +00:00
searchResults: z.any() // TODO
2023-05-27 00:38:55 +00:00
})
)
.output(
z.object({
summary: z.string(),
linkToLearnMore: z.string(),
2023-05-27 00:47:59 +00:00
metadata: z.object({
2023-05-27 00:38:55 +00:00
title: z.string(),
keyTopics: z.string().array(),
datePublished: z.string()
}),
isWorthFollowingUp: z.boolean()
})
)
.call({
searchResults
})
}
main()