microsoft / promptflow

Build high-quality LLM apps - from prototyping, testing to production deployment and monitoring.
https://microsoft.github.io/promptflow/
MIT License
8.32k stars 712 forks source link

Enhance Traceability of Generator Outputs in Promptflow Tracing #3120

Closed liucheng-ms closed 5 days ago

liucheng-ms commented 1 week ago

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

New Implementation

image

All Promptflow Contribution checklist:

General Guidelines and Best Practices

Testing Guidelines

github-actions[bot] commented 1 week ago

promptflow-tracing test result

 12 files   12 suites   7m 23s :stopwatch:  19 tests  19 :white_check_mark: 0 :zzz: 0 :x: 228 runs  228 :white_check_mark: 0 :zzz: 0 :x:

Results for commit e20a5d01.

:recycle: This comment has been updated with latest results.

github-actions[bot] commented 1 week ago

promptflow SDK CLI Azure E2E Test Result liucheng/20240430_generator_span

  4 files    4 suites   4m 14s :stopwatch: 242 tests 204 :white_check_mark:  38 :zzz: 0 :x: 968 runs  816 :white_check_mark: 152 :zzz: 0 :x:

Results for commit e20a5d01.

:recycle: This comment has been updated with latest results.

github-actions[bot] commented 1 week ago

promptflow-core test result

0 tests   0 :white_check_mark:  0s :stopwatch: 0 suites  0 :zzz: 0 files    0 :x:

Results for commit e20a5d01.

:recycle: This comment has been updated with latest results.

github-actions[bot] commented 1 week ago

SDK CLI Global Config Test Result liucheng/20240430_generator_span

6 tests   6 :white_check_mark:  1m 17s :stopwatch: 1 suites  0 :zzz: 1 files    0 :x:

Results for commit e20a5d01.

:recycle: This comment has been updated with latest results.

github-actions[bot] commented 1 week ago

Executor Unit Test Result liucheng/20240430_generator_span

796 tests   796 :white_check_mark:  3m 44s :stopwatch:   1 suites    0 :zzz:   1 files      0 :x:

Results for commit e20a5d01.

:recycle: This comment has been updated with latest results.

github-actions[bot] commented 1 week ago

Executor E2E Test Result liucheng/20240430_generator_span

245 tests   239 :white_check_mark:  5m 11s :stopwatch:   1 suites    6 :zzz:   1 files      0 :x:

Results for commit e20a5d01.

:recycle: This comment has been updated with latest results.

github-actions[bot] commented 1 week ago

SDK CLI Test Result liucheng/20240430_generator_span

    4 files      4 suites   1h 4m 5s :stopwatch:   722 tests   680 :white_check_mark:  42 :zzz: 0 :x: 2 888 runs  2 720 :white_check_mark: 168 :zzz: 0 :x:

Results for commit e20a5d01.

:recycle: This comment has been updated with latest results.