Skip to content

Commit 1ebdedb

Browse files
authored
Merge pull request #275174 from lgayhardt/release-build-2024-azureml
Trace for Azure ML
2 parents d535d6f + 712b976 commit 1ebdedb

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
---
2+
title: How to trace your application with prompt flow SDK
3+
titleSuffix: Azure Machine Learning
4+
description: This article provides instructions on how to trace your application with prompt flow SDK.
5+
services: machine-learning
6+
ms.topic: how-to
7+
ms.date: 5/21/2024
8+
author: jiaochenlu
9+
ms.author: chenlujiao
10+
ms.reviewer: lagayhar
11+
---
12+
13+
# How to trace your application with prompt flow SDK
14+
15+
[!INCLUDE [machine-learning-preview-generic-disclaimer](../includes/machine-learning-preview-generic-disclaimer.md)]
16+
17+
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.
18+
19+
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 Azure Machine Learning 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.
20+
21+
## Benefits of Azure Machine Learning tracing on the enterprise-grade cloud platform
22+
23+
Moreover, we now offer persistent local testing on Azure Machine Learning, which is the enterprise-grade cloud platform, significantly enhancing collaboration, persistence, and test history management.
24+
25+
With tracing, you can:
26+
- Have a cloud-based location to persist and track your historical tests.
27+
- Easily extract and visualize the test results, comparing the outputs of different test cases.
28+
- Reuse your previous test assets for later usage, for example, human feedback, data curation, etc.
29+
- Facilitate better resource utilization in the future.
30+
- 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)
31+
- Analyze retrieval and generation processes in RAG applications.
32+
- 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).
33+
34+
## Log and view traces of your applications
35+
36+
Azure Machine Learning provides the tracing capability for logging and managing your LLM applications tests and evaluations, while debugging and observing by drilling down the trace view.
37+
38+
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/).
39+
40+
## Enable trace in your application
41+
42+
Code first - Make sure you have annotated your code for tracing in prompt flow!
43+
44+
- [Installing prompt flow](https://microsoft.github.io/promptflow/how-to-guides/quick-start.html#set-up-your-dev-environment) : require promptflow-tracing
45+
- [Instrumenting your application code](https://microsoft.github.io/promptflow/how-to-guides/tracing/index.html#instrumenting-user-s-code) : using `@trace` and `start_trace()`.
46+
- [Test and view trace in local](https://microsoft.github.io/promptflow/how-to-guides/tracing/trace-ui.html)
47+
48+
More details about tracing in prompt flow, refer to [this prompt flow documentation](https://microsoft.github.io/promptflow/how-to-guides/tracing/index.html#tracing).
49+
50+
## Log the trace to Azure Machine Learning
51+
52+
### Set the trace destination
53+
54+
By default, the trace is logged and viewed in your local environment. To log it in the Azure Machine Learning in the cloud, you need to set the `trace destination` to a specified Azure Machine Learning workspace.
55+
56+
You can refer to the following steps to set the trace destination to Azure Machine Learning workspace.
57+
58+
First, ensure that Azure CLI is installed and logged in:
59+
60+
```azurecli
61+
az login
62+
```
63+
64+
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 Azure Machine Learning workspace name:
65+
66+
```azurecli
67+
pf config set trace.destination=azureml://subscriptions/<your_subscription_id>/resourcegroups/<your_resourcegroup_name>/providers/Microsoft.MachineLearningServices/workspaces//<your_azureml_workspace_name>
68+
```
69+
70+
### Collections
71+
72+
A **collection** is a group of associated traces. These collections along with their internal traces are managed and stored in the **Tracing** module under the **Collections** tab.
73+
74+
1. Go to your workspace in [Azure Machine Learning studio](https://ml.azure.com/).
75+
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.
76+
77+
:::image type="content" source="./media/how-to-trace-local-sdk/collection.png" alt-text="Screenshot of the button to add a new connection." lightbox="./media/how-to-trace-local-sdk/collection.png":::
78+
79+
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.
80+
81+
- **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.
82+
- **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.
83+
- **Created on**: The time when the collection was initially created.
84+
85+
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.
86+
87+
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**.
88+
89+
#### Customize the collections
90+
91+
For better organization of your traces, you can specify a custom collection name when logging a trace.
92+
93+
# [Python SDK](#tab/python)
94+
95+
If you're tracing your own application, you can set the collection name in the `start_trace()` function in your code:
96+
97+
```python
98+
from promptflow.tracing import start_trace, trace
99+
100+
@trace
101+
def my_function(input: str) -> str:
102+
output = input + "Hello World!"
103+
return output
104+
105+
my_function("This is my function")
106+
start_trace(collection="my_custom_collection")
107+
```
108+
109+
# [Azure CLI](#tab/cli)
110+
```azurecli
111+
# Test flow
112+
pf flow test --flow <flow-name> --collection my_custom_collection
113+
```
114+
115+
---
116+
117+
More details about customizing collections, 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).
118+
119+
### View the traces
120+
121+
First, you must complete the previous steps to view the traces in the cloud:
122+
- [Enable trace in your application](#enable-trace-in-your-application).
123+
- [Set cloud trace destination](#set-the-trace-destination).
124+
125+
Now, run your python script directly. Upon successful execution, a cloud trace link appears in the output. It might look something like this:
126+
127+
```bash
128+
Starting prompt flow service...
129+
...
130+
You can view the traces in cloud from Azure portal: https://ml.azure.com/projecttrace/detail/....
131+
```
132+
133+
Selecting the URL to navigate to a trace detail page on the cloud portal. This page is similar to the local trace view.
134+
135+
The **trace detail view** provides a comprehensive and structured overview of the operations within your application.
136+
137+
**Understand the trace detail view**
138+
139+
In the top right corner of the trace view, you find:
140+
141+
- Trace name: This is same as the root span name, representing the entry function name of your application.
142+
- Status: This could either be "completed" or "failed".
143+
- Total duration: This is total duration time of the test execution. Hover over to view the start and end times.
144+
- Total tokens: This is the total token cost of the test. Hover over to view the prompt tokens and completed tokens.
145+
- Created time: The time at which the trace was created.
146+
147+
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.
148+
149+
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.
150+
151+
Each span allows you to view:
152+
153+
- 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/).
154+
- Duration: This represents the duration for which the function ran. Hover over to view the start and end times.
155+
- Tokens for LLM calls: This is the token cost of the LLM call. Hover over to view the prompt tokens and completed tokens.
156+
157+
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.
158+
159+
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.
160+
161+
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.
162+
163+
### Share trace
164+
165+
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.
166+
167+
> [!NOTE]
168+
> The shared trace is read-only, and only the people who has the project permission can view it via the link.
169+
170+
## Next steps
171+
172+
- [Deploy for real time inference](how-to-deploy-for-real-time-inference.md)
193 KB
Loading

articles/machine-learning/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,8 @@
690690
href: ./prompt-flow/how-to-integrate-with-llm-app-devops.md
691691
- name: Deploy a flow to online endpoint for real-time inference with CLI
692692
href: ./prompt-flow/how-to-deploy-to-code.md
693+
- name: Trace your application with prompt flow
694+
href: ./prompt-flow/how-to-trace-local-sdk.md
693695
- name: How to use streaming endpoints deployed from prompt flow
694696
href: ./prompt-flow/how-to-enable-streaming-mode.md
695697
- name: Set up end-to-end LLMOps with prompt flow and GitHub

0 commit comments

Comments
 (0)