mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-11-13 12:43:42 +08:00
feat: #313
This commit is contained in:
85
app/api/langchain-tools/google_custom_search.ts
Normal file
85
app/api/langchain-tools/google_custom_search.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import { getEnvironmentVariable } from "@langchain/core/utils/env";
|
||||
import { Tool } from "@langchain/core/tools";
|
||||
|
||||
const API_PROXY_PREFIX = process.env.GOOGLE_PLUGIN_API_PROXY_PREFIX ?? "";
|
||||
|
||||
/**
|
||||
* Interface for parameters required by GoogleCustomSearch class.
|
||||
*/
|
||||
export interface GoogleCustomSearchParams {
|
||||
apiKey?: string;
|
||||
googleCSEId?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class that uses the Google Search API to perform custom searches.
|
||||
* Requires environment variables `GOOGLE_API_KEY` and `GOOGLE_CSE_ID` to
|
||||
* be set.
|
||||
*/
|
||||
export class GoogleCustomSearch extends Tool {
|
||||
static lc_name() {
|
||||
return "GoogleCustomSearch";
|
||||
}
|
||||
|
||||
get lc_secrets(): { [key: string]: string } | undefined {
|
||||
return {
|
||||
apiKey: "GOOGLE_API_KEY",
|
||||
};
|
||||
}
|
||||
|
||||
name = "google-custom-search";
|
||||
|
||||
protected apiKey: string;
|
||||
|
||||
protected googleCSEId: string;
|
||||
|
||||
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.";
|
||||
|
||||
constructor(
|
||||
fields: GoogleCustomSearchParams = {
|
||||
apiKey: getEnvironmentVariable("GOOGLE_API_KEY"),
|
||||
googleCSEId: getEnvironmentVariable("GOOGLE_CSE_ID"),
|
||||
},
|
||||
) {
|
||||
super(...arguments);
|
||||
if (!fields.apiKey) {
|
||||
throw new Error(
|
||||
`Google API key not set. You can set it as "GOOGLE_API_KEY" in your environment variables.`,
|
||||
);
|
||||
}
|
||||
if (!fields.googleCSEId) {
|
||||
throw new Error(
|
||||
`Google custom search engine id not set. You can set it as "GOOGLE_CSE_ID" in your environment variables.`,
|
||||
);
|
||||
}
|
||||
this.apiKey = fields.apiKey;
|
||||
this.googleCSEId = fields.googleCSEId;
|
||||
}
|
||||
|
||||
async _call(input: string) {
|
||||
const res = await fetch(
|
||||
`${API_PROXY_PREFIX}https://www.googleapis.com/customsearch/v1?key=${this.apiKey}&cx=${
|
||||
this.googleCSEId
|
||||
}&q=${encodeURIComponent(input)}`,
|
||||
);
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(
|
||||
`Got ${res.status} error from Google custom search: ${res.statusText}`,
|
||||
);
|
||||
}
|
||||
|
||||
const json = await res.json();
|
||||
|
||||
const results =
|
||||
json?.items?.map(
|
||||
(item: { title?: string; link?: string; snippet?: string }) => ({
|
||||
title: item.title,
|
||||
link: item.link,
|
||||
snippet: item.snippet,
|
||||
}),
|
||||
) ?? [];
|
||||
return JSON.stringify(results);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import { Tool } from "@langchain/core/tools";
|
||||
import * as cheerio from "cheerio";
|
||||
import { getRandomUserAgent } from "./ua_tools";
|
||||
|
||||
const API_PROXY_PREFIX = process.env.GOOGLE_PLUGIN_API_PROXY_PREFIX ?? "";
|
||||
|
||||
interface SearchResults {
|
||||
/** The web results of the search. */
|
||||
results: SearchResult[];
|
||||
@@ -31,7 +33,7 @@ async function search(
|
||||
const headers = new Headers();
|
||||
headers.append("User-Agent", getRandomUserAgent());
|
||||
const resp = await fetch(
|
||||
`https://www.google.com/search?nfpr=1&num=${maxResults}&pws=0&q=${encodeURIComponent(
|
||||
`${API_PROXY_PREFIX}https://www.google.com/search?nfpr=1&num=${maxResults}&pws=0&q=${encodeURIComponent(
|
||||
input,
|
||||
)}`,
|
||||
{
|
||||
|
||||
@@ -8,10 +8,6 @@ export {
|
||||
Serper,
|
||||
type SerperParameters,
|
||||
} from "@langchain/community/tools/serper";
|
||||
export {
|
||||
GoogleCustomSearch,
|
||||
type GoogleCustomSearchParams,
|
||||
} from "@langchain/community/tools/google_custom_search";
|
||||
export { AIPluginTool } from "@langchain/community/tools/aiplugin";
|
||||
export {
|
||||
WikipediaQueryRun,
|
||||
|
||||
Reference in New Issue
Block a user