跳到内容

types#

DeploymentDefinition #

基类: BaseModel

参数

名称 类型 描述 默认值
name str
必需
源代码在 llama_deploy/types/apiserver.py
19
20
class DeploymentDefinition(BaseModel):
    name: str

Status #

基类: BaseModel

参数

名称 类型 描述 默认值
status StatusEnum
必需
status_message str
必需
max_deployments int | None
deployments list[str] | None
源代码在 llama_deploy/types/apiserver.py
12
13
14
15
16
class Status(BaseModel):
    status: StatusEnum
    status_message: str
    max_deployments: int | None = None
    deployments: list[str] | None = None

ActionTypes #

基类: str, Enum

消息的动作类型。不同的消费者将处理(或忽略)不同的动作类型。

源代码在 llama_deploy/types/core.py
17
18
19
20
21
22
23
24
25
26
27
28
29
class ActionTypes(str, Enum):
    """
    Action types for messages.
    Different consumers will handle (or ignore) different action types.
    """

    NEW_TASK = "new_task"
    COMPLETED_TASK = "completed_task"
    REQUEST_FOR_HELP = "request_for_help"
    NEW_TOOL_CALL = "new_tool_call"
    COMPLETED_TOOL_CALL = "completed_tool_call"
    TASK_STREAM = "task_stream"
    SEND_EVENT = "send_event"

ChatMessage #

基类: BaseModel

聊天消息。

参数

名称 类型 描述 默认值
role MessageRole
<MessageRole.USER: 'user'>
blocks list[Annotated[Union[TextBlock, ImageBlock, AudioBlock], FieldInfo]]

内置可变序列。

如果未给出参数,构造函数将创建一个新的空列表。如果指定了参数,它必须是可迭代的。

<dynamic>
源代码在 llama-index-core/llama_index/core/base/llms/types.py
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
class ChatMessage(BaseModel):
    """Chat message."""

    role: MessageRole = MessageRole.USER
    additional_kwargs: dict[str, Any] = Field(default_factory=dict)
    blocks: list[ContentBlock] = Field(default_factory=list)

    def __init__(self, /, content: Any | None = None, **data: Any) -> None:
        """
        Keeps backward compatibility with the old `content` field.

        If content was passed and contained text, store a single TextBlock.
        If content was passed and it was a list, assume it's a list of content blocks and store it.
        """
        if content is not None:
            if isinstance(content, str):
                data["blocks"] = [TextBlock(text=content)]
            elif isinstance(content, list):
                data["blocks"] = content

        super().__init__(**data)

    @model_validator(mode="after")
    def legacy_additional_kwargs_image(self) -> Self:
        """
        Provided for backward compatibility.

        If `additional_kwargs` contains an `images` key, assume the value is a list
        of ImageDocument and convert them into image blocks.
        """
        if documents := self.additional_kwargs.get("images"):
            documents = cast(list[ImageDocument], documents)
            for doc in documents:
                img_base64_bytes = doc.resolve_image(as_base64=True).read()
                self.blocks.append(ImageBlock(image=img_base64_bytes))
        return self

    @property
    def content(self) -> str | None:
        """
        Keeps backward compatibility with the old `content` field.

        Returns:
            The cumulative content of the TextBlock blocks, None if there are none.

        """
        content = ""
        for block in self.blocks:
            if isinstance(block, TextBlock):
                content += block.text

        return content or None

    @content.setter
    def content(self, content: str) -> None:
        """
        Keeps backward compatibility with the old `content` field.

        Raises:
            ValueError: if blocks contains more than a block, or a block that's not TextBlock.

        """
        if not self.blocks:
            self.blocks = [TextBlock(text=content)]
        elif len(self.blocks) == 1 and isinstance(self.blocks[0], TextBlock):
            self.blocks = [TextBlock(text=content)]
        else:
            raise ValueError(
                "ChatMessage contains multiple blocks, use 'ChatMessage.blocks' instead."
            )

    def __str__(self) -> str:
        return f"{self.role.value}: {self.content}"

    @classmethod
    def from_str(
        cls,
        content: str,
        role: Union[MessageRole, str] = MessageRole.USER,
        **kwargs: Any,
    ) -> Self:
        if isinstance(role, str):
            role = MessageRole(role)
        return cls(role=role, blocks=[TextBlock(text=content)], **kwargs)

    def _recursive_serialization(self, value: Any) -> Any:
        if isinstance(value, BaseModel):
            value.model_rebuild()  # ensures all fields are initialized and serializable
            return value.model_dump()  # type: ignore
        if isinstance(value, dict):
            return {
                key: self._recursive_serialization(value)
                for key, value in value.items()
            }
        if isinstance(value, list):
            return [self._recursive_serialization(item) for item in value]
        return value

    @field_serializer("additional_kwargs", check_fields=False)
    def serialize_additional_kwargs(self, value: Any, _info: Any) -> Any:
        return self._recursive_serialization(value)

