Skip to content

Build and run agents you can see, understand and trust.

fix(formatter, token_counter): fix the bug in OpenAI formatter and anthropic token counter (#1154)

qbcbb797fdd9691
diff --git a/src/agentscope/formatter/_openai_formatter.py b/src/agentscope/formatter/_openai_formatter.py
index adce8be08..441f34da5 100644
--- a/src/agentscope/formatter/_openai_formatter.py
+++ b/src/agentscope/formatter/_openai_formatter.py
@@ -323,6 +323,11 @@ class OpenAIChatFormatter(TruncatedFormatterBase):
)
elif typ == "audio":
+ # Filter out audio content when the multimodal model
+ # outputs both text and audio, to prevent errors in
+ # subsequent model calls
+ if msg.role == "assistant":
+ continue
input_audio = _to_openai_audio_data(block["source"])
content_blocks.append(
{
@@ -456,6 +461,11 @@ class OpenAIMultiAgentFormatter(TruncatedFormatterBase):
elif block["type"] == "image":
images.append(_format_openai_image_block(block))
elif block["type"] == "audio":
+ # Filter out audio content when the multimodal model
+ # outputs both text and audio, to prevent errors in
+ # subsequent model calls
+ if msg.role == "assistant":
+ continue
input_audio = _to_openai_audio_data(block["source"])
audios.append(
{
diff --git a/src/agentscope/token/_anthropic_token_counter.py b/src/agentscope/token/_anthropic_token_counter.py
index 584b667af..76f99b2ef 100644
--- a/src/agentscope/token/_anthropic_token_counter.py
+++ b/src/agentscope/token/_anthropic_token_counter.py
@@ -1,9 +1,10 @@
# -*- coding: utf-8 -*-
"""The Anthropic token counter class."""
from typing import Any
+from ._token_base import TokenCounterBase
-class AnthropicTokenCounter:
+class AnthropicTokenCounter(TokenCounterBase):
"""The Anthropic token counter class."""
def __init__(self, model_name: str, api_key: str, **kwargs: Any) -> None: