---
title: How to Use Vultr Serverless Inference in Node.js with Langchain
url: https://docs.vultr.com/how-to-use-vultr-serverless-inference-in-nodejs-with-langchain
description: Learn how to implement Vultr Serverless Inference in your Node.js applications using Langchain. Step-by-step guide for efficient AI model deployment.
publish_date: 2024-03-18T13:59:56.240267Z
last_updated: 2024-03-18T13:59:56.240267Z
author: Mayank Debnath
header_image: https://docs.vultr.com/public/doc-assets/1775/1b57875a-49c6-4e96-992b-aa8a76a64fd7.png
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

Vultr Serverless Inference allows you to run inference workloads for large language models such as Mixtral 8x7B, Mistral 7B, Meta Llama 2 70B, and more. Using Vultr Serverless Inference, you can run inference workloads without having to worry about the infrastructure, and you only pay for the input and output tokens.

This article demonstrates step-by-step process to start using Vultr Serverless Inference in Node.js with Langchain.

## Prerequisites

Before you begin, you must:

* [Create a Vultr Serverless Inference Subscription](https://docs.vultr.com/products/serverless/inference/provisioning)
* Fetch the API key for Vultr Serverless Inference
* Node.js 20.x or later

## Set Up the Environment

1. Create a new project directory and navigate to the project directory.

    ```console
    $ mkdir vultr-serverless-inference-nodejs-langchain
    $ cd vultr-serverless-inference-nodejs-langchain
    ```

1. .Create a new Node.js project.

    ```console
    $ npm init -y
    ```

1. Install the required Node.js packages.

    ```console
    $ npm install @langchain/openai
    ```

## Inference via Langchain

Langchain provides a Node.js SDK to run inference workloads for Vultr Serverless Inference. You can use the `@langchain/openai` package to make the API calls.

1. Create a new JavaScript file name `inference-langchain.js`.

    ```console
    $ nano inference-langchain.js
    ```

1. Add the following code to `inference-langchain.js`.

    ```javascript
    const { ChatOpenAI } = require('@langchain/openai');
    const { HumanMessage, SystemMessage } = require('@langchain/core/messages');

    const apiKey = process.env.VULTR_SERVERLESS_INFERENCE_API_KEY;

    // Set the model
    // List of available models: https://api.vultrinference.com/v1/chat/models
    const model = '';
    const messages = [
    new HumanMessage('What is the capital of India?'),
    ];

    async function main() {
        const client = new ChatOpenAI({
            openAIApiKey: apiKey,
            modelName: model,
            configuration: {
                baseURL: 'https://api.vultrinference.com/v1',
            }
        });
        
        const llmRsponse = await client.invoke(messages);
        console.log(llmRsponse.content);
    }

    main();
    ```

1. Run the `inference-langchain.js` file.

    ```console
    $ export VULTR_SERVERLESS_INFERENCE_API_KEY=<your_api_key>
    $ node inference-langchain.js
    ```

    Here, the `inference-langchain.js` file uses the `@langchain/openai` package to run inference workloads for Vultr Serverless Inference. Langchain uses [Langchain Expression Language](https://js.langchain.com/docs/expression_language/) (LCEL) for defining different types of messages such as `HumanMessage` and `SystemMessage`. For more information, refer to the [Langchain documentation](https://js.langchain.com/docs/modules/model_io/chat/quick_start).

## Conclusion

In this article, you learned how to use Vultr Serverless Inference in Node.js with Langchain. You can now integrate Vultr Serverless Inference into your Node.js applications that uses Langchain to generate completions for large language models.
