UNPKG

n8n-nodes-doubao-api

Version:
247 lines 9.88 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DoubaoApi = void 0; const n8n_workflow_1 = require("n8n-workflow"); const axios_1 = __importStar(require("axios")); class DoubaoApi { constructor() { this.description = { displayName: '豆包 API', name: 'doubaoApi', icon: 'file:doubaoApi.svg', group: ['transform', 'output'], version: 1, description: '使用豆包大模型 API 进行文本生成', defaults: { name: '豆包 API', color: '#3A8DE1', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'doubaoApi', required: true, displayOptions: { show: { authentication: ['apiKey'], }, }, }, ], properties: [ { displayName: '认证方式', name: 'authentication', type: 'options', options: [ { name: 'API Key', value: 'apiKey', }, ], default: 'apiKey', }, { displayName: '模型', name: 'model', type: 'options', options: [ { name: 'doubao-1.5-pro-32k-250115', value: 'doubao-1.5-pro-32k-250115', }, { name: 'doubao-1.5-7b-Chat', value: 'doubao-1.5-7b-Chat', }, ], default: 'doubao-1.5-pro-32k-250115', description: '选择要使用的豆包模型', }, { displayName: '系统提示词', name: 'systemPrompt', type: 'string', typeOptions: { rows: 4, }, default: '', description: '设置系统提示词,指导AI的回答方向和风格', }, { displayName: '用户提示词', name: 'userPrompt', type: 'string', typeOptions: { rows: 4, }, default: '', description: '设置用户提示词,将与系统提示词组合', required: true, }, { displayName: '用户输入', name: 'userInput', type: 'string', typeOptions: { rows: 4, }, default: '', description: '用户的输入内容', required: true, }, { displayName: '选项', name: 'options', type: 'collection', placeholder: '添加选项', default: {}, options: [ { displayName: '超时时间(毫秒)', name: 'timeout', type: 'number', default: 30000, description: 'API请求超时时间,默认30秒', }, { displayName: '最大重试次数', name: 'maxRetries', type: 'number', default: 3, description: 'API请求失败时的最大重试次数', }, ], }, ], }; } async execute() { var _a, _b; const items = this.getInputData(); const returnData = []; const authentication = this.getNodeParameter('authentication', 0); let apiKey; if (authentication === 'apiKey') { const credentials = await this.getCredentials('doubaoApi'); apiKey = credentials.apiKey; } else { throw new n8n_workflow_1.NodeOperationError(this.getNode(), '不支持的认证方式'); } const options = this.getNodeParameter('options', 0, {}); const timeout = options.timeout || 30000; const maxRetries = options.maxRetries || 3; for (let itemIndex = 0; itemIndex < items.length; itemIndex++) { try { const model = this.getNodeParameter('model', itemIndex); const systemPrompt = this.getNodeParameter('systemPrompt', itemIndex, ''); const userPrompt = this.getNodeParameter('userPrompt', itemIndex); const userInput = this.getNodeParameter('userInput', itemIndex); const requestBody = { model, messages: [ { role: 'system', content: `${systemPrompt} ${userPrompt}`, }, { role: 'user', content: userInput, }, ], }; const headers = { 'Content-Type': 'application/json', 'Authorization': `Bearer ${apiKey}`, }; let response; let retries = 0; let lastError; while (retries <= maxRetries) { try { response = await axios_1.default.post('https://ark.cn-beijing.volces.com/api/v3/chat/completions', requestBody, { headers, timeout, }); break; } catch (error) { lastError = error; retries += 1; if (retries > maxRetries) { break; } await new Promise(resolve => setTimeout(resolve, 1000 * retries)); } } if (!response && lastError) { throw lastError; } if (!response) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), '未能获取到API响应'); } const responseData = response.data; const content = responseData.choices[0].message.content; const usage = responseData.usage || { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0, }; returnData.push({ json: { content, model: responseData.model, created: responseData.created, id: responseData.id, usage, }, }); } catch (error) { if (error instanceof axios_1.AxiosError && error.response) { const errorData = { message: `API错误: ${error.response.status} - ${((_b = (_a = error.response.data) === null || _a === void 0 ? void 0 : _a.error) === null || _b === void 0 ? void 0 : _b.message) || JSON.stringify(error.response.data)}`, }; throw new n8n_workflow_1.NodeApiError(this.getNode(), errorData); } else if (error instanceof axios_1.AxiosError && error.request) { const errorData = { message: `网络错误: ${error.message}`, }; throw new n8n_workflow_1.NodeApiError(this.getNode(), errorData); } else { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `请求错误: ${error.message}`); } } } return [returnData]; } } exports.DoubaoApi = DoubaoApi; //# sourceMappingURL=DoubaoApi.node.js.map