Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Executor] Update openai tokens to trace (#1918)
# Description This pull request primarily introduces changes to the `src/promptflow/promptflow/_core/tracer.py` file. The main focus of the changes is to enhance the tracing mechanism by adding a `TokenCollector` class for collecting OpenAI tokens associated with spans, and integrating it into the existing tracing flow. The changes also include a minor import modification for the OpenTelemetry library. Main Changes: * [`src/promptflow/promptflow/_core/tracer.py`](diffhunk://#diff-8f8c2ae53e5ffd37a14e8a899119fbb2742486db8faab6df3fcf506e1b720ad8R159-R198): Introduced a new class `TokenCollector` to collect OpenAI tokens associated with spans. The class provides methods to collect tokens, merge tokens for parent spans, and retrieve tokens for a given span id. * [`src/promptflow/promptflow/_core/tracer.py`](diffhunk://#diff-8f8c2ae53e5ffd37a14e8a899119fbb2742486db8faab6df3fcf506e1b720ad8R272-R274): The `enrich_span_with_output` function now also sets token attributes to the span if any tokens are associated with the span id. * [`src/promptflow/promptflow/_core/tracer.py`](diffhunk://#diff-8f8c2ae53e5ffd37a14e8a899119fbb2742486db8faab6df3fcf506e1b720ad8R341-R350): The `wrapped` function (both synchronous and asynchronous versions) now collects OpenAI tokens if the trace type is `LLM`, and collects tokens for the parent span before returning the output. # All Promptflow Contribution checklist: - [x] **The pull request does not introduce [breaking changes].** - [ ] **CHANGELOG is updated for new features, bug fixes or other significant changes.** - [x] **I have read the [contribution guidelines](../CONTRIBUTING.md).** - [ ] **Create an issue and link to the pull request to get dedicated review from promptflow team. Learn more: [suggested workflow](../CONTRIBUTING.md#suggested-workflow).** ## General Guidelines and Best Practices - [x] Title of the pull request is clear and informative. - [x] There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, [see this page](https://github.com/Azure/azure-powershell/blob/master/documentation/development-docs/cleaning-up-commits.md). ### Testing Guidelines - [ ] Pull request includes test coverage for the included changes. --------- Co-authored-by: Lina Tang <linatang@microsoft.com>
- Loading branch information