追踪#

实验性功能

这是一个实验性功能,可能会随时更改。了解更多更多

跟踪记录特定事件或应用程序在执行期间的状态。它可以包括关于函数调用、变量值、系统事件等的数据。跟踪有助于将应用程序的组件分解为离散的输入和输出,这对于调试和理解应用程序至关重要。您可以从这里了解更多关于跟踪的信息。

Prompt flow 提供了跟踪功能,使用户能够跟踪 LLM 调用或函数,以及像 LangChainAutoGen 这样的 LLM 框架,遵循 OpenTelemetry 规范

安装包#

pip install promptflow-tracing

检测用户的代码#

启用LLM调用的跟踪#

让我们从最简单的例子开始,添加单行代码 start_trace() 以启用应用程序中LLM调用的跟踪。

from openai import OpenAI
from promptflow.tracing import start_trace

# instrument OpenAI
start_trace()

client = OpenAI()

completion = client.chat.completions.create(
  model="gpt-4",
  messages=[
    {"role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},
    {"role": "user", "content": "Compose a poem that explains the concept of recursion in programming."}
  ]
)

print(completion.choices[0].message)

然后OpenAI被集成,由于提示流遵循OpenTelemetry规范,用户可以充分利用OpenTelemetry知识在OpenAI调用期间使用这些跟踪。

跟踪任何函数#

更常见的场景是应用程序具有复杂的代码结构,开发人员希望在关键路径上添加跟踪,以便调试和监控。

查看math_to_code示例,了解如何使用@trace

执行以下命令将获得一个URL,用于显示每个测试的跟踪记录和跟踪详细信息。

from promptflow.tracing import trace

# trace your function
@trace
def code_gen(client: AzureOpenAI, question: str) -> str:
    sys_prompt = (
        "I want you to act as a Math expert specializing in Algebra, Geometry, and Calculus. "
        "Given the question, develop python code to model the user's question. "
        "Make sure only reply the executable code, no other words."
    )
    completion = client.chat.completions.create(
        model=os.getenv("CHAT_DEPLOYMENT_NAME", "gpt-35-turbo"),
        messages=[
            {
                "role": "system",
                "content": sys_prompt,
            },
            {"role": "user", "content": question},
        ],
    )
    raw_code = completion.choices[0].message.content
    result = code_refine(raw_code)
    return result
python math_to_code.py

追踪LLM和框架#

提示流追踪不仅适用于一般的LLM应用,还适用于更多框架,如autogenlangchain。除了基本的追踪能力外,提示流还提供了多种追踪工具包,可以提升追踪体验(例如,用于可视化的追踪UI)。

  1. 示例:添加LLM的跟踪

llm-trace-detail

  1. 示例:为Autogen添加跟踪

autogen-trace-detail

  1. 示例:为Langchain添加跟踪

langchain-trace-detail