UNPKG

@n8n/n8n-nodes-langchain

Version:

![Banner image](https://user-images.githubusercontent.com/10284570/173569848-c624317f-42b1-45a6-ab09-f0ea3c247648.png)

215 lines 9.03 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatInputMessages = formatInputMessages; exports.createRequest = createRequest; const get_1 = __importDefault(require("lodash/get")); const isObject_1 = __importDefault(require("lodash/isObject")); const n8n_workflow_1 = require("n8n-workflow"); const binary_data_1 = require("../../../../helpers/binary-data"); const toArray = (str) => str.split(',').map((e) => e.trim()); const removeEmptyProperties = (rest) => { return Object.keys(rest) .filter((k) => rest[k] !== '' && rest[k] !== undefined && !((0, isObject_1.default)(rest[k]) && (0, n8n_workflow_1.isObjectEmpty)(rest[k]))) .reduce((a, k) => ({ ...a, [k]: rest[k] }), {}); }; async function formatInputMessages(i, messages) { return await Promise.all(messages.map(async (message) => { const role = message.role; let content = []; if (message.type === 'text' || !message.type) { content = [{ type: 'input_text', text: message.content }]; } else if (message.type === 'image') { const detail = message.imageDetail || 'auto'; if (message.imageType === 'base64') { const { fileContent, contentType } = await (0, binary_data_1.getBinaryDataFile)(this, i, message.binaryPropertyName); const buffer = await this.helpers.binaryToBuffer(fileContent); content = [ { type: 'input_image', detail, image_url: `data:${contentType};base64,${buffer.toString('base64')}`, }, ]; } else { content = [ { type: 'input_image', detail, ...(message.imageType === 'url' && { image_url: message.imageUrl }), ...(message.imageType === 'fileId' && { file_id: message.fileId }), }, ]; } } else if (message.type === 'file') { if (message.fileType === 'base64') { const { fileContent, contentType } = await (0, binary_data_1.getBinaryDataFile)(this, i, message.binaryPropertyName); const buffer = await this.helpers.binaryToBuffer(fileContent); content = [ { type: 'input_file', filename: message.fileName, file_data: `data:${contentType};base64,${buffer.toString('base64')}`, }, ]; } else { content = [ { type: 'input_file', ...(message.fileType === 'url' && { file_url: message.fileUrl }), ...(message.fileType === 'fileId' && { file_id: message.fileId }), }, ]; } } return { role, content }; })); } async function createRequest(i, { model, messages, options, builtInTools, tools }) { const body = { model, input: await formatInputMessages.call(this, i, messages), parallel_tool_calls: (0, get_1.default)(options, 'parallelToolCalls', true), store: (0, get_1.default)(options, 'store', true), instructions: options.instructions, max_output_tokens: options.maxTokens, previous_response_id: options.previousResponseId, prompt_cache_key: options.promptCacheKey, safety_identifier: options.safetyIdentifier, service_tier: options.serviceTier, temperature: options.temperature, top_p: options.topP, top_logprobs: options.topLogprobs, tools, max_tool_calls: options.maxToolCalls, background: (0, get_1.default)(options, 'backgroundMode.values.enabled', false), }; if (options.truncation !== undefined) { body.truncation = !!options.truncation ? 'auto' : 'disabled'; } if (options.conversationId) { body.conversation = options.conversationId; } if (Array.isArray(options.include) && options.include?.length) { body.include = options.include; } if (options.metadata) { body.metadata = (0, n8n_workflow_1.jsonParse)(options.metadata, { errorMessage: 'Failed to parse metadata', }); } if (options.promptConfig) { const prompt = (0, get_1.default)(options, 'promptConfig.promptOptions'); body.prompt = removeEmptyProperties({ id: prompt.promptId, version: prompt.version, ...(prompt.variables && { variables: (0, n8n_workflow_1.jsonParse)(prompt.variables, { errorMessage: 'Failed to parse prompt variables', }), }), }); } if (options.reasoning) { const reasoning = (0, get_1.default)(options, 'reasoning.reasoningOptions'); body.reasoning = removeEmptyProperties({ effort: reasoning.effort, summary: reasoning.summary && reasoning.summary !== 'none' ? reasoning.summary : undefined, }); } if (options.textFormat) { const textOptions = (0, get_1.default)(options, 'textFormat.textOptions'); const textConfig = { verbosity: textOptions.verbosity, }; if (textOptions.type === 'json_schema') { textConfig.format = { type: textOptions.type, name: textOptions.name, schema: (0, n8n_workflow_1.jsonParse)(textOptions.schema, { errorMessage: 'Failed to parse schema', }), }; } else if (textOptions.type === 'json_object') { textConfig.format = { type: textOptions.type, }; body.input = [ { role: 'system', content: [ { type: 'input_text', text: 'You are a helpful assistant designed to output JSON.' }, ], }, ...body.input, ]; } else if (textOptions.type === 'text') { textConfig.format = { type: textOptions.type, }; } if (textConfig.format) { textConfig.format = removeEmptyProperties(textConfig.format); } body.text = textConfig; } if (builtInTools) { const newTools = body.tools ?? []; const webSearchOptions = (0, get_1.default)(builtInTools, 'webSearch'); if (webSearchOptions) { let allowedDomains; const allowedDomainsRaw = (0, get_1.default)(webSearchOptions, 'allowedDomains', ''); if (allowedDomainsRaw) { allowedDomains = toArray(allowedDomainsRaw); } let userLocation; if (webSearchOptions.country || webSearchOptions.city || webSearchOptions.region) { userLocation = { type: 'approximate', country: webSearchOptions.country, city: webSearchOptions.city, region: webSearchOptions.region, }; } newTools.push(removeEmptyProperties({ type: 'web_search', search_context_size: (0, get_1.default)(webSearchOptions, 'searchContextSize', 'medium'), user_location: userLocation, ...(allowedDomains && { filters: { allowed_domains: allowedDomains } }), })); } if (builtInTools.codeInterpreter) { newTools.push({ type: 'code_interpreter', container: { type: 'auto', }, }); } if (builtInTools.fileSearch) { const vectorStoreIds = (0, get_1.default)(builtInTools.fileSearch, 'vectorStoreIds', '[]'); const filters = (0, get_1.default)(builtInTools.fileSearch, 'filters', '{}'); newTools.push(removeEmptyProperties({ type: 'file_search', vector_store_ids: (0, n8n_workflow_1.jsonParse)(vectorStoreIds, { errorMessage: 'Failed to parse vector store IDs', }), filters: filters ? (0, n8n_workflow_1.jsonParse)(filters, { errorMessage: 'Failed to parse filters' }) : undefined, max_num_results: (0, get_1.default)(builtInTools.fileSearch, 'maxResults'), })); } body.tools = newTools; } return await removeEmptyProperties(body); } //# sourceMappingURL=responses.js.map