implemented serpapi loader integration and added its corresponding template

This commit is contained in:
Hamzah Abdulfattah
2023-08-19 17:23:19 +01:00
parent 260b13bb99
commit 3b0c5b0c0d
3 changed files with 678 additions and 0 deletions
@@ -0,0 +1,82 @@
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface';
import { TextSplitter } from 'langchain/text_splitter';
import { SerpAPILoader } from 'langchain/document_loaders/web/serpapi';import { getCredentialData, getCredentialParam } from '../../../src';
class SerpAPI_DocumentLoaders implements INode {
label: string;
name: string;
version: number;
description: string;
type: string;
icon: string;
category: string;
baseClasses: string[];
credential: INodeParams;
inputs: INodeParams[];
constructor() {
this.label = 'SerpApi For Web Search';
this.name = 'serpApi';
this.version = 1.0;
this.type = 'Document';
this.icon = 'serp.png';
this.category = 'Document Loaders';
this.description = 'Load and process data from web search results';
this.baseClasses = [this.type];
this.credential = {
label: 'Connect Credential',
name: 'credential',
type: 'credential',
optional: false,
credentialNames: ['serpApi']
};
this.inputs = [
{
label: 'Query',
name: 'query',
type: 'string'
},
{
label: 'Text Splitter',
name: 'textSplitter',
type: 'TextSplitter',
optional: true
},
{
label: 'Metadata',
name: 'metadata',
type: 'json',
optional: true,
additionalParams: true
}
];
}
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
const textSplitter = nodeData.inputs?.textSplitter as TextSplitter;
const query = nodeData.inputs?.query as string;
const metadata = nodeData.inputs?.metadata;
const credentialData = await getCredentialData(nodeData.credential ?? '', options);
const serpApiKey = getCredentialParam('serpApiKey', credentialData, nodeData);
const loader = new SerpAPILoader({ q: query, apiKey: serpApiKey });
const docs = textSplitter ? await loader.loadAndSplit() : await loader.load();
if (metadata) {
const parsedMetadata = typeof metadata === 'object' ? metadata : JSON.parse(metadata);
return docs.map((doc) => {
return {
...doc,
metadata: {
...doc.metadata,
...parsedMetadata
}
};
});
}
return docs;
}
}
module.exports = { nodeClass: SerpAPI_DocumentLoaders };
Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB