|
from dataclasses import asdict, dataclass |
|
from enum import IntEnum |
|
from typing import Any, Dict, List, Optional, Union |
|
|
|
from pydantic import BaseModel |
|
|
|
|
|
def enum_dict_factory(inputs): |
|
inputs = [(i[0], i[-1].value) if isinstance(i[-1], IntEnum) else i |
|
for i in inputs] |
|
return dict(inputs) |
|
|
|
|
|
def dataclass2dict(data): |
|
return asdict(data, dict_factory=enum_dict_factory) |
|
|
|
|
|
@dataclass |
|
class FunctionCall: |
|
name: str |
|
parameters: Union[Dict, str] |
|
|
|
|
|
class ActionStatusCode(IntEnum): |
|
ING = 1 |
|
SUCCESS = 0 |
|
HTTP_ERROR = -1000 |
|
ARGS_ERROR = -1001 |
|
API_ERROR = -1002 |
|
|
|
|
|
class ActionValidCode(IntEnum): |
|
FINISH = 1 |
|
OPEN = 0 |
|
CLOSED = -1 |
|
INVALID = -2 |
|
ABSENT = -3 |
|
|
|
|
|
@dataclass |
|
class ActionReturn: |
|
args: Optional[dict] = None |
|
url: Optional[str] = None |
|
type: Optional[str] = None |
|
result: Optional[List[dict]] = None |
|
errmsg: Optional[str] = None |
|
state: Union[ActionStatusCode, int] = ActionStatusCode.SUCCESS |
|
thought: Optional[str] = None |
|
valid: Optional[ActionValidCode] = ActionValidCode.OPEN |
|
|
|
def format_result(self) -> str: |
|
"""Concatenate items in result.""" |
|
result = [] |
|
for item in self.result or []: |
|
if item['type'] == 'text': |
|
result.append(item['content']) |
|
else: |
|
result.append(f"[{item['type']}]({item['content']})") |
|
result = '\n'.join(result) |
|
return result |
|
|
|
|
|
|
|
class ModelStatusCode(IntEnum): |
|
END = 0 |
|
STREAM_ING = 1 |
|
SERVER_ERR = -1 |
|
SESSION_CLOSED = -2 |
|
SESSION_OUT_OF_LIMIT = -3 |
|
SESSION_INVALID_ARG = -4 |
|
SESSION_READY = 2 |
|
|
|
|
|
class AgentStatusCode(IntEnum): |
|
END = 0 |
|
STREAM_ING = 1 |
|
SERVER_ERR = -1 |
|
SESSION_CLOSED = -2 |
|
SESSION_OUT_OF_LIMIT = -3 |
|
SESSION_INVALID_ARG = -4 |
|
SESSION_READY = 2 |
|
PLUGIN_START = 3 |
|
PLUGIN_END = 4 |
|
PLUGIN_RETURN = 5 |
|
CODING = 6 |
|
CODE_END = 7 |
|
CODE_RETURN = 8 |
|
|
|
|
|
class AgentMessage(BaseModel): |
|
content: Any |
|
sender: str = 'user' |
|
formatted: Optional[Any] = None |
|
extra_info: Optional[Any] = None |
|
type: Optional[str] = None |
|
receiver: Optional[str] = None |
|
stream_state: Union[ModelStatusCode, AgentStatusCode] = AgentStatusCode.END |
|
|