Skip to content

[pull] main from MicrosoftDocs:main #306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 40 commits into from
Apr 28, 2025
Merged
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
f49b05a
JS - Langchain agent on Azure
diberry Mar 26, 2025
734ec88
Add repo link
diberry Mar 26, 2025
ecc1163
update media location
diberry Mar 26, 2025
e69950b
edit
diberry Mar 26, 2025
a0bb27c
Changes based on Yohan's feedback
diberry Apr 14, 2025
ecb9def
John's feedback
diberry Apr 14, 2025
e3b3314
edits
diberry Apr 17, 2025
b5c35df
fix code URL
diberry Apr 17, 2025
f2d4e8d
fix route to code files
diberry Apr 17, 2025
3d409fd
fix code URL for 1 file
diberry Apr 17, 2025
4b6b83f
edits
diberry Apr 21, 2025
16830d6
edit model
diberry Apr 21, 2025
654a7e5
edits
diberry Apr 21, 2025
7d85aea
edit
diberry Apr 21, 2025
1ce5495
pull in code again
diberry Apr 21, 2025
2ee7865
JS - freshness - tool selection
diberry Apr 21, 2025
9b29444
Update articles/javascript/ai/langchain-agent-on-azure.md
diberry Apr 22, 2025
f78b1e9
edits
diberry Apr 22, 2025
2f3b77f
fix links
diberry Apr 22, 2025
4081598
JS - JS @ Microsoft
diberry Apr 23, 2025
10a8851
includes
diberry Apr 23, 2025
a8f6451
Updated ms.date values and fixed one date typo
mcleanbyron Apr 25, 2025
27789ca
Merge pull request #7195 from mcleanbyron/freshness-updates
v-regandowner Apr 25, 2025
7532061
fix model name
diberry Apr 25, 2025
8238714
update gitignore with copilot files
ms-johnalex Apr 25, 2025
aef1f68
edit for acrolinx
diberry Apr 25, 2025
0794755
Changes as requested
diberry Apr 26, 2025
e7bb5ce
Remove extra asterisk
diberry Apr 26, 2025
74dc47a
more sentence case
diberry Apr 26, 2025
c78ae38
Merge pull request #7059 from diberry/diberry/0326-langchain-agent
JamesJBarnett Apr 26, 2025
6918237
Update package index with latest published versions
azure-sdk Apr 27, 2025
dda8451
Merge branch 'main' into diberry/0421-fresh-tool-selection
diberry Apr 28, 2025
aea5770
Merge pull request #7196 from ms-johnalex/upd-gitignore-042525
KarlErickson Apr 28, 2025
a059e1f
Merge pull request #7194 from azure-sdk/PackageIndexUpdates
KarlErickson Apr 28, 2025
f21a8cd
Acrolinx fix
Court72 Apr 28, 2025
b9e9981
Merge pull request #7175 from diberry/diberry/0421-fresh-tool-selection
Court72 Apr 28, 2025
d86bb03
Merge pull request #7182 from diberry/diberry/0423-js-at-microsoft
Court72 Apr 28, 2025
6b06f37
Update package index with latest published versions
azure-sdk Apr 28, 2025
08a83ca
Merge pull request #7199 from azure-sdk/PackageIndexUpdates
KarlErickson Apr 28, 2025
fe6a050
Merge pull request #7200 from MicrosoftDocs/main
Taojunshen Apr 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
edits
  • Loading branch information
diberry committed Apr 21, 2025
commit 4b6b83f95bb58ff614009f9754b2fdbf60b02b14
73 changes: 46 additions & 27 deletions articles/javascript/ai/langchain-agent-on-azure.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@ ms.custom: devx-track-ts, devx-track-ts-ai

# Tutorial: Build a LangChain.js agent with Azure AI Search

In this tutorial, you'll use LangChain.js to build a LangChain.js agent that enables NorthWind company employees to ask human resources–related questions. By leveraging the framework, you avoid boilerplate code typically required for LangChain.js agents and Azure service integration, allowing you to focus on your business needs.
In this tutorial, you'll use LangChain.js to build a LangChain.js agent that enables the NorthWind company employees to ask human resources–related questions. By leveraging the framework, you avoid boilerplate code typically required for LangChain.js agents and Azure service integration, allowing you to focus on your business needs.

In this tutorial, you will:

> [!div class="checklist"]
> * [Set up a LangChain.js agent]
> * [Integrate Azure resources into your LangChain.js agent]
> * [Optionally test your LangChain.js agent in LangSmith Studio]
> * Set up a LangChain.js agent
> * Integrate Azure resources into your LangChain.js agent
> * Optionally test your LangChain.js agent in LangSmith Studio

NorthWind relies on two data sources: public HR documentation accessible to all employees and a confidential HR database containing sensitive employee data. This tutorial focuses on building a LangChain.js agent that determines whether an employee’s question can be answered using the public HR documents. If so, the LangChain.js agent provides the answer directly.

:::image type="content" source="./media/langchain-agent-on-azure/agent-workflow.png" alt-text="Diagram illustrating the LangChain.js agent workflow and its decision branch to use HR documentation for answering questions.":::


> [!WARNING]
> This article uses keys to access resources. In a production environment, the recommended best practice is to use Azure RBAC and managed identity. This approach eliminates the need to manage or rotate keys, enhancing security and simplifying access control.


## Prerequisites

* An active Azure account. [Create an account for free](https://azure.microsoft.com/free) if you don't have one.
Expand All @@ -37,11 +42,11 @@ NorthWind relies on two data sources: public HR documentation accessible to all

## Agent architecture

The LangChain.js framework provides a decision flow for building intelligent agents as a graph. In this tutorial, you will create a LangChain.js agent that integrates with Azure AI Search and Azure OpenAI to answer HR-related questions. The agent's architecture is designed to:
The LangChain.js framework provides a decision flow for building intelligent agents as a LangGraph. In this tutorial, you will create a LangChain.js agent that integrates with Azure AI Search and Azure OpenAI to answer HR-related questions. The agent's architecture is designed to:

* Determine if a question is relevant to HR documentation.
* Retrieve relevant documents from Azure AI Search.
* Use Azure OpenAI to generate an answer based on the retrieved documents.
* Use Azure OpenAI to generate an answer based on the retrieved documents and LLM model.

**Key Components**:

Expand All @@ -50,16 +55,16 @@ The LangChain.js framework provides a decision flow for building intelligent age
* **Edges** define the flow between nodes, determining the sequence of operations.

2. **Azure AI Search Integration**:
* Stores HR documents as vector embeddings.
* Inserts HR documents into vector store as embeddings.
* Uses an embeddings model (`text-embedding-ada-002`) to create these embeddings.
* Retrieves relevant documents using a query key.
* Retrieves relevant documents based on user prompt.

3. **Azure OpenAI Integration**:
* Uses a large language model (`gpt-35-turbo-instruct`) to:
* Determine if a question is HR-related.
* Generate answers based on retrieved documents.
* Uses a large language model (`4o`) to:
* Determines if a question is answerable from general HR documents.
* Generates answer with prompt using context from documents and user question.

**Workflow Example**:
The following table has examples of user questions which are and aren't relevant and answerable from general Human resources documents:

| Question | Relevance to HR Documents |
|----------|----------------------------|
Expand Down Expand Up @@ -110,7 +115,7 @@ To manage the various Azure resources and models used in this tutorial, create s

### Azure AI Search configuration

The Azure AI Search configuration file uses the admin key to insert documents into the vector store. This key is essential for managing the ingestion of data into Azure AI Search. When querying the vector store, use the query key instead. This separation of keys ensures secure and efficient access to the resource.
The Azure AI Search configuration file uses the admin key to insert documents into the vector store. This key is essential for managing the ingestion of data into Azure AI Search.

:::code language="typescript" source="~/../azure-typescript-langchainjs/packages/langgraph-agent/src/config/vector_store_admin.ts" :::

Expand All @@ -121,29 +126,37 @@ LangChain.js abstracts the need to define a schema for data ingestion into Azure
The Azure OpenAI resource requires two models:

1. **Embeddings Model**: Used to create embeddings for inserting documents into the Azure AI Search vector store.
2. **LLM Model**: Used to query the vector store and generate answers.

Create configuration files to manage the separate resources:
:::code language="typescript" source="~/../azure-typescript-langchainjs/packages/langgraph-agent/src/config/embeddings.ts" :::

2. **LLM Model**: Used to query the vector store and generate answers.

* [embeddings.ts](https://github.com/Azure-Samples/azure-typescript-langchainjs/packages/langgraph_agent/src/config/embeddings.ts)
* [llm.ts](https://github.com/Azure-Samples/azure-typescript-langchainjs/packages/langgraph_agent/src/config/llm.ts)
:::code language="typescript" source="~/../azure-typescript-langchainjs/packages/langgraph-agent/src/config/llm.ts" :::

### Query configuration

For querying the vector store, create a separate configuration file:

* [vector_store_query.ts](https://github.com/Azure-Samples/azure-typescript-langchainjs/packages/langgraph_agent/src/config/vector_store_query.ts)
:::code language="typescript" source="~/../azure-typescript-langchainjs/packages/langgraph-agent/src/config/vector_store_query.ts" :::

When querying the vector store, use the query key instead. This separation of keys ensures secure and efficient access to the resource.

### Constants and prompts

AI applications often rely on constant strings and prompts. Create files to manage these constants:
AI applications often rely on constant strings and prompts. Create files to manage these constants.

* [system_prompt.ts](https://github.com/Azure-Samples/azure-typescript-langchainjs/packages/langgraph_agent/src/config/system_prompt.ts)
* [nodes.ts: define LangChain.js graph nodes](https://github.com/Azure-Samples/azure-typescript-langchainjs/packages/langgraph_agent/src/config/nodes.ts)
* [user_queries.ts: example user queries](https://github.com/Azure-Samples/azure-typescript-langchainjs/packages/langgraph_agent/src/config.ts)
Create the system prompt:

:::code language="typescript" source="~/../azure-typescript-langchainjs/packages/langgraph-agent/src/config/system_prompt.ts" :::

Create the nodes constants:

:::code language="typescript" source="~/../azure-typescript-langchainjs/packages/langgraph-agent/src/config/nodes.ts" :::

Create example user queries:

:::code language="typescript" source="~/../azure-typescript-langchainjs/packages/langgraph-agent/src/config/user_queries.ts" :::

> [!WARNING]
> This article uses keys to access resources. In a production environment, the recommended best practice is to use Azure RBAC and managed identity. This approach eliminates the need to manage or rotate keys, enhancing security and simplifying access control.

## Load documents into Azure AI Search

Expand Down Expand Up @@ -196,8 +209,14 @@ For the **addEdge** method, its name is START ("__start__" defined in the ./src/

This app provides common LangChain functionality:

- [state](https://github.com/Azure-Samples/azure-typescript-langchainjs/packages/langgraph_agent/src/langchain/state.ts)
- [END route](https://github.com/Azure-Samples/azure-typescript-langchainjs/packages/langgraph_agent/src/langchain/check_route_end.ts)
* state management:

:::code language="typescript" source="~/../azure-typescript-langchainjs/packages/langgraph-agent/src/langchain/state.ts" :::

* route termination:

:::code language="typescript" source="~/../azure-typescript-langchainjs/packages/langgraph-agent/src/langchain/check_route_end.ts" :::


The only custom route for this application is the **routeRequiresHrResources**. This route is used to determine if the answer from the **requiresHrResources** node indicates that the user's question should continue on to the **ANSWER_NODE** node. Because this route receives the output of **requiresHrResources**, it is in the same file.

Expand All @@ -208,7 +227,7 @@ The Azure OpenAI integration uses two different models:
- **Embeddings**: Used to insert the documents into the vector store.
- **LLM**: Used to answer questions by querying the vector store and generating responses.

The embeddings client and the LLM client serve distinct purposes. Do not reduce them to a single model or client.
The embeddings client and the LLM client serve different purposes. Do not reduce them to a single model or client.

### Embeddings model

Expand Down