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.
- Important: Ensure you can run
In order to deploy the Azure OpenAI resources, you also need the following:
- Azure account. If you're new to Azure, get an Azure account for free to get free Azure credits to get started. If you're a student, you can also get free credits with Azure for Students.
- Azure subscription with access enabled for the Azure OpenAI service. You can request access with this form.
- Azure account permissions:
- Your Azure account must have
Microsoft.Authorization/roleAssignments/write
permissions, such as Role Based Access Control Administrator, User Access Administrator, or Owner. If you don't have subscription-level permissions, you must be granted RBAC for an existing resource group and deploy to that existing group. - Your Azure account also needs
Microsoft.Resources/deployments/write
permissions on the subscription level.
- Your Azure account must have
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:
- Run
azd down --purge
- 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:
- Azure OpenAI Service
- LangChain.js documentation
- Generative AI For Beginners
- Ask YouTube: LangChain.js + Azure Quickstart sample
- Serverless AI Chat with RAG using LangChain.js
- Chat + Enterprise data with Azure OpenAI and Azure AI Search
You can also find more Azure AI samples here.