集成框架Mirascope

使用 Litefuse 监控 Mirascope 应用

本指南演示如何使用 Litefuse 跟踪并调试 Mirascope 应用。

什么是 Mirascope?

Mirascope (GitHub) 是一个用于构建 LLM 应用的 Python 工具包。

使用 Mirascope 开发 LLM 应用,感觉就像在写你早已熟悉的 Python 代码。 面向 LLM 的 Python 工具包:Mirascope 通过提供与标准 Python 编码习惯相似的直观接口,简化了基于大语言模型(LLM)的应用开发。

什么是 Litefuse?

Litefuse (GitHub) 是一个面向 LLM 工程的开源平台。它提供 tracing、评估、prompt 管理与指标,帮助你调试并增强你的 LLM 应用。

如何在 Mirascope 中使用 Litefuse

借助本集成,你可以自动捕获用户与你的应用交互的详细 trace。 你可以直接安装所需依赖,或使用 langfuse extras 标志:

pip install "mirascope[langfuse]"

Mirascope 会通过其 with_langfuse 装饰器,自动将 Litefuse 的 observe() 装饰器 应用到 Mirascope 中所有相关函数。

from mirascope.integrations.langfuse import with_langfuse

快速开始

1. Calls

with_langfuse 装饰器可用于所有 Mirascope 函数,自动跨 Mirascope 支持的所有 LLM 提供方 记录调用。

下面是一个使用工具的简单示例:

from mirascope.core import anthropic, prompt_template
from mirascope.integrations.langfuse import with_langfuse
 
 
def format_book(title: str, author: str):
    return f"{title} by {author}"
 
 
@with_langfuse()
@anthropic.call(model="claude-3-5-sonnet-20240620", tools=[format_book])
@prompt_template("Recommend a {genre} book.")
def recommend_book(genre: str):
    ...
 
 
print(recommend_book("fantasy"))
# > Certainly! I'd be happy to recommend a fantasy book...

它将提供:

  • 围绕 recommend_book 函数的 trace,捕获 prompt 模板、输入/输出属性等信息
  • 人类可读的 Agent 对话展示
  • 响应详情,包括所用 token 数

Litefuse 中的示例 trace

Litefuse 中的示例 trace

2. Streams

你可以用完全相同的方式捕获 stream:

from mirascope.core import openai, prompt_template
from mirascope.integrations.langfuse import with_langfuse
 
 
@with_langfuse()
@openai.call(
    model="gpt-4o-mini",
    stream=True,
    call_params={"stream_options": {"include_usage": True}},
)
@prompt_template("Recommend a {genre} book.")
def recommend_book(genre: str):
    ...
 
 
for chunk, _ in recommend_book("fantasy"):
    print(chunk.content, end="", flush=True)
# > I recommend **"The Name of the Wind"** by Patrick Rothfuss. It's the first book...

对于某些提供方,需要设置特定的 call_params 才能跟踪使用情况。

了解更多

这个页面对你有帮助吗?