Mirascope

Mirascope is an intuitive way to build AI-driven applications using LLMs. Their library integrates with Logfire, making observability and monitoring of LLMs easy and seamless.

You can enable it using their @with_logire decorator, which will apply to all model providers they support (e.g. OpenAI, Anthropic, Groq, etc.).

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: ...

This will give you:

  • spans around AnthropicCall.call(), capturing items such as prompt templates, template attributes and fields, and input/output attributes
  • Human-readable display of conversations with agents
  • Details of the response, including the number of tokens used
Logfire Mirascope Anthropic call
Mirascope Anthropic Call span and Anthropic span and conversation

Because Mirascope is built on top of Pydantic, you can use the Pydantic plug-in to track additional logs and metrics related to model validation, which you can use [ pydantic_plugin ][logfire.configure(pydantic_plugin)] Configure to enable it.

This can be particularly useful when using LLMs to extract structured information:

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'

This will give you:

  • Pydantic model validation tracking
  • spans around OpenAIExtractor.extract(), capturing items such as prompt templates, template attributes and fields, and input/output attributes
  • Human-readable display of conversations with agents, including function calls
  • Details of the response, including the number of tokens used
Logfire Mirascope Anthropic call
Mirascope OpenAI Extractor span and OpenAI span and function call

Mirascope OpenAI Extractor Span and OpenAI Span and Function Calls

For more information about Mirascope and what you can do with it, check out its documentation.


本文总阅读量次