Skip to content

Commit

Permalink
✨ add serpApi image search
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannLai committed Jan 30, 2024
1 parent a453fd8 commit 2df1f5a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 53 deletions.
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.
- 🏞️ serperImagesSearch: Use SerpAPI to search images. Input should be a search query.
- 🏞️ SerpApiImageSearch: 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
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ export { createCalculator } from './tools/calculator';
export { createGoogleCustomSearch } from './tools/googleCustomSearch';
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
43 changes: 43 additions & 0 deletions tools/serpApiImageSearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Tool } from './tool';
import { z } from 'zod';

function createSerpApiImageSearch({ apiKey }: { apiKey: string }) {
if (!apiKey) {
throw new Error('SerpApi key must be set.');
}

const paramsSchema = z.object({
input: z.string(),
});
const name = 'serpApiImageSearch';
const description = 'Allows you to scrape results from the Google Images page. Input should be a search query. Outputs a JSON array of results.';

const execute = async ({ input }: z.infer<typeof paramsSchema>) => {
try {
const res = await fetch(
`https://serpapi.com/search?api_key=${apiKey}&engine=google_images&q=${encodeURIComponent(input)}&google_domain=google.com`
);

if (!res.ok) {
throw new Error(`HTTP error! status: ${res.status}`);
}

const data = await res.json();

const results =
data.images_results?.map((item: any) => ({
title: item.title,
original: item.original,
original_width: item.original_width,
original_height: item.original_height,
})) ?? [];
return JSON.stringify(results);
} catch (error) {
throw new Error(`Error in SerpApiCustomSearch: ${error}`);
}
};

return new Tool(paramsSchema, name, description, execute).tool;
}

export { createSerpApiImageSearch };
51 changes: 0 additions & 51 deletions tools/serperImagesSearch.ts

This file was deleted.

0 comments on commit 2df1f5a

Please sign in to comment.