@n8n/n8n-nodes-langchain
Version:

221 lines • 6.34 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var analyze_operation_exports = {};
__export(analyze_operation_exports, {
description: () => description,
execute: () => execute
});
module.exports = __toCommonJS(analyze_operation_exports);
var import_n8n_workflow = require("n8n-workflow");
var import_transport = require("../../../transport");
var import_descriptions = require("../descriptions");
const properties = [
{
...(0, import_descriptions.modelRLC)("imageModelSearch"),
displayOptions: { show: { "@version": [{ _cnd: { gte: 1.4 } }] } }
},
{
displayName: "Text Input",
name: "text",
type: "string",
placeholder: "e.g. What's in this image?",
default: "What's in this image?",
typeOptions: {
rows: 2
}
},
{
displayName: "Input Type",
name: "inputType",
type: "options",
default: "url",
options: [
{
name: "Image URL(s)",
value: "url"
},
{
name: "Binary File(s)",
value: "base64"
}
]
},
{
displayName: "URL(s)",
name: "imageUrls",
type: "string",
placeholder: "e.g. https://example.com/image.jpeg",
description: "URL(s) of the image(s) to analyze, multiple URLs can be added separated by comma",
default: "",
displayOptions: {
show: {
inputType: ["url"]
}
}
},
{
displayName: "Input Data Field Name",
name: "binaryPropertyName",
type: "string",
default: "data",
placeholder: "e.g. data",
hint: "The name of the input field containing the binary file data to be processed",
description: "Name of the binary property which contains the image(s)",
displayOptions: {
show: {
inputType: ["base64"]
}
}
},
{
displayName: "Simplify Output",
name: "simplify",
type: "boolean",
default: true,
description: "Whether to simplify the response or not"
},
{
displayName: "Options",
name: "options",
placeholder: "Add Option",
type: "collection",
default: {},
options: [
{
displayName: "Detail",
name: "detail",
type: "options",
default: "auto",
options: [
{
name: "Auto",
value: "auto",
description: "Model will look at the image input size and decide if it should use the low or high setting"
},
{
name: "Low",
value: "low",
description: "Return faster responses and consume fewer tokens"
},
{
name: "High",
value: "high",
description: "Return more detailed responses, consumes more tokens"
}
]
},
{
displayName: "Length of Description (Max Tokens)",
description: "Fewer tokens will result in shorter, less detailed image description",
name: "maxTokens",
type: "number",
default: 300,
typeOptions: {
minValue: 1
}
}
]
}
];
const displayOptions = {
show: {
operation: ["analyze"],
resource: ["image"]
}
};
const description = (0, import_n8n_workflow.updateDisplayOptions)(displayOptions, properties);
async function execute(i) {
let model = "gpt-4-vision-preview";
if (this.getNode().typeVersion >= 1.4) {
model = this.getNodeParameter("modelId", i, "gpt-4o", { extractValue: true });
}
const text = this.getNodeParameter("text", i, "");
const inputType = this.getNodeParameter("inputType", i);
const options = this.getNodeParameter("options", i, {});
const content = [
{
type: "text",
text
}
];
const detail = options.detail || "auto";
if (inputType === "url") {
const imageUrls = this.getNodeParameter("imageUrls", i).split(",").map((url) => url.trim());
for (const url of imageUrls) {
content.push({
type: "image_url",
image_url: {
url,
detail
}
});
}
} else {
const binaryPropertyName = this.getNodeParameter("binaryPropertyName", i).split(",").map((propertyName) => propertyName.trim());
for (const propertyName of binaryPropertyName) {
const binaryData = this.helpers.assertBinaryData(i, propertyName);
let fileBase64;
if (binaryData.id) {
const chunkSize = 256 * 1024;
const stream = await this.helpers.getBinaryStream(binaryData.id, chunkSize);
const buffer = await this.helpers.binaryToBuffer(stream);
fileBase64 = buffer.toString("base64");
} else {
fileBase64 = binaryData.data;
}
if (!binaryData) {
throw new import_n8n_workflow.NodeOperationError(this.getNode(), "No binary data exists on item!");
}
content.push({
type: "image_url",
image_url: {
url: `data:${binaryData.mimeType};base64,${fileBase64}`,
detail
}
});
}
}
const body = {
model,
messages: [
{
role: "user",
content
}
],
max_tokens: options.maxTokens || 300
};
let response = await import_transport.apiRequest.call(this, "POST", "/chat/completions", { body });
const simplify = this.getNodeParameter("simplify", i);
if (simplify && response.choices) {
response = { content: response.choices[0].message.content };
}
return [
{
json: response,
pairedItem: { item: i }
}
];
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
description,
execute
});
//# sourceMappingURL=analyze.operation.js.map