Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance Traceability of Generator Outputs in Promptflow Tracing #3120

Merged
merged 17 commits into from May 14, 2024

Conversation

liucheng-ms
Copy link
Contributor

@liucheng-ms liucheng-ms commented May 6, 2024

Description

This PR introduces an enhanced method for capturing the trace of generator output functions within the Promptflow tracing framework. In the previous approach, when a function returned a generator, the associated span would terminate, and the output would be a string representing the generator object. Subsequently, a new span would be initiated when another function consumed this generator.

With the modifications proposed in this PR, the span persists beyond the point where a function returns a generator, concluding only after the generator has been fully consumed. Consequently, the output of the span encapsulating the generator or iterator object is now a comprehensive list of the iterated objects.

These changes significantly improve the transparency and intelligibility of the tracing process for generator outputs.

Example with OpenAI Call

Consider the following code for a node:

@trace
def chat(connection: AzureOpenAIConnection, question: str, stream: bool = False):
    connection_dict = normalize_connection_config(connection)
    client = AzureOpenAI(**connection_dict)

    messages = [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": question}]
    response = client.chat.completions.create(model="gpt-35-turbo", messages=messages, stream=stream)

    if stream:
        def generator():
            for chunk in response:
                if chunk.choices:
                    yield chunk.choices[0].delta.content or ""
        return "".join(generator())
    return response.choices[0].message.content or ""

@tool
def my_python_tool(connection: AzureOpenAIConnection, question: str, stream: bool) -> str:
    return chat(connection, question, stream)

With the changes in this PR, specifying stream=True yields a generator object from the client.chat.completions.create call.

Original Implementation

image

  • In the original implementation, two spans were generated for openai_chat. The first span ended when the call finished, and the output was a string representing the generator object. The second span started when the generator was consumed and ended when the generator was fully consumed.

New Implementation

image

  • In the new implementation, only one span is generated for openai_chat. The span ends when the generator is fully consumed.

All Promptflow Contribution checklist:

  • The pull request does not introduce [breaking changes].
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.
  • Create an issue and link to the pull request to get dedicated review from promptflow team. Learn more: suggested workflow.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • 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.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

Copy link

github-actions bot commented May 6, 2024

promptflow-tracing test result

 12 files   12 suites   7m 23s ⏱️
 19 tests  19 ✅ 0 💤 0 ❌
228 runs  228 ✅ 0 💤 0 ❌

Results for commit e20a5d0.

♻️ This comment has been updated with latest results.

Copy link

github-actions bot commented May 6, 2024

promptflow SDK CLI Azure E2E Test Result liucheng/20240430_generator_span

  4 files    4 suites   4m 14s ⏱️
242 tests 204 ✅  38 💤 0 ❌
968 runs  816 ✅ 152 💤 0 ❌

Results for commit e20a5d0.

♻️ This comment has been updated with latest results.

Copy link

github-actions bot commented May 6, 2024

promptflow-core test result

0 tests   0 ✅  0s ⏱️
0 suites  0 💤
0 files    0 ❌

Results for commit e20a5d0.

♻️ This comment has been updated with latest results.

Copy link

github-actions bot commented May 6, 2024

SDK CLI Global Config Test Result liucheng/20240430_generator_span

6 tests   6 ✅  1m 17s ⏱️
1 suites  0 💤
1 files    0 ❌

Results for commit e20a5d0.

♻️ This comment has been updated with latest results.

Copy link

github-actions bot commented May 6, 2024

Executor Unit Test Result liucheng/20240430_generator_span

796 tests   796 ✅  3m 44s ⏱️
  1 suites    0 💤
  1 files      0 ❌

Results for commit e20a5d0.

♻️ This comment has been updated with latest results.

Copy link

github-actions bot commented May 6, 2024

Executor E2E Test Result liucheng/20240430_generator_span

245 tests   239 ✅  5m 11s ⏱️
  1 suites    6 💤
  1 files      0 ❌

Results for commit e20a5d0.

♻️ This comment has been updated with latest results.

Copy link

github-actions bot commented May 6, 2024

SDK CLI Test Result liucheng/20240430_generator_span

    4 files      4 suites   1h 4m 5s ⏱️
  722 tests   680 ✅  42 💤 0 ❌
2 888 runs  2 720 ✅ 168 💤 0 ❌

Results for commit e20a5d0.

♻️ This comment has been updated with latest results.

@liucheng-ms liucheng-ms merged commit cd07d6d into main May 14, 2024
80 checks passed
@liucheng-ms liucheng-ms deleted the liucheng/20240430_generator_span branch May 14, 2024 08:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants