Skip to content

Commit 8005e79

Browse files
authored
fix(formatter): fix the local uri error with a monkey patch with agentscope==1.0.16dev (agentscope-ai#627)
1 parent d259783 commit 8005e79

1 file changed

Lines changed: 37 additions & 1 deletion

File tree

src/copaw/agents/model_factory.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@
99
>>> model, formatter = create_model_and_formatter()
1010
"""
1111

12+
1213
import logging
1314
import os
14-
from typing import TYPE_CHECKING, Optional, Sequence, Tuple, Type
15+
from typing import TYPE_CHECKING, Optional, Sequence, Tuple, Type, Any
16+
from functools import wraps
1517

1618
from agentscope.formatter import FormatterBase, OpenAIChatFormatter
1719
from agentscope.model import ChatModelBase, OpenAIChatModel
20+
from agentscope.message import Msg
21+
import agentscope
1822

1923
try:
2024
from agentscope.formatter import AnthropicChatFormatter
@@ -32,6 +36,38 @@
3236
load_providers_json,
3337
)
3438

39+
40+
def _monkey_patch(func):
41+
"""A monkey patch wrapper for agentscope <= 1.0.16dev"""
42+
43+
@wraps(func)
44+
async def wrapper(
45+
self,
46+
msgs: list[Msg],
47+
**kwargs: Any,
48+
) -> list[dict[str, Any]]:
49+
for msg in msgs:
50+
if isinstance(msg.content, str):
51+
continue
52+
if isinstance(msg.content, list):
53+
for block in msg.content:
54+
if (
55+
block["type"] in ["audio", "image", "video"]
56+
and block.get("source", {}).get("type") == "url"
57+
):
58+
url = block["source"]["url"]
59+
if url.startswith("file://"):
60+
block["source"]["url"] = url.removeprefix(
61+
"file://",
62+
)
63+
return await func(self, msgs, **kwargs)
64+
65+
return wrapper
66+
67+
68+
if agentscope.__version__ == "1.0.16dev":
69+
OpenAIChatFormatter.format = _monkey_patch(OpenAIChatFormatter.format)
70+
3571
if TYPE_CHECKING:
3672
from ..providers import ResolvedModelConfig
3773

0 commit comments

Comments
 (0)