Skip to content

Commit f5b3b8b

Browse files
committed
Merge branch 'patch-1' of https://github.com/truptiparkar7/azure-ai-docs-pr into aiftrace0225
2 parents 948b398 + 284eded commit f5b3b8b

File tree

1 file changed

+107
-16
lines changed

1 file changed

+107
-16
lines changed

articles/ai-foundry/how-to/develop/trace-local-sdk.md

Lines changed: 107 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,66 @@ ms.author: lagayhar
1414
author: lgayhardt
1515
---
1616

17-
# How to trace your application with Azure AI Inference SDK
17+
# How to trace your application with Azure AI Foundry Project Library
1818

1919
[!INCLUDE [feature-preview](../../includes/feature-preview.md)]
2020

21-
In this article you'll learn how to trace your application with Azure AI Inference SDK with your choice between using Python, JavaScript, or C#. The Azure AI Inference client library provides support for tracing with OpenTelemetry.
22-
23-
## Enable trace in your application
21+
In this article you'll learn how to trace your application with Azure AI Foundry SDK with your choice between using Python, JavaScript, or C#. This provides support for tracing with OpenTelemetry.
2422

2523
### Prerequisites
2624

2725
- An [Azure Subscription](https://azure.microsoft.com/).
28-
- An Azure AI project, see [Create a project in Azure AI Foundry portal](../create-projects.md).
26+
- An Azure AI project, see [Create a project in Azure AI Foundry portal](https://learn.microsoft.com/en-us/azure/ai-studio/how-to/create-projects?tabs=ai-studio).
2927
- An AI model supporting the [Azure AI model inference API](https://aka.ms/azureai/modelinference) deployed through Azure AI Foundry.
3028
- If using Python, you need Python 3.8 or later installed, including pip.
3129
- If using JavaScript, the supported environments are LTS versions of Node.js.
3230

31+
## Tracing using Azure AI Foundry Project Library
32+
# [Python](#tab/python)
33+
The best way to get started using the Azure AI Foundry SDK is by using a project. AI projects connect together different data, assets, and services you need to build AI applications. The AI project client allows you to easily access these project components from your code by using a single connection string. First follow steps to [create an AI Project](https://learn.microsoft.com/en-us/azure/ai-studio/how-to/create-projects?tabs=ai-studio) if you don't have one already.
34+
To enable tracing, first ensure your project has an attached Application Insights resource. Go to the **Tracing** page of your project in Azure AI Foundry portal and follow instructions to create or attach Application Insights. If one was enabled, you can get the Application Insights connection string, and observe the full execution path through Azure Monitor.
35+
36+
Make sure to install following packages via
37+
38+
```
39+
pip install opentelemetry-sdk
40+
pip install azure-core-tracing-opentelemetry
41+
pip install azure-monitor-opentelemetry
42+
```
43+
44+
Refer the following samples to get started with tracing using Azure AI Project SDK:
45+
- [Python Sample with console tracing for Azure AI Inference Client](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/inference/sample_chat_completions_with_azure_ai_inference_client_and_console_tracing.py) containing fully runnable Python code for tracing using synchronous and asynchronous clients.
46+
- [Python Sample with Azure Monitor for Azure AI Inference Client](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-inference/samples/sample_chat_completions_with_tracing.py) containing fully runnable Python code for tracing using synchronous and asynchronous clients.
47+
- [Python Sample with console tracing for Azure Open AI](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/inference/sample_chat_completions_with_azure_openai_client_and_console_tracing.py) containing fully runnable Python code for tracing using synchronous and asynchronous clients.
48+
- [Python Sample with Azure Monitor for Azure Open AI](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/inference/sample_chat_completions_with_azure_openai_client_and_azure_monitor_tracing.py) containing fully runnable Python code for tracing using synchronous and asynchronous clients.
49+
50+
# [JavaScript](#tab/javascript)
51+
52+
Tracing is not yet integrated into the Azure AI Projects SDK for JS. For instructions on how to instrument and log traces from the Azure AI Inference package, see [JavaScript samples](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src).
53+
54+
# [C#](#tab/csharp)
55+
56+
Tracing is not yet integrated into the Azure AI Projects SDK for C#. For instructions on how to instrument and log traces from the Azure AI Inference package, see [azure-sdk-for-dotnet](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/ai/Azure.AI.Inference/samples/Sample8_ChatCompletionsWithOpenTelemetry.md).
57+
58+
----
59+
60+
\
61+
Refer the following samples to get started with tracing using Azure AI Project Library for Azure OpenAI:
62+
63+
64+
### [JavaScript](#tab/javascript)
65+
Currently this is supported in Python only.
66+
67+
### [C#](#tab/csharp)
68+
Currently this is supported in Python only.
69+
70+
----
71+
72+
## Enable Tracing using Azure AI Inference Library
73+
3374
### Installation
3475

35-
# [Python](#tab/python)
76+
### [Python](#tab/python)
3677

3778
Install the package `azure-ai-inference` using your package manager, like pip:
3879

@@ -43,7 +84,7 @@ Install the package `azure-ai-inference` using your package manager, like pip:
4384
Install the Azure Core OpenTelemetry Tracing plugin, OpenTelemetry, and the OTLP exporter for sending telemetry to your observability backend. To install the necessary packages for Python, use the following pip commands:
4485

4586
```bash
46-
pip install opentelemetry
87+
pip install opentelemetry-sdk
4788

4889
pip install opentelemetry-exporter-otlp
4990
```
@@ -81,14 +122,6 @@ To learn more , see the [Inference SDK reference](../../../ai-foundry/model-infe
81122
You need to add following configuration settings as per your use case:
82123

83124
- To capture prompt and completion contents, set the `AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED` environment variable to true (case insensitive). By default, prompts, completions, function names, parameters, or outputs aren't recorded.
84-
- To enable Azure SDK tracing, set the `AZURE_SDK_TRACING_IMPLEMENTATION` environment variable to opentelemetry. Alternatively, you can configure it in the code with the following snippet:
85-
86-
```python
87-
from azure.core.settings import settings
88-
89-
settings.tracing_implementation = "opentelemetry"
90-
```
91-
92125
To learn more, see [Azure Core Tracing OpenTelemetry client library for Python](/python/api/overview/azure/core-tracing-opentelemetry-readme).
93126

94127
# [JavaScript](#tab/javascript)
@@ -131,7 +164,7 @@ AIInferenceInstrumentor().instrument()
131164

132165
```
133166

134-
It's also possible to uninstrument the Azure AI Inferencing API by using the uninstrument call. After this call, the traces will no longer be emitted by the Azure AI Inferencing API until instrument is called again:
167+
It's also possible to uninstrument the Azure AI Inference API by using the uninstrument call. After this call, the traces will no longer be emitted by the Azure AI Inference API until instrument is called again:
135168

136169
```python
137170
AIInferenceInstrumentor().uninstrument()
@@ -207,12 +240,70 @@ To configure OpenTelemetry and enable Azure AI Inference tracing follow these st
207240

208241
To trace your own custom functions, you can leverage OpenTelemetry, you'll need to instrument your code with the OpenTelemetry SDK. This involves setting up a tracer provider and creating spans around the code you want to trace. Each span represents a unit of work and can be nested to form a trace tree. You can add attributes to spans to enrich the trace data with additional context. Once instrumented, configure an exporter to send the trace data to a backend for analysis and visualization. For detailed instructions and advanced usage, refer to the [OpenTelemetry documentation](https://opentelemetry.io/docs/). This will help you monitor the performance of your custom functions and gain insights into their execution.
209242
243+
### Using service name in trace data
244+
245+
To identify your service via a unique ID in Application Insights, you can use the service name OpenTelemetry property in your trace data. This is particularly useful if you're logging data from multiple applications to the same Application Insights resource, and you want to differentiate between them. For example, lets say you have two applications: **App-1** and **App-2**, with tracing configured to log data to the same Application Insights resource. Perhaps you'd like to set up **App-1** to be evaluated continuously by **Relevance** and **App-2** to be evaluated continuously by **Groundedness**. You can use the service name to differentiate between the applications in your Online Evaluation configurations.
246+
247+
To set up the service name property, you can do so directly in your application code by following the steps, see [Using multiple tracer providers with different Resource](https://opentelemetry.io/docs/languages/python/cookbook/#using-multiple-tracer-providers-with-different-resource). Alternatively, you can set the environment variable `OTEL_SERVICE_NAME` prior to deploying your app. To learn more about working with the service name, see [OTEL Environment Variables](https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/#general-sdk-configuration) and [Service Resource Semantic Conventions](https://opentelemetry.io/docs/specs/semconv/resource/#service).
248+
249+
To query trace data for a given service name, query for the `cloud_roleName` property. In case you are leveraging Online Evaluation, add the following line to the KQL query you use within your Online Evaluation set-up:
250+
251+
```sql
252+
| where cloud_RoleName == "service_name"
253+
```
254+
255+
## Enable Tracing for Langchain
256+
257+
# [Python](#tab/python)
258+
You can enable tracing for Langchain that follows Opentelemetry standards as per [opentelemetry-instrumentation-langchain](https://pypi.org/project/opentelemetry-instrumentation-langchain/) To enable tracing for Langchain, follow following steps:
259+
260+
Install the package `opentelemetry-instrumentation-langchain` using your package manager, like pip:
261+
262+
```bash
263+
pip install opentelemetry-instrumentation-langchain
264+
```
265+
266+
Once necessary packages are installed, you can easily enable tracing via [Tracing using Azure AI Foundry Project](#tracing-using-azure-ai-foundry-project)
267+
268+
# [JavaScript](#tab/javascript)
269+
Currently this is supported in Python only.
270+
271+
# [C#](#tab/csharp)
272+
Currently this is supported in Python only.
273+
274+
----
275+
210276
## Attach User feedback to traces
211277

278+
212279
To attach user feedback to traces and visualize them in Azure AI Foundry portal using OpenTelemetry's semantic conventions, you can instrument your application enabling tracing and logging user feedback. By correlating feedback traces with their respective chat request traces using the response ID, you can use view and manage these traces in Azure AI Foundry portal. OpenTelemetry's specification allows for standardized and enriched trace data, which can be analyzed in Azure AI Foundry portal for performance optimization and user experience insights. This approach helps you use the full power of OpenTelemetry for enhanced observability in your applications.
280+
Please follow the following format to log user feedback:
281+
The user feedback evaluation event can be captured if and only if user provided a reaction to GenAI model response.
282+
It SHOULD, when possible, be parented to the GenAI span describing such response.
283+
284+
<!-- prettier-ignore-start -->
285+
<!-- markdownlint-capture -->
286+
<!-- markdownlint-disable -->
287+
The event name MUST be `gen_ai.evaluation.user_feedback`.
288+
289+
| Attribute | Type | Description | Examples | [Requirement Level](https://opentelemetry.io/docs/specs/semconv/general/attribute-requirement-level/) | Stability |
290+
|---|---|---|---|---|---|
291+
| [`gen_ai.response.id`](/docs/attributes-registry/gen-ai.md) | string | The unique identifier for the completion. | `chatcmpl-123` | `Required` | ![Experimental](https://img.shields.io/badge/-experimental-blue) |
292+
| [`gen_ai.evaluation.score`](/docs/attributes-registry/gen-ai.md) | double | Quantified score calculated based on the user reaction in [-1.0, 1.0] range with 0 representing a neutral reaction. | `0.42` | `Recommended` | ![Experimental](https://img.shields.io/badge/-experimental-blue) |
293+
294+
<!-- markdownlint-restore -->
295+
<!-- prettier-ignore-end -->
296+
<!-- END AUTOGENERATED TEXT -->
297+
The user feedback event body has the following structure:
298+
299+
| Body Field | Type | Description | Examples | Requirement Level |
300+
|---|---|---|---|---|
301+
| `comment` | string | Additional details about the user feedback | `"I did not like it"` | `Opt-in` |
213302

214303
## Related content
215304

216305
- [Python samples](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-inference/samples/sample_chat_completions_with_tracing.py) containing fully runnable Python code for tracing using synchronous and asynchronous clients.
306+
- [Sample Agents with Console tracing](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/agents/sample_agents_functions_with_console_tracing.py)
307+
- [Sample Agents with Azure Monitor](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/agents/sample_agents_basics_with_azure_monitor_tracing.py)
217308
- [JavaScript samples](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/ai/ai-inference-rest/samples/v1-beta/typescript/src) containing fully runnable JavaScript code for tracing using synchronous and asynchronous clients.
218309
- [C# Samples](https://github.com/Azure/azure-sdk-for-net/blob/Azure.AI.Inference_1.0.0-beta.2/sdk/ai/Azure.AI.Inference/samples/Sample8_ChatCompletionsWithOpenTelemetry.md) containing fully runnable C# code for doing inference using synchronous and asynchronous methods.

0 commit comments

Comments
 (0)