Share via

Azure OpenAI with LangChain.js Sample

Overview

This sample project demonstrates how to use Azure OpenAI using LangChain.js and the @langchain/openai package.

Prerequisites

You need to install following tools to run the sample:

  • Node.js LTS
  • Azure Developer CLI
  • PowerShell 7+ (for Windows users only)
    • Important: Ensure you can run pwsh.exe from a PowerShell command. If this fails, you likely need to upgrade PowerShell.
    • Instead of Powershell, you can also use Git Bash or WSL to run the Azure Developer CLI commands.

In order to deploy the Azure OpenAI resources, you also need the following:

Run the sample

First, fork and clone the repository or open it in GitHub Codespaces.

In the project directory, open a terminal and run the following command to install the dependencies:

npm install

Create AzureOpenAI Resources

[!INFO] See the cost estimation details for running this sample on Azure.

Open a terminal in the project directory and run the following commands to create the Azure resources needed to run this sample:

azd auth login
azd up

You will be prompted to select a base location for the resources. If you're unsure of which location to choose, select eastus2.

Once the deployment is complete a .env file will be created at the root of the project with the necessary environment variables to run the sample.

You only need to deploy the resource once.

Run the code

To run the sample, use the following command:

npm start

This will run the sample and output the results to the console, you should see an answer to the "Hello World!" prompt.

Open the file src/index.ts to see how the code works.

Clean up

To clean up all the Azure resources created by this sample:

  1. Run azd down --purge
  2. When asked if you are sure you want to continue, enter y

The resource group and all the resources will be deleted.

FAQ

You can find answers to frequently asked questions in the FAQ.

Next steps

Here are some resources to learn more about the technologies used in this sample:

You can also find more Azure AI samples here.