Build Your First LangChain Application – Part 2: Setting Up Azure OpenAI and Your First Connection

Build Your First LangChain Application – Part 2: Setting Up Azure OpenAI and Your First Connection

In Part 1, we set up our development environment. Now it’s time to create your Azure OpenAI resource and establish your first LangChain connection!

Creating Azure OpenAI Resource

Step 1: Create Azure OpenAI Service

  1. Log into Azure Portal
  2. Navigate to “Create a resource”
  3. Search for “Azure OpenAI”
  4. Fill in the required details:
    • Subscription: Your Azure subscription
    • Resource Group: Create new or use existing
    • Region: Choose a region that supports OpenAI (e.g., East US, West Europe)
    • Name: Your unique resource name
    • Pricing Tier: Standard S0

Step 2: Deploy a Model

  1. Go to Azure OpenAI Studio
  2. Navigate to “Deployments”
  3. Click “Create new deployment”
  4. Select model: gpt-35-turbo or gpt-4
  5. Choose deployment name (e.g., “chat-model”)
  6. Set capacity as needed

Creating Your First LangChain Connection

// src/config/azure.js
import { AzureChatOpenAI } from "@langchain/openai";
import dotenv from 'dotenv';

dotenv.config();

export const azureConfig = {
  apiKey: process.env.AZURE_OPENAI_API_KEY,
  endpoint: process.env.AZURE_OPENAI_ENDPOINT,
  deploymentName: process.env.AZURE_OPENAI_DEPLOYMENT_NAME,
  apiVersion: process.env.AZURE_OPENAI_API_VERSION,
};

export const llm = new AzureChatOpenAI({
  azureOpenAIApiKey: azureConfig.apiKey,
  azureOpenAIApiInstanceName: extractInstanceName(azureConfig.endpoint),
  azureOpenAIApiDeploymentName: azureConfig.deploymentName,
  azureOpenAIApiVersion: azureConfig.apiVersion,
  temperature: 0.7,
});

function extractInstanceName(endpoint) {
  const match = endpoint.match(/https:\/\/(.+?)\.openai\.azure\.com/);
  return match ? match[1] : null;
}

Testing Your Connection

curl -X POST http://localhost:3000/api/chat \
  -H "Content-Type: application/json" \
  -d '{"message": "Hello, can you tell me about LangChain?"}'

In Part 3, we’ll explore LangChain core concepts including prompts, chains, and memory!

Written by:

191 Posts

View All Posts
Follow Me :
How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site