n8n-nodes-lmstudio-embeddings
Version:
n8n community node for LM Studio Embeddings API with encoding format selection
119 lines (118 loc) • 4.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LMStudioEmbeddings = void 0;
const openai_1 = require("@langchain/openai");
class LMStudioEmbeddings {
constructor() {
this.description = {
displayName: 'LM Studio Embeddings',
name: 'lmStudioEmbeddings',
icon: 'file:lmstudio.svg',
group: ['transform'],
version: 1,
description: 'Generate embeddings using LM Studio API',
defaults: {
name: 'LM Studio Embeddings',
},
inputs: [],
outputs: ["ai_embedding" /* NodeConnectionType.AiEmbedding */],
credentials: [
{
name: 'lmStudioApi',
required: true,
},
],
properties: [
{
displayName: 'Model',
name: 'model',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getModels',
},
default: '',
description: 'The embedding model to use',
required: true,
},
{
displayName: 'Encoding Format',
name: 'encodingFormat',
type: 'options',
options: [
{
name: 'Float',
value: 'float',
description: 'Return embeddings as floating point numbers',
},
{
name: 'Base64',
value: 'base64',
description: 'Return embeddings encoded as base64',
},
],
default: 'float',
description: 'The format for the embeddings',
}
],
};
this.methods = {
loadOptions: {
async getModels() {
const credentials = await this.getCredentials('lmStudioApi');
try {
const response = await fetch(`${credentials.baseUrl}/models`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
...(credentials.apiKey && { Authorization: `Bearer ${credentials.apiKey}` }),
},
});
if (!response.ok) {
throw new Error(`Failed to fetch models: ${response.status} ${response.statusText}`);
}
const result = await response.json();
const models = result.data || [];
return models.map((model) => ({
name: model.id,
value: model.id,
}));
}
catch (error) {
throw new Error(`Error fetching models from LM Studio: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
},
},
};
}
async supplyData(itemIndex) {
// Создаем объект с методом embedQuery для vector store
const embeddingProvider = {
embedQuery: async (text) => {
const credentials = await this.getCredentials('lmStudioApi');
const model = this.getNodeParameter('model', itemIndex);
const encodingFormat = this.getNodeParameter('encodingFormat', itemIndex);
if (!text || !text.trim()) {
throw new Error('Text content is empty');
}
const embeddings = new openai_1.OpenAIEmbeddings({
configuration: {
baseURL: credentials.baseUrl,
},
apiKey: credentials.apiKey,
model: model,
});
// @ts-ignore
const { data } = await embeddings.embeddingWithRetry({
model: model,
input: text,
encoding_format: encodingFormat,
});
return data[0].embedding;
}
};
return {
response: embeddingProvider,
};
}
}
exports.LMStudioEmbeddings = LMStudioEmbeddings;