Mirascope

Mirascope 是使用 LLMs 构建人工智能驱动应用程序的直观方法。他们的库与 Logfire 集成,使 LLMs 的可观测性和监控变得轻松且无缝。

您可以使用他们的 @with_logire 装饰器启用它,这将适用于他们支持的所有模型提供商(例如 OpenAI、Anthropic、Groq 等)。

import logfire
from mirascope.logfire import with_logfire
from mirascope.anthropic import AnthropicCall

logfire.configure()


@with_logfire
class BookRecommender(AnthropicCall):
    prompt_template = "Please recommend some {genre} books"

    genre: str


recommender = BookRecommender(genre="fantasy")
response = recommender.call()  # this will automatically get logged with logfire
print(response.content)
#> Here are some recommendations for great fantasy book series: ...

这将给您:

  • 围绕 AnthropicCall.call() 的跨度,捕获诸如提示模板、模板属性和字段以及输入/输出属性之类的项目
  • 与代理的对话的人类可读显示
  • 响应的详细信息,包括使用的令牌数量
Logfire Mirascope Anthropic call
Mirascope Anthropic Call span and Anthropic span and conversation

由于 Mirascope 构建在 Pydantic 之上,您可以使用 Pydantic 插件来跟踪有关模型验证的其他日志和指标,您可以使用 [ pydantic_plugin ][logfire.configure(pydantic_plugin)] 配置来启用它。

在使用 LLMs 提取结构化信息时,这可能特别有用:

from typing import Literal, Type

import logfire
from mirascope.logfire import with_logfire
from mirascope.openai import OpenAIExtractor
from pydantic import BaseModel

logfire.configure(pydantic_plugin=logfire.PydanticPlugin(record="all"))


class TaskDetails(BaseModel):
    description: str
    due_date: str
    priority: Literal["low", "normal", "high"]


@with_logfire
class TaskExtractor(OpenAIExtractor[TaskDetails]):
    extract_schema: Type[TaskDetails] = TaskDetails
    prompt_template = """
    Extract the task details from the following task:
    {task}
    """

    task: str


task = "Submit quarterly report by next Friday. Task is high priority."
task_details = TaskExtractor(
    task=task
).extract()  # this will be logged automatically with logfire
assert isinstance(task_details, TaskDetails)
print(task_details)
#> description='Submit quarterly report' due_date='next Friday' priority='high'

这将给您:

  • Pydantic 模型验证的跟踪
  • 围绕 OpenAIExtractor.extract() 的跨度,捕获诸如提示模板、模板属性和字段以及输入/输出属性之类的项目
  • 与代理的对话的人类可读显示,包括函数调用
  • 响应的详细信息,包括使用的令牌数量
Logfire Mirascope Anthropic call
Mirascope OpenAI Extractor span and OpenAI span and function call

Mirascope OpenAI 提取器跨度和 OpenAI 跨度以及函数调用

有关 Mirascope 以及您可用其做什么的更多信息,请查看其文档。


本文总阅读量次