import formatChatHistoryAsString from '@/lib/utils/formatHistory'; import { searchSearxng } from '@/lib/searxng'; import { videoSearchFewShots, videoSearchPrompt, } from '@/lib/prompts/media/videos'; import { ChatTurnMessage } from '@/lib/types'; import BaseLLM from '@/lib/models/base/llm'; import z from 'zod'; type VideoSearchChainInput = { chatHistory: ChatTurnMessage[]; query: string; }; type VideoSearchResult = { img_src: string; url: string; title: string; iframe_src: string; }; const searchVideos = async ( input: VideoSearchChainInput, llm: BaseLLM, ) => { const schema = z.object({ query: z.string().describe('The video search query.'), }); const res = await llm.generateObject({ messages: [ { role: 'system', content: videoSearchPrompt, }, ...videoSearchFewShots, { role: 'user', content: `\n${formatChatHistoryAsString(input.chatHistory)}\n\n\n${input.query}\n`, }, ], schema: schema, }); const searchRes = await searchSearxng(res.query, { engines: ['youtube'], }); const videos: VideoSearchResult[] = []; searchRes.results.forEach((result) => { if (result.thumbnail && result.url && result.title && result.iframe_src) { videos.push({ img_src: result.thumbnail, url: result.url, title: result.title, iframe_src: result.iframe_src, }); } }); return videos.slice(0, 10); }; export default searchVideos;