n8n-nodes-databricks
Version:
Databricks node for n8n
155 lines • 6.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmbeddingsDatabricks = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const embeddings_1 = require("@langchain/core/embeddings");
const logWrapper_1 = require("../../../utils/logWrapper");
const sharedFields_1 = require("../../../utils/sharedFields");
class DatabricksEmbeddings extends embeddings_1.Embeddings {
constructor(fields) {
super({});
this.apiKey = fields.apiKey;
this.host = fields.host;
this.endpoint = fields.endpoint;
}
async embedDocuments(texts) {
const response = await fetch(`${this.host}/serving-endpoints/${this.endpoint}/invocations`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
input: texts,
}),
});
if (!response.ok) {
const error = await response.text();
throw new Error(`Databricks API error (${response.status}): ${error}`);
}
const result = await response.json();
if (Array.isArray(result.data)) {
return result.data.map((item) => item.embedding);
}
if (Array.isArray(result.predictions)) {
return result.predictions;
}
throw new Error('Unexpected Databricks embeddings API response format.');
}
async embedQuery(text) {
const embeddings = await this.embedDocuments([text]);
if (!embeddings || !embeddings[0]) {
throw new Error('No embedding returned from Databricks API. Check your endpoint and input.');
}
return embeddings[0];
}
}
class EmbeddingsDatabricks {
constructor() {
this.description = {
displayName: 'Embeddings Databricks',
name: 'embeddingsDatabricks',
icon: { light: 'file:databricks.svg', dark: 'file:databricks.dark.svg' },
group: ['transform'],
version: 1,
description: 'Use Embeddings Databricks',
defaults: {
name: 'Embeddings Databricks',
},
credentials: [
{
name: 'databricks',
required: true,
},
],
requestDefaults: {
baseURL: '={{$credentials.host}}',
headers: {
Authorization: '=Bearer {{$credentials.token}}',
},
},
codex: {
categories: ['AI'],
subcategories: {
AI: ['Embeddings'],
},
resources: {
primaryDocumentation: [
{
url: 'https://docs.databricks.com/aws/en/generative-ai/create-query-vector-search',
},
],
},
},
inputs: [],
outputs: [n8n_workflow_1.NodeConnectionTypes.AiEmbedding],
outputNames: ['Embeddings'],
properties: [
(0, sharedFields_1.getConnectionHintNoticeField)([n8n_workflow_1.NodeConnectionTypes.AiVectorStore]),
{
displayName: 'Make sure the vector store and embedding model have the same dimensionality.',
name: 'notice',
type: 'notice',
default: '',
},
{
displayName: 'Serving Endpoint',
name: 'servingEndpoint',
type: 'options',
typeOptions: {
loadOptions: {
routing: {
request: {
method: 'GET',
url: '/api/2.0/serving-endpoints',
},
output: {
postReceive: [
{
type: 'rootProperty',
properties: {
property: 'endpoints',
},
},
{
type: 'setKeyValue',
properties: {
name: '={{$responseItem.name}}',
value: '={{$responseItem.name}}',
description: '={{($responseItem.config.served_entities || []).map(entity => entity.external_model?.name || entity.foundation_model?.name).filter(Boolean).join(", ")}}',
},
},
{
type: 'sort',
properties: {
key: 'name',
},
},
],
},
},
},
},
default: '',
required: true,
description: 'Name of the embeddings serving endpoint',
},
],
};
}
async supplyData(itemIndex) {
this.logger.debug('Supply data for embeddings Databricks');
const servingEndpoint = this.getNodeParameter('servingEndpoint', itemIndex);
const credentials = await this.getCredentials('databricks');
const embeddings = new DatabricksEmbeddings({
apiKey: credentials.token,
host: credentials.host,
endpoint: servingEndpoint,
});
return {
response: (0, logWrapper_1.logWrapper)(embeddings, this),
};
}
}
exports.EmbeddingsDatabricks = EmbeddingsDatabricks;
//# sourceMappingURL=EmbeddingsDatabricks.node.js.map