|
9 | 9 | >>> model, formatter = create_model_and_formatter() |
10 | 10 | """ |
11 | 11 |
|
| 12 | + |
12 | 13 | import logging |
13 | 14 | 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 |
15 | 17 |
|
16 | 18 | from agentscope.formatter import FormatterBase, OpenAIChatFormatter |
17 | 19 | from agentscope.model import ChatModelBase, OpenAIChatModel |
| 20 | +from agentscope.message import Msg |
| 21 | +import agentscope |
18 | 22 |
|
19 | 23 | try: |
20 | 24 | from agentscope.formatter import AnthropicChatFormatter |
|
32 | 36 | load_providers_json, |
33 | 37 | ) |
34 | 38 |
|
| 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 | + |
35 | 71 | if TYPE_CHECKING: |
36 | 72 | from ..providers import ResolvedModelConfig |
37 | 73 |
|
|
0 commit comments