rawwerks / magentic

Seamlessly integrate LLMs as Python functions
https://magentic.dev/
MIT License
0 stars 0 forks source link

implementation of message_to_anthropic_message for UserImageMessage #3

Open rawwerks opened 4 months ago

rawwerks commented 4 months ago

@mentatbot - here is the answer, implement this exactly and make a PR

This should just be a case of registering an implementation of message_to_anthropic_message for UserImageMessage, which can actually be done externally to magentic / without needing a new version, but of course should be added to magentic.

Here is the UserImageMessage message registration for message_to_openai_message.

magentic/src/magentic/vision.py

Lines 38 to 55 in f0c2fce @message_to_openaimessage.register(UserImageMessage) def ( message: UserImageMessage[bytes] | UserImageMessage[str], ) -> ChatCompletionMessageParam: if isinstance(message.content, bytes): mime_type = filetype.guess_mime(message.content) base64_image = base64.b64encode(message.content).decode("utf-8") url = f"data:{mime_type};base64,{base64_image}" elif isinstance(message.content, str): url = message.content else: msg = f"Invalid content type: {type(message.content)}" raise TypeError(msg)

 return { 
     "role": OpenaiMessageRole.USER.value, 
     "content": [{"type": "image_url", "image_url": {"url": url, "detail": "auto"}}], 
 } 

and message_to_anthropic_message is here

magentic/src/magentic/chat_model/anthropic_chat_model.py

Line 69 in f0c2fce def message_to_anthropic_message(message: Message[Any]) -> MessageParam:

With that implemented you should be able to use images with Anthropic models as shown for OpenAI in the docs here https://magentic.dev/vision/

Let me know if you run into any issues with this. I'd happily accept a PR to add this to vision.py so it works by default.

mentatbot[bot] commented 4 months ago

I will start working on this issue