Skip to content

Commit

Permalink
Add SearchApi tool
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastjanPrachovskij committed Sep 17, 2024
1 parent 66ddb4a commit c0c716a
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 0 deletions.
42 changes: 42 additions & 0 deletions packages/@n8n/nodes-langchain/credentials/SearchApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';

export class SearchApi implements ICredentialType {
name = 'searchApi';

displayName = 'SearchApi';

documentationUrl = 'searchapi';

properties: INodeProperties[] = [
{
displayName: 'API Key',
name: 'apiKey',
type: 'string',
typeOptions: { password: true },
required: true,
default: '',
},
];

authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
Authorization: '=Bearer {{$credentials.apiKey}}',
"X-SearchApi-Source": "N8n"
},
},
};

test: ICredentialTestRequest = {
request: {
baseURL: 'https://www.searchapi.io',
url: '/api/v1/search?engine=google&q=n8n',
},
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
import {
NodeConnectionType,
type IExecuteFunctions,
type INodeType,
type INodeTypeDescription,
type SupplyData,
} from 'n8n-workflow';
import { SearchApi } from '@langchain/community/tools/searchapi';
import { logWrapper } from '../../../utils/logWrapper';
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';

export class ToolSearchApi implements INodeType {
description: INodeTypeDescription = {
displayName: 'SearchApi (Google Search)',
name: 'toolSearchApi',
icon: 'file:searchApi.svg',
group: ['transform'],
version: 1,
description: 'Search in Google using SearchApi',
defaults: {
name: 'SearchApi',
},
codex: {
categories: ['AI'],
subcategories: {
AI: ['Tools'],
Tools: ['Other Tools'],
},
resources: {
primaryDocumentation: [
{
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.toolsearchapi/',
},
],
},
},
// eslint-disable-next-line n8n-nodes-base/node-class-description-inputs-wrong-regular-node
inputs: [],
// eslint-disable-next-line n8n-nodes-base/node-class-description-outputs-wrong
outputs: [NodeConnectionType.AiTool],
outputNames: ['Tool'],
credentials: [
{
name: 'searchApi',
required: true,
},
],
properties: [
getConnectionHintNoticeField([NodeConnectionType.AiAgent]),
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
default: {},
options: [
{
displayName: 'Engine',
name: 'engine',
type: 'string',
default: 'google',
description:
'Defines the engine of the search. Supported engines are google, bing, baidu, google_news, youtube_transcripts, and more. Check the full list of supported <a href="https://www.searchapi.io/" engines</a> in the documentation.',
},
{
displayName: 'Country',
name: 'gl',
type: 'string',
default: 'us',
description:
'Defines the country of the search. Check the full list of supported <a href="https://www.searchapi.io/docs/parameters/google/gl" Google countries</a>.',
displayOptions: {
hide: {
engine: ['youtube_transcripts'],
},
},
},
{
displayName: 'Device',
name: 'device',
type: 'options',
options: [
{
name: 'Desktop',
value: 'desktop',
},
{
name: 'Mobile',
value: 'mobile',
},
{
name: 'Tablet',
value: 'tablet',
},
],
default: 'desktop',
description: 'Device to use to get the results',
displayOptions: {
hide: {
engine: ['youtube_transcripts'],
},
},
},
{
displayName: 'Google Domain',
name: 'google_domain',
type: 'string',
default: 'google.com',
description:
'Defines the Google domain of the search. Check the full list of supported <a href="https://www.searchapi.io/docs/parameters/google/domain" Google domains</a>.',
displayOptions: {
hide: {
engine: ['youtube_transcripts'],
},
},
},
{
displayName: 'Language',
name: 'hl',
type: 'string',
default: 'en',
description:
'Defines the interface language of the search. Check the full list of supported <a href="https://www.searchapi.io/docs/parameters/google/hl" Google languages</a>.',
displayOptions: {
hide: {
engine: ['youtube_transcripts'],
},
},
},
{
displayName: 'Video ID (For YouTube Transcripts)',
name: 'video_id',
type: 'string',
default: '',
description:
'Specifies the ID of the video for which you want to retrieve transcripts. Only applicable for YouTube Transcripts engine.',
displayOptions: {
show: {
engine: ['youtube_transcripts'],
},
},
},
{
displayName: 'Language (For YouTube Transcripts)',
name: 'lang',
type: 'string',
default: 'en',
description:
'Specifies the language for transcripts. Only applicable for YouTube Transcripts engine.',
displayOptions: {
show: {
engine: ['youtube_transcripts'],
},
},
},
],
},
],
};

async supplyData(this: IExecuteFunctions, itemIndex: number): Promise<SupplyData> {
const credentials = await this.getCredentials('searchApi');

const options = this.getNodeParameter('options', itemIndex) as object;

return {
response: logWrapper(new SearchApi(credentials.apiKey as string, options), this),
};
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions packages/@n8n/nodes-langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"dist/credentials/OllamaApi.credentials.js",
"dist/credentials/PineconeApi.credentials.js",
"dist/credentials/QdrantApi.credentials.js",
"dist/credentials/SearchApi.credentials.js",
"dist/credentials/SerpApi.credentials.js",
"dist/credentials/WolframAlphaApi.credentials.js",
"dist/credentials/XataApi.credentials.js",
Expand Down Expand Up @@ -98,6 +99,7 @@
"dist/nodes/tools/ToolCalculator/ToolCalculator.node.js",
"dist/nodes/tools/ToolCode/ToolCode.node.js",
"dist/nodes/tools/ToolHttpRequest/ToolHttpRequest.node.js",
"dist/nodes/tools/ToolSearchApi/ToolSearchApi.node.js",
"dist/nodes/tools/ToolSerpApi/ToolSerpApi.node.js",
"dist/nodes/tools/ToolVectorStore/ToolVectorStore.node.js",
"dist/nodes/tools/ToolWikipedia/ToolWikipedia.node.js",
Expand Down

0 comments on commit c0c716a

Please sign in to comment.