Skip to content

Commit

Permalink
Merge pull request #393 from pipecat-ai/khk/anthropic-tools-ordering
Browse files Browse the repository at this point in the history
fix for out-of-order image messages in anthropic context
  • Loading branch information
kwindla authored Aug 17, 2024
2 parents 5b4061b + 17f8c93 commit 655b468
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/pipecat/services/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ def add_image_frame_message(
buffer = io.BytesIO()
Image.frombytes(format, size, image).save(buffer, format="JPEG")
encoded_image = base64.b64encode(buffer.getvalue()).decode("utf-8")

# Anthropic docs say that the image should be the first content block in the message.
content = [{"type": "image",
"source": {
Expand Down Expand Up @@ -457,6 +458,7 @@ def __init__(self, user_context_aggregator: AnthropicUserContextAggregator):
self._user_context_aggregator = user_context_aggregator
self._function_call_in_progress = None
self._function_call_result = None
self._pending_image_frame_message = None

async def process_frame(self, frame, direction):
await super().process_frame(frame, direction)
Expand All @@ -477,15 +479,7 @@ async def process_frame(self, frame, direction):
self._function_call_in_progress = None
self._function_call_result = None
elif isinstance(frame, AnthropicImageMessageFrame):
try:
self._context.add_image_frame_message(
format=frame.user_image_raw_frame.format,
size=frame.user_image_raw_frame.size,
image=frame.user_image_raw_frame.image,
text=frame.text)
await self._user_context_aggregator.push_context_frame()
except Exception as e:
logger.error(f"Error processing AnthropicImageMessageFrame: {e}")
self._pending_image_frame_message = frame

def add_message(self, message):
self._user_context_aggregator.add_message(message)
Expand Down Expand Up @@ -532,6 +526,16 @@ async def _push_aggregation(self):
else:
self._context.add_message({"role": "assistant", "content": aggregation})

if self._pending_image_frame_message:
frame = self._pending_image_frame_message
self._pending_image_frame_message = None
self._context.add_image_frame_message(
format=frame.user_image_raw_frame.format,
size=frame.user_image_raw_frame.size,
image=frame.user_image_raw_frame.image,
text=frame.text)
run_llm = True

if run_llm:
await self._user_context_aggregator.push_context_frame()

Expand Down

0 comments on commit 655b468

Please sign in to comment.