---
title: How to Use Vultr Serverless Inference in Python with Langchain
url: https://docs.vultr.com/how-to-use-vultr-serverless-inference-in-python-with-langchain
description: Learn how to integrate Vultr Serverless Inference with Python using Langchain. Follow our step-by-step guide to deploy AI models efficiently and cost-effectively.
publish_date: 2024-03-18T13:58:35.827775Z
last_updated: 2024-03-18T13:58:35.827775Z
author: Mayank Debnath
header_image: https://docs.vultr.com/public/doc-assets/1772/197749bd-db2c-4675-a0dd-1850f8da2c80.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 guide demonstrates step-by-step process to start using Vultr Serverless Inference in Python 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
* Python 3.10 or later

## Set Up the Environment

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

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

1. Create a new Python virtual environment.

    ```console
    $ python3 -m venv venv
    $ source venv/bin/activate
    ```

1. Install the required Python packages.

    ```console
    (venv) $ pip install langchain-openai
    ```

## Inference via Langchain

Langchain provides a Python 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 Python file name `inference_langchain.py`.

    ```console
    (venv) $ nano inference_langchain.py
    ```

1. Add the following code to `inference_langchain.py`.

    ```python
    import os
    from langchain_openai import ChatOpenAI
    from langchain_core.messages import HumanMessage, SystemMessage

    api_key = os.environ.get('VULTR_SERVERLESS_INFERENCE_API_KEY')

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

    client = ChatOpenAI(openai_api_key=api_key, openai_api_base='https://api.vultrinference.com/v1' model=model)
    llm_response = chat.invoke(messages)

    print(llm_response.content)
    ```

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

    ```console
    (venv) $ export VULTR_SERVERLESS_INFERENCE_API_KEY=<your_api_key>
    (venv) $ python inference-langchain.py
    ```

    Here, the `inference_langchain.py` file uses the `langchain-openai` package to run inference workloads for Vultr Serverless Inference. Langchain uses [Langchain Expression Language](https://python.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://python.langchain.com/docs/modules/model_io/chat/quick_start).

## Conclusion

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