You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/ai-foundry/how-to/develop/langchain.md
+44-44Lines changed: 44 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
-
title: Develop application with LangChain and Azure AI Foundry
2
+
title: Develop applications with LangChain and Azure AI Foundry
3
3
titleSuffix: Azure AI Foundry
4
-
description: This article explains how to use LangChain with models deployed in Azure AI Foundry portal to build advance intelligent applications.
4
+
description: Learn how to use LangChain with models deployed in Azure AI Foundry to build advanced, intelligent applications.
5
5
ms.service: azure-ai-foundry
6
6
ms.custom:
7
7
- ignite-2024
@@ -15,31 +15,31 @@ author: sdgilley
15
15
16
16
# Develop applications with LangChain and Azure AI Foundry
17
17
18
-
LangChain is a development ecosystem that makes as easy possible for developers to build applications that reason. The ecosystem is composed by multiple components. Most of the them can be used by themselves, allowing you to pick and choose whichever components you like best.
18
+
LangChain is a developer ecosystem that makes it easier to build reasoning applications. It includes multiple components, and most of them can be used independently, allowing you to pick and choose the pieces you need.
19
19
20
20
Models deployed to [Azure AI Foundry](https://ai.azure.com/?cid=learnDocs) can be used with LangChain in two ways:
21
21
22
-
-**Using the Azure AI Model Inference API:** All models deployed to Azure AI Foundry support the [Model Inference API](../../../ai-foundry/model-inference/reference/reference-model-inference-api.md), which offers a common set of functionalities that can be used for most of the models in the catalog. The benefit of this API is that, since it's the same for all the models, changing from one to another is as simple as changing the model deployment being use. No further changes are required in the code. When working with LangChain, install the extensions `langchain-azure-ai`.
22
+
-**Use the Azure AI Model Inference API:** All models deployed in Azure AI Foundry support the [Model Inference API](../../../ai-foundry/model-inference/reference/reference-model-inference-api.md), which offers a common set of capabilities across most models in the catalog. Because the API is consistent, switching modelsis as simple as changing the deployment; no code changes are required. With LangChain, install the `langchain-azure-ai` integration.
23
23
24
-
-**Using the model's provider specific API:** Some models, like OpenAI, Cohere, or Mistral, offer their own set of APIs and extensions for LangChain. Those extensions might include specific functionalities that the model support and hence are suitable if you want to exploit them. When working with LangChain, install the extension specific for the model you want to use, like`langchain-openai` or `langchain-cohere`.
24
+
-**Use the model provider’s API:** Some models, such as OpenAI, Cohere, or Mistral, offer their own APIs and LangChain extensions. These extensions might include model-specific capabilities and are suitable if you need to use them. Install the extension for your chosen model, such as`langchain-openai` or `langchain-cohere`.
25
25
26
-
In this tutorial, you learn how to use the packages `langchain-azure-ai`to build applications with LangChain.
26
+
This tutorial shows how to use the `langchain-azure-ai`package with LangChain.
* A model deployment supporting the [Model Inference API](https://aka.ms/azureai/modelinference) deployed. In this example, we use a `Mistral-Large-2411` deployment in the [Foundry Models](../../../ai-foundry/model-inference/overview.md).
34
+
* A model deployment that supports the [Model Inference API](https://aka.ms/azureai/modelinference). This article uses a `Mistral-Large-2411` deployment available in the [Azure AI Foundry model catalog](../../../ai-foundry/model-inference/overview.md).
35
35
* Python 3.9 or later installed, including pip.
36
-
* LangChain installed. You can do it with:
36
+
* LangChain installed. You can install it with:
37
37
38
38
```bash
39
39
pip install langchain
40
40
```
41
41
42
-
*In this example, we're working with the Model Inference API, hence we install the following packages:
Once configured, create a client to connect with the chat model by using the `init_chat_model`. For Azure OpenAI models, configure the client as indicated at [Using Azure OpenAI models](#using-azure-openai-models).
52
+
After configuration, create a client to connect to the chat model using `init_chat_model`. For Azure OpenAI models, see [Use Azure OpenAI models](#use-azure-openai-models).
53
53
54
54
```python
55
55
from langchain.chat_models import init_chat_model
@@ -82,7 +82,7 @@ model = AzureAIChatCompletionsModel(
82
82
> [!NOTE]
83
83
> When using Microsoft Entra ID, make sure that the endpoint was deployed with that authentication method and that you have the required permissions to invoke it.
84
84
85
-
If you're planning to use asynchronous calling, it's a best practice to use the asynchronous version for the credentials:
85
+
If you plan to use asynchronous calls, use the asynchronous version of the credentials:
86
86
87
87
```python
88
88
from azure.identity.aio import (
@@ -97,7 +97,7 @@ model = AzureAIChatCompletionsModel(
97
97
)
98
98
```
99
99
100
-
If your endpoint is serving one model, like with the serverless API deployment, you don't have to indicate`model` parameter:
100
+
If your endpoint serves a single model (for example, serverless API deployments), omit the`model` parameter:
101
101
102
102
```python
103
103
import os
@@ -109,13 +109,13 @@ model = AzureAIChatCompletionsModel(
109
109
)
110
110
```
111
111
112
-
## Use chat completions models
112
+
## Use chat completion models
113
113
114
-
Let's first use the model directly. `ChatModels` are instances of LangChain `Runnable`, which means they expose a standard interface for interacting with them. To call the model, we can pass in a list of messages to the `invoke` method.
114
+
Use the model directly. `ChatModels` are instances of the LangChain `Runnable` interface, which provides a standard way to interact with them. To call the model, pass a list of messages to the `invoke` method.
Models deployed to Azure AI Foundry support the Model Inference API, which is standard across all the models. Chain multiple LLM operations based on the capabilities of each model so you can optimize for the right model based on capabilities.
148
+
Because models in Azure AI Foundry expose a common Model Inference API, you can chain multiple LLM operations and choose the model best suited to each step.
149
149
150
-
In the following example, we create two model clients. One is a producer and another one is a verifier. To make the distinction clear, we're using a multi-model endpoint like the [Foundry Models API](../../model-inference/overview.md) and hence we're passing the parameter `model` to use a `Mistral-Large` and a `Mistral-Small` model, quoting the fact that **producing content is more complex than verifying it**.
150
+
In the following example, we create two model clients: one producer and one verifier. To make the distinction clear, we use a multi-model endpoint such as the [Model Inference API](../../model-inference/overview.md) and pass the `model`parameter to use `Mistral-Large`for generation and `Mistral-Small`for verification. Producing content generally requires a larger model, while verification can use a smaller one.
The previous chain returns the output of the step `verifier`only. Since we want to access the intermediate result generated by the `producer`, in LangChain you need to use a `RunnablePassthrough`object to also output that intermediate step.
166
+
The previous chain returns only the output of the `verifier`step. To access the intermediate result generated by the `producer`, use a `RunnablePassthrough` to output that intermediate step.
167
167
168
168
```python
169
169
from langchain_core.runnables import RunnablePassthrough, RunnableParallel
In the same way, you create an LLM client, you can connect to an embeddings model. In the following example, we're setting the environment variable to now point to an embeddings model:
184
+
Create an embeddings client similarly. Set the environment variables to point to an embeddings model:
If you need to debug your application and understand the requests sent to the models in Azure AI Foundry, you can use the debug capabilities of the integration as follows:
225
+
If you need to debug your application and understand the requests sent to models in Azure AI Foundry, use the integration’s debug capabilities:
226
226
227
-
First, configure logging to the level you are interested in:
@@ -238,7 +238,7 @@ Use the client as usual in your code.
238
238
239
239
## Tracing
240
240
241
-
You can use the tracing capabilities in Azure AI Foundry by creating a tracer. Logs are stored in Azure Application Insights and can be queried at any time using Azure Monitor or Azure AI Foundry portal. Each AI Hub has an Azure Application Insights associated with it.
241
+
Use tracing in Azure AI Foundry by creating a tracer. Logs are stored in Azure Application Insights and can be queried at any time using Azure Monitor or the Azure AI Foundry portal. Each AI hub has an associated Azure Application Insights instance.
242
242
243
243
### Get your instrumentation connection string
244
244
@@ -282,27 +282,27 @@ You can configure your application to send telemetry to Azure Application Insigh
282
282
283
283
### Configure tracing for Azure AI Foundry
284
284
285
-
The following code creates a tracer connected to the Azure Application Insights behind a project inAzure AI Foundry. Notice that the parameter `enable_content_recording` is set to `True`. This enables the capture of the inputs and outputs of the entire application as well as the intermediate steps. Such is helpful when debugging and building applications, but you might want to disable it on production environments. It defaults to the environment variable `AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED`:
285
+
The following code creates a tracer connected to the Azure Application Insights behind an Azure AI Foundry project. The `enable_content_recording`parameter is set to `True`, which captures inputs and outputs across the application, including intermediate steps. This is helpful when debugging and building applications, but you might want to disable it in production environments. You can also control this via the `AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED` environment variable:
286
286
287
287
```python
288
-
from langchain_azure_ai.callbacks.tracers import AzureAIInferenceTracer
288
+
from langchain_azure_ai.callbacks.tracers import AzureAIOpenTelemetryTracer
3. Identify the trace you have created. It may take a couple of seconds for the trace to show.
322
+
3. Identify the trace you created. It may take a few seconds to appear.
323
323
324
324
:::image type="content" source="../../media/how-to/develop-langchain/langchain-portal-tracing-example.png" alt-text="A screenshot showing the trace of a chain." lightbox="../../media/how-to/develop-langchain/langchain-portal-tracing-example.png":::
0 commit comments