Add SearchApi documentloader, tool & podcast QA example

This commit is contained in:
SebastjanPrachovskij
2023-10-31 16:27:37 +02:00
parent 06552416bb
commit 7bc41939e4
6 changed files with 879 additions and 0 deletions
@@ -0,0 +1,109 @@
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { TextSplitter } from 'langchain/text_splitter'
import { SearchApiLoader } from 'langchain/document_loaders/web/searchapi'
import { getCredentialData, getCredentialParam } from '../../../src'
// Provides access to multiple search engines using the SearchApi.
// For available parameters & engines, refer to: https://www.searchapi.io/docs/google
class SearchAPI_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 = 'SearchApi For Web Search'
this.name = 'searchApi'
this.version = 1.0
this.type = 'Document'
this.icon = 'searchapi.svg'
this.category = 'Document Loaders'
this.description = 'Load data from real-time search results'
this.baseClasses = [this.type]
this.credential = {
label: 'Connect Credential',
name: 'credential',
type: 'credential',
optional: false,
credentialNames: ['searchApi']
}
this.inputs = [
{
label: 'Query',
name: 'query',
type: 'string',
optional: true
},
{
label: 'Custom Parameters',
name: 'customParameters',
type: 'json',
optional: true,
additionalParams: true
},
{
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 customParameters = nodeData.inputs?.customParameters
const metadata = nodeData.inputs?.metadata
// Fetch the API credentials for this node
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const searchApiKey = getCredentialParam('searchApiKey', credentialData, nodeData)
// Check and parse custom parameters (should be JSON or object)
const parsedParameters = typeof customParameters === 'object' ? customParameters : JSON.parse(customParameters || '{}');
// Prepare the configuration for the SearchApiLoader
const loaderConfig = {
q: query,
apiKey: searchApiKey,
...parsedParameters
};
// Initialize the loader with the given configuration
const loader = new SearchApiLoader(loaderConfig);
// Fetch documents, split if a text splitter is provided
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: SearchAPI_DocumentLoaders }
@@ -0,0 +1 @@
<svg id="SvgjsSvg1001" width="75.27131652832031" height="63.92396926879883" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs" viewBox="0 0 75.27131652832031 63.92396926879883"><defs id="SvgjsDefs1002"></defs><rect id="SvgjsRect1008" width="75.27131652832031" height="63.92396926879883" fill="transparent"></rect><g id="SvgjsG1009" transform="matrix(1,0,0,1,-39.50003433227539,-50.53549575805664)"><title>0479_octopus_verti</title><path id="color_1" d="M97.24234,109.8245a2.57759,2.57759,0,0,1-2.57778,2.57778,9.80672,9.80672,0,0,1-9.79558-9.79557V91.58366a2.57779,2.57779,0,0,1,5.15557,0v11.02305a4.64509,4.64509,0,0,0,4.64,4.64A2.57759,2.57759,0,0,1,97.24234,109.8245ZM112.19348,93.223a2.57759,2.57759,0,0,0-2.57778,2.57779,4.64,4.64,0,1,1-9.28,0V73.73554a23.2,23.2,0,1,0-46.40009,0V95.80076a4.64,4.64,0,1,1-9.28,0,2.57779,2.57779,0,1,0-5.15557,0,9.79558,9.79558,0,0,0,19.59115,0V73.73554a18.04449,18.04449,0,0,1,36.089,0V95.80076a9.79558,9.79558,0,0,0,19.59115,0A2.57759,2.57759,0,0,0,112.19348,93.223ZM77.13563,91.78a2.57759,2.57759,0,0,0-2.57778,2.57778v17.52893a2.57779,2.57779,0,0,0,5.15557,0V94.3578A2.57759,2.57759,0,0,0,77.13563,91.78ZM66.8245,89.00588a2.57759,2.57759,0,0,0-2.57778,2.57778v11.02305a4.64509,4.64509,0,0,1-4.64,4.64,2.57778,2.57778,0,1,0,0,5.15556,9.80671,9.80671,0,0,0,9.79557-9.79557V91.58366A2.57759,2.57759,0,0,0,66.8245,89.00588ZM69.918,70.12639a3.6089,3.6089,0,1,0,3.6089,3.6089A3.60891,3.60891,0,0,0,69.918,70.12639Zm18.04448,3.6089a3.6089,3.6089,0,1,0-3.60889,3.60889A3.60891,3.60891,0,0,0,87.96251,73.73529Z" fill="#3730a3"></path></g></svg>

After

Width:  |  Height:  |  Size: 1.6 KiB