Skip to content

Releases: jackmpcollins/magentic

v0.19.0

15 Apr 06:41
Compare
Choose a tag to compare

What's Changed

Full Changelog: v0.18.1...v0.19.0


🎉 Native support for Anthropic using AnthropicChatModel 🎉

from magentic import prompt
from magentic.chat_model.anthropic_chat_model import AnthropicChatModel


@prompt(
    "Name three fruits. Be sure to return this in correct JSON format matching the schema.",
    model=AnthropicChatModel("claude-3-opus-20240229")
)
def say_fruit() -> list[str]: ...

say_fruit()

v0.19.0a0

11 Apr 04:23
Compare
Choose a tag to compare
v0.19.0a0 Pre-release
Pre-release

Prerelease for testing PR #174

v0.18.1

25 Mar 00:36
Compare
Choose a tag to compare

What's Changed

Full Changelog: v0.18.0...v0.18.1

v0.18.0

22 Mar 08:19
Compare
Choose a tag to compare

What's Changed

New Contributors

Full Changelog: v0.17.0...v0.18.0


Now fully migrated to tool calls 🎉 Which enables using the new Claude-3 models via litellm, as well as parallel function calls. See more at https://magentic.dev/function-calling/

@prompt(
    "Sum {a} and {b}. Also subtract {a} from {b}.",
    functions=[plus, minus],
)
def plus_and_minus(a: int, b: int) -> ParallelFunctionCall[int]: ...


output = plus_and_minus(2, 3)
print(list(output))
# > [FunctionCall(<function plus at 0x106b8f010>, 2, 3), FunctionCall(<function minus at 0x106b8ef80>, 3, 2)]
output()
# (5, 1)

Warning

Breaking Change: FunctionResultMessage now accepts FunctionCall as argument instead of a function

FunctionResultMessage now (again) takes a FunctionCall instance as its second argument. Further, when constructing a chat message history, the same FunctionCall instance must be passed to an AssistantMessage and the corresponding FunctionResultMessage. This so that the result can be correctly linked back to the function call that created it.

def plus(a: int, b: int) -> int:
    return a + b

plus_1_2 = FunctionCall(plus, 1, 2)

@chatprompt(
    UserMessage("Use the plus function to add 1 and 2."),
    AssistantMessage(plus_1_2),
    FunctionResultMessage(3, plus_1_2),
)
def do_math() -> str: ...

Example of using the new formatting classes to format a prompt. See more at https://magentic.dev/formatting/

from magentic import prompt
from magentic.formatting import NumberedList


@prompt("Continue the list:\n{items}")
def get_next_items(items: NumberedList[str]) -> list[str]: ...


items = NumberedList(["apple", "banana", "cherry"])
print(get_next_items.format(items=items))
# Continue the list:
# 1. apple
# 2. banana
# 3. cherry

v0.18.0a0

21 Mar 07:33
Compare
Choose a tag to compare
v0.18.0a0 Pre-release
Pre-release

Prerelease for testing PR #144

v0.17.0

03 Mar 05:22
Compare
Choose a tag to compare

What's Changed

Full Changelog: v0.16.0...v0.17.0


Add support for GPT4 Vision with new UserImageMessage message type. See https://magentic.dev/vision/

from magentic import chatprompt, OpenaiChatModel, Placeholder, UserMessage
from magentic.vision import UserImageMessage


IMAGE_URL_WOODEN_BOARDWALK = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"


@chatprompt(
    UserMessage("Describe the following image in one sentence."),
    UserImageMessage(Placeholder(str, "image_url")),
    model=OpenaiChatModel("gpt-4-vision-preview", max_tokens=2000),
)
def describe_image(image_url: str) -> str:
    ...


describe_image(IMAGE_URL_WOODEN_BOARDWALK)
# 'A wooden boardwalk meanders through lush green wetlands under a partly cloudy blue sky.'

v0.16.0

28 Feb 07:09
Compare
Choose a tag to compare

What's Changed

Full Changelog: v0.15.0...v0.16.0


New docs site 🎉 https://magentic.dev


Custom types can now be registered to be used as return type annotations with @prompt-decorated functions. For example, after registering a pandas DataFrame the following example works. See the docs page for how to do this: https://magentic.dev/examples/registering_custom_type/

import pandas as pd
from magentic import prompt


@prompt(
    "Create a table listing the ingredients needed to cook {dish}."
    "Include a column for the quantity of each ingredient."
    "Also include a column with alergy information."
)
def list_ingredients(dish: str) -> pd.DataFrame:
    ...


list_ingredients("lasagna")
# <DataFrame of ingredients>

The Placeholder class has been added to enable better templating of AssistantMessage and custom Message subclasses. More info in docs https://magentic.dev/chat-prompting/#placeholder . Sample usage:

from magentic import chatprompt, AssistantMessage, Placeholder, UserMessage
from pydantic import BaseModel


class Quote(BaseModel):
    quote: str
    character: str


@chatprompt(
    UserMessage("Tell me a quote from {movie}"),
    AssistantMessage(Placeholder(Quote, "quote")),
    UserMessage("What is a similar quote from the same movie?"),
)
def get_similar_quote(movie: str, quote: Quote) -> Quote:
    ...


get_similar_quote(
    movie="Star Wars",
    quote=Quote(quote="I am your father", character="Darth Vader"),
)
# Quote(quote='The Force will be with you, always.', character='Obi-Wan Kenobi')

v0.15.0

20 Feb 02:41
Compare
Choose a tag to compare

What's Changed

Full Changelog: v0.14.1...v0.15.0


Message subclasses now have a format method to customize their value using the function arguments when used with the @chatprompt decorator. This allows for more complex templating than just the string templating that is currently supported. For a real use case with GPT-4-vision see in-progress PR #116

from typing import Any
from magentic import chatprompt
from magentic.chat_model.message import Message


class UppercaseMessage(Message[str]):
    def format(self, **kwargs: Any) -> "UppercaseMessage":
        upper_kwargs = {k: str(v).upper() for k, v in kwargs.items()}
        return UppercaseMessage(self.content.format(**upper_kwargs))


@chatprompt(
    UppercaseMessage("hello {x}"),
)
def say_hello(x: str) -> str:
    ...


say_hello.format("world")
# [UppercaseMessage('hello WORLD')]

In addition, the openai JSON serialization for custom Message subclasses can be added without modifying magentic itself! This uses the functools.singledispatch decorator. Together, these changes will allow users to create Message classes that they can template exactly as needed, which will be useful as the variety of types of inputs to LLMs increases.

from typing import Any

from magentic.chat_model.openai_chat_model import OpenaiMessageRole, message_to_openai_message
from magentic.chat_model.message import Message
from openai.types.chat import ChatCompletionMessageParam


class CustomMessage(Message[str]):
    def format(self, **kwargs: Any) -> "CustomMessage":
        return CustomMessage(self.content)


@message_to_openai_message.register
def _(message: CustomMessage) -> ChatCompletionMessageParam:
    return {"role": OpenaiMessageRole.USER.value, "content": message.content}


message_to_openai_message(CustomMessage("hello"))
# {'role': 'user', 'content': 'hello'}

PR #114


Warning

Breaking Change: FunctionResultMessage init param function_call: FunctionCall replaced by function: Callable

Only the name of the function is needed when serializing the FunctionResultMessage so we do not need the whole FunctionCall. This simplifies creating @chatprompt function where the chat contains a function call. e.g.

from magentic import (
    chatprompt,
    AssistantMessage,
    UserMessage,
    FunctionCall,
    FunctionResultMessage,
)


def plus(a: int, b: int) -> int:
    return a + b


@chatprompt(
    UserMessage("Use the plus function to add 1 and 2."),
    AssistantMessage(FunctionCall(plus, 1, 2)),
    FunctionResultMessage(3, plus),
)
def do_math() -> str:
    ...


do_math()
# 'The sum of 1 and 2 is 3.'

PR #110

v0.14.1

18 Feb 04:56
Compare
Choose a tag to compare

What's Changed

Dependabot

Full Changelog: v0.14.0...v0.14.1

v0.14.0

08 Jan 06:41
Compare
Choose a tag to compare

What's Changed

Full Changelog: v0.13.0...v0.14.0