content property writable #

content: str | None

保持与旧 content 字段的向后兼容性。

返回

类型 描述
str | None

TextBlock 块的累计内容,如果没有则为 None。

legacy_additional_kwargs_image #

legacy_additional_kwargs_image() -> Self

提供用于向后兼容。

如果 additional_kwargs 包含 images 键,则假定该值为 ImageDocument 列表并将其转换为图像块。

源代码在 llama-index-core/llama_index/core/base/llms/types.py
216
217
218
219
220
221
222
223
224
225
226
227
228
229
@model_validator(mode="after")
def legacy_additional_kwargs_image(self) -> Self:
    """
    Provided for backward compatibility.

    If `additional_kwargs` contains an `images` key, assume the value is a list
    of ImageDocument and convert them into image blocks.
    """
    if documents := self.additional_kwargs.get("images"):
        documents = cast(list[ImageDocument], documents)
        for doc in documents:
            img_base64_bytes = doc.resolve_image(as_base64=True).read()
            self.blocks.append(ImageBlock(image=img_base64_bytes))
    return self

EventDefinition #

基类: BaseModel

事件的定义。

当希望发送序列化事件时,用作服务端点的有效负载。

参数

名称 类型 描述 默认值
agent_id str
必需
event_obj_str str
必需

属性

名称 类型 描述
event_object_str str

事件的序列化字符串。

源代码在 llama_deploy/types/core.py
79
80
81
82
83
84
85
86
87
88
89
90
class EventDefinition(BaseModel):
    """The definition of event.

    To be used as payloads for service endpoints when wanting to send serialized
    Events.

    Attributes:
        event_object_str (str): serialized string of event.
    """

    agent_id: str
    event_obj_str: str

HumanResponse #

基类: BaseModel

简单的人类响应。

参数

名称 类型 描述 默认值
result str
必需

属性

名称 类型 描述
response str

人类响应。

源代码在 llama_deploy/types/core.py
219
220
221
222
223
224
225
226
227
228
class HumanResponse(BaseModel):
    """
    A simple human response.

    Attributes:
        response (str):
            The human response.
    """

    result: str

ServiceDefinition #

基类: BaseModel

服务的定义,捆绑了描述服务的有用信息。

参数

名称 类型 描述 默认值
service_name str

服务的名称。

必需
description str

服务及其用途的描述。

必需
prompt list[ChatMessage]

服务的特定指令。

<dynamic>
host str | None
port int | None

属性

名称 类型 描述
service_name str

服务的名称。

description str

服务及其用途的描述。

prompt list[ChatMessage]

服务的特定指令。

host str | None

服务的主机,如果它是网络服务。

port int | None

服务的端口,如果它是网络服务。

源代码在 llama_deploy/types/core.py
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
class ServiceDefinition(BaseModel):
    """
    The definition of a service, bundles useful information describing the service.

    Attributes:
        service_name (str):
            The name of the service.
        description (str):
            A description of the service and it's purpose.
        prompt (list[ChatMessage]):
            Specific instructions for the service.
        host (str | None):
            The host of the service, if its a network service.
        port (int | None):
            The port of the service, if its a network service.
    """

    service_name: str = Field(description="The name of the service.")
    description: str = Field(
        description="A description of the service and it's purpose."
    )
    prompt: list[ChatMessage] = Field(
        default_factory=list, description="Specific instructions for the service."
    )
    host: str | None = None
    port: int | None = None

SessionDefinition #

基类: BaseModel

会话的定义。

参数

名称 类型 描述 默认值
session_id str
'defafa3e-e59d-43a2-b638-a34a2e239c70'
task_ids list[str]

内置可变序列。

如果未给出参数,构造函数将创建一个新的空列表。如果指定了参数,它必须是可迭代的。

<dynamic>

属性

名称 类型 描述
session_id str

会话 ID。默认为随机 UUID。

task_definitions list[str]

按顺序排列的任务 ID,表示会话。

state dict

当前的会话状态。

源代码在 llama_deploy/types/core.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class SessionDefinition(BaseModel):
    """
    The definition of a session.

    Attributes:
        session_id (str):
            The session ID. Defaults to a random UUID.
        task_definitions (list[str]):
            The task ids in order, representing the session.
        state (dict):
            The current session state.
    """

    session_id: str = Field(default_factory=generate_id)
    task_ids: list[str] = Field(default_factory=list)
    state: dict = Field(default_factory=dict)

    @property
    def current_task_id(self) -> str | None:
        if len(self.task_ids) == 0:
            return None

        return self.task_ids[-1]

TaskDefinition #

基类: BaseModel

任务的定义和状态。

参数

名称 类型 描述 默认值
input str
必需
task_id str
'b187a3d4-cb9b-474b-b52f-f0634df613e5'
session_id str | None
agent_id str | None

属性

名称 类型 描述
input str

任务输入。

session_id str

任务所属的会话 ID。

task_id str

任务 ID。默认为随机 UUID。

agent_id str

任务应发送到的代理 ID。如果为空,则由编排器决定。

源代码在 llama_deploy/types/core.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class TaskDefinition(BaseModel):
    """
    The definition and state of a task.

    Attributes:
        input (str):
            The task input.
        session_id (str):
            The session ID that the task belongs to.
        task_id (str):
            The task ID. Defaults to a random UUID.
        agent_id (str):
            The agent ID that the task should be sent to.
            If blank, the orchestrator decides.
    """

    input: str
    task_id: str = Field(default_factory=generate_id)
    session_id: str | None = None
    agent_id: str | None = None

TaskResult #

基类: BaseModel

任务的结果。

参数

名称 类型 描述 默认值
task_id str
必需
history list[ChatMessage]
必需
result str
必需

属性

名称 类型 描述
task_id str

任务 ID。

history list[ChatMessage]

任务历史。

result str

任务结果。

data dict

关于任务或结果的附加数据。

is_last bool

如果不为 true,则还有更多结果需要流式传输。

index int

任务在会话中的索引。

源代码在 llama_deploy/types/core.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
class TaskResult(BaseModel):
    """
    The result of a task.

    Attributes:
        task_id (str):
            The task ID.
        history (list[ChatMessage]):
            The task history.
        result (str):
            The task result.
        data (dict):
            Additional data about the task or result.
        is_last (bool):
            If not true, there are more results to be streamed.
        index (int):
            The index of the task in the session.
    """

    task_id: str
    history: list[ChatMessage]
    result: str
    data: dict = Field(default_factory=dict)

TaskStream #

基类: BaseModel

任务生成的数据流。

参数

名称 类型 描述 默认值
task_id str
必需
session_id str | None
必需
data dict
必需
index int
必需

属性

名称 类型 描述
task_id str

关联的任务 ID。

data list[dict]

流数据。

index int

流数据的索引。

源代码在 llama_deploy/types/core.py
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class TaskStream(BaseModel):
    """
    A stream of data generated by a task.

    Attributes:
        task_id (str):
            The associated task ID.
        data (list[dict]):
            The stream data.
        index (int):
            The index of the stream data.
    """

    task_id: str
    session_id: str | None
    data: dict
    index: int

ToolCall #

基类: BaseModel

工具调用。

参数

名称 类型 描述 默认值
id_ str
'e52e682a-75b0-441b-b5bd-49ff35e82934'
tool_call_bundle ToolCallBundle
必需
source_id str
必需

属性

名称 类型 描述
id_ str

工具调用 ID。默认为随机 UUID。

tool_call_bundle ToolCallBundle

工具调用包。

source_id str

源 ID。

源代码在 llama_deploy/types/core.py
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
class ToolCall(BaseModel):
    """
    A tool call.

    Attributes:
        id_ (str):
            The tool call ID. Defaults to a random UUID.
        tool_call_bundle (ToolCallBundle):
            The tool call bundle.
        source_id (str):
            The source ID.
    """

    id_: str = Field(default_factory=generate_id)
    tool_call_bundle: ToolCallBundle
    source_id: str

ToolCallBundle #

基类: BaseModel

工具调用信息包。

参数

名称 类型 描述 默认值
tool_name str
必需
tool_args list[Any]
必需
tool_kwargs dict[str, Any]
必需

属性

名称 类型 描述
tool_name str

工具名称。

tool_args list[Any]

工具参数。

tool_kwargs dict[str, Any]

工具关键字参数。

源代码在 llama_deploy/types/core.py
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
class ToolCallBundle(BaseModel):
    """
    A bundle of information for a tool call.

    Attributes:
        tool_name (str):
            The name of the tool.
        tool_args (list[Any]):
            The tool arguments.
        tool_kwargs (dict[str, Any]):
            The tool keyword arguments
    """

    tool_name: str
    tool_args: list[Any]
    tool_kwargs: dict[str, Any]

ToolCallResult #

基类: BaseModel

工具调用结果。

参数

名称 类型 描述 默认值
id_ str
必需
tool_message ChatMessage
必需
result str
必需

属性

名称 类型 描述
id_ str

工具调用 ID。应与工具调用的 ID 匹配。

tool_message ChatMessage

工具消息。

result str

工具结果。

源代码在 llama_deploy/types/core.py
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
class ToolCallResult(BaseModel):
    """
    A tool call result.

    Attributes:
        id_ (str):
            The tool call ID. Should match the ID of the tool call.
        tool_message (ChatMessage):
            The tool message.
        result (str):
            The tool result.
    """

    id_: str
    tool_message: ChatMessage
    result: str