diff --git a/README.md b/README.md index 03fbbd1..bf10d44 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ The repo provides the following tools you can use out of the box: - 🔍 GoogleCustomSearch: A wrapper around the Google Custom Search API. Useful for when you need to answer questions about current events. Input should be a search query. - 🔍 BingCustomSearch: A wrapper around the Bing Custom Search API. Useful for when you need to answer questions about current events. Input should be a search query. - 🔍 SerperCustomSearch: A wrapper around the SerpAPI. Useful for when you need to answer questions about current events. Input should be a search query. -- 🏞ī¸ SerpApiImageSearch: Use SerpAPI to search images. Input should be a search query. +- 🏞ī¸ SerperImagesSearch: Use SerpAPI to search images. Input should be a search query. - 📁 fs: WriteFileTool abd ReadFileTool access to the file system. Input should be a file path and text written to the file. - đŸĒŠ webbrowser: A web browser that can open a website. Input should be a URL. - 🚧 sql: Input to this tool is a detailed and correct SQL query, output is a result from the database. diff --git a/index.ts b/index.ts index 84cd47d..b86c34a 100644 --- a/index.ts +++ b/index.ts @@ -4,6 +4,7 @@ export { createBingCustomSearch } from './tools/bingCustomSearch'; export { createSerpApiCustomSearch } from './tools/serpApiCustomSearch'; export { createSerpApiImageSearch } from './tools/serpApiImageSearch'; export { createSerperCustomSearch } from './tools/serperCustomSearch'; +export { createSerperImagesSearch } from './tools/serperImagesSearch'; export { createClock } from './tools/clock'; export { createWebBrowser } from './tools/webbrowser'; export { createReadFileTool, createWriteFileTool } from './tools/fs'; diff --git a/tools/serperImagesSearch.ts b/tools/serperImagesSearch.ts new file mode 100644 index 0000000..4bcab12 --- /dev/null +++ b/tools/serperImagesSearch.ts @@ -0,0 +1,51 @@ +import { Tool } from './tool'; +import { z } from 'zod'; + +function createSerperImagesSearch({ apiKey }: { apiKey: string }) { + if (!apiKey) { + throw new Error('Serper key must be set.'); + } + + const paramsSchema = z.object({ + input: z.string(), + }); + const name = 'serperImagesSearch'; + const description = 'Useful for searching images. Input should be a search query. Outputs a JSON array of image results.'; + + const execute = async ({ input }: z.infer) => { + try { + const res = await fetch( + 'https://google.serper.dev/images', + { + method: 'POST', + headers: { + 'X-API-KEY': apiKey, + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ q: input }) + } + ); + + if (!res.ok) { + throw new Error(`HTTP error! status: ${res.status}`); + } + + const data = await res.json(); + + const results = + data.images?.map((item: any) => ({ + title: item.title, + imageUrl: item.imageUrl, + imageWidth: item.imageWidth, + imageHeight: item.imageHeight, + })) ?? []; + return JSON.stringify(results); + } catch (error) { + throw new Error(`Error in SerperImagesSearch: ${error}`); + } + }; + + return new Tool(paramsSchema, name, description, execute).tool; +} + +export { createSerperImagesSearch };