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
- Log into Azure Portal
- Navigate to “Create a resource”
- Search for “Azure OpenAI”
- 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
- Go to Azure OpenAI Studio
- Navigate to “Deployments”
- Click “Create new deployment”
- Select model:
gpt-35-turbo
orgpt-4
- Choose deployment name (e.g., “chat-model”)
- 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!