Download app/tools/serp-api.ts from matthoffner/chat-with-internet: direct link, hf CLI and curl.
- Browser
- Download file 963 Bytes
-
https://huggingface.co/spaces/matthoffner/chat-with-internet/resolve/6480c62d981c11698be0a792ce307050f3c7e7a2/app/tools/serp-api.ts
- Command line
-
hf download hf://spaces/matthoffner/chat-with-internet@6480c62d981c11698be0a792ce307050f3c7e7a2/app/tools/serp-api.ts
-
curl -L -o serp-api.ts https://huggingface.co/spaces/matthoffner/chat-with-internet/resolve/6480c62d981c11698be0a792ce307050f3c7e7a2/app/tools/serp-api.ts
963 Bytes
| import { Tool } from 'openai-function-calling-tools'; | |
| import { z } from 'zod'; | |
| import { getJson } from 'serpapi'; | |
| function createSerpApi({ apiKey }: { apiKey: string }) { | |
| const paramsSchema = z.object({ | |
| input: z.string(), | |
| }); | |
| const name = 'serpApi'; | |
| const description = 'A custom search engine. Useful for when you need to answer questions about current events. Input should be a search query. Outputs a JSON array of results.'; | |
| const execute = async ({ input }: z.infer<typeof paramsSchema>) => { | |
| try { | |
| const response = await getJson({ | |
| engine: "google", | |
| api_key: apiKey, | |
| q: input, | |
| location: "Seattle, Washington", | |
| }); | |
| console.log(response); | |
| return JSON.stringify(response); | |
| } catch (error) { | |
| throw new Error(`Error in serpApi: ${error}`); | |
| } | |
| }; | |
| return new Tool(paramsSchema, name, description, execute).tool; | |
| } | |
| export { createSerpApi }; |