|
1 | 1 | ---
|
2 |
| -title: How to trace your application with prompt flow SDK |
| 2 | +title: How to trace your application with Azure AI Inference SDK |
3 | 3 | titleSuffix: Azure AI Studio
|
4 |
| -description: This article provides instructions on how to trace your application with prompt flow SDK. |
| 4 | +description: This article provides instructions on how to trace your application with Azure AI Inference SDK. |
5 | 5 | manager: scottpolly
|
6 | 6 | ms.service: azure-ai-studio
|
7 | 7 | ms.custom:
|
8 | 8 | - build-2024
|
9 | 9 | ms.topic: how-to
|
10 |
| -ms.date: 5/21/2024 |
11 |
| -ms.reviewer: chenlujiao |
| 10 | +ms.date: 11/19/2024 |
| 11 | +ms.reviewer: truptiparkar |
12 | 12 | ms.author: lagayhar
|
13 | 13 | author: lgayhardt
|
14 | 14 | ---
|
15 | 15 |
|
16 |
| -# How to trace your application with prompt flow SDK | Azure AI Studio |
| 16 | +# How to trace your application with Azure AI Inference SDK |
17 | 17 |
|
18 | 18 | [!INCLUDE [feature-preview](../../includes/feature-preview.md)]
|
19 | 19 |
|
20 |
| -Tracing is a powerful tool that offers developers an in-depth understanding of the execution process of their generative AI applications such as agents, [AutoGen](https://microsoft.github.io/autogen/docs/Use-Cases/agent_chat), and retrieval augmented generation (RAG) use cases. It provides a detailed view of the execution flow, including the inputs and outputs of each node within the application. This essential information proves critical while debugging complex applications or optimizing performance. |
| 20 | +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. |
21 | 21 |
|
22 |
| -While more developers are using various frameworks such as Langchain, Semantic Kernel, OpenAI, and various types of agents to create LLM-based applications. Tracing with the prompt flow SDK offers enhanced visibility and simplified troubleshooting for LLM-based applications, effectively supporting development, iteration, and production monitoring. Tracing in AI Studio follows the [OpenTelemetry specification](https://opentelemetry.io/docs/specs/otel/), capturing and visualizing the internal execution details of any AI application, enhancing the overall development experience. |
23 |
| - |
24 |
| -## Benefits of AI Studio tracing on the enterprise-grade cloud platform |
| 22 | +## Enable trace in your application |
25 | 23 |
|
26 |
| -Moreover, we now offer persistent local testing on AI Studio, which is the enterprise-grade cloud platform, significantly enhancing collaboration, persistence, and test history management. |
| 24 | +### Prerequisites |
27 | 25 |
|
28 |
| -With tracing, you can: |
29 |
| -- Have a cloud-based location to persist and track your historical tests. |
30 |
| -- Easily extract and visualize the test results, comparing the outputs of different test cases. |
31 |
| -- Reuse your previous test assets for later usage, for example, human feedback, data curation, etc. |
32 |
| -- Facilitate better resource utilization in the future. |
33 |
| -- Debug and optimize your application with ease. To get started with debugging LLM application scenarios, refer to [Tracing with LLM application](https://microsoft.github.io/promptflow/tutorials/trace-llm.html) |
34 |
| -- Analyze retrieval and generation processes in RAG applications. |
35 |
| -- Observe the multi-agents interactions in the multi-agent scenarios. To get started with tracing in multi-agent scenarios, refer to [Tracing with AutoGen](https://microsoft.github.io/promptflow/tutorials/trace-autogen-groupchat.html). |
| 26 | +- An [Azure Subscription](https://azure.microsoft.com/). |
| 27 | +- An Azure AI project, see [Create a project in Azure AI Studio](../create-projects.md). |
| 28 | +- An AI model supporting the [Azure AI model inference API](https://aka.ms/azureai/modelinference) deployed through AI Studio. |
| 29 | +- If using Python, you need Python 3.8 or later installed, including pip. |
| 30 | +- If using JavaScript, the supported environments are LTS versions of Node.js. |
36 | 31 |
|
37 |
| -## Log and view traces of your applications |
| 32 | +### Installation |
38 | 33 |
|
39 |
| -AI Studio provides the tracing capability for logging and managing your LLM applications tests and evaluations, while debugging and observing by drilling down the trace view. |
| 34 | +# [Python](#tab/python) |
40 | 35 |
|
41 |
| -The tracing any application feature today is implemented in the [prompt flow open-source package](https://microsoft.github.io/promptflow/), to enable user to trace LLM call or function, and LLM frameworks like LangChain and AutoGen, regardless of which framework you use, following [OpenTelemetry specification](https://opentelemetry.io/docs/specs/otel/). |
| 36 | +Install the package `azure-ai-inference` using your package manager, like pip: |
42 | 37 |
|
43 |
| -## Enable trace in your application |
| 38 | +```bash |
| 39 | + pip install azure-ai-inference[opentelemetry] |
| 40 | +``` |
44 | 41 |
|
45 |
| -Code first - Make sure you have annotated your code for tracing in prompt flow! |
| 42 | +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: |
46 | 43 |
|
47 |
| -- [Installing prompt flow](https://microsoft.github.io/promptflow/how-to-guides/quick-start.html#set-up-your-dev-environment) : require promptflow-tracing |
48 |
| -- [Instrumenting your application code](https://microsoft.github.io/promptflow/how-to-guides/tracing/index.html#instrumenting-user-s-code) : using `@trace` and `start_trace()`. |
49 |
| -- [Test and view trace in local](https://microsoft.github.io/promptflow/how-to-guides/tracing/trace-ui.html) |
| 44 | +```bash |
| 45 | +pip install opentelemetry |
50 | 46 |
|
51 |
| -More details about tracing in prompt flow, please refer to [this prompt flow documentation](https://microsoft.github.io/promptflow/how-to-guides/tracing/index.html#tracing). |
| 47 | +pip install opentelemetry-exporter-otlp |
| 48 | +``` |
52 | 49 |
|
53 |
| -## Log the trace to AI Studio |
| 50 | +# [JavaScript](#tab/javascript) |
54 | 51 |
|
55 |
| -### Set the trace destination |
| 52 | +Install the package `@azure-rest/ai-inference` for JavaScript using npm: |
56 | 53 |
|
57 |
| -By default, the trace is logged and viewed in your local environment. To log it in the AI Studio in the cloud, you need to set the `trace destination` to a specified AI Studio project. |
| 54 | +```bash |
| 55 | + npm install @azure-rest/ai-inference |
| 56 | +``` |
58 | 57 |
|
59 |
| -You can refer to the following steps to set the trace destination to AI Studio project. |
| 58 | +# [C#](#tab/csharp) |
60 | 59 |
|
61 |
| -First, ensure that Azure CLI is installed and logged in: |
| 60 | +Install the Azure AI Inference client library for .NET with [NuGet](https://aka.ms/azsdk/azure-ai-inference/csharp/package): |
62 | 61 |
|
63 |
| -```azurecli |
64 |
| -az login |
| 62 | +```dotnetcli |
| 63 | + dotnet add package Azure.AI.Inference --prerelease |
65 | 64 | ```
|
66 | 65 |
|
67 |
| -Next, execute the following command to set the trace destination. Replace `<your_subscription_id>`, `<your_resourcegroup_name>`, and `<your_studio_project_name>` with your specific subscription ID, resource group name, and AI Studio project name: |
| 66 | +--- |
| 67 | + |
| 68 | +To learn more, see the [Inference SDK reference](../../reference/reference-model-inference-api.md). |
68 | 69 |
|
69 |
| -```azurecli |
70 |
| -pf config set trace.destination=azureml://subscriptions/<your_subscription_id>/resourcegroups/<your_resourcegroup_name>/providers/Microsoft.MachineLearningServices/workspaces/<your_studio_project_name> |
71 |
| -``` |
| 70 | +### Configuration |
| 71 | + |
| 72 | +# [Python](#tab/python) |
| 73 | + |
| 74 | +You need to add following configuration settings as per your use case: |
| 75 | + |
| 76 | +- 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. |
| 77 | +- 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: |
72 | 78 |
|
73 |
| -> [!NOTE] |
74 |
| -> The West US 3 (`westus3`) region does not support tracing. |
| 79 | + ```python |
| 80 | + from azure.core.settings import settings |
| 81 | + |
| 82 | + settings.tracing_implementation = "opentelemetry" |
| 83 | + ``` |
75 | 84 |
|
76 |
| -### Collections |
| 85 | + To learn more, see [Azure Core Tracing OpenTelemetry client library for Python](/python/api/overview/azure/core-tracing-opentelemetry-readme). |
77 | 86 |
|
78 |
| -A **collection** is a group of associated traces. In [AI Studio](https://ai.azure.com), these collections along with their internal traces are managed and stored in the **Tracing** module under the **Collections** tab. |
| 87 | +# [JavaScript](#tab/javascript) |
79 | 88 |
|
80 |
| -1. Go to your project in [AI Studio](https://ai.azure.com). |
81 |
| -1. From the left pane, select **Tracing**. You can see the **Collections** tab. You can only see your own collections in the list of collections. In this example, there aren't any collections yet. |
| 89 | +Instrumentation is only supported for Chat Completion without streaming. To enable instrumentation, you need to register exporter(s). Following is an example of how to add a console exporter. |
82 | 90 |
|
83 |
| - :::image type="content" source="../../media/how-to/tracing/tracing-collections-empty.png" alt-text="Screenshot of the button to add a new connection." lightbox="../../media/how-to/tracing/tracing-collections-empty.png"::: |
| 91 | +Console Exporter: |
84 | 92 |
|
85 |
| -The collection tab displays a comprehensive list of all the collections you've created. It shows essential metadata for each collection, including its name, run location, last updated time, and created time. |
| 93 | +```javascript |
| 94 | +import { ConsoleSpanExporter, NodeTracerProvider, SimpleSpanProcessor } from "@opentelemetry/sdk-trace-node"; |
86 | 95 |
|
87 |
| -- **Run location**: Indicates whether the application runs locally or in the cloud. The cloud collection is associated with specific prompt flow cloud authoring test history and generated traces. In this case, the collection name is the same as the prompt flow display name. |
88 |
| -- **Updated on**: Shows the most recent time a new trace was logged to a collection. By default, collections are sorted in descending order based on their updated times. |
89 |
| -- **Created on**: The time when the collection was initially created. |
| 96 | +const provider = new NodeTracerProvider(); |
90 | 97 |
|
91 |
| -By selecting a collection's name, you can access a list of all the traces within that collection. Only a subset of traces can be shared with others. Refer to [share trace](#share-trace) for more information. |
| 98 | +provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); |
92 | 99 |
|
93 |
| -When logging a trace, you have the option to [specify a collection name](#customize-the-collections) to group it with other related traces. You can create multiple collections for better organization of your traces. If a collection name isn't specified when logging a trace, it defaults to the **project folder name** or to the **default collection**. |
| 100 | +provider.register(); |
| 101 | +``` |
| 102 | + |
| 103 | +# [C#](#tab/csharp) |
| 104 | + |
| 105 | +Distributed tracing and metrics with OpenTelemetry are supported in Azure AI Inference in experimental mode and could be enabled through either of these steps: |
94 | 106 |
|
95 |
| -#### Customize the collections |
| 107 | +- Set the `AZURE_EXPERIMENTAL_ENABLE_ACTIVITY_SOURCE` environment variable to true. |
| 108 | +- Set the `Azure.Experimental.EnableActivitySource` context switch to true in your application code. |
| 109 | + |
| 110 | +--- |
96 | 111 |
|
97 |
| -For better organization of your traces, you can specify a custom collection name when logging a trace. |
| 112 | +### Enable Instrumentation |
98 | 113 |
|
99 |
| -# [Python SDK](#tab/python) |
| 114 | +# [Python](#tab/python) |
100 | 115 |
|
101 |
| -If you're tracing your own application, you can set the collection name in the `start_trace()` function in your code: |
| 116 | +The final step is to enable Azure AI Inference instrumentation with the following code snippet: |
102 | 117 |
|
103 | 118 | ```python
|
104 |
| -from promptflow.tracing import start_trace, trace |
| 119 | +from azure.ai.inference.tracing import AIInferenceInstrumentor |
105 | 120 |
|
106 |
| -@trace |
107 |
| -def my_function(input: str) -> str: |
108 |
| - output = input + "Hello World!" |
109 |
| - return output |
| 121 | +# Instrument AI Inference API |
| 122 | + |
| 123 | +AIInferenceInstrumentor().instrument() |
110 | 124 |
|
111 |
| -my_function("This is my function") |
112 |
| -start_trace(collection="my_custom_collection") |
113 | 125 | ```
|
114 | 126 |
|
115 |
| -# [Azure CLI](#tab/cli) |
116 |
| -```azurecli |
117 |
| -# Test flow |
118 |
| -pf flow test --flow <flow-name> --collection my_custom_collection |
| 127 | +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: |
| 128 | + |
| 129 | +```python |
| 130 | +AIInferenceInstrumentor().uninstrument() |
119 | 131 | ```
|
120 | 132 |
|
121 |
| ---- |
| 133 | +# [JavaScript](#tab/javascript) |
122 | 134 |
|
123 |
| -More details about customizing collections, please refer to [tracing tutorial](https://microsoft.github.io/promptflow/reference/python-library-reference/promptflow-tracing/promptflow.tracing.html) and [prompt flow command](https://microsoft.github.io/promptflow/reference/pf-command-reference.html#pf). |
| 135 | +To use instrumentation for Azure SDK, you need to register it before importing any dependencies from `@azure/core-tracing`, such as `@azure-rest/ai-inference`. |
124 | 136 |
|
| 137 | +```Javascript |
| 138 | +import { registerInstrumentations } from "@opentelemetry/instrumentation"; |
125 | 139 |
|
126 |
| -### View the traces |
| 140 | +import { createAzureSdkInstrumentation } from "@azure/opentelemetry-instrumentation-azure-sdk"; |
127 | 141 |
|
128 |
| -First, you must complete the previous steps to view the traces in the cloud: |
129 |
| -- [Enable trace in your application](#enable-trace-in-your-application). |
130 |
| -- [Set cloud trace destination](#set-the-trace-destination). |
131 | 142 |
|
132 |
| -Now, run your python script directly. Upon successful execution, a cloud trace link appears in the output. It might look something like this: |
| 143 | +registerInstrumentations({ |
| 144 | + |
| 145 | + instrumentations: [createAzureSdkInstrumentation()], |
| 146 | + |
| 147 | +}); |
| 148 | + |
| 149 | +import ModelClient from "@azure-rest/ai-inference"; |
133 | 150 |
|
134 |
| -```bash |
135 |
| -Starting prompt flow service... |
136 |
| -... |
137 |
| -You can view the traces in cloud from AI Studio: https://ai.azure.com/projecttrace/detail/.... |
138 | 151 | ```
|
139 | 152 |
|
140 |
| -Selecting the URL to navigate to a trace detail page on the cloud portal. This page is similar to the local trace view. |
| 153 | +When making a call for chat completion, you need to include the tracingOptions with the active tracing context: |
141 | 154 |
|
142 |
| -The **trace detail view** provides a comprehensive and structured overview of the operations within your application. |
| 155 | +```javascript |
143 | 156 |
|
144 |
| -**Understand the trace detail view** |
| 157 | +import { context } from "@opentelemetry/api"; |
145 | 158 |
|
146 |
| -In the top right corner of the trace view, you find: |
147 |
| -* Trace name: This is same as the root span name, representing the entry function name of your application. |
148 |
| -* Status: This could either be "completed" or "failed". |
149 |
| -* Total duration: This is total duration time of the test execution. Hover over to view the start and end times. |
150 |
| -* Total tokens: This is the total token cost of the test. Hover over to view the prompt tokens and completed tokens. |
151 |
| -* Created time: The time at which the trace was created. |
| 159 | +client.path("/chat/completions").post({ |
152 | 160 |
|
153 |
| -On the left side, you can see a **hierarchical tree structure**. This structure shows the sequence of function calls. Each function call's metadata is organized into [spans](https://opentelemetry.io/docs/concepts/signals/traces/#spans). These spans are linked together in a tree-like structure, illustrating the sequence of execution. |
| 161 | + body: {...}, |
154 | 162 |
|
155 |
| -In prompt flow SDK, we defined several span types, including LLM, Function, Embedding, Retrieval, and Flow. And the system automatically creates spans with execution information in designated attributes and events. |
| 163 | + tracingOptions: { tracingContext: context.active() } |
156 | 164 |
|
157 |
| -Each span allows you to view: |
158 |
| -* Function name: By default, this is the name of the function as defined in your code. However, it can also be a customized span name defined via [Open Telemetry](https://opentelemetry.io/docs/what-is-opentelemetry/). |
159 |
| -* Duration: This represents the duration for which the function ran. Hover over to view the start and end times. |
160 |
| -* Tokens for LLM calls: This is the token cost of the LLM call. Hover over to view the prompt tokens and completed tokens. |
| 165 | +}); |
| 166 | + |
| 167 | +``` |
161 | 168 |
|
162 |
| -By selecting a specific span, you can shift to viewing its associated detailed information on the right side. This information includes *input*, *output*, *raw JSON*, *logs*, and *exceptions*, which are essential for observing and debugging your application. |
| 169 | +# [C#](#tab/csharp) |
163 | 170 |
|
164 |
| -For the **LLM** span, a clear conversation view is provided. This includes the *system prompt*, *user prompt*, and *assistant response*. This information is especially crucial in multi-agent cases, as it allows you to understand the flow of the conversation and the interaction within the LLM intermediate auto-calling. |
| 171 | +To configure OpenTelemetry and enable Azure AI Inference tracing follow these steps: |
| 172 | + |
| 173 | +1. **Install OpenTelemetry Packages**: Install the following dependencies for HTTP tracing and metrics instrumentation as well as console and [OTLP](https://opentelemetry.io/docs/specs/otel/protocol/) exporters: |
| 174 | + |
| 175 | + ```csharp |
| 176 | + dotnet add package OpenTelemetry.Instrumentation.Http |
| 177 | + |
| 178 | + dotnet add package OpenTelemetry.Exporter.Console |
| 179 | + |
| 180 | + dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol |
| 181 | + ``` |
| 182 | + |
| 183 | +1. **Enable Experimental Azure SDK Observability**: Set the context switch to enable experimental Azure SDK observability: |
| 184 | + |
| 185 | + ```csharp |
| 186 | + AppContext.SetSwitch("Azure.Experimental.EnableActivitySource", true); |
| 187 | + ``` |
| 188 | + |
| 189 | +1. **Enable Content Recording**: By default, instrumentation captures chat messages without content. To enable content recording, set the following context switch: |
| 190 | + |
| 191 | + ```csharp |
| 192 | + AppContext.SetSwitch("Azure.Experimental.TraceGenAIMessageContent", true); |
| 193 | + ``` |
| 194 | + |
| 195 | +1. **Configure Tracer Provider**: Configure the tracer provider to export traces and metrics to console and to the local OTLP destination as needed. |
| 196 | + |
| 197 | +--- |
165 | 198 |
|
166 |
| -You can select the **raw JSON** tab to view the json data of the span. This format might be more suitable for developers when it comes to debugging and troubleshooting. |
| 199 | +### Tracing your own functions |
167 | 200 |
|
168 |
| -### Share trace |
| 201 | +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. |
169 | 202 |
|
170 |
| -If you want to share the trace with others who has the project permission, you can select the **Share** button on the right corner of the trace detail page, then you have the page link copied to share with others. |
| 203 | +## Attach User feedback to traces |
171 | 204 |
|
172 |
| -> [!NOTE] |
173 |
| -> The shared trace is read-only, and only the people who has the project permission can view it via the link. |
| 205 | +To attach user feedback to traces and visualize them in AI Studio 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 AI studio. OpenTelemetry's specification allows for standardized and enriched trace data, which can be analyzed in AI Studio for performance optimization and user experience insights. This approach helps you use the full power of OpenTelemetry for enhanced observability in your applications. |
174 | 206 |
|
175 | 207 | ## Related content
|
176 | 208 |
|
177 |
| -- [Get started building a chat app using the prompt flow SDK](../../quickstarts/get-started-code.md) |
178 |
| -- [Work with projects in VS Code](vscode.md) |
| 209 | +- [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. |
| 210 | +- [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. |
| 211 | +- [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