Skip to content

Commit

Permalink
✨ add serper image search
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannLai committed Jan 30, 2024
1 parent 2df1f5a commit 5f84620
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
51 changes: 51 additions & 0 deletions tools/serperImagesSearch.ts
Original file line number Diff line number Diff line change
@@ -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<typeof paramsSchema>) => {
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 };

0 comments on commit 5f84620

Please sign in to comment.