@n8n/n8n-nodes-langchain
Version:
183 lines • 6.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToolSearXng = void 0;
const tools_1 = require("@langchain/core/tools");
const n8n_workflow_1 = require("n8n-workflow");
const ai_utilities_1 = require("@n8n/ai-utilities");
function stripHtml(html) {
let content = html;
let previous;
do {
previous = content;
content = content.replace(/<[^>]+>/gi, '');
} while (content !== previous);
return content;
}
function formatResponse(data, numResults) {
const results = data.results ?? [];
if (results.length) {
return results
.slice(0, numResults)
.map((result) => JSON.stringify({
title: result.title ?? '',
link: result.url ?? '',
snippet: result.content ?? '',
}))
.join(',');
}
if (data.answers?.length)
return data.answers[0];
if (data.infoboxes?.length)
return stripHtml(data.infoboxes[0]?.content ?? '');
if (data.suggestions?.length)
return `Suggestions: ${data.suggestions.join(', ')}`;
return 'No good results found.';
}
async function getTool(ctx, itemIndex) {
const credentials = await ctx.getCredentials('searXngApi');
const options = ctx.getNodeParameter('options', itemIndex);
const apiBase = credentials.apiUrl.replace(/\/+$/, '');
const numResults = options.numResults ?? 10;
const qs = {
format: 'json',
pageno: options.pageNumber ?? 1,
safesearch: options.safesearch ?? 0,
};
if (options.language)
qs.language = options.language;
return new tools_1.DynamicTool({
name: 'searxng-search',
description: 'A meta search engine. Useful for when you need to answer questions about current events. Input should be a search query. Output is a JSON array of the query results',
func: async (query) => {
const response = (await ctx.helpers.httpRequest({
method: 'POST',
url: `${apiBase}/search`,
qs: { q: query, ...qs },
headers: { Accept: 'application/json' },
json: true,
timeout: 15_000,
}));
return formatResponse(response, numResults);
},
});
}
class ToolSearXng {
constructor() {
this.description = {
displayName: 'SearXNG',
name: 'toolSearXng',
icon: 'file:searXng.svg',
group: ['transform'],
version: 1,
description: 'Search in SearXNG',
defaults: {
name: 'SearXNG',
},
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.toolsearxng',
},
],
},
},
inputs: [],
outputs: [n8n_workflow_1.NodeConnectionTypes.AiTool],
outputNames: ['Tool'],
credentials: [
{
name: 'searXngApi',
required: true,
},
],
properties: [
(0, ai_utilities_1.getConnectionHintNoticeField)([n8n_workflow_1.NodeConnectionTypes.AiAgent]),
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
default: {},
options: [
{
displayName: 'Number of Results',
name: 'numResults',
type: 'number',
default: 10,
},
{
displayName: 'Search Page Number',
name: 'pageNumber',
type: 'number',
default: 1,
},
{
displayName: 'Language',
name: 'language',
type: 'string',
default: 'en',
description: 'Defines the language to use. It\'s a two-letter language code. (e.g., `en` for English, `es` for Spanish, or `fr` for French). Head to <a href="https://docs.searxng.org/user/search-syntax.html#select-language">SearXNG search syntax page</a> for more info.',
},
{
displayName: 'Safe Search',
name: 'safesearch',
type: 'options',
options: [
{
name: 'None',
value: 0,
},
{
name: 'Moderate',
value: 1,
},
{
name: 'Strict',
value: 2,
},
],
default: 0,
description: 'Filter search results of engines which support safe search',
},
],
},
],
};
}
async supplyData(itemIndex) {
return {
response: (0, ai_utilities_1.logWrapper)(await getTool(this, itemIndex), this),
};
}
async execute() {
const result = [];
const input = this.getInputData();
for (let i = 0; i < input.length; i++) {
const item = input[i];
const tool = await getTool(this, i);
const query = item.json.input;
if (typeof query !== 'string' || query === '') {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Input item is missing', {
itemIndex: i,
});
}
result.push({
json: {
response: await tool.invoke(query),
},
pairedItem: {
item: i,
},
});
}
return [result];
}
}
exports.ToolSearXng = ToolSearXng;
//# sourceMappingURL=ToolSearXng.node.js.map