File size: 7,566 Bytes
55fc0a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import json
from functools import wraps
from typing import Any, Callable

from openai.types.chat.chat_completion_message_tool_call_param import (
    ChatCompletionMessageToolCallParam,
    Function,
)

from neollm.constants import (
    MAX_IMAGE_URL_LEN,
    ROLE2CPRINT_PARAMS,
    TITLE_COLOR,
    YEN_PAR_DOLLAR,
)
from neollm.types import (
    ChatCompletionMessage,
    Chunk,
    ClientSettings,
    InputType,
    LLMSettings,
    Message,
    Messages,
    OutputType,
    PriceInfo,
    TokenInfo,
)
from neollm.utils.postprocess import json2dict
from neollm.utils.utils import cprint


def exception_log(func: Callable[..., None]) -> Callable[..., None]:
    @wraps(func)
    def wrapper(*args: Any, **kwargs: Any) -> None:
        try:
            return func(*args, **kwargs)
        except Exception as e:
            cprint(e, color="red", background=True)

    return wrapper


def _get_tool_calls(message_dict: Message) -> list[ChatCompletionMessageToolCallParam]:
    tool_calls: list[ChatCompletionMessageToolCallParam] = []
    if "tool_calls" in message_dict:
        _tool_calls = message_dict.get("tool_calls", None)
        if _tool_calls is not None and isinstance(_tool_calls, list):  # isinstance(_tool_calls, list)ないと通らん,,,
            for _tool_call in _tool_calls:
                tool_call = ChatCompletionMessageToolCallParam(
                    id=_tool_call["id"],
                    function=Function(
                        arguments=_tool_call["function"]["arguments"],
                        name=_tool_call["function"]["name"],
                    ),
                    type=_tool_call["type"],
                )
                tool_calls.append(tool_call)
    if "function_call" in message_dict:
        function_call = message_dict.get("function_call", None)
        if function_call is not None and isinstance(
            function_call, dict
        ):  # isinstance(function_call, dict)ないと通らん,,,
            tool_calls.append(
                ChatCompletionMessageToolCallParam(
                    id="",
                    function=Function(
                        arguments=function_call["arguments"],
                        name=function_call["name"],
                    ),
                    type="function",
                )
            )
    return tool_calls


@exception_log
def print_metadata(time: float, token: TokenInfo, price: PriceInfo) -> None:
    cprint("[metadata]", color=TITLE_COLOR, kwargs={"end": " "})
    print(
        f"{time:.1f}s; "
        f"{token.total:,}({token.input:,}+{token.output:,})tokens; "
        f"${price.total:.2g}; ¥{price.total*YEN_PAR_DOLLAR:.2g}"
    )


@exception_log
def print_inputs(inputs: InputType) -> None:
    cprint("[inputs]", color=TITLE_COLOR)
    print(json.dumps(_arange_dumpable_object(inputs), indent=2, ensure_ascii=False))


@exception_log
def print_outputs(outputs: OutputType) -> None:
    cprint("[outputs]", color=TITLE_COLOR)
    print(json.dumps(_arange_dumpable_object(outputs), indent=2, ensure_ascii=False))


@exception_log
def print_messages(messages: list[ChatCompletionMessage] | Messages | None, title: bool = True) -> None:
    if messages is None:
        cprint("Not yet running _preprocess", color="red")
        return
    if title:
        cprint("[messages]", color=TITLE_COLOR)
    messages = [
        message.to_typeddict_message() if isinstance(message, ChatCompletionMessage) else message
        for message in messages
    ]
    for message in messages:
        # roleの出力 ----------------------------------------
        cprint(" " + message["role"], **ROLE2CPRINT_PARAMS[message["role"]])

        # contentの出力 ----------------------------------------
        if message["content"] is None:
            pass
        elif isinstance(message["content"], str):
            print("  " + message["content"].replace("\n", "\n  "))
        elif isinstance(message["content"], list):
            for part in message["content"]:
                if part["type"] == "text":
                    print("  " + part["text"].replace("\n", "\n  "))
                elif part["type"] == "image_url":
                    cprint("  <image_url>", color="green", kwargs={"end": " "})
                    print(str(part["image_url"])[:MAX_IMAGE_URL_LEN])
                    if len(str(part["image_url"])) > MAX_IMAGE_URL_LEN:
                        print("...")
                elif part["type"] == "refusal":
                    cprint("  <refusal>", color="yellow", kwargs={"end": " "})
                    print(part["refusal"])

        # tool_calls, function_callsの出力 ----------------------------------------
        for tool_call in _get_tool_calls(message):
            print(" ", end="")
            cprint(tool_call["function"]["name"], color="green", background=True)
            print("  " + str(json2dict(tool_call["function"]["arguments"], error_key=None)).replace("\n", "\n    "))


@exception_log
def print_delta(chunk: Chunk) -> None:
    if len(chunk.choices) == 0:
        return
    choice = chunk.choices[0]  # TODO: n>2の対応
    if choice.delta.role is not None:
        cprint(f" {choice.delta.role}\n" + "  ", color="green")
    if choice.delta.content is not None:
        print(choice.delta.content.replace("\n", "\n  "), end="")
    if choice.delta.function_call is not None:
        if choice.delta.function_call.name is not None:
            cprint(choice.delta.function_call.name, color="green", background=True)
            print("  ", end="")
        if choice.delta.function_call.arguments is not None:
            print(choice.delta.function_call.arguments.replace("\n", "\n  "), end="")
    if choice.delta.tool_calls is not None:
        for tool_call in choice.delta.tool_calls:
            if tool_call.function is None:
                continue
            if tool_call.index >= 1:
                print("\n  ", end="")
            if tool_call.function.name is not None:
                cprint(f"{tool_call.function.name}\n" + "  ", color="green", background=True)
            if tool_call.function.arguments is not None:
                print(tool_call.function.arguments.replace("\n", "\n  "), end="")
    if choice.finish_reason is not None:
        print()


@exception_log
def print_llm_settings(llm_settings: LLMSettings, model: str, engine: str | None, platform: str) -> None:
    cprint("[llm_settings]", color=TITLE_COLOR, kwargs={"end": " "})
    llm_settings_copy = dict(platform=platform, model=model, **llm_settings)
    # Azureの場合
    if platform == "azure":
        llm_settings_copy["engine"] = engine  # engineを追加
    print(llm_settings_copy or "-")


@exception_log
def print_client_settings(client_settings: ClientSettings) -> None:
    cprint("[client_settings]", color=TITLE_COLOR, kwargs={"end": " "})
    print(client_settings or "-")


# -------

_DumplableEntity = int | float | str | bool | None | list[Any] | dict[Any, Any]
DumplableType = _DumplableEntity | list["DumplableType"] | dict["DumplableType", "DumplableType"]


def _arange_dumpable_object(obj: Any) -> DumplableType:
    if obj is None or isinstance(obj, (int, float, str, bool)):
        return obj

    # list -> Array
    if isinstance(obj, list):
        return [_arange_dumpable_object(item) for item in obj]

    # dict -> Object
    if isinstance(obj, dict):
        return {_arange_dumpable_object(key): _arange_dumpable_object(value) for key, value in obj.items()}

    # Other -> String
    return f"<{type(obj).__name__}>{str(obj)}"