UNPKG

n8n-nodes-openai-analytics

Version:
87 lines (86 loc) 3.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.llmClassifier = void 0; /** * LLM 기반 텍스트 분류기 */ async function llmClassifier(context) { var _a, _b, _c; const { openai, functionThis, i } = context; // 대상 텍스트 가져오기 const targetText = functionThis.getNodeParameter('llmTargetText', i); // 카테고리 목록 가져오기 const categories = functionThis.getNodeParameter('llmCategories', i); // LLM 모델 가져오기 const model = functionThis.getNodeParameter('llmModel', i); // 시스템 프롬프트 구성 const systemPrompt = `너는 텍스트를 분류하는 전문가입니다. 주어진 텍스트를 다음 카테고리 중 하나로 정확하게 분류해야 합니다: ${categories.map(cat => `- ${cat}`).join('\n')} 오직 위 카테고리 중 하나만을 선택하세요. 다른 설명이나 추가 텍스트는 필요하지 않습니다. 카테고리명만 정확히 반환하세요.`; const userPrompt = `다음 텍스트를 분류해주세요: "${targetText}"`; try { // OpenAI API 호출 const response = await openai.chat.completions.create({ model, messages: [ { role: 'system', content: systemPrompt, }, { role: 'user', content: userPrompt, }, ], temperature: 0, max_tokens: 50, // 짧은 응답만 필요 }); // AI 응답 추출 const aiResponse = ((_c = (_b = (_a = response.choices[0]) === null || _a === void 0 ? void 0 : _a.message) === null || _b === void 0 ? void 0 : _b.content) === null || _c === void 0 ? void 0 : _c.trim()) || ''; // 카테고리 정규화 (정확한 일치 확인) let matchedCategory = ''; let confidenceScore = 0; // 가장 유사한 카테고리 찾기 for (const category of categories) { if (aiResponse === category) { matchedCategory = category; confidenceScore = 1.0; // 정확히 일치하면 100% 확신 break; } // 부분 포함 확인 (정확히 일치하는 것이 없는 경우) if (matchedCategory === '' && (aiResponse.includes(category) || category.includes(aiResponse))) { matchedCategory = category; confidenceScore = 0.7; // 부분 일치에 70% 확신 } } // 매칭되는 카테고리가 없으면 '미분류'로 처리 if (matchedCategory === '') { matchedCategory = 'unclassified'; confidenceScore = 0; } // 결과 반환 return { json: { category: matchedCategory, confidence: confidenceScore, original_response: aiResponse, text: targetText, categories: categories, model: model, }, }; } catch (error) { return { json: { success: false, error: error.message, }, }; } } exports.llmClassifier = llmClassifier; //# sourceMappingURL=llmClassifier.js.map