From a127b4982c1406c1fce7bb70abc960605613bf17 Mon Sep 17 00:00:00 2001 From: Lina Tang Date: Fri, 9 Feb 2024 11:17:27 +0800 Subject: [PATCH] [Executor] Update prompt info in otel traces (#1969) # Description This pull request primarily focuses on enhancing the traceability of the `promptflow` application. The changes involve modifying the function parameters, adding new functions, and updating tests to validate the new changes. The most significant changes include the addition of a new `enrich_span_with_prompt_info` function in the `tracer.py` file, the modification of the `render_template_jinja2` function in the `template_rendering.py` file, and the addition of new tests in the `test_traces.py` file. Changes to function parameters: * [`src/promptflow-tools/promptflow/tools/template_rendering.py`](diffhunk://#diff-cd8fa4e9bc7912dced2d024c72d70fb9c3853cd69df5543ce19686bfb0ae196aR4-R9): The `render_template_jinja2` function's `template` parameter type has been changed from `str` to `PromptTemplate`. This change ensures that the function only accepts `PromptTemplate` type inputs, providing a more defined contract for the function usage. New function additions: * [`src/promptflow/promptflow/_core/tracer.py`](diffhunk://#diff-8f8c2ae53e5ffd37a14e8a899119fbb2742486db8faab6df3fcf506e1b720ad8R267-R282): A new function `enrich_span_with_prompt_info` has been added. This function enriches a span with prompt information, assuming there is only one prompt template parameter in the function. It is used in both the `wrapped` functions within the same file. [[1]](diffhunk://#diff-8f8c2ae53e5ffd37a14e8a899119fbb2742486db8faab6df3fcf506e1b720ad8R267-R282) [[2]](diffhunk://#diff-8f8c2ae53e5ffd37a14e8a899119fbb2742486db8faab6df3fcf506e1b720ad8R370) [[3]](diffhunk://#diff-8f8c2ae53e5ffd37a14e8a899119fbb2742486db8faab6df3fcf506e1b720ad8R421) Updated tests: * [`src/promptflow/tests/executor/e2etests/test_traces.py`](diffhunk://#diff-a36689a949893c227689d9dadb4c1e7008a0551206871e371658f4cc70f16f07R390-R430): New tests have been added to validate the changes made in the `tracer.py` file. The `test_otel_trace_with_prompt` test checks if the OpenTelemetry traces include the correct prompt template and variables. Example: After this PR, if the function has input with type `PromptTemplate`, we will add prompt information in the otel traces: ``` "prompt.template": "