Skip to content

Commit

Permalink
refactor: rename GoogleTrendsAPI to SERPGoogleTrendsTool. Also Add ne…
Browse files Browse the repository at this point in the history
…w interfaces for lint warnings, and minor documentation updates
  • Loading branch information
zhaoroger committed Dec 4, 2024
1 parent 510433b commit f314c19
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 19 deletions.
3 changes: 3 additions & 0 deletions docs/core_docs/docs/integrations/tools/google_trends.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ This can be useful for understanding trending topics, regional search interest,

For API details see [here](https://serpapi.com/google-trends-api)

SerpApi caches queries, so the first query will be slower while subsequent identical queries will be fast.
Occasionally, related queries will not work while interest over time will be fine. You can check your query [here](https://serpapi.com/playground?engine=google_trends&q=monster&data_type=RELATED_QUERIES).

## Setup

To use this tool, you will need to configure access to the Google Trends API from SerpApi.
Expand Down
6 changes: 3 additions & 3 deletions examples/src/tools/google_trends.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { GoogleTrendsAPI } from "@langchain/community/tools/google_trends";
import { SERPGoogleTrendsTool } from "@langchain/community/tools/google_trends";

export async function run() {
const tool = new GoogleTrendsAPI();
const tool = new SERPGoogleTrendsTool();

const res = await tool._call("Monster");
const res = await tool.invoke("Monster");

console.log(res);
}
38 changes: 29 additions & 9 deletions libs/langchain-community/src/tools/google_trends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,33 @@ import { getEnvironmentVariable } from "@langchain/core/utils/env";
import { Tool } from "@langchain/core/tools";

/**
* Interface for parameters required by GoogleTrendsAPI class.
* Interfaces for the response from the SerpApi Google Trends API.
*/
export interface GoogleTrendsAPIParams {
interface TimelineValue {
query: string;
value: string;
extracted_value: number;
}

interface TimelineData {
date: string;
timestamp: string;
values: TimelineValue[];
}

/**
* Interface for parameters required by SERPGoogleTrendsTool class.
*/
export interface SERPGoogleTrendsToolParams {
apiKey?: string;
}

/**
* Tool that queries the Google Trends API. Uses default interest over time.
*/
export class GoogleTrendsAPI extends Tool {
export class SERPGoogleTrendsTool extends Tool {
static lc_name() {
return "GoogleTrendsAPI";
return "SERPGoogleTrendsTool";
}

get lc_secrets(): { [key: string]: string } | undefined {
Expand All @@ -29,7 +44,7 @@ export class GoogleTrendsAPI extends Tool {
description = `A wrapper around Google Trends API. Useful for analyzing and retrieving trending search data based on keywords,
categories, or regions. Input should be a search query or specific parameters for trends analysis.`;

constructor(fields?: GoogleTrendsAPIParams) {
constructor(fields?: SERPGoogleTrendsToolParams) {
super(...arguments);
const apiKey = fields?.apiKey ?? getEnvironmentVariable("SERPAPI_API_KEY");
if (apiKey === undefined) {
Expand All @@ -45,6 +60,9 @@ export class GoogleTrendsAPI extends Tool {
* Related queries only accepts one at a time, and multiple
* queries at once on interest over time (default) is effectively the same as
* each query one by one.
*
* SerpApi caches queries, so the first time will be slower
* and subsequent identical queries will be very fast.
*/
if (query.split(",").length > 1) {
throw new Error("Please do one query at a time");
Expand Down Expand Up @@ -80,7 +98,7 @@ export class GoogleTrendsAPI extends Tool {
const startDate = totalResults[0].date.split(" ");
const endDate = totalResults[totalResults.length - 1].date.split(" ");
const values = totalResults.map(
(result: any) => result.values[0].extracted_value
(result: TimelineData) => result.values[0].extracted_value
);
const minValue = Math.min(...values);
const maxValue = Math.max(...values);
Expand Down Expand Up @@ -109,18 +127,20 @@ export class GoogleTrendsAPI extends Tool {
let rising = [];
let top = [];
if (!relatedRes.ok) {
// Sometimes related queries from SerpAPI will fail, but interest over time will be fine
console.error(
`Error fetching related queries from SerpAPI: ${relatedRes.statusText}`
);
} else {
const relatedDict = await relatedRes.json();
rising =
relatedDict.related_queries?.rising?.map(
(result: any) => result.query
(result: { query: string }) => result.query
) ?? [];
top =
relatedDict.related_queries?.top?.map((result: any) => result.query) ??
[];
relatedDict.related_queries?.top?.map(
(result: { query: string }) => result.query
) ?? [];
}

const doc = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { expect, describe } from "@jest/globals";
import { GoogleTrendsAPI } from "../google_trends.js";
import { SERPGoogleTrendsTool } from "../google_trends.js";

describe("GoogleTrendsAPI", () => {
describe("SERPGoogleTrendsTool", () => {
test("should be setup with correct parameters", async () => {
const instance = new GoogleTrendsAPI();
const instance = new SERPGoogleTrendsTool();
expect(instance.name).toBe("google_trends");
});

test("GoogleTrendsAPI returns expected result for valid query", async () => {
const tool = new GoogleTrendsAPI();
test("SERPGoogleTrendsTool returns expected result for valid query", async () => {
const tool = new SERPGoogleTrendsTool();

const result = await tool._call("Coffee");

Expand All @@ -24,8 +24,8 @@ describe("GoogleTrendsAPI", () => {
expect(result).toContain("Top Related Queries:");
});

test("GoogleTrendsAPI returns 'No good Trend Result was found' for a non-existent query", async () => {
const tool = new GoogleTrendsAPI();
test("SERPGoogleTrendsTool returns 'No good Trend Result was found' for a non-existent query", async () => {
const tool = new SERPGoogleTrendsTool();

const result = await tool._call("earghajgpajrpgjaprgag");

Expand Down

0 comments on commit f314c19

Please sign in to comment.