Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Mirascope Third Party Integration Docs Page #98

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
96 changes: 96 additions & 0 deletions docs/integrations/third_party/mirascope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
[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.

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

```py hl_lines="1 2 5 8"
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:

* A span around the `AnthropicCall.call()` that captures items like the prompt template, templating properties and fields, and input/output attributes.
* Human-readable display of the conversation with the agent
* Details of the response, including the number of tokens used

<figure markdown="span">
![Logfire Mirascope Anthropic call](../../images/logfire-screenshot-mirascope-anthropic-call.png){ width="500" }
<figcaption>Mirascope Anthropic Call span and Anthropic span and conversation</figcaption>
</figure>

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.

This can be particularly useful when [extracting structured information][mirascope-extracting-structured-information] using LLMs:

```py hl_lines="3 4 8 17"
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:

* Tracking for validation of Pydantic models.
* A span around the `OpenAIExtractor.extract()` that captures items like the prompt template, templating properties and fields, and input/output attributes.
* Human-readable display of the conversation with the agent including the function call
* Details of the response, including the number of tokens used

<figure markdown="span">
![Logfire Mirascope Anthropic call](../../images/logfire-screenshot-mirascope-openai-extractor.png){ width="500" }
<figcaption>Mirascope OpenAI Extractor span and OpenAI span and function call</figcaption>
</figure>

For more information on Mirascope and what you can do with it, check out their [documentation](https://docs.mirascope.io)

[mirascope-logfire]: https://docs.mirascope.io/latest/integrations/logfire/#how-to-use-logfire-with-mirascope
[mirascope-supported-providers]: https://docs.mirascope.io/latest/concepts/supported_llm_providers/
[mirascope-extracting-structured-information]: https://docs.mirascope.io/latest/concepts/extracting_structured_information_using_llms/
[pydantic]: https://docs.pydantic.dev/latest/
[pydantic-plugin]: https://docs.pydantic.dev/latest/concepts/plugins/
21 changes: 19 additions & 2 deletions docs/plugins/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def on_page_markdown(markdown: str, page: Page, config: Config, files: Files) ->
markdown = build_environment_variables_table(markdown, page)
markdown = logfire_print_help(markdown, page)
markdown = install_logfire(markdown, page)
if page.file.src_uri == 'guides/onboarding_checklist/06_add_metrics.md':
check_documented_system_metrics(markdown, page)
markdown = check_documented_system_metrics(markdown, page)
markdown = warning_on_third_party(markdown, page)
return markdown


Expand All @@ -36,6 +36,9 @@ def check_documented_system_metrics(markdown: str, page: Page) -> str:

This function checks that all the metrics in `DEFAULT_CONFIG` are documented.
"""
if page.file.src_uri != 'guides/onboarding_checklist/06_add_metrics.md':
return markdown

metrics_documented: set[str] = set()
for line in markdown.splitlines():
match = re.search(r'\* `(.*)`: ', line)
Expand Down Expand Up @@ -134,3 +137,17 @@ def install_logfire(markdown: str, page: Page) -> str:
```
"""
return re.sub(r'{{ *install_logfire\(.*\) *}}', instructions, markdown)


def warning_on_third_party(markdown: str, page: Page) -> str:
if not page.file.src_uri.startswith('integrations/third_party/'):
return markdown

note = """
!!! note "Third-party integrations"
Third-party integrations are not officially supported by **Logfire**.

They are maintained by the community and may not be as reliable as the integrations developed by **Logfire**.
"""

return note + markdown
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ nav:
- Logging: integrations/logging.md
- Structlog: integrations/structlog.md
- Loguru: integrations/loguru.md
- Third Party:
- Mirascope: integrations/third_party/mirascope.md
- Use Cases:
- Web Frameworks: integrations/use_cases/web_frameworks.md
- Reference:
Expand Down