|
| 1 | +[Mirascope](https://github.com/Mirascope/mirascope) is an intuitive approach to building AI-powered applications using LLMs. Their library integrates with Logfire to make observability and monitoring for LLMs easy and seamless. |
| 2 | + |
| 3 | +You can enable it using their [`@with_logire`][mirascope-logfire] decorator, which will work with all of the [model providers that they support][mirascope-supported-providers] (e.g. OpenAI, Anthropic, Groq, and more). |
| 4 | + |
| 5 | +```py hl_lines="1 2 5 8" |
| 6 | +import logfire |
| 7 | +from mirascope.logfire import with_logfire |
| 8 | +from mirascope.anthropic import AnthropicCall |
| 9 | + |
| 10 | +logfire.configure() |
| 11 | + |
| 12 | + |
| 13 | +@with_logfire |
| 14 | +class BookRecommender(AnthropicCall): |
| 15 | + prompt_template = "Please recommend some {genre} books" |
| 16 | + |
| 17 | + genre: str |
| 18 | + |
| 19 | + |
| 20 | +recommender = BookRecommender(genre="fantasy") |
| 21 | +response = recommender.call() # this will automatically get logged with logfire |
| 22 | +print(response.content) |
| 23 | +#> Here are some recommendations for great fantasy book series: ... |
| 24 | +``` |
| 25 | + |
| 26 | +This will give you: |
| 27 | + |
| 28 | +* A span around the `AnthropicCall.call()` that captures items like the prompt template, templating properties and fields, and input/output attributes. |
| 29 | +* Human-readable display of the conversation with the agent |
| 30 | +* Details of the response, including the number of tokens used |
| 31 | + |
| 32 | +<figure markdown="span"> |
| 33 | + { width="500" } |
| 34 | + <figcaption>Mirascope Anthropic Call span and Anthropic span and conversation</figcaption> |
| 35 | +</figure> |
| 36 | + |
| 37 | +Since Mirascope is build on top of [Pydantic][pydantic], you can use the [Pydantic plugin][pydantic-plugin] to track additional logs and metrics about model validation, which you can enable using the [`pydantic_plugin`][logfire.configure(pydantic_plugin)] configuration. |
| 38 | + |
| 39 | +This can be particularly useful when [extracting structured information][mirascope-extracting-structured-information] using LLMs: |
| 40 | + |
| 41 | +```py hl_lines="3 4 8 17" |
| 42 | +from typing import Literal, Type |
| 43 | + |
| 44 | +import logfire |
| 45 | +from mirascope.logfire import with_logfire |
| 46 | +from mirascope.openai import OpenAIExtractor |
| 47 | +from pydantic import BaseModel |
| 48 | + |
| 49 | +logfire.configure(pydantic_plugin=logfire.PydanticPlugin(record="all")) |
| 50 | + |
| 51 | + |
| 52 | +class TaskDetails(BaseModel): |
| 53 | + description: str |
| 54 | + due_date: str |
| 55 | + priority: Literal["low", "normal", "high"] |
| 56 | + |
| 57 | + |
| 58 | +@with_logfire |
| 59 | +class TaskExtractor(OpenAIExtractor[TaskDetails]): |
| 60 | + extract_schema: Type[TaskDetails] = TaskDetails |
| 61 | + prompt_template = """ |
| 62 | + Extract the task details from the following task: |
| 63 | + {task} |
| 64 | + """ |
| 65 | + |
| 66 | + task: str |
| 67 | + |
| 68 | + |
| 69 | +task = "Submit quarterly report by next Friday. Task is high priority." |
| 70 | +task_details = TaskExtractor( |
| 71 | + task=task |
| 72 | +).extract() # this will be logged automatically with logfire |
| 73 | +assert isinstance(task_details, TaskDetails) |
| 74 | +print(task_details) |
| 75 | +#> description='Submit quarterly report' due_date='next Friday' priority='high' |
| 76 | +``` |
| 77 | + |
| 78 | +This will give you: |
| 79 | + |
| 80 | +* Tracking for validation of Pydantic models. |
| 81 | +* A span around the `OpenAIExtractor.extract()` that captures items like the prompt template, templating properties and fields, and input/output attributes. |
| 82 | +* Human-readable display of the conversation with the agent including the function call |
| 83 | +* Details of the response, including the number of tokens used |
| 84 | + |
| 85 | +<figure markdown="span"> |
| 86 | + { width="500" } |
| 87 | + <figcaption>Mirascope OpenAI Extractor span and OpenAI span and function call</figcaption> |
| 88 | +</figure> |
| 89 | + |
| 90 | +For more information on Mirascope and what you can do with it, check out their [documentation](https://docs.mirascope.io) |
| 91 | + |
| 92 | +[mirascope-logfire]: https://docs.mirascope.io/latest/integrations/logfire/#how-to-use-logfire-with-mirascope |
| 93 | +[mirascope-supported-providers]: https://docs.mirascope.io/latest/concepts/supported_llm_providers/ |
| 94 | +[mirascope-extracting-structured-information]: https://docs.mirascope.io/latest/concepts/extracting_structured_information_using_llms/ |
| 95 | +[pydantic]: https://docs.pydantic.dev/latest/ |
| 96 | +[pydantic-plugin]: https://docs.pydantic.dev/latest/concepts/plugins/ |
0 commit comments