Skip to content

Commit

Permalink
Serp base param (langchain-ai#1011)
Browse files Browse the repository at this point in the history
* Add base url to serpapi and test

* don't export url params

* Lint

---------

Co-authored-by: Nuno Campos <[email protected]>
  • Loading branch information
Quafadas and nfcampos authored Apr 27, 2023
1 parent 93ea6cb commit d699ddb
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 17 deletions.
43 changes: 26 additions & 17 deletions langchain/src/tools/serpapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,17 +283,6 @@ type UrlParameters = Record<
string | number | boolean | undefined | null
>;

function buildUrl<P extends UrlParameters>(
path: string,
parameters: P
): string {
const nonUndefinedParams: [string, string][] = Object.entries(parameters)
.filter(([_, value]) => value !== undefined)
.map(([key, value]) => [key, `${value}`]);
const searchParams = new URLSearchParams(nonUndefinedParams);
return `https://serpapi.com/${path}?${searchParams}`;
}

/**
* Wrapper around SerpAPI.
*
Expand All @@ -304,12 +293,15 @@ export class SerpAPI extends Tool {

protected params: Partial<SerpAPIParameters>;

protected baseUrl: string;

constructor(
apiKey: string | undefined = typeof process !== "undefined"
? // eslint-disable-next-line no-process-env
process.env?.SERPAPI_API_KEY
: undefined,
params: Partial<SerpAPIParameters> = {}
params: Partial<SerpAPIParameters> = {},
baseUrl = "https://serpapi.com"
) {
super();

Expand All @@ -321,19 +313,36 @@ export class SerpAPI extends Tool {

this.key = apiKey;
this.params = params;
this.baseUrl = baseUrl;
}

name = "search";

protected buildUrl<P extends UrlParameters>(
path: string,
parameters: P,
baseUrl: string
): string {
const nonUndefinedParams: [string, string][] = Object.entries(parameters)
.filter(([_, value]) => value !== undefined)
.map(([key, value]) => [key, `${value}`]);
const searchParams = new URLSearchParams(nonUndefinedParams);
return `${baseUrl}/${path}?${searchParams}`;
}

/** @ignore */
async _call(input: string) {
const { timeout, ...params } = this.params;
const resp = await fetch(
buildUrl("search", {
...params,
api_key: this.key,
q: input,
}),
this.buildUrl(
"search",
{
...params,
api_key: this.key,
q: input,
},
this.baseUrl
),
{
signal: timeout ? AbortSignal.timeout(timeout) : undefined,
}
Expand Down
37 changes: 37 additions & 0 deletions langchain/src/tools/tests/serpapi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { test, expect } from "@jest/globals";
import { SerpAPI } from "../../tools/serpapi.js";

describe("serp api test suite", () => {
class SerpApiUrlTester extends SerpAPI {
testThisUrl(): string {
return this.buildUrl("search", this.params, this.baseUrl);
}
}

test("Test default url", async () => {
const serpApi = new SerpApiUrlTester(
"Not a real key but constructor error if not set",
{
hl: "en",
gl: "us",
}
);
expect(serpApi.testThisUrl()).toEqual(
"https://serpapi.com/search?hl=en&gl=us"
);
});

test("Test override url", async () => {
const serpApiProxied = new SerpApiUrlTester(
"Not a real key but constructor error if not set",
{
gl: "us",
},
"https://totallyProxied.com"
);

expect(
serpApiProxied.testThisUrl() === "https://totallyProxied.com/search?gl=us"
);
});
});

0 comments on commit d699ddb

Please sign in to comment.