Skip to content

NVIDIA NIM fixes #4215

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

Merged
merged 20 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/components/credentials/NvdiaNIMApi.credential.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { INodeParams, INodeCredential } from '../src/Interface'
import { INodeCredential, INodeParams } from '../src/Interface'

class NvidiaNIMApi implements INodeCredential {
label: string
Expand All @@ -8,12 +8,12 @@ class NvidiaNIMApi implements INodeCredential {
inputs: INodeParams[]

constructor() {
this.label = 'Nvdia NIM API Key'
this.label = 'NVIDIA NGC API Key'
this.name = 'nvidiaNIMApi'
this.version = 1.0
this.inputs = [
{
label: 'Nvidia NIM API Key',
label: 'NVIDIA NGC API Key',
name: 'nvidiaNIMApiKey',
type: 'password'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChatOpenAI, ChatOpenAIFields } from '@langchain/openai'
import { BaseCache } from '@langchain/core/caches'
import { ChatOpenAI, ChatOpenAIFields } from '@langchain/openai'
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'

Expand All @@ -16,13 +16,13 @@ class ChatNvdiaNIM_ChatModels implements INode {
inputs: INodeParams[]

constructor() {
this.label = 'Chat Nvidia NIM'
this.name = 'chatNvidiaNIM'
this.version = 1.0
this.type = 'ChatNvidiaNIM'
this.label = 'Chat NVIDIA NIM'
this.name = 'Chat NVIDIA NIM'
this.version = 1.1
this.type = 'Chat NVIDIA NIM'
this.icon = 'nvdia.svg'
this.category = 'Chat Models'
this.description = 'Wrapper around Nvdia NIM Inference API'
this.description = 'Wrapper around NVIDIA NIM Inference API'
this.baseClasses = [this.type, ...getBaseClasses(ChatOpenAI)]
this.credential = {
label: 'Connect Credential',
Expand Down Expand Up @@ -153,7 +153,7 @@ class ChatNvdiaNIM_ChatModels implements INode {
try {
parsedBaseOptions = typeof baseOptions === 'object' ? baseOptions : JSON.parse(baseOptions)
} catch (exception) {
throw new Error("Invalid JSON in the ChatNvidiaNIM's baseOptions: " + exception)
throw new Error("Invalid JSON in the Chat NVIDIA NIM's baseOptions: " + exception)
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"multer": "^1.4.5-lts.1",
"multer-s3": "^3.0.1",
"mysql2": "^3.11.3",
"nim-container-manager": "^1.0.5",
"flowise-nim-container-manager": "^1.0.11",
"openai": "^4.82.0",
"pg": "^8.11.1",
"posthog-node": "^3.5.0",
Expand Down
60 changes: 51 additions & 9 deletions packages/server/src/controllers/nvidia-nim/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios'
import { Request, Response, NextFunction } from 'express'
import { NextFunction, Request, Response } from 'express'

const { NimContainerManager } = require('nim-container-manager')
const { NimContainerManager } = require('flowise-nim-container-manager')

const getToken = async (req: Request, res: Response, next: NextFunction) => {
try {
Expand Down Expand Up @@ -55,7 +55,13 @@ const startContainer = async (req: Request, res: Response, next: NextFunction) =
try {
const imageTag = req.body.imageTag
const apiKey = req.body.apiKey
await NimContainerManager.startContainer(imageTag, apiKey)
const hostPort = req.body.hostPort
const nimRelaxMemConstraints = parseInt(req.body.nimRelaxMemConstraints)
// Validate nimRelaxMemConstraints
if (isNaN(nimRelaxMemConstraints) || (nimRelaxMemConstraints !== 0 && nimRelaxMemConstraints !== 1)) {
return res.status(400).send('nimRelaxMemConstraints must be 0 or 1')
}
await NimContainerManager.startContainer(imageTag, apiKey, hostPort, nimRelaxMemConstraints)
return res.send(`Starting container ${imageTag}`)
} catch (error) {
next(error)
Expand All @@ -79,17 +85,51 @@ const getImage = async (req: Request, res: Response, next: NextFunction) => {
const getContainer = async (req: Request, res: Response, next: NextFunction) => {
try {
const imageTag = req.body.imageTag
const port = req.body.port

// First check if the image exists
const images = await NimContainerManager.userImageLibrary()
const image = images.find((img: any) => img.tag === imageTag)
if (!image) {
return res.status(404).send(`Image ${imageTag} not found`)
}
if (!image.container) {
return res.status(404).send(`Container of ${imageTag} not found`)

const containers = await NimContainerManager.listRunningContainers()
const portInUse = containers.find((cont: any) => cont.port === port)
if (portInUse) {
const isModelContainer = portInUse.image === image.tag
if (isModelContainer) {
portInUse.image = image.name
return res.json(portInUse)
} else {
return res.status(409).send({
message: `Port ${port} is already in use by another container`,
container: portInUse
})
}
}
const container = image.container
container.image = image.name
return res.json(container)

// If no container found with matching port, return 404
return res.status(404).send(`Container of ${imageTag} with port ${port} not found`)
} catch (error) {
next(error)
}
}

const listRunningContainers = async (req: Request, res: Response, next: NextFunction) => {
try {
const containers = await NimContainerManager.listRunningContainers()
return res.json(containers)
} catch (error) {
next(error)
}
}

const stopContainer = async (req: Request, res: Response, next: NextFunction) => {
try {
const containerId = req.body.containerId
const containerInfo = await NimContainerManager.stopContainer(containerId)
return res.json(containerInfo)
} catch (error) {
next(error)
}
Expand All @@ -102,5 +142,7 @@ export default {
pullImage,
startContainer,
getImage,
getContainer
getContainer,
listRunningContainers,
stopContainer
}
2 changes: 2 additions & 0 deletions packages/server/src/middlewares/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { InternalFlowiseError } from '../../errors/internalFlowiseError'
// we need eslint because we have to pass next arg for the error middleware
// eslint-disable-next-line
async function errorHandlerMiddleware(err: InternalFlowiseError, req: Request, res: Response, next: NextFunction) {
if (err.message.includes('401 Incorrect API key provided'))
err.message = '401 Invalid model key or Incorrect local model configuration.'
let displayedError = {
statusCode: err.statusCode || StatusCodes.INTERNAL_SERVER_ERROR,
success: false,
Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/routes/nvidia-nim/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ const router = express.Router()
router.get('/preload', nimController.preload)
router.get('/get-token', nimController.getToken)
router.get('/download-installer', nimController.downloadInstaller)
router.get('/list-running-containers', nimController.listRunningContainers)
router.post('/pull-image', nimController.pullImage)
router.post('/start-container', nimController.startContainer)
router.post('/stop-container', nimController.stopContainer)
router.post('/get-image', nimController.getImage)
router.post('/get-container', nimController.getContainer)

Expand Down
1 change: 1 addition & 0 deletions packages/server/src/utils/SSEStreamer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export class SSEStreamer implements IServerSideEventStreamer {
}

streamErrorEvent(chatId: string, msg: string) {
if (msg.includes('401 Incorrect API key provided')) msg = '401 Invalid model key or Incorrect local model configuration.'
const client = this.clients[chatId]
if (client) {
const clientResponse = {
Expand Down
Loading