n8n-nodes-openai-analytics
Version:
n8n node for OpenAI Analytics
130 lines • 5.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAssistant = void 0;
async function createAssistant(context) {
var _a;
const { openai, functionThis, i } = context;
// 기본 정보 가져오기
const name = functionThis.getNodeParameter('assistantName', i);
const description = functionThis.getNodeParameter('assistantDescription', i, '');
const instructions = functionThis.getNodeParameter('assistantInstructions', i, '');
const model = functionThis.getNodeParameter('assistantModel', i);
// 도구 설정 가져오기
const useCodeInterpreter = functionThis.getNodeParameter('useCodeInterpreter', i, false);
const useFileSearch = functionThis.getNodeParameter('useRetrieval', i, false);
const useFunctionCalling = functionThis.getNodeParameter('useFunctionCalling', i, false);
// 응답 형식 가져오기
const useResponseFormat = functionThis.getNodeParameter('useResponseFormat', i, false);
const responseFormat = useResponseFormat
? functionThis.getNodeParameter('responseFormat', i, 'text')
: 'text';
// 고급 설정 가져오기
const useAdvancedSettings = functionThis.getNodeParameter('useAdvancedSettings', i, false);
// 기본값 설정 - 고급 설정을 사용하지 않을 경우에도 기본값을 명시적으로 설정
let temperature = 1.0;
let topP = 1.0;
console.log('Creating assistant with advanced settings:', useAdvancedSettings);
if (useAdvancedSettings) {
try {
temperature = functionThis.getNodeParameter('temperature', i, 1.0);
topP = functionThis.getNodeParameter('topP', i, 1.0);
console.log(`Advanced settings: temperature=${temperature}, topP=${topP}`);
}
catch (error) {
console.error('Advanced 설정 가져오기 오류:', error);
// 오류 발생시 기본값 사용
temperature = 1.0;
topP = 1.0;
}
}
// 파일 첨부
const useFileAttachments = functionThis.getNodeParameter('useFileAttachments', i, false);
let fileIds = [];
if (useFileAttachments) {
try {
fileIds = functionThis.getNodeParameter('assistantFileIds', i, []);
console.log('Attached file IDs:', fileIds);
}
catch (error) {
console.error('파일 ID를 가져오는 중 오류 발생:', error);
fileIds = [];
}
}
// 도구 배열 생성
const tools = [];
// tool_resources 객체 초기화
const toolResources = {};
if (useCodeInterpreter) {
tools.push({ type: 'code_interpreter' });
toolResources.code_interpreter = { file_ids: [] };
}
if (useFileSearch) {
tools.push({ type: 'file_search' });
// file_search 도구를 위한 vector_store_ids는 필요 시 추가
toolResources.file_search = { vector_store_ids: [] };
}
if (useFunctionCalling) {
const functionJson = functionThis.getNodeParameter('functionJson', i, '');
if (functionJson) {
try {
const parsedFunction = JSON.parse(functionJson);
tools.push({
type: 'function',
function: parsedFunction
});
}
catch (error) {
console.error('함수 JSON 파싱 오류:', error);
}
}
}
// Assistant 생성 파라미터 구성
const assistantParams = {
name,
model,
tools,
temperature,
top_p: topP,
};
// 선택적 매개변수 추가
if (description)
assistantParams.description = description;
if (instructions)
assistantParams.instructions = instructions;
// tool_resources가 비어있지 않은 경우에만 추가
if (Object.keys(toolResources).length > 0) {
assistantParams.tool_resources = toolResources;
}
// 파일 첨부 - code_interpreter에 파일 추가
if (fileIds.length > 0 && toolResources.code_interpreter) {
toolResources.code_interpreter.file_ids = fileIds;
}
// 응답 형식 설정
if (useResponseFormat) {
assistantParams.response_format = { type: responseFormat };
}
try {
// Assistant 생성 API 호출
const assistant = await openai.beta.assistants.create(assistantParams);
return {
json: {
success: true,
assistant,
message: `Assistant "${name}" successfully created with ID: ${assistant.id}`
}
};
}
catch (error) {
console.error('Assistant 생성 오류:', error);
// 오류 응답
return {
json: {
success: false,
error: error.message || 'Unknown error occurred',
details: ((_a = error.response) === null || _a === void 0 ? void 0 : _a.data) || error
}
};
}
}
exports.createAssistant = createAssistant;
//# sourceMappingURL=createAssistant.js.map