2024-01-27 00:06:38 +08:00
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import typing
|
|
|
|
|
import enum
|
|
|
|
|
import pydantic
|
|
|
|
|
|
2024-03-31 14:38:15 +08:00
|
|
|
|
import mirai
|
|
|
|
|
|
2024-01-27 00:06:38 +08:00
|
|
|
|
|
2024-01-27 21:50:40 +08:00
|
|
|
|
class FunctionCall(pydantic.BaseModel):
|
|
|
|
|
name: str
|
2024-01-27 00:06:38 +08:00
|
|
|
|
|
2024-01-27 21:50:40 +08:00
|
|
|
|
arguments: str
|
2024-01-27 00:06:38 +08:00
|
|
|
|
|
|
|
|
|
|
2024-01-27 21:50:40 +08:00
|
|
|
|
class ToolCall(pydantic.BaseModel):
|
|
|
|
|
id: str
|
2024-01-27 00:06:38 +08:00
|
|
|
|
|
2024-01-27 21:50:40 +08:00
|
|
|
|
type: str
|
2024-01-27 00:06:38 +08:00
|
|
|
|
|
2024-01-27 21:50:40 +08:00
|
|
|
|
function: FunctionCall
|
2024-01-27 00:06:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Message(pydantic.BaseModel):
|
2024-03-03 16:34:59 +08:00
|
|
|
|
"""消息"""
|
|
|
|
|
|
2024-03-20 23:32:28 +08:00
|
|
|
|
role: str # user, system, assistant, tool, command, plugin
|
2024-03-22 16:41:46 +08:00
|
|
|
|
"""消息的角色"""
|
2024-01-27 00:06:38 +08:00
|
|
|
|
|
2024-01-27 21:50:40 +08:00
|
|
|
|
name: typing.Optional[str] = None
|
2024-03-22 16:41:46 +08:00
|
|
|
|
"""名称,仅函数调用返回时设置"""
|
2024-01-27 00:06:38 +08:00
|
|
|
|
|
2024-03-31 14:38:15 +08:00
|
|
|
|
content: typing.Optional[str] | typing.Optional[mirai.MessageChain] = None
|
2024-03-22 16:41:46 +08:00
|
|
|
|
"""内容"""
|
2024-01-27 00:06:38 +08:00
|
|
|
|
|
|
|
|
|
function_call: typing.Optional[FunctionCall] = None
|
2024-03-22 16:41:46 +08:00
|
|
|
|
"""函数调用,不再受支持,请使用tool_calls"""
|
2024-01-27 21:50:40 +08:00
|
|
|
|
|
|
|
|
|
tool_calls: typing.Optional[list[ToolCall]] = None
|
2024-03-22 16:41:46 +08:00
|
|
|
|
"""工具调用"""
|
2024-01-27 21:50:40 +08:00
|
|
|
|
|
|
|
|
|
tool_call_id: typing.Optional[str] = None
|
2024-02-20 22:56:42 +08:00
|
|
|
|
|
|
|
|
|
def readable_str(self) -> str:
|
|
|
|
|
if self.content is not None:
|
2024-03-31 14:38:15 +08:00
|
|
|
|
return str(self.content)
|
2024-02-20 22:56:42 +08:00
|
|
|
|
elif self.function_call is not None:
|
|
|
|
|
return f'{self.function_call.name}({self.function_call.arguments})'
|
|
|
|
|
elif self.tool_calls is not None:
|
|
|
|
|
return f'调用工具: {self.tool_calls[0].id}'
|
|
|
|
|
else:
|
|
|
|
|
return '未知消息'
|