-
-
Notifications
You must be signed in to change notification settings - Fork 24.2k
feat(embeddings): add LM Studio embeddings node #5877
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
stablegenius49
wants to merge
2
commits into
FlowiseAI:main
from
stablegenius49:feature/lmstudio-embeddings
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { INodeCredential, INodeParams } from '../src/Interface' | ||
|
|
||
| class LMStudioApi implements INodeCredential { | ||
| label: string | ||
| name: string | ||
| version: number | ||
| inputs: INodeParams[] | ||
|
|
||
| constructor() { | ||
| this.label = 'LM Studio API' | ||
| this.name = 'lmStudioApi' | ||
| this.version = 1.0 | ||
| this.inputs = [ | ||
| { | ||
| label: 'LM Studio API Key', | ||
| name: 'lmStudioApiKey', | ||
| type: 'password' | ||
| } | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| module.exports = { credClass: LMStudioApi } |
114 changes: 114 additions & 0 deletions
114
packages/components/nodes/embeddings/LMStudioEmbedding/LMStudioEmbedding.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import { ClientOptions, OpenAIEmbeddings, OpenAIEmbeddingsParams } from '@langchain/openai' | ||
| import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface' | ||
| import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils' | ||
|
|
||
| class LMStudioEmbedding_Embeddings implements INode { | ||
| label: string | ||
| name: string | ||
| version: number | ||
| type: string | ||
| icon: string | ||
| category: string | ||
| description: string | ||
| baseClasses: string[] | ||
| credential: INodeParams | ||
| inputs: INodeParams[] | ||
|
|
||
| constructor() { | ||
| this.label = 'LM Studio Embeddings' | ||
| this.name = 'lmStudioEmbeddings' | ||
| this.version = 1.0 | ||
| this.type = 'LMStudioEmbeddings' | ||
| this.icon = 'lmstudio.svg' | ||
| this.category = 'Embeddings' | ||
| this.description = "Generate embeddings using LM Studio's OpenAI-compatible API" | ||
| this.baseClasses = [this.type, ...getBaseClasses(OpenAIEmbeddings)] | ||
| this.credential = { | ||
| label: 'Connect Credential', | ||
| name: 'credential', | ||
| type: 'credential', | ||
| credentialNames: ['lmStudioApi'], | ||
| optional: true | ||
| } | ||
| this.inputs = [ | ||
| { | ||
| label: 'Base URL', | ||
| name: 'baseUrl', | ||
| type: 'string', | ||
| default: 'http://localhost:1234/v1' | ||
| }, | ||
| { | ||
| label: 'Model Name', | ||
| name: 'modelName', | ||
| type: 'string', | ||
| default: 'text-embedding-nomic-embed-text-v1.5' | ||
| }, | ||
| { | ||
| label: 'Strip New Lines', | ||
| name: 'stripNewLines', | ||
| type: 'boolean', | ||
| optional: true, | ||
| additionalParams: true | ||
| }, | ||
| { | ||
| label: 'Batch Size', | ||
| name: 'batchSize', | ||
| type: 'number', | ||
| optional: true, | ||
| additionalParams: true | ||
| }, | ||
| { | ||
| label: 'Timeout', | ||
| name: 'timeout', | ||
| type: 'number', | ||
| optional: true, | ||
| additionalParams: true | ||
| }, | ||
| { | ||
| label: 'Dimensions', | ||
| name: 'dimensions', | ||
| type: 'number', | ||
| optional: true, | ||
| additionalParams: true | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { | ||
| const baseUrl = nodeData.inputs?.baseUrl as string | ||
| const modelName = nodeData.inputs?.modelName as string | ||
| const stripNewLines = nodeData.inputs?.stripNewLines as boolean | ||
| const batchSize = nodeData.inputs?.batchSize as string | ||
| const timeout = nodeData.inputs?.timeout as string | ||
| const dimensions = nodeData.inputs?.dimensions as string | ||
|
|
||
| if (nodeData.inputs?.credentialId) { | ||
| nodeData.credential = nodeData.inputs?.credentialId | ||
| } | ||
|
|
||
| const credentialData = await getCredentialData(nodeData.credential ?? '', options) | ||
| const lmStudioApiKey = getCredentialParam('lmStudioApiKey', credentialData, nodeData) | ||
|
|
||
| const obj: Partial<OpenAIEmbeddingsParams> & { openAIApiKey?: string; configuration?: ClientOptions } = { | ||
| modelName, | ||
| openAIApiKey: 'sk-' | ||
| } | ||
|
|
||
| if (lmStudioApiKey) obj.openAIApiKey = lmStudioApiKey | ||
| if (stripNewLines !== undefined) obj.stripNewLines = stripNewLines | ||
| if (batchSize) obj.batchSize = parseInt(batchSize, 10) | ||
| if (timeout) obj.timeout = parseInt(timeout, 10) | ||
| if (dimensions) obj.dimensions = parseInt(dimensions, 10) | ||
|
|
||
| if (baseUrl) { | ||
| obj.configuration = { | ||
| baseURL: baseUrl | ||
| } | ||
| } | ||
|
|
||
| const model = new OpenAIEmbeddings(obj) | ||
| return model | ||
| } | ||
| } | ||
|
|
||
| module.exports = { nodeClass: LMStudioEmbedding_Embeddings } | ||
15 changes: 15 additions & 0 deletions
15
packages/components/nodes/embeddings/LMStudioEmbedding/lmstudio.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation for handling numeric inputs (along with their declarations on lines 81-83) can be made more robust. The
if (variable)check evaluates tofalsefor a numeric value of0, which might be a valid input fortimeout. Also, theas stringtype assertion is unsafe, andparseIntcan result inNaNfor invalid inputs.Consider this more robust approach that validates the input before parsing and assigning it. This also makes the separate variable declarations on lines 81-83 unnecessary. This suggestion aligns with the repository's guideline to use loose equality (
== null) for nullish checks in TypeScript, covering bothnullandundefined.References
== null) as a standard idiom for a 'nullish' check that covers bothnullandundefined.