Skip to content

Commit ee9d207

Browse files
Merge pull request #2068 from santiagxf/santiagxf-langchain
LangChain documentation improvements
2 parents 511ee80 + fb950cf commit ee9d207

File tree

2 files changed

+172
-16
lines changed

2 files changed

+172
-16
lines changed

articles/ai-studio/how-to/develop/langchain.md

Lines changed: 172 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ To run this tutorial, you need:
3535

3636
* You can follow the instructions at [Deploy models as serverless APIs](../deploy-models-serverless.md).
3737

38-
* Python 3.8 or later installed, including pip.
38+
* Python 3.9 or later installed, including pip.
3939
* LangChain installed. You can do it with:
4040

4141
```bash
@@ -73,7 +73,7 @@ Once configured, create a client to connect to the endpoint. In this case, we ar
7373

7474
```python
7575
import os
76-
from langchain_azure_ai import AzureAIChatCompletionsModel
76+
from langchain_azure_ai.chat_models import AzureAIChatCompletionsModel
7777
7878
model = AzureAIChatCompletionsModel(
7979
endpoint=os.environ["AZURE_INFERENCE_ENDPOINT"],
@@ -88,7 +88,7 @@ If your endpoint is serving more than one model, like with the [Azure AI model i
8888

8989
```python
9090
import os
91-
from langchain_azure_ai import AzureAIChatCompletionsModel
91+
from langchain_azure_ai.chat_models import AzureAIChatCompletionsModel
9292
9393
model = AzureAIChatCompletionsModel(
9494
endpoint=os.environ["AZURE_INFERENCE_ENDPOINT"],
@@ -97,16 +97,17 @@ model = AzureAIChatCompletionsModel(
9797
)
9898
```
9999

100-
Alternatively, if your endpoint support Microsoft Entra ID, you can use the following code to create the client:
100+
You can use the following code to create the client if your endpoint supports Microsoft Entra ID:
101101

102102
```python
103103
import os
104104
from azure.identity import DefaultAzureCredential
105-
from langchain_azure_ai import AzureAIChatCompletionsModel
105+
from langchain_azure_ai.chat_models import AzureAIChatCompletionsModel
106106
107107
model = AzureAIChatCompletionsModel(
108108
endpoint=os.environ["AZURE_INFERENCE_ENDPOINT"],
109109
credential=DefaultAzureCredential(),
110+
model_name="mistral-large-2407",
110111
)
111112
```
112113

@@ -119,11 +120,12 @@ If you are planning to use asynchronous calling, it's a best practice to use the
119120
from azure.identity.aio import (
120121
DefaultAzureCredential as DefaultAzureCredentialAsync,
121122
)
122-
from langchain_azure_ai import AzureAIChatCompletionsModel
123+
from langchain_azure_ai.chat_models import AzureAIChatCompletionsModel
123124
124125
model = AzureAIChatCompletionsModel(
125126
endpoint=os.environ["AZURE_INFERENCE_ENDPOINT"],
126127
credential=DefaultAzureCredentialAsync(),
128+
model_name="mistral-large-2407",
127129
)
128130
```
129131
@@ -145,7 +147,6 @@ model.invoke(messages)
145147
You can also compose operations as needed in what's called **chains**. Let's now use a prompt template to translate sentences:
146148

147149
```python
148-
from langchain_core.prompts import ChatPromptTemplate
149150
from langchain_core.output_parsers import StrOutputParser
150151
151152
system_template = "Translate the following into {language}:"
@@ -157,6 +158,7 @@ prompt_template = ChatPromptTemplate.from_messages(
157158
As you can see from the prompt template, this chain has a `language` and `text` input. Now, let's create an output parser:
158159
159160
```python
161+
from langchain_core.prompts import ChatPromptTemplate
160162
parser = StrOutputParser()
161163
```
162164
@@ -180,9 +182,11 @@ chain.invoke({"language": "italian", "text": "hi"})
180182
181183
Models deployed to Azure AI Foundry support the Azure AI 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.
182184
183-
In the following example, we create 2 model clients, one is a producer and another one is a verifier. To make the distinction clear, we are using a multi-model endpoint like the [Azure AI model inference service](../../ai-services/model-inference.md) and hence we are passing the parameter `model_name` to use a `Mistral-Large` and a `Mistral-Small` model, quoting the fact that **producing content is more complex than verifying it**.
185+
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 are using a multi-model endpoint like the [Azure AI model inference service](../../ai-services/model-inference.md) and hence we are passing the parameter `model_name` to use a `Mistral-Large` and a `Mistral-Small` model, quoting the fact that **producing content is more complex than verifying it**.
184186
185187
```python
188+
from langchain_azure_ai.chat_models import AzureAIChatCompletionsModel
189+
186190
producer = AzureAIChatCompletionsModel(
187191
endpoint=os.environ["AZURE_INFERENCE_ENDPOINT"],
188192
credential=os.environ["AZURE_INFERENCE_CREDENTIAL"],
@@ -196,15 +200,20 @@ verifier = AzureAIChatCompletionsModel(
196200
)
197201
```
198202
203+
> [!TIP]
204+
> Explore the model card of each of the models to understand the best use cases for each model.
205+
199206
The following example generates a poem written by an urban poet:
200207
201208
```python
209+
from langchain_core.prompts import PromptTemplate
210+
202211
producer_template = PromptTemplate(
203212
template="You are an urban poet, your job is to come up \
204213
verses based on a given topic.\n\
205214
Here is the topic you have been asked to generate a verse on:\n\
206215
{topic}",
207-
input_variables=["topic"]
216+
input_variables=["topic"],
208217
)
209218
210219
verifier_template = PromptTemplate(
@@ -213,14 +222,25 @@ verifier_template = PromptTemplate(
213222
report it. Your response should be only one word either True or False.\n \
214223
Here is the lyrics submitted to you:\n\
215224
{input}",
216-
input_variables=["input"]
225+
input_variables=["input"],
217226
)
218227
```
219228
220229
Now let's chain the pieces:
221230

222231
```python
223-
chain = producer_template | producer | parser | verifier_template | verifier
232+
chain = producer_template | producer | parser | verifier_template | verifier | parser
233+
```
234+
235+
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. The following code shows how to do it:
236+
237+
```python
238+
from langchain_core.runnables import RunnablePassthrough, RunnableParallel
239+
240+
generate_poem = producer_template | producer | parser
241+
verify_poem = verifier_template | verifier | parser
242+
243+
chain = generate_poem | RunnableParallel(poem=RunnablePassthrough(), verification=RunnablePassthrough() | verify_poem)
224244
```
225245

226246
To invoke the chain, identify the inputs required and provide values using the `invoke` method:
@@ -229,9 +249,12 @@ To invoke the chain, identify the inputs required and provide values using the `
229249
chain.invoke({"topic": "living in a foreign country"})
230250
```
231251

232-
> [!TIP]
233-
> Explore the model card of each of the models to understand the best use cases for each model.
234-
252+
```output
253+
{
254+
"peom": "...",
255+
"verification: "false"
256+
}
257+
```
235258
236259
## Use embeddings models
237260
@@ -250,6 +273,7 @@ from langchain_azure_ai.embeddings import AzureAIEmbeddingsModel
250273
embed_model = AzureAIEmbeddingsModel(
251274
endpoint=os.environ["AZURE_INFERENCE_ENDPOINT"],
252275
credential=os.environ['AZURE_INFERENCE_CREDENTIAL'],
276+
model_name="text-embedding-3-large",
253277
)
254278
```
255279
@@ -286,7 +310,7 @@ for doc in results:
286310
If you are using Azure OpenAI service or Azure AI model inference service with OpenAI models with `langchain-azure-ai` package, you may need to use `api_version` parameter to select a specific API version. The following example shows how to connect to an Azure OpenAI model deployment in Azure OpenAI service:
287311
288312
```python
289-
from langchain_azure_ai import AzureAIChatCompletionsModel
313+
from langchain_azure_ai.chat_models import AzureAIChatCompletionsModel
290314
291315
llm = AzureAIChatCompletionsModel(
292316
endpoint="https://<resource>.openai.azure.com/openai/deployments/<deployment-name>",
@@ -301,7 +325,7 @@ llm = AzureAIChatCompletionsModel(
301325
If the deployment is hosted in Azure AI Services, you can use the Azure AI model inference service:
302326
303327
```python
304-
from langchain_azure_ai import AzureAIChatCompletionsModel
328+
from langchain_azure_ai.chat_models import AzureAIChatCompletionsModel
305329
306330
llm = AzureAIChatCompletionsModel(
307331
endpoint="https://<resource>.services.ai.azure.com/models",
@@ -311,8 +335,140 @@ llm = AzureAIChatCompletionsModel(
311335
)
312336
```
313337
338+
## Debugging and troubleshooting
339+
340+
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:
341+
342+
First, configure logging to the level you are interested in:
343+
344+
```python
345+
import sys
346+
import logging
347+
348+
# Acquire the logger for this client library. Use 'azure' to affect both
349+
# 'azure.core` and `azure.ai.inference' libraries.
350+
logger = logging.getLogger("azure")
351+
352+
# Set the desired logging level. logging.INFO or logging.DEBUG are good options.
353+
logger.setLevel(logging.DEBUG)
354+
355+
# Direct logging output to stdout:
356+
handler = logging.StreamHandler(stream=sys.stdout)
357+
# Or direct logging output to a file:
358+
# handler = logging.FileHandler(filename="sample.log")
359+
logger.addHandler(handler)
360+
361+
# Optional: change the default logging format. Here we add a timestamp.
362+
formatter = logging.Formatter("%(asctime)s:%(levelname)s:%(name)s:%(message)s")
363+
handler.setFormatter(formatter)
364+
```
365+
366+
To see the payloads of the requests, when instantiating the client, pass the argument `logging_enable`=`True` to the `client_kwargs`:
367+
368+
```python
369+
import os
370+
from langchain_azure_ai.chat_models import AzureAIChatCompletionsModel
371+
372+
model = AzureAIChatCompletionsModel(
373+
endpoint=os.environ["AZURE_INFERENCE_ENDPOINT"],
374+
credential=os.environ["AZURE_INFERENCE_CREDENTIAL"],
375+
model_name="mistral-large-2407",
376+
client_kwargs={"logging_enable": True},
377+
)
378+
```
379+
380+
Use the client as usual in your code.
381+
382+
## Tracing
383+
384+
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.
385+
386+
### Get your instrumentation connection string
387+
388+
You can configure your application to send telemetry to Azure Application Insights either by:
389+
390+
1. Using the connection string to Azure Application Insights directly:
391+
392+
1. Go to [Azure AI Foundry portal](https://ai.azure.com) and select **Tracing**.
393+
394+
2. Select **Manage data source**. In this screen you can see the instance that is associated with the project.
395+
396+
3. Copy the value at **Connection string** and set it to the following variable:
397+
398+
```python
399+
import os
400+
401+
application_insights_connection_string = "instrumentation...."
402+
```
403+
404+
2. Using the Azure AI Foundry SDK and the project connection string.
405+
406+
1. Ensure you have the package `azure-ai-projects` installed in your environment.
407+
408+
2. Go to [Azure AI Foundry portal](https://ai.azure.com).
409+
410+
3. Copy your project's connection string and set it the following code:
411+
412+
```python
413+
from azure.ai.projects import AIProjectClient
414+
from azure.identity import DefaultAzureCredential
415+
416+
project_client = AIProjectClient.from_connection_string(
417+
credential=DefaultAzureCredential(),
418+
conn_str="<your-project-connection-string>",
419+
)
420+
421+
application_insights_connection_string = project_client.telemetry.get_connection_string()
422+
```
423+
424+
### Configure tracing for Azure AI Foundry
425+
426+
The following code creates a tracer connected to the Azure Application Insights behind a project in Azure 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 may want to disable it on production environments. It defaults to the environment variable `AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED`:
427+
428+
```python
429+
from langchain_azure_ai.callbacks.tracers import AzureAIInferenceTracer
430+
431+
tracer = AzureAIInferenceTracer(
432+
connection_string=application_insights_connection_string,
433+
enable_content_recording=True,
434+
)
435+
```
436+
437+
To configure tracing with your chain, indicate the value config in the `invoke` operation as a callback:
438+
439+
```python
440+
chain.invoke({"topic": "living in a foreign country"}, config={"callbacks": [tracer]})
441+
```
442+
443+
To configure the chain itself for tracing, use the `.with_config()` method:
444+
445+
```python
446+
chain = chain.with_config({"callbacks": [tracer]})
447+
```
448+
449+
Then use the `invoke()` method as usual:
450+
451+
```python
452+
chain.invoke({"topic": "living in a foreign country"})
453+
```
454+
455+
### View traces
456+
457+
To see traces:
458+
459+
1. Go to [Azure AI Foundry portal](https://ai.azure.com).
460+
461+
2. Navigate to **Tracing** section.
462+
463+
3. Identify the trace you have created. It may take a couple of seconds for the trace to show.
464+
465+
:::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":::
466+
467+
Learn more about [how to visualize and manage traces](visualize-traces.md).
468+
314469
## Next steps
315470
316471
* [Develop applications with LlamaIndex](llama-index.md)
472+
* [Visualize and manage traces in Azure AI Foundry](visualize-traces.md)
317473
* [Use the Azure AI model inference service](../../ai-services/model-inference.md)
318474
* [Reference: Azure AI model inference API](../../reference/reference-model-inference-api.md)
215 KB
Loading

0 commit comments

Comments
 (0)