|
| 1 | +--- |
| 2 | +title: NVIDIA NIM |
| 3 | +description: Use NVIDIA NIM OpenAI compatible API with the AI SDK. |
| 4 | +--- |
| 5 | + |
| 6 | +# NVIDIA NIM Provider |
| 7 | + |
| 8 | +[NVIDIA NIM](https://www.nvidia.com/en-us/ai/) provides optimized inference microservices for deploying foundation models. It offers an OpenAI-compatible API that you can use with the AI SDK. |
| 9 | + |
| 10 | +## Setup |
| 11 | + |
| 12 | +The NVIDIA NIM provider is available via the `@ai-sdk/openai-compatible` module as it is compatible with the OpenAI API. |
| 13 | +You can install it with: |
| 14 | + |
| 15 | +<Tabs items={['pnpm', 'npm', 'yarn']}> |
| 16 | + <Tab> |
| 17 | + <Snippet text="pnpm add @ai-sdk/openai-compatible" dark /> |
| 18 | + </Tab> |
| 19 | + <Tab> |
| 20 | + <Snippet text="npm install @ai-sdk/openai-compatible" dark /> |
| 21 | + </Tab> |
| 22 | + <Tab> |
| 23 | + <Snippet text="yarn add @ai-sdk/openai-compatible" dark /> |
| 24 | + </Tab> |
| 25 | +</Tabs> |
| 26 | + |
| 27 | +## Provider Instance |
| 28 | + |
| 29 | +To use NVIDIA NIM, you can create a custom provider instance with the `createOpenAICompatible` function from `@ai-sdk/openai-compatible`: |
| 30 | + |
| 31 | +```ts |
| 32 | +import { createOpenAICompatible } from '@ai-sdk/openai-compatible'; |
| 33 | + |
| 34 | +const nim = createOpenAICompatible({ |
| 35 | + name: 'nim', |
| 36 | + baseURL: 'https://integrate.api.nvidia.com/v1', |
| 37 | + headers: { |
| 38 | + Authorization: `Bearer ${process.env.NIM_API_KEY}`, |
| 39 | + }, |
| 40 | +}); |
| 41 | +``` |
| 42 | + |
| 43 | +<Note> |
| 44 | + You can obtain an API key and free credits by registering at [NVIDIA |
| 45 | + Build](https://build.nvidia.com/explore/discover). New users receive 1,000 |
| 46 | + inference credits to get started. |
| 47 | +</Note> |
| 48 | + |
| 49 | +## Language Models |
| 50 | + |
| 51 | +You can interact with NIM models using a provider instance. For example, to use [DeepSeek-R1](https://build.nvidia.com/deepseek-ai/deepseek-r1), a powerful open-source language model: |
| 52 | + |
| 53 | +```ts |
| 54 | +const model = nim.chatModel('deepseek-ai/deepseek-r1'); |
| 55 | +``` |
| 56 | + |
| 57 | +### Example - Generate Text |
| 58 | + |
| 59 | +You can use NIM language models to generate text with the `generateText` function: |
| 60 | + |
| 61 | +```ts |
| 62 | +import { createOpenAICompatible } from '@ai-sdk/openai-compatible'; |
| 63 | +import { generateText } from 'ai'; |
| 64 | + |
| 65 | +const nim = createOpenAICompatible({ |
| 66 | + name: 'nim', |
| 67 | + baseURL: 'https://integrate.api.nvidia.com/v1', |
| 68 | + headers: { |
| 69 | + Authorization: `Bearer ${process.env.NIM_API_KEY}`, |
| 70 | + }, |
| 71 | +}); |
| 72 | + |
| 73 | +const { text, usage, finishReason } = await generateText({ |
| 74 | + model: nim.chatModel('deepseek-ai/deepseek-r1'), |
| 75 | + prompt: 'Tell me the history of the San Francisco Mission-style burrito.', |
| 76 | +}); |
| 77 | + |
| 78 | +console.log(text); |
| 79 | +console.log('Token usage:', usage); |
| 80 | +console.log('Finish reason:', finishReason); |
| 81 | +``` |
| 82 | + |
| 83 | +### Example - Stream Text |
| 84 | + |
| 85 | +NIM language models can also generate text in a streaming fashion with the `streamText` function: |
| 86 | + |
| 87 | +```ts |
| 88 | +import { createOpenAICompatible } from '@ai-sdk/openai-compatible'; |
| 89 | +import { streamText } from 'ai'; |
| 90 | + |
| 91 | +const nim = createOpenAICompatible({ |
| 92 | + name: 'nim', |
| 93 | + baseURL: 'https://integrate.api.nvidia.com/v1', |
| 94 | + headers: { |
| 95 | + Authorization: `Bearer ${process.env.NIM_API_KEY}`, |
| 96 | + }, |
| 97 | +}); |
| 98 | + |
| 99 | +const result = streamText({ |
| 100 | + model: nim.chatModel('deepseek-ai/deepseek-r1'), |
| 101 | + prompt: 'Tell me the history of the Northern White Rhino.', |
| 102 | +}); |
| 103 | + |
| 104 | +for await (const textPart of result.textStream) { |
| 105 | + process.stdout.write(textPart); |
| 106 | +} |
| 107 | + |
| 108 | +console.log(); |
| 109 | +console.log('Token usage:', await result.usage); |
| 110 | +console.log('Finish reason:', await result.finishReason); |
| 111 | +``` |
| 112 | + |
| 113 | +NIM language models can also be used with other AI SDK functions like `generateObject` and `streamObject`. |
| 114 | + |
| 115 | +<Note> |
| 116 | + Model support for tool calls and structured object generation varies. For |
| 117 | + example, the |
| 118 | + [`meta/llama-3.3-70b-instruct`](https://build.nvidia.com/meta/llama-3_3-70b-instruct) |
| 119 | + model supports object generation capabilities. Check each model's |
| 120 | + documentation on NVIDIA Build for specific supported features. |
| 121 | +</Note> |
0 commit comments