From 6dcc7bb1528b5ed93ebd6296140316d82453b153 Mon Sep 17 00:00:00 2001 From: May Ramati Kroitero <105562192+MayRamati@users.noreply.github.com> Date: Mon, 28 Apr 2025 10:45:05 +0300 Subject: [PATCH] feature: add additional parameters to Tavily API tool (#4353) * feat: add additional parameters to Tavily API tool * update descirption * fix version * lint fix --------- Co-authored-by: Henry Heng --- .../credentials/TavilyApi.credential.ts | 4 +- .../nodes/tools/TavilyAPI/TavilyAPI.ts | 170 +++++++++++++++++- 2 files changed, 168 insertions(+), 6 deletions(-) diff --git a/packages/components/credentials/TavilyApi.credential.ts b/packages/components/credentials/TavilyApi.credential.ts index 161ff4df..32e1380b 100644 --- a/packages/components/credentials/TavilyApi.credential.ts +++ b/packages/components/credentials/TavilyApi.credential.ts @@ -10,8 +10,8 @@ class TavilyApi implements INodeCredential { constructor() { this.label = 'Tavily API' this.name = 'tavilyApi' - this.version = 1.0 - this.description = 'Tavily API is a real-time API to access Google search results' + this.version = 1.1 + this.description = 'Tavily API is a search engine designed for LLMs and AI agents' this.inputs = [ { label: 'Tavily Api Key', diff --git a/packages/components/nodes/tools/TavilyAPI/TavilyAPI.ts b/packages/components/nodes/tools/TavilyAPI/TavilyAPI.ts index 68e44c54..92d07dc3 100644 --- a/packages/components/nodes/tools/TavilyAPI/TavilyAPI.ts +++ b/packages/components/nodes/tools/TavilyAPI/TavilyAPI.ts @@ -13,16 +13,145 @@ class TavilyAPI_Tools implements INode { baseClasses: string[] credential: INodeParams inputs: INodeParams[] + additionalParams: boolean constructor() { this.label = 'Tavily API' this.name = 'tavilyAPI' - this.version = 1.0 + this.version = 1.1 this.type = 'TavilyAPI' this.icon = 'tavily.svg' this.category = 'Tools' - this.description = 'Wrapper around TavilyAPI - a real-time API to access Google search results' - this.inputs = [] + this.description = 'Wrapper around TavilyAPI - A specialized search engine designed for LLMs and AI agents' + this.inputs = [ + { + label: 'Query', + name: 'query', + type: 'string', + optional: false, + description: 'The search query to execute with Tavily', + additionalParams: true + }, + { + label: 'Topic', + name: 'topic', + type: 'options', + options: [ + { label: 'General', name: 'general' }, + { label: 'News', name: 'news' } + ], + default: 'general', + description: 'The category of the search. News for real-time updates, general for broader searches', + additionalParams: true, + optional: true + }, + { + label: 'Search Depth', + name: 'searchDepth', + type: 'options', + options: [ + { label: 'Basic', name: 'basic' }, + { label: 'Advanced', name: 'advanced' } + ], + default: 'basic', + description: 'The depth of the search. Advanced costs 2 API Credits, basic costs 1', + additionalParams: true, + optional: true + }, + { + label: 'Chunks Per Source', + name: 'chunksPerSource', + type: 'number', + default: 3, + description: 'Number of content chunks per source (1-3). Only for advanced search', + additionalParams: true, + optional: true + }, + { + label: 'Max Results', + name: 'maxResults', + type: 'number', + default: 5, + additionalParams: true, + description: 'Maximum number of search results (0-20)', + optional: true + }, + { + label: 'Time Range', + name: 'timeRange', + type: 'options', + options: [ + { label: 'Day', name: 'day' }, + { label: 'Week', name: 'week' }, + { label: 'Month', name: 'month' }, + { label: 'Year', name: 'year' } + ], + optional: true, + additionalParams: true, + description: 'Time range to filter results' + }, + { + label: 'Days', + name: 'days', + type: 'number', + default: 7, + additionalParams: true, + description: 'Number of days back from current date (only for news topic)', + optional: true + }, + { + label: 'Include Answer', + name: 'includeAnswer', + type: 'boolean', + default: false, + description: 'Include an LLM-generated answer to the query', + additionalParams: true, + optional: true + }, + { + label: 'Include Raw Content', + name: 'includeRawContent', + type: 'boolean', + default: false, + description: 'Include cleaned and parsed HTML content of each result', + additionalParams: true, + optional: true + }, + { + label: 'Include Images', + name: 'includeImages', + type: 'boolean', + default: false, + description: 'Include image search results', + additionalParams: true, + optional: true + }, + { + label: 'Include Image Descriptions', + name: 'includeImageDescriptions', + type: 'boolean', + default: false, + description: 'Include descriptive text for each image', + additionalParams: true, + optional: true + }, + { + label: 'Include Domains', + name: 'includeDomains', + type: 'string', + optional: true, + description: 'Comma-separated list of domains to include in results', + additionalParams: true + }, + { + label: 'Exclude Domains', + name: 'excludeDomains', + type: 'string', + optional: true, + description: 'Comma-separated list of domains to exclude from results', + additionalParams: true + } + ] this.credential = { label: 'Connect Credential', name: 'credential', @@ -35,7 +164,40 @@ class TavilyAPI_Tools implements INode { async init(nodeData: INodeData, _: string, options: ICommonObject): Promise { const credentialData = await getCredentialData(nodeData.credential ?? '', options) const tavilyApiKey = getCredentialParam('tavilyApiKey', credentialData, nodeData) - return new TavilySearchResults({ apiKey: tavilyApiKey }) + + const query = nodeData.inputs?.query as string + const topic = nodeData.inputs?.topic as string + const searchDepth = nodeData.inputs?.searchDepth as string + const chunksPerSource = nodeData.inputs?.chunksPerSource as number + const maxResults = nodeData.inputs?.maxResults as number + const timeRange = nodeData.inputs?.timeRange as string + const days = nodeData.inputs?.days as number + const includeAnswer = nodeData.inputs?.includeAnswer as boolean + const includeRawContent = nodeData.inputs?.includeRawContent as boolean + const includeImages = nodeData.inputs?.includeImages as boolean + const includeImageDescriptions = nodeData.inputs?.includeImageDescriptions as boolean + const includeDomains = nodeData.inputs?.includeDomains as string + const excludeDomains = nodeData.inputs?.excludeDomains as string + + const config: any = { + apiKey: tavilyApiKey, + query, + topic, + searchDepth, + maxResults, + includeAnswer, + includeRawContent, + includeImages, + includeImageDescriptions + } + + if (chunksPerSource) config.chunksPerSource = chunksPerSource + if (timeRange) config.timeRange = timeRange + if (days) config.days = days + if (includeDomains) config.includeDomains = includeDomains.split(',').map((d) => d.trim()) + if (excludeDomains) config.excludeDomains = excludeDomains.split(',').map((d) => d.trim()) + + return new TavilySearchResults(config) } }