⭐ If you like this sample, star it on GitHub — it helps a lot!
This sample project demonstrates how to use Azure OpenAI using LangChain.js and the @langchain/openai
package.
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
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
Note
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.
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.
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.
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.
You can find answers to frequently asked questions in the FAQ.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.