n8n
Version:
n8n Workflow Automation Tool
52 lines • 1.69 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.AIProviderOpenAI = void 0;
const openai_1 = require("@langchain/openai");
const zod_to_json_schema_1 = require("zod-to-json-schema");
class AIProviderOpenAI {
constructor({ openAIApiKey, modelName }) {
this.model = new openai_1.ChatOpenAI({
openAIApiKey,
modelName,
timeout: 60000,
maxRetries: 2,
temperature: 0,
});
this.embeddings = new openai_1.OpenAIEmbeddings({
openAIApiKey,
modelName: 'text-embedding-3-small',
});
}
modelWithOutputParser(schema) {
return this.model.bind({
functions: [
{
name: 'output_formatter',
description: 'Should always be used to properly format output',
parameters: (0, zod_to_json_schema_1.zodToJsonSchema)(schema),
},
],
function_call: {
name: 'output_formatter',
},
});
}
mapResponse(data) {
if (Array.isArray(data.content)) {
return data.content
.map((message) => 'text' in message
? message.text
: 'image_url' in message
? message.image_url
: '')
.join('\n');
}
return data.content;
}
async invoke(messages, options) {
const data = await this.model.invoke(messages, options);
return this.mapResponse(data);
}
}
exports.AIProviderOpenAI = AIProviderOpenAI;
//# sourceMappingURL=openai.js.map
;