From 7c5067e9c953fb507b18caef9a8a13d2c8fcc083 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Mon, 4 Nov 2024 10:43:06 -0800 Subject: [PATCH] Fix for bug where attachments ignored prompt, refs #20 --- llm_claude_3.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/llm_claude_3.py b/llm_claude_3.py index 1e817d1..049f177 100644 --- a/llm_claude_3.py +++ b/llm_claude_3.py @@ -139,6 +139,7 @@ def build_messages(self, prompt, conversation) -> List[dict]: } for attachment in response.attachments ] + content.append({"type": "text", "text": response.prompt.prompt}) else: content = response.prompt.prompt messages.extend( @@ -151,24 +152,26 @@ def build_messages(self, prompt, conversation) -> List[dict]: ] ) if prompt.attachments: + content = [ + { + "type": ( + "document" + if attachment.resolve_type() == "application/pdf" + else "image" + ), + "source": { + "data": attachment.base64_content(), + "media_type": attachment.resolve_type(), + "type": "base64", + }, + } + for attachment in prompt.attachments + ] + content.append({"type": "text", "text": prompt.prompt}) messages.append( { "role": "user", - "content": [ - { - "type": ( - "document" - if attachment.resolve_type() == "application/pdf" - else "image" - ), - "source": { - "data": attachment.base64_content(), - "media_type": attachment.resolve_type(), - "type": "base64", - }, - } - for attachment in prompt.attachments - ], + "content": content, } ) else: