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

Add magentic third-party integration docs page #584

Merged
merged 7 commits into from
Nov 26, 2024
Merged
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
1 change: 1 addition & 0 deletions .hyperlint/styles/config/vocabularies/hyperlint/accept.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
validator
[Pp]ydantic
validators
[Mm]agentic
namespace
Hyperlint
preprocess
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions docs/integrations/third-party/magentic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[Magentic](https://github.com/jackmpcollins/magentic) is a lightweight library for working with
structured output from LLMs, built around standard python type annotations and **Pydantic**. It
integrates with **Logfire** to provide observability into prompt-templating, retries, tool/function
call execution, and [other features](https://magentic.dev/#features).

Magentic instrumentation requires no additional setup beyond configuring **Logfire** itself.
hyperlint-ai[bot] marked this conversation as resolved.
Show resolved Hide resolved
You might also want to enable the [OpenAI](../openai.md) and/or [Anthropic](../anthropic.md) integrations.

```python hl_lines="3 8 9"
from typing import Annotated

import logfire
from magentic import chatprompt, OpenaiChatModel, SystemMessage, UserMessage
from pydantic import BaseModel, Field
from pydantic.functional_validators import AfterValidator

logfire.configure()
logfire.instrument_openai()


def assert_upper(value: str) -> str:
if not value.isupper():
raise ValueError('Value must be upper case')
return value


class Superhero(BaseModel):
name: Annotated[str, AfterValidator(assert_upper)]
powers: list[str]
city: Annotated[str, Field(examples=["New York, NY"])]


@chatprompt(
SystemMessage('You are professor A, in charge of the A-people.'),
UserMessage('Create a new superhero named {name}.'),
model=OpenaiChatModel("gpt-4o"),
max_retries=3,
)
def make_superhero(name: str) -> Superhero: ...


hero = make_superhero("The Bark Night")
print(hero)
```

This creates the following in **Logfire**:

* A span for the call to `make_superhero` showing the input arguments
* A span showing that retries have been enabled for this query
* A warning for each retry that was needed in order to generate a valid output
* The chat messages to/from the LLM, including tool calls and invalid outputs that required retrying

<figure markdown="span">
![Logfire Magentic Superhero](../../images/logfire-screenshot-magentic-create-superhero.png){ width="500" }
<figcaption>Magentic chatprompt-function call span and conversation</figcaption>
</figure>

To learn more about Magentic, check out [magentic.dev](https://magentic.dev).
hyperlint-ai[bot] marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ nav:
- Third Party: integrations/third-party/index.md
- Mirascope: integrations/third-party/mirascope.md
- LiteLLM: integrations/third-party/litellm.md
- Magentic: integrations/third-party/magentic.md
- Use Cases:
- Web Frameworks: integrations/use-cases/web-frameworks.md
- Reference:
Expand Down