Skip to content

Nvidia

NVIDIAMultiModal #

Bases: MultiModalLLM

Source code in llama-index-integrations/multi_modal_llms/llama-index-multi-modal-llms-nvidia/llama_index/multi_modal_llms/nvidia/base.py
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
class NVIDIAMultiModal(MultiModalLLM):
    model: str = Field(description="The Multi-Modal model to use from NVIDIA.")
    temperature: float = Field(description="The temperature to use for sampling.")
    max_tokens: Optional[int] = Field(
        description=" The maximum numbers of tokens to generate, ignoring the number of tokens in the prompt.",
        gt=0,
    )
    timeout: float = Field(
        default=60.0,
        description="The timeout, in seconds, for API requests.",
        ge=0,
    )
    api_key: str = Field(default=None, description="The NVIDIA API key.", exclude=True)
    base_url: str = Field(default=BASE_URL, description="The base URL for NVIDIA API.")
    additional_kwargs: Dict[str, Any] = Field(
        default_factory=dict, description="Additional kwargs for the NVIDIA API."
    )

    def __init__(
        self,
        model: str = "microsoft/phi-3-vision-128k-instruct",
        temperature: float = DEFAULT_TEMPERATURE,
        max_tokens: Optional[int] = 300,
        nvidia_api_key: Optional[str] = None,
        api_key: Optional[str] = None,
        base_url: Optional[str] = BASE_URL,
        callback_manager: Optional[CallbackManager] = None,
        **kwargs: Any,
    ) -> None:
        api_key = get_from_param_or_env(
            "api_key",
            nvidia_api_key or api_key,
            "NVIDIA_API_KEY",
            "NO_API_KEY_PROVIDED",
        )

        is_hosted = base_url in KNOWN_URLS

        if is_hosted and api_key == "NO_API_KEY_PROVIDED":
            raise ValueError(
                "An API key is required for the hosted NIM. This will become an error in 0.2.0."
            )

        super().__init__(
            model=model,
            temperature=temperature,
            max_tokens=max_tokens,
            api_key=api_key,
            api_base=base_url,
            callback_manager=callback_manager,
            **kwargs,
        )

    @property
    def _client(self) -> NVIDIAClient:
        return NVIDIAClient(**self._get_credential_kwargs())

    @classmethod
    def class_name(cls) -> str:
        return "nvidia_multi_modal_llm"

    @property
    def metadata(self) -> MultiModalLLMMetadata:
        """Multi Modal LLM metadata."""
        return MultiModalLLMMetadata(
            num_output=self.max_tokens or DEFAULT_NUM_OUTPUTS,
            model_name=self.model,
        )

    @property
    def available_models(self) -> List[Model]:
        return self._client.get_model_details()

    def _get_credential_kwargs(self) -> Dict[str, Any]:
        return {"api_key": self.api_key}

    # Model Params for NVIDIA Multi Modal model.
    def _get_model_kwargs(self, **kwargs: Any) -> Dict[str, Any]:
        if self.model not in NVIDIA_MULTI_MODAL_MODELS:
            raise ValueError(
                f"Invalid model {self.model}. "
                f"Available models are: {list(NVIDIA_MULTI_MODAL_MODELS.keys())}"
            )
        base_kwargs = {"model": self.model, "temperature": self.temperature, **kwargs}
        if self.max_tokens is not None:
            base_kwargs["max_tokens"] = self.max_tokens
        return {**base_kwargs, **self.additional_kwargs}

    def _get_response_token_counts(self, raw_response: Any) -> dict:
        """Get the token usage reported by the response."""
        if not isinstance(raw_response, dict):
            return {}

        usage = raw_response.get("usage", {})
        # NOTE: other model providers that use the NVIDIA client may not report usage
        if usage is None:
            return {}

        return {
            "prompt_tokens": usage.get("prompt_tokens", 0),
            "completion_tokens": usage.get("completion_tokens", 0),
            "total_tokens": usage.get("total_tokens", 0),
        }

    def _complete(
        self, prompt: str, image_documents: Sequence[ImageNode], **kwargs: Any
    ) -> CompletionResponse:
        all_kwargs = self._get_model_kwargs(**kwargs)
        content, extra_headers = generate_nvidia_multi_modal_chat_message(
            prompt=prompt, image_documents=image_documents, model=self.model
        )
        message_dict = [{"role": MessageRole.USER, "content": content}]

        response = self._client.request(
            endpoint=NVIDIA_MULTI_MODAL_MODELS[self.model]["endpoint"],
            stream=False,
            messages=message_dict,
            extra_headers=extra_headers,
            **all_kwargs,
        )
        response = response.json()
        text = response["choices"][0]["message"]["content"]
        return CompletionResponse(
            text=text,
            raw=response,
            additional_kwargs=self._get_response_token_counts(response),
        )

    def _stream_complete(
        self, prompt: str, image_documents: Sequence[ImageNode], **kwargs: Any
    ) -> CompletionResponseGen:
        all_kwargs = self._get_model_kwargs(**kwargs)
        content, extra_headers = generate_nvidia_multi_modal_chat_message(
            prompt=prompt, image_documents=image_documents, model=self.model
        )
        message_dict = [{"role": MessageRole.USER, "content": content}]

        def gen() -> CompletionResponseGen:
            response = self._client.request(
                messages=message_dict,
                stream=True,
                endpoint=NVIDIA_MULTI_MODAL_MODELS[self.model]["endpoint"],
                extra_headers=extra_headers,
                **all_kwargs,
            )
            for line in response.iter_lines():
                if line and line.strip() != b"data: [DONE]":
                    line = line.decode("utf-8")
                    line = line[5:]

                    msg, final_line = aggregate_msgs(process_response(line))

                    yield CompletionResponse(
                        **msg,
                        additional_kwargs=self._get_response_token_counts(line),
                    )

                    if final_line:
                        break

        return gen()

    def complete(
        self, prompt: str, image_documents: Sequence[ImageNode], **kwargs: Any
    ) -> CompletionResponse:
        return self._complete(prompt, image_documents, **kwargs)

    def stream_complete(
        self, prompt: str, image_documents: Sequence[ImageNode], **kwargs: Any
    ) -> CompletionResponseGen:
        return self._stream_complete(prompt, image_documents, **kwargs)

    def _chat(self, messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponse:
        all_kwargs = self._get_model_kwargs(**kwargs)
        content, extra_headers = generate_nvidia_multi_modal_chat_message(
            inputs=messages, model=self.model
        )

        response = self._client.request(
            endpoint=NVIDIA_MULTI_MODAL_MODELS[self.model]["endpoint"],
            stream=False,
            messages=content,
            extra_headers=extra_headers,
            **all_kwargs,
        )
        response = response.json()
        text = response["choices"][0]["message"]["content"]

        return ChatResponse(
            delta=text,
            message=ChatMessage(
                role=response["choices"][0]["message"]["role"], content=text
            ),
            raw=response,
            additional_kwargs=self._get_response_token_counts(response),
        )

    def chat(
        self,
        messages: Sequence[ChatMessage],
        **kwargs: Any,
    ) -> ChatResponse:
        return self._chat(messages, **kwargs)

    def stream_chat(
        self,
        messages: Sequence[ChatMessage],
        **kwargs: Any,
    ) -> ChatResponseGen:
        all_kwargs = self._get_model_kwargs(**kwargs)
        content, extra_headers = generate_nvidia_multi_modal_chat_message(
            inputs=messages, model=self.model
        )

        def gen() -> CompletionResponseGen:
            response = self._client.request(
                messages=content,
                stream=True,
                endpoint=NVIDIA_MULTI_MODAL_MODELS[self.model]["endpoint"],
                extra_headers=extra_headers,
                **all_kwargs,
            )
            for line in response.iter_lines():
                if line and line.strip() != b"data: [DONE]":
                    line = line.decode("utf-8")
                    line = line[5:]

                    msg, final_line = aggregate_msgs(process_response(line))

                    role = msg.get("role", MessageRole.ASSISTANT)
                    additional_kwargs = {}

                    yield ChatResponse(
                        message=ChatMessage(
                            role=role,
                            content=msg.get("content"),
                            additional_kwargs=additional_kwargs,
                        ),
                        delta=msg.get("content"),
                        raw=response,
                        additional_kwargs=self._get_response_token_counts(line),
                    )

                    if final_line:
                        break

        return gen()

    # ===== Async Endpoints =====

    async def _acomplete(
        self, prompt: str, image_documents: Sequence[ImageNode], **kwargs: Any
    ) -> CompletionResponse:
        all_kwargs = self._get_model_kwargs(**kwargs)
        content, extra_headers = generate_nvidia_multi_modal_chat_message(
            prompt=prompt, image_documents=image_documents, model=self.model
        )
        message_dict = [{"role": MessageRole.USER, "content": content}]

        response_json = await self._client.request_async(
            endpoint=NVIDIA_MULTI_MODAL_MODELS[self.model]["endpoint"],
            stream=False,
            messages=message_dict,
            extra_headers=extra_headers,
            **all_kwargs,
        )
        text = response_json["choices"][0]["message"]["content"]
        return CompletionResponse(
            text=text,
            raw=response_json,
            additional_kwargs=self._get_response_token_counts(response_json),
        )

    async def acomplete(
        self, prompt: str, image_documents: Sequence[ImageNode], **kwargs: Any
    ) -> CompletionResponse:
        return await self._acomplete(prompt, image_documents, **kwargs)

    async def astream_complete(
        self, prompt: str, image_documents: Sequence[ImageNode], **kwargs: Any
    ) -> CompletionResponseAsyncGen:
        all_kwargs = self._get_model_kwargs(**kwargs)
        content, extra_headers = generate_nvidia_multi_modal_chat_message(
            prompt=prompt, image_documents=image_documents, model=self.model
        )
        payload = {
            "messages": [{"role": MessageRole.USER, "content": content}],
            "stream": True,
            **all_kwargs,
        }
        headers = {
            **self._client._get_headers(stream=True),
            **extra_headers,
        }

        async def gen() -> CompletionResponseAsyncGen:
            async with aiohttp.ClientSession() as session:
                async with session.post(
                    NVIDIA_MULTI_MODAL_MODELS[self.model]["endpoint"],
                    json=payload,
                    headers=headers,
                ) as response:
                    response.raise_for_status()
                    text = ""
                    async for line in response.content:
                        if line and line.strip() != b"data: [DONE]":
                            line = line.decode("utf-8").strip()
                            if line.startswith("data:"):
                                data = json.loads(line[5:])

                                delta = data["choices"][0]["delta"]["content"]
                                text += delta

                                yield CompletionResponse(
                                    text=text,
                                    raw=data,
                                    delta=text,
                                    additional_kwargs=self._get_response_token_counts(
                                        line
                                    ),
                                )

        return gen()

    async def _achat(
        self, messages: Sequence[ChatMessage], **kwargs: Any
    ) -> ChatResponse:
        all_kwargs = self._get_model_kwargs(**kwargs)
        content, extra_headers = generate_nvidia_multi_modal_chat_message(
            inputs=messages, model=self.model
        )

        response_json = await self._client.request_async(
            endpoint=NVIDIA_MULTI_MODAL_MODELS[self.model]["endpoint"],
            stream=False,
            messages=content,
            extra_headers=extra_headers,
            **all_kwargs,
        )

        text = response_json["choices"][0]["message"]["content"]

        return ChatResponse(
            delta=text,
            message=ChatMessage(
                role=response_json["choices"][0]["message"]["role"], content=text
            ),
            raw=response_json,
            additional_kwargs=self._get_response_token_counts(response_json),
        )

    async def achat(
        self, messages: Sequence[ChatMessage], **kwargs: Any
    ) -> ChatResponse:
        return await self._achat(messages, **kwargs)

    async def astream_chat(
        self, messages: Sequence[ChatMessage], **kwargs: Any
    ) -> ChatResponseAsyncGen:
        all_kwargs = self._get_model_kwargs(**kwargs)
        content, extra_headers = generate_nvidia_multi_modal_chat_message(
            inputs=messages, model=self.model
        )
        payload = {"messages": content, "stream": True, **all_kwargs}
        headers = {
            **self._client._get_headers(stream=True),
            **extra_headers,
        }

        async def gen() -> ChatResponseAsyncGen:
            async with aiohttp.ClientSession() as session:
                async with session.post(
                    NVIDIA_MULTI_MODAL_MODELS[self.model]["endpoint"],
                    json=payload,
                    headers=headers,
                ) as response:
                    response.raise_for_status()

                    text = ""

                    async for line in response.content:
                        if line and line.strip() != b"data: [DONE]":
                            line_text = line.decode("utf-8").strip()

                            if line_text.startswith("data:"):
                                data = json.loads(line_text[5:])
                                delta = data["choices"][0]["delta"]["content"]
                                role = data["choices"][0]["delta"].get(
                                    "role", MessageRole.ASSISTANT
                                )
                                text += delta

                                yield ChatResponse(
                                    message=ChatMessage(
                                        role=role,
                                        content=delta,
                                        additional_kwargs={},
                                    ),
                                    delta=delta,
                                    raw=data,
                                    additional_kwargs=self._get_response_token_counts(
                                        data
                                    ),
                                )

        return gen()

metadata property #

Multi Modal LLM metadata.