Skip to content

Build a generative AI application using LangChain.js, from local to Azure

License

Notifications You must be signed in to change notification settings

Azure-Samples/langchainjs-quickstart-demo

Repository files navigation

Ask YouTube: LangChain.js + Azure Quickstart

Open project in GitHub Codespaces Node version License JavaScript Watch how to use this sample on YouTube

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

OverviewInstallationRun the demoResources

Animation showing the app in action

Discover the journey of building a generative AI application using LangChain.js and Azure. This demo explores the development process from idea to production, using a RAG-based approach for a Q&A system based on YouTube video transcripts.

Overview

This application allows to ask text-based questions about a YouTube video, and uses the transcript of the video to generate responses.

The code comes in two versions:

  • local prototype: uses FAISS and Ollama with LLaMa3 model for completion and all-minilm-l6-v2 for embeddings
  • Azure cloud version: uses Azure AI Search and GPT-4 Turbo model for completion and text-embedding-3-large for embeddings

Either version can be run as an API using the Azure Functions runtime.

Note

This sample uses the new HTTP streaming support in Azure Functions that's currently in preview. You can find more information about this feature in the blog post announcement.

Application architecture

Installation

You need Node.js and Ollama installed to run this demo.

After you complete the installation, the next step is to clone this repository on your machine:

  1. Fork the project to create your own copy of this repository.
  2. On your forked repository, select the Code button, then the Local tab, and copy the URL of your forked repository.
    Screenshot showing how to copy the repository URL
  3. Open a terminal and run this command to clone the repo: git clone <your-repo-url>

Then open a terminal inside the project directory and run the following commands:

npm install
ollama pull llama3
ollama pull all-minilm:l6-v2

This will install the required dependencies and download the models needed for the demo.

Run the demo

This demo comes in three versions: a local prototype, an Azure cloud version, and an API version using Azure Functions. The fastest way to get started is to run the local prototype.

Local prototype

npm run start:local

Azure version

To run the Azure version, you need to have an Azure account and a subscription enabled for Azure OpenAI usage. If you don't have an Azure account, you can create a free account to get started.

For Azure OpenAI, you can request access with this form.

Create the Azure resources

First you need to create an Azure OpenAI instance. You can deploy a version on Azure Portal following this guide.

In Azure AI Studio, you'll need to deploy these two models:

  • text-embedding-3-large with a deployment name of text-embedding-3-large
  • gpt-4 version 0125-preview (aka GPT-4 Turbo) with a deployment name of gpt-4-turbo

Important

GPT-4 Turbo is currently in preview and may not be available in all regions, see this table for region availability.

You'll also need to have an Azure AI Search instance running. You can deploy a free version on Azure Portal without any cost, following this guide.

Set up the environment

You need to create a .env file with the following content:

AZURE_AISEARCH_ENDPOINT=https://<your-service-name>.search.windows.net
AZURE_AISEARCH_KEY=<your-aisearch-key>
AZURE_OPENAI_API_KEY=<your-openai-key>
AZURE_OPENAI_API_ENDPOINT=<your-openai-endpoint>
AZURE_OPENAI_API_DEPLOYMENT_NAME="gpt-4-turbo"
AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME="text-embedding-3-large"
AZURE_OPENAI_API_VERSION="2024-02-01"

Then you can run:

npm run start:azure

API version

npm start

Once the server is running, you can test the API in another terminal:

curl -N http://localhost:7071/api/ask \
  -H 'Content-Type: application/json' \
  -d '{ "question": "Will GPT-4 Turbo be available on Azure?" }' 

By default, the API runs the local version. To run the Azure version, you need to set the USE_AZURE environment variable to true:

USE_AZURE=true npm start

Resources

If you want to learn more about the technologies used in this demo, check out the following resources:

You can also find more Azure AI samples here.

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.