UNPKG

n8n-nodes-gemini-search

Version:

n8n nodes to interact with Google Gemini API for search and content generation

73 lines 2.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getModels = exports.geminiRequest = void 0; const n8n_workflow_1 = require("n8n-workflow"); async function geminiRequest(model, body) { var _a, _b; const credentials = await this.getCredentials('geminiSearchApi'); if (!credentials) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No credentials provided!'); } const apiKey = credentials.apiKey; const host = credentials.host || 'https://generativelanguage.googleapis.com'; const options = { method: 'POST', uri: `${host}/v1beta/models/${model}:generateContent`, qs: { key: apiKey, }, headers: { 'Content-Type': 'application/json', }, body, json: true, }; try { if (!this.helpers) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Helpers are not available!'); } const response = await ((_b = (_a = this.helpers).request) === null || _b === void 0 ? void 0 : _b.call(_a, options)); if (!response) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No response from API request'); } return response; } catch (error) { throw new n8n_workflow_1.NodeApiError(this.getNode(), error); } } exports.geminiRequest = geminiRequest; async function getModels() { var _a, _b; const credentials = await this.getCredentials('geminiSearchApi'); if (!credentials) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No credentials provided!'); } const apiKey = credentials.apiKey; const host = credentials.host || 'https://generativelanguage.googleapis.com'; const options = { method: 'GET', uri: `${host}/v1beta/models`, qs: { key: apiKey, }, json: true, }; try { if (!this.helpers) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Helpers are not available!'); } const response = await ((_b = (_a = this.helpers).request) === null || _b === void 0 ? void 0 : _b.call(_a, options)); if (!response || !response.models) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No response or models from API request'); } return response.models .filter((model) => { var _a; return (_a = model.supportedGenerationMethods) === null || _a === void 0 ? void 0 : _a.includes('generateContent'); }) .map((model) => model.name.replace('models/', '')); } catch (error) { throw new n8n_workflow_1.NodeApiError(this.getNode(), error); } } exports.getModels = getModels; //# sourceMappingURL=GenericFunctions.js.map