Skip to content

Demonstrates how to use Azure OpenAI with LangChain.js

License

Notifications You must be signed in to change notification settings

Azure-Samples/openai-langchainjs

Repository files navigation

Azure OpenAI with LangChain.js Sample

Open project in GitHub Codespaces Node version TypeScript License

⭐ If you like this sample, star it on GitHub — it helps a lot!

Run the sampleResourcesFAQ

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

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.

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.

Resources

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

You can also find more Azure AI samples here.

FAQ

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

Contributing

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.

Trademarks

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.