mlflow.types

The mlflow.types 模块定义了供其他 MLflow 组件使用的数据类型和实用程序,用于描述独立于其他框架或语言的接口。

class mlflow.types.ColSpec(type: Union[mlflow.types.schema.Array, mlflow.types.schema.DataType, mlflow.types.schema.Map, mlflow.types.schema.Object, mlflow.types.schema.AnyType, str], name: Optional[str] = None, required: bool = True)[source]

Bases: object

Specifies the name and type of a single column in a dataset.

classmethod from_json_dict(**kwargs)[source]

从 JSON 加载的字典进行反序列化。预期字典包含 type 和可选的 namerequired 键。

property name: str | None

列名,如果列未命名则为 None。

property required: bool

此列是否为必需的。

property type: mlflow.types.schema.DataType | mlflow.types.schema.Array | mlflow.types.schema.Object | mlflow.types.schema.Map | mlflow.types.schema.AnyType

列数据类型。

class mlflow.types.DataType(value)[source]

Bases: enum.Enum

MLflow 数据类型。

binary = 7

原始字节序列。

boolean = 1

逻辑数据(True,False)。

datetime = 8

64 位日期时间数据。

double = 5

64 位浮点数。

float = 4

32 位浮点数。

integer = 2

32 位带符号整数。

long = 3

64 位带符号整数。

string = 6

文本数据。

to_numpy() numpy.dtype[source]

获取等效的 numpy 数据类型。

to_pandas() numpy.dtype[source]

获取等效的 pandas 数据类型。

to_python()[source]

获取等效的 python 数据类型。

class mlflow.types.ParamSchema(params: list[mlflow.types.schema.ParamSpec])[source]

Bases: object

Specifies parameters applicable to the model. ParamSchema is represented as a list of ParamSpec.

classmethod from_json(json_str: str)[source]

从 JSON 字符串进行反序列化。

property params: list[mlflow.types.schema.ParamSpec]

ParamSchema 表示为 ParamSpec 列表。

to_dict() list[dict[str, typing.Any]][source]

序列化为 JSON 可序列化的字典。

to_json() str[source]

序列化为 JSON 字符串。

class mlflow.types.ParamSpec(name: str, dtype: mlflow.types.schema.DataType | mlflow.types.schema.Object | str, default: Any, shape: Optional[tuple[int, ...]] = None)[source]

Bases: object

用于表示模型参数的规范。

class mlflow.types.ParamSpecTypedDict[source]

Bases: TypedDict

property default: Any

参数的默认值。

property dtype: mlflow.types.schema.DataType | mlflow.types.schema.Object

参数数据类型。

classmethod from_json_dict(**kwargs)[source]

从 JSON 加载的字典进行反序列化。预期字典包含 nametypedefault 键。

property name: str

参数的名称。

property shape: tuple[int, ...] | None

参数形状。如果形状为 None,则参数为标量。

classmethod validate_type_and_shape(spec: str, value: Any, value_type: mlflow.types.schema.DataType | mlflow.types.schema.Object, shape: tuple[int, ...] | None)[source]

验证值是否具有预期的类型和形状。

class mlflow.types.Schema(inputs: list[mlflow.types.schema.ColSpec | mlflow.types.schema.TensorSpec])[source]

Bases: object

数据集的规范。

Schema 由 ColSpec 或 TensorSpec 列表表示。不允许 ColSpec 和 TensorSpec 的组合。

由 schema 表示的数据集可以命名,每个输入都有唯一的非空名称。对于 ColSpec,数据集列可以未命名,并根据其列表索引隐式定义整数索引。不允许命名和未命名数据输入的组合。

as_spark_schema()[source]

转换为 Spark schema。如果此 schema 是单个未命名列,则直接转换为相应的 spark 数据类型,否则将其作为 struct 返回(缺失的列名会用整数序列填充)。TensorSpec 不支持。

classmethod from_json(json_str: str)[source]

从 JSON 字符串进行反序列化。

has_input_names() bool[source]

如果此 schema 声明了名称,则返回 true,否则返回 false。

input_dict() dict[str, mlflow.types.schema.ColSpec | mlflow.types.schema.TensorSpec][source]

将列名映射到输入,如果此 schema 声明了名称。

input_names() list[str | int][source]

获取数据名称列表或索引范围(如果 schema 没有名称)。

input_types() list[mlflow.types.schema.DataType | numpy.dtype | mlflow.types.schema.Array | mlflow.types.schema.Object][source]

获取 schema 中每列的类型。

input_types_dict() dict[str, mlflow.types.schema.DataType | numpy.dtype | mlflow.types.schema.Array | mlflow.types.schema.Object][source]

将列名映射到类型,如果此 schema 声明了名称。

property inputs: list[mlflow.types.schema.ColSpec | mlflow.types.schema.TensorSpec]

定义此 schema 的数据集的表示。

is_tensor_spec() bool[source]

如果此 schema 使用 TensorSpec 指定,则返回 true,否则返回 false。

numpy_types() list[numpy.dtype][source]

方便快捷地将数据类型获取为 numpy 类型。

optional_input_names() list[str | int][source]

获取可选数据名称列表或索引范围(如果 schema 没有名称)。

pandas_types() list[numpy.dtype][source]

方便快捷地将数据类型获取为 pandas 类型。TensorSpec 不支持。

required_input_names() list[str | int][source]

获取必需数据名称列表或索引范围(如果 schema 没有名称)。

to_dict() list[dict[str, typing.Any]][source]

序列化为 JSON 可序列化的字典。

to_json() str[source]

序列化为 JSON 字符串。

class mlflow.types.TensorSpec(type: numpy.dtype, shape: tuple[int, ...] | list[int], name: Optional[str] = None)[source]

Bases: object

用于表示存储为 Tensor 的数据集的规范。

classmethod from_json_dict(**kwargs)[source]

从 JSON 加载的字典进行反序列化。预期字典包含 typetensor-spec 键。

property name: str | None

张量名称,如果张量未命名则为 None。

property required: bool

此张量是否为必需的。

property shape: tuple[int, ...]

张量形状。

property type: numpy.dtype

21 种不同的 numpy 内置类型的唯一字符代码。有关详细信息,请参阅 https://numpy.com.cn/devdocs/reference/generated/numpy.dtype.html#numpy.dtype

class mlflow.types.responses.ResponsesAgentRequest(*, tool_choice: str | mlflow.types.responses_helpers.ToolChoiceFunction | None = None, truncation: str | None = None, max_output_tokens: int | None = None, metadata: dict[str, str] | None = None, parallel_tool_calls: bool | None = None, tools: list[mlflow.types.responses_helpers.Tool] | None = None, reasoning: mlflow.types.responses_helpers.ReasoningParams | None = None, store: bool | None = None, stream: bool | None = None, temperature: float | None = None, text: Optional[Any] = None, top_p: float | None = None, user: str | None = None, input: list[mlflow.types.responses_helpers.Message | mlflow.types.responses_helpers.OutputItem], custom_inputs: dict[str, typing.Any] | None = None, context: mlflow.types.agent.ChatContext | None = None)[source]

ResponsesAgent 的请求对象。

参数
class mlflow.types.responses.ResponsesAgentResponse(*, tool_choice: str | mlflow.types.responses_helpers.ToolChoiceFunction | None = None, truncation: str | None = None, id: str | None = None, created_at: float | None = None, error: mlflow.types.responses_helpers.ResponseError | None = None, incomplete_details: mlflow.types.responses_helpers.IncompleteDetails | None = None, instructions: str | None = None, metadata: dict[str, str] | None = None, model: str | None = None, object: str = 'response', output: list[mlflow.types.responses_helpers.OutputItem], parallel_tool_calls: bool | None = None, temperature: float | None = None, tools: list[mlflow.types.responses_helpers.Tool] | None = None, top_p: float | None = None, max_output_tokens: int | None = None, previous_response_id: str | None = None, reasoning: mlflow.types.responses_helpers.ReasoningParams | None = None, status: str | None = None, text: Optional[Any] = None, usage: mlflow.types.responses_helpers.ResponseUsage | None = None, user: str | None = None, custom_outputs: dict[str, typing.Any] | None = None)[source]

ResponsesAgent 的响应对象。

参数
class mlflow.types.responses.ResponsesAgentStreamEvent(*, type: str, custom_outputs: dict[str, typing.Any] | None = None, **extra_data: Any)[source]

ResponsesAgent 的流式事件。请参见 https://mlflow.org.cn/docs/latest/genai/flavors/responses-agent-intro#streaming-agent-output 中的示例

参数
  • type (str) – 流式事件的类型

  • custom_outputs (Dict[str, Any]) – 一个可选参数,用于提供来自模型的任意附加上下文。字典值必须是 JSON 可序列化的。可选,默认为 None

class mlflow.types.responses_helpers.Annotation(*, type: str, **extra_data: Any)[source]
class mlflow.types.responses_helpers.AnnotationFileCitation(*, file_id: str, index: int, type: str = 'file_citation')[source]
class mlflow.types.responses_helpers.AnnotationFilePath(*, file_id: str, index: int, type: str = 'file_path')[source]
class mlflow.types.responses_helpers.AnnotationURLCitation(*, end_index: int | None = None, start_index: int | None = None, title: str, type: str = 'url_citation', url: str)[source]
class mlflow.types.responses_helpers.BaseRequestPayload(*, tool_choice: str | mlflow.types.responses_helpers.ToolChoiceFunction | None = None, truncation: str | None = None, max_output_tokens: int | None = None, metadata: dict[str, str] | None = None, parallel_tool_calls: bool | None = None, tools: list[mlflow.types.responses_helpers.Tool] | None = None, reasoning: mlflow.types.responses_helpers.ReasoningParams | None = None, store: bool | None = None, stream: bool | None = None, temperature: float | None = None, text: Optional[Any] = None, top_p: float | None = None, user: str | None = None)[source]
class mlflow.types.responses_helpers.Content(*, type: str, **extra_data: Any)[source]
class mlflow.types.responses_helpers.FunctionCallOutput(*, status: str | None = None, call_id: str, output: str, type: str = 'function_call_output')[source]
class mlflow.types.responses_helpers.FunctionTool(*, name: str, parameters: dict[str, typing.Any], strict: bool | None = None, type: str = 'function', description: str | None = None)[source]
class mlflow.types.responses_helpers.IncompleteDetails(*, reason: str | None = None)[source]
class mlflow.types.responses_helpers.InputTokensDetails(*, cached_tokens: int)[source]
class mlflow.types.responses_helpers.McpApprovalRequest(*, status: str | None = None, id: str, arguments: str, name: str, server_label: str, type: str = 'mcp_approval_request')[source]
class mlflow.types.responses_helpers.McpApprovalResponse(*, status: str | None = None, approval_request_id: str, approve: bool, type: str = 'mcp_approval_response', id: str | None = None, reason: str | None = None)[source]
class mlflow.types.responses_helpers.Message(*, status: str | None = None, content: str | list[mlflow.types.responses_helpers.ResponseInputTextParam | dict[str, typing.Any]], role: str, type: str = 'message')[source]
class mlflow.types.responses_helpers.OutputItem(*, type: str, **extra_data: Any)[source]
class mlflow.types.responses_helpers.OutputTokensDetails(*, reasoning_tokens: int)[source]
class mlflow.types.responses_helpers.ReasoningParams(*, effort: str | None = None, generate_summary: str | None = None)[source]
class mlflow.types.responses_helpers.Response(*, tool_choice: str | mlflow.types.responses_helpers.ToolChoiceFunction | None = None, truncation: str | None = None, id: str | None = None, created_at: float | None = None, error: mlflow.types.responses_helpers.ResponseError | None = None, incomplete_details: mlflow.types.responses_helpers.IncompleteDetails | None = None, instructions: str | None = None, metadata: dict[str, str] | None = None, model: str | None = None, object: str = 'response', output: list[mlflow.types.responses_helpers.OutputItem], parallel_tool_calls: bool | None = None, temperature: float | None = None, tools: list[mlflow.types.responses_helpers.Tool] | None = None, top_p: float | None = None, max_output_tokens: int | None = None, previous_response_id: str | None = None, reasoning: mlflow.types.responses_helpers.ReasoningParams | None = None, status: str | None = None, text: Optional[Any] = None, usage: mlflow.types.responses_helpers.ResponseUsage | None = None, user: str | None = None)[source]
property output_text: str

便捷属性,用于聚合“output”列表中的所有“output_text”项。

如果不存在“output_text”内容块,则返回空字符串。

class mlflow.types.responses_helpers.ResponseCompletedEvent(*, response: mlflow.types.responses_helpers.Response, type: str = 'response.completed')[source]
class mlflow.types.responses_helpers.ResponseError(*, code: str | None = None, message: str)[source]
class mlflow.types.responses_helpers.ResponseErrorEvent(*, code: str | None = None, message: str, param: str | None = None, type: str = 'error')[source]
class mlflow.types.responses_helpers.ResponseFunctionToolCall(*, status: str | None = None, arguments: str, call_id: str, name: str, type: str = 'function_call', id: str | None = None)[source]
class mlflow.types.responses_helpers.ResponseInputTextParam(*, text: str, type: str = 'input_text')[source]
class mlflow.types.responses_helpers.ResponseOutputItemDoneEvent(*, item: mlflow.types.responses_helpers.OutputItem, output_index: int | None = None, type: str = 'response.output_item.done')[source]
class mlflow.types.responses_helpers.ResponseOutputMessage(*, status: str | None = None, id: str, content: list[mlflow.types.responses_helpers.Content], role: str = 'assistant', type: str = 'message')[source]
class mlflow.types.responses_helpers.ResponseOutputRefusal(*, refusal: str, type: str = 'refusal')[source]
class mlflow.types.responses_helpers.ResponseOutputText(*, annotations: list[mlflow.types.responses_helpers.Annotation] | None = None, text: str, type: str = 'output_text')[source]
class mlflow.types.responses_helpers.ResponseReasoningItem(*, status: str | None = None, id: str, summary: list[mlflow.types.responses_helpers.Summary], type: str = 'reasoning')[source]
class mlflow.types.responses_helpers.ResponseTextAnnotationDeltaEvent(*, annotation: mlflow.types.responses_helpers.Annotation, annotation_index: int, content_index: int | None = None, item_id: str, output_index: int | None = None, type: str = 'response.output_text.annotation.added')[source]
class mlflow.types.responses_helpers.ResponseTextDeltaEvent(*, content_index: int | None = None, delta: str, item_id: str, output_index: int | None = None, type: str = 'response.output_text.delta')[source]
class mlflow.types.responses_helpers.ResponseUsage(*, input_tokens: int, input_tokens_details: mlflow.types.responses_helpers.InputTokensDetails, output_tokens: int, output_tokens_details: mlflow.types.responses_helpers.OutputTokensDetails, total_tokens: int)[source]
class mlflow.types.responses_helpers.Status(*, status: str | None = None)[source]
class mlflow.types.responses_helpers.Summary(*, text: str, type: str = 'summary_text')[source]
class mlflow.types.responses_helpers.Tool(*, type: str, **extra_data: Any)[source]
class mlflow.types.responses_helpers.ToolChoice(*, tool_choice: str | mlflow.types.responses_helpers.ToolChoiceFunction | None = None)[source]
class mlflow.types.responses_helpers.ToolChoiceFunction(*, name: str, type: str = 'function')[source]
class mlflow.types.responses_helpers.Truncation(*, truncation: str | None = None)[source]
class mlflow.types.agent.ChatAgentChunk(*, delta: mlflow.types.agent.ChatAgentMessage, finish_reason: str | None = None, custom_outputs: dict[str, typing.Any] | None = None, usage: mlflow.types.chat.ChatUsage | None = None)[source]

代表 ChatAgent 流式响应中的单个块。

参数
  • delta – 一个 ChatAgentMessage,表示组成代理输出的消息列表中的单个块。特别是,客户端应假定此 ChatAgentMessage 中的 content 字段仅包含消息内容的一部分,并按 ID 聚合跨块的消息内容。有关更多信息,请参阅 ChatAgent.predict_stream 的文档字符串。

  • finish_reason (str) – 生成停止的原因。**可选**,默认为 None

  • custom_outputs (Dict[str, Any]) – 一个可选参数,用于提供来自模型的任意附加上下文。字典值必须是 JSON 可序列化的。可选,默认为 None

  • usage (mlflow.types.chat.ChatUsage) – 请求的 token 用量。**可选**,默认为 None

check_message_id()[source]

确保消息 ID 是唯一的。

class mlflow.types.agent.ChatAgentMessage(*, role: str, content: str | None = None, name: str | None = None, id: str | None = None, tool_calls: list[mlflow.types.chat.ToolCall] | None = None, tool_call_id: str | None = None, attachments: dict[str, str] | None = None)[source]

ChatAgent 模型请求或响应中的一条消息。

参数
  • role (str) – 发送消息的实体的角色(例如,"user""system""assistant""tool")。

  • content (str) – 消息的内容。**可选**。如果提供了 tool_calls,则可以为 None

  • name (str) – 发送消息的实体的名称。**可选**,默认为 None

  • id (str) – 消息的 ID。当它是 ChatAgentResponseChatAgentChunk 的一部分时,则为必需。

  • tool_calls (List[mlflow.types.chat.ToolCall]) – 模型进行的一系列工具调用。**可选**,默认为 None

  • tool_call_id (str) – 此消息响应的工具调用的 ID。**可选**,默认为 None

  • attachments (Dict[str, str]) – 附件字典。**可选**,默认为 None

check_content_and_tool_calls()[source]

确保 'content' 或 'tool_calls' 至少有一个被设置。

check_tool_messages()[source]

确保工具消息的 'name' 和 'tool_call_id' 字段被设置。

class mlflow.types.agent.ChatAgentRequest(*, messages: list[mlflow.types.agent.ChatAgentMessage], context: mlflow.types.agent.ChatContext | None = None, custom_inputs: dict[str, typing.Any] | None = None, stream: bool | None = False)[source]

ChatAgent 接口请求的格式。

参数
  • messages – 将传递给模型的 ChatAgentMessage 列表。

  • context (ChatContext) – 用于聊天端点的上下文。包括 conversation_id 和 user_id。**可选**,默认为 None

  • custom_inputs (Dict[str, Any]) – 一个可选参数,用于向模型提供任意的附加上下文。字典值必须是 JSON 可序列化的。可选,默认为 None

  • stream (bool) – 是否在生成响应时立即流式传输。**可选**,默认为 False

class mlflow.types.agent.ChatAgentResponse(*, messages: list[mlflow.types.agent.ChatAgentMessage], finish_reason: str | None = None, custom_outputs: dict[str, typing.Any] | None = None, usage: mlflow.types.chat.ChatUsage | None = None)[source]

代表 ChatAgent 的响应。

参数
  • messages – 从模型返回的 ChatAgentMessage 列表。

  • finish_reason (str) – 生成停止的原因。**可选**,默认为 None

  • custom_outputs (Dict[str, Any]) – 一个可选参数,用于提供来自模型的任意附加上下文。字典值必须是 JSON 可序列化的。可选,默认为 None

  • usage (mlflow.types.chat.ChatUsage) – 请求的 token 用量。**可选**,默认为 None

check_message_ids()[source]

确保所有消息都有 ID 并且 ID 是唯一的。

class mlflow.types.agent.ChatContext(*, conversation_id: str | None = None, user_id: str | None = None)[source]

要在 ChatAgent 端点中使用的上下文。

参数
  • conversation_id (str) – 对话的 ID。**可选**,默认为 None

  • user_id (str) – 用户的 ID。**可选**,默认为 None

class mlflow.types.llm.ChatChoice(message: mlflow.types.llm.ChatMessage, index: int = 0, finish_reason: str = 'stop', logprobs: Optional[mlflow.types.llm.ChatChoiceLogProbs] = None)[source]

模型生成的单个聊天响应。参考:https://platform.openai.com/docs/api-reference/chat/object

参数
  • message (ChatMessage) – 生成的消息。

  • index (int) – 响应在响应列表中的索引。默认为 0

  • finish_reason (str) – 生成停止的原因。**可选**,默认为 "stop"

  • logprobs (ChatChoiceLogProbs) – 选择的对数概率信息。**可选**,默认为 None

class mlflow.types.llm.ChatChoiceDelta(role: str | None = 'assistant', content: Optional[str] = None, refusal: Optional[str] = None, name: Optional[str] = None, tool_calls: Optional[list[mlflow.types.llm.ToolCall]] = None)[source]

聊天响应中的流式消息增量。

参数
  • role (str) – 发送消息的实体的角色(例如,"user""system""assistant""tool")。**可选**,默认为 "assistant"。此项为可选,因为 OpenAI 客户端可以显式返回 None 作为角色。

  • content (str) – 要流式传输的新 token 的内容。**可选**。在最后一个增量块上,或者如果提供了 refusal 或 tool_calls,则可能为 None

  • refusal (str) – 拒绝消息的内容。**可选**。如果提供了拒绝响应,则会填充此字段。

  • name (str) – 发送消息的实体的名称。**可选**。

  • tool_calls (List[ToolCall]) – 模型进行的一系列工具调用。**可选**,默认为 None

class mlflow.types.llm.ChatChoiceLogProbs(content: Optional[list[mlflow.types.llm.TokenLogProb]] = None)[source]

选择的对数概率信息。

参数

content – 包含对数概率信息的消息内容 token 列表。

class mlflow.types.llm.ChatChunkChoice(delta: mlflow.types.llm.ChatChoiceDelta, index: int = 0, finish_reason: Optional[str] = None, logprobs: Optional[mlflow.types.llm.ChatChoiceLogProbs] = None)[source]

模型生成的单个聊天响应块。参考:https://platform.openai.com/docs/api-reference/chat/streaming

参数
  • index (int) – 响应在响应列表中的索引。默认为 0

  • delta (ChatChoiceDelta) – 生成的流式消息块。

  • finish_reason (str) – 生成停止的原因。**可选**,默认为 None

  • logprobs (ChatChoiceLogProbs) – 选择的对数概率信息。**可选**,默认为 None

class mlflow.types.llm.ChatCompletionChunk(choices: list[mlflow.types.llm.ChatChunkChoice], usage: Optional[mlflow.types.llm.TokenUsageStats] = None, id: Optional[str] = None, model: Optional[str] = None, object: str = 'chat.completion.chunk', created: int = <factory>, custom_outputs: Optional[dict[str, typing.Any]] = None)[source]

聊天端点返回的流式块。参考:https://platform.openai.com/docs/api-reference/chat/streaming

参数
  • choices (List[ChatChunkChoice]) – 包含流式响应生成块的 ChatChunkChoice 对象列表。

  • usage (TokenUsageStats) – 描述请求使用的 token 的对象。可选,默认为 None

  • id (str) – 响应的 ID。可选,默认为 None

  • model (str) – 使用的模型名称。可选,默认为 None

  • object (str) – 对象类型。默认为 'chat.completion.chunk'。

  • created (int) – 响应创建时间。可选,默认为当前时间。

  • custom_outputs (Dict[str, Any]) – 一个可以包含任意附加上下文的字段。字典值必须是 JSON 可序列化的。可选,默认为 None

class mlflow.types.llm.ChatCompletionRequest(temperature: float = 1.0, max_tokens: Optional[int] = None, stop: Optional[list[str]] = None, n: int = 1, stream: bool = False, top_p: Optional[float] = None, top_k: Optional[int] = None, frequency_penalty: Optional[float] = None, presence_penalty: Optional[float] = None, custom_inputs: Optional[dict[str, typing.Any]] = None, tools: Optional[list[mlflow.types.llm.ToolDefinition]] = None, messages: list[mlflow.types.llm.ChatMessage] = <factory>)[source]

聊天端点期望的请求对象格式。

参数
  • messages (List[ChatMessage]) – 将传递给模型的 ChatMessage 对象列表。可选,默认为空列表 ([])。

  • temperature (float) – 用于在推理过程中控制随机性和创造性的参数。可选,默认为 1.0

  • max_tokens (int) – 要生成的最大 token 数。可选,默认为 None(无限制)。

  • stop (List[str]) – 停止生成的 token 列表。可选,默认为 None

  • n (int) – 要生成的响应数。可选,默认为 1

  • stream (bool) – 是否在生成响应时立即流式传输。**可选**,默认为 False

  • top_p (float) – 一个可选参数,用于控制与 temperature 一起采样,模型会考虑 top_p 概率质量的 token 结果。例如,0.1 表示仅考虑占 top 10% 概率质量的 token。

  • top_k (int) – 一个可选参数,用于将词汇表大小减少到 top k 个 token(按概率降序排序)。

  • frequency_penalty – (float): 一个可选参数,可以是正值或负值。正值根据 token 在文本中已存在的频率来惩罚新 token,降低模型逐字重复相同行的可能性。

  • presence_penalty – (float): 一个可选参数,可以是正值或负值。正值根据 token 在文本中是否已出现来惩罚新 token,增加模型谈论新主题的可能性。

  • custom_inputs (Dict[str, Any]) – 一个可选参数,用于向模型提供任意附加上下文。字典值必须是 JSON 可序列化的。

  • tools (List[ToolDefinition]) – 可以被模型调用的工具的可选列表。

警告

在即将发布的 MLflow 版本中,temperaturenstream 的默认值将被移除。如果需要,请在代码中显式提供这些值。

class mlflow.types.llm.ChatCompletionResponse(choices: list[mlflow.types.llm.ChatChoice], usage: Optional[mlflow.types.llm.TokenUsageStats] = None, id: Optional[str] = None, model: Optional[str] = None, object: str = 'chat.completion', created: int = <factory>, custom_outputs: Optional[dict[str, typing.Any]] = None)[source]

聊天端点返回的完整响应对象。

参数
  • choices (List[ChatChoice]) – 包含生成响应的 ChatChoice 对象列表。

  • usage (TokenUsageStats) – 描述请求使用的 token 的对象。可选,默认为 None

  • id (str) – 响应的 ID。可选,默认为 None

  • model (str) – 使用的模型名称。可选,默认为 None

  • object (str) – 对象类型。默认为 'chat.completion'。

  • created (int) – 响应创建时间。可选,默认为当前时间。

  • custom_outputs (Dict[str, Any]) – 一个可以包含任意附加上下文的字段。字典值必须是 JSON 可序列化的。可选,默认为 None

class mlflow.types.llm.ChatMessage(role: str, content: Optional[str] = None, refusal: Optional[str] = None, name: Optional[str] = None, tool_calls: Optional[list[mlflow.types.llm.ToolCall]] = None, tool_call_id: Optional[str] = None)[source]

聊天请求或响应中的一条消息。

参数
  • role (str) – 发送消息的实体的角色(例如,"user""system""assistant""tool")。

  • content (str) – 消息的内容。可选,如果提供了 refusal 或 tool_calls,则可以为 None

  • refusal (str) – 拒绝消息的内容。**可选**。如果提供了拒绝响应,则会填充此字段。

  • name (str) – 发送消息的实体的名称。**可选**。

  • tool_calls (List[ToolCall]) – 模型进行的一系列工具调用。**可选**,默认为 None

  • tool_call_id (str) – 此消息响应的工具调用的 ID。**可选**,默认为 None

class mlflow.types.llm.ChatParams(temperature: float = 1.0, max_tokens: Optional[int] = None, stop: Optional[list[str]] = None, n: int = 1, stream: bool = False, top_p: Optional[float] = None, top_k: Optional[int] = None, frequency_penalty: Optional[float] = None, presence_penalty: Optional[float] = None, custom_inputs: Optional[dict[str, typing.Any]] = None, tools: Optional[list[mlflow.types.llm.ToolDefinition]] = None)[source]

聊天推理的常用参数。

参数
  • temperature (float) – 用于在推理过程中控制随机性和创造性的参数。可选,默认为 1.0

  • max_tokens (int) – 要生成的最大 token 数。可选,默认为 None(无限制)。

  • stop (List[str]) – 停止生成的 token 列表。可选,默认为 None

  • n (int) – 要生成的响应数。可选,默认为 1

  • stream (bool) – 是否在生成响应时立即流式传输。**可选**,默认为 False

  • top_p (float) – 一个可选参数,用于控制与 temperature 一起采样,模型会考虑 top_p 概率质量的 token 结果。例如,0.1 表示仅考虑占 top 10% 概率质量的 token。

  • top_k (int) – 一个可选参数,用于将词汇表大小减少到 top k 个 token(按概率降序排序)。

  • frequency_penalty – (float): 一个可选参数,可以是正值或负值。正值根据 token 在文本中已存在的频率来惩罚新 token,降低模型逐字重复相同行的可能性。

  • presence_penalty – (float): 一个可选参数,可以是正值或负值。正值根据 token 在文本中是否已出现来惩罚新 token,增加模型谈论新主题的可能性。

  • custom_inputs (Dict[str, Any]) – 一个可选参数,用于向模型提供任意附加上下文。字典值必须是 JSON 可序列化的。

  • tools (List[ToolDefinition]) – 可以被模型调用的工具的可选列表。

警告

在即将发布的 MLflow 版本中,temperaturenstream 的默认值将被移除。如果需要,请在代码中显式提供这些值。

classmethod keys() set[str][source]

返回 dataclass 的键。

class mlflow.types.llm.FunctionToolCallArguments(name: str, arguments: str)[source]

模型进行的函数工具调用的参数。

参数
  • arguments (str) – 应传递给工具的参数的 JSON 字符串。

  • name (str) – 正在调用的工具的名称。

class mlflow.types.llm.FunctionToolDefinition(name: str, description: Optional[str] = None, parameters: Optional[mlflow.types.llm.ToolParamsSchema] = None, strict: bool = False)[source]

函数工具的定义(目前唯一支持的工具类型)。

参数
  • name (str) – 工具的名称。

  • description (str) – 工具功能及其使用方法的描述。可选,默认为 None

  • parameters – 参数名称到其定义的映射。如果未提供,则定义一个不带参数的函数。可选,默认为 None

  • strict (bool) – 一个标志,表示模型是否应严格遵循提供的模式。

to_tool_definition()[source]

用于将此包装为 ToolDefinition 的便捷函数。

class mlflow.types.llm.ParamProperty(type: Literal['string', 'number', 'integer', 'object', 'array', 'boolean', 'null'], description: Optional[str] = None, enum: Optional[list[str]] = None, items: Optional[mlflow.types.llm.ParamType] = None)[source]

函数定义中的单个参数。

参数
  • type (str) – 参数的类型。可能的值为“string”、“number”、“integer”、“object”、“array”、“boolean”或“null”,符合 JSON Schema 规范。

  • description (str) – 参数的描述。可选,默认为 None

  • enum (List[str]) – 用于约束参数的可能值。可选,默认为 None

  • items (ParamProperty) – 如果参数类型为 array,则此字段可用于指定其项的类型。可选,默认为 None

class mlflow.types.llm.ParamType(type: Literal['string', 'number', 'integer', 'object', 'array', 'boolean', 'null'])[source]
class mlflow.types.llm.TokenLogProb(token: str, logprob: float, top_logprobs: list[mlflow.types.llm.TopTokenLogProb], bytes: Optional[list[int]] = None)[source]

带有 log probability 信息的聊天内容 token。

参数
  • token – token。

  • logprob – 该 token 的 log probability,如果它在最可能的 20 个 token 中。否则,使用 -9999.0 来表示该 token 非常不可能。

  • bytes – 一个整数列表,表示 token 的 UTF-8 字节表示。在字符由多个 token 表示且必须组合其字节表示以生成正确文本表示的情况下很有用。如果没有 token 的字节表示,则可以为 null。

  • top_logprobs – 在此 token 位置,最可能的 token 及其 log probability 列表。在极少数情况下,返回的 top_logprobs 可能少于请求的数量。

class mlflow.types.llm.TokenUsageStats(prompt_tokens: Optional[int] = None, completion_tokens: Optional[int] = None, total_tokens: Optional[int] = None)[source]

推理中使用的 token 数量的统计信息。

参数
  • prompt_tokens (int) – prompt 中的 token 数量。可选,默认为 None

  • completion_tokens (int) – 生成的 completion 中的 token 数量。可选,默认为 None

  • total_tokens (int) – 使用的总 token 数量。可选,默认为 None

class mlflow.types.llm.ToolCall(function: mlflow.types.llm.FunctionToolCallArguments, id: str = <factory>, type: str = 'function')[source]

模型进行的工具调用。

参数
  • function (FunctionToolCallArguments) – 函数工具调用的参数。

  • id (str) – 工具调用的 ID。默认为随机 UUID。

  • type (str) – 对象类型。默认为“function”。

class mlflow.types.llm.ToolDefinition(function: mlflow.types.llm.FunctionToolDefinition, type: Literal['function'] = 'function')[source]

模型可以调用的工具的定义。

参数
  • function (FunctionToolDefinition) – 函数工具的定义。

  • type (str) – 工具的类型。目前仅支持“function”。

class mlflow.types.llm.ToolParamsSchema(properties: dict[str, mlflow.types.llm.ParamProperty], type: Literal['object'] = 'object', required: Optional[list[str]] = None, additionalProperties: Optional[bool] = None)[source]

工具参数定义。

参数
  • properties (Dict[str, ParamProperty]) – 参数名称到其定义的映射。

  • type (str) – 参数的类型。目前仅支持“object”。

  • required (List[str]) – 必需参数名称列表。可选,默认为 None

  • additionalProperties (bool) – 对象是否允许其他属性。可选,默认为 None

class mlflow.types.llm.TopTokenLogProb(token: str, logprob: float, bytes: Optional[list[int]] = None)[source]

Token 及其 log probability。

参数
  • token – token。

  • logprob – 该 token 的 log probability,如果它在最可能的 20 个 token 中。否则,使用 -9999.0 来表示该 token 非常不可能。

  • bytes – 一个整数列表,表示 token 的 UTF-8 字节表示。在字符由多个 token 表示且必须组合其字节表示以生成正确文本表示的情况下很有用。如果没有 token 的字节表示,则可以为 null。

class mlflow.types.chat.AudioContentPart(*, type: Literal['input_audio'], input_audio: mlflow.types.chat.InputAudio)[source]
class mlflow.types.chat.BaseRequestPayload(*, temperature: float = 0.0, n: int = 1, stop: list[str] | None = None, max_tokens: int | None = None, stream: bool | None = None, stream_options: dict[str, typing.Any] | None = None, model: str | None = None)[source]

chat completions and completion endpoints通用参数。

class mlflow.types.chat.ChatChoice(*, index: int, message: mlflow.types.chat.ChatMessage, finish_reason: str | None = None)[source]
class mlflow.types.chat.ChatChoiceDelta(*, role: str | None = None, content: str | None = None, tool_calls: list[mlflow.types.chat.ToolCallDelta] | None = None)[source]
class mlflow.types.chat.ChatChunkChoice(*, index: int, finish_reason: str | None = None, delta: mlflow.types.chat.ChatChoiceDelta)[source]
class mlflow.types.chat.ChatCompletionChunk(*, id: str | None = None, object: str = 'chat.completion.chunk', created: int, model: str, choices: list[mlflow.types.chat.ChatChunkChoice])[source]

chat completion stream response 的一个分块。

class mlflow.types.chat.ChatCompletionRequest(*, temperature: float = 0.0, n: int = 1, stop: list[str] | None = None, max_tokens: int | None = None, stream: bool | None = None, stream_options: dict[str, typing.Any] | None = None, model: str | None = None, messages: list[mlflow.types.chat.ChatMessage], tools: list[mlflow.types.chat.ChatTool] | None = None)[source]

chat completion API 的一个请求。

必须与 OpenAI 的 Chat Completion API 兼容。 https://platform.openai.com/docs/api-reference/chat

class mlflow.types.chat.ChatCompletionResponse(*, id: str | None = None, object: str = 'chat.completion', created: int, model: str, choices: list[mlflow.types.chat.ChatChoice], usage: mlflow.types.chat.ChatUsage)[source]

chat completion API 的一个响应。

必须与 OpenAI 的 Chat Completion API 兼容。 https://platform.openai.com/docs/api-reference/chat

class mlflow.types.chat.ChatMessage(*, role: str, content: Optional[str | list[typing.Annotated[mlflow.types.chat.TextContentPart | mlflow.types.chat.ImageContentPart | mlflow.types.chat.AudioContentPart, FieldInfo(annotation=NoneType, required=True, discriminator='type')]]] = None, tool_calls: list[mlflow.types.chat.ToolCall] | None = None, refusal: str | None = None, tool_call_id: str | None = None)[source]

聊天请求。`content` 可以是字符串,也可以是内容部分的数组。

内容部分是以下之一:

class mlflow.types.chat.ChatTool(*, type: Literal['function'], function: mlflow.types.chat.FunctionToolDefinition | None = None)[source]

传递给聊天完成 API 的工具定义。

参考: https://platform.openai.com/docs/guides/function-calling

class mlflow.types.chat.ChatUsage(*, prompt_tokens: int | None = None, completion_tokens: int | None = None, total_tokens: int | None = None)[source]
class mlflow.types.chat.Function(*, name: str | None = None, arguments: str | None = None)[source]
class mlflow.types.chat.FunctionParams(*, properties: dict[str, mlflow.types.chat.ParamProperty], type: Literal['object'] = 'object', required: list[str] | None = None, additionalProperties: bool | None = None)[source]
class mlflow.types.chat.FunctionToolDefinition(*, name: str, description: str | None = None, parameters: mlflow.types.chat.FunctionParams | None = None, strict: bool | None = None)[source]
class mlflow.types.chat.ImageContentPart(*, type: Literal['image_url'], image_url: mlflow.types.chat.ImageUrl)[source]
class mlflow.types.chat.ImageUrl(*, url: str, detail: Optional[Literal['auto', 'low', 'high']] = None)[source]

表示一个图片 URL。

url

可以是图片 URL 或 base64 编码的数据。 https://platform.openai.com/docs/guides/vision?lang=curl#uploading-base64-encoded-images

类型

str

detail

模型接收图片时的分辨率级别。例如,当设置为“low”时,模型会看到一个调整到 512x512 像素的图片,这会消耗更少的 token。在 OpenAI 中,此选项是可选的,默认为“auto”。 https://platform.openai.com/docs/guides/vision?lang=curl#low-or-high-fidelity-image-understanding

类型

Optional[Literal[‘auto’, ‘low’, ‘high’]]

class mlflow.types.chat.InputAudio(*, data: str, format: Literal['wav', 'mp3'])[source]
class mlflow.types.chat.ParamProperty(*, type: Optional[Union[Literal['string', 'number', 'integer', 'object', 'array', 'boolean', 'null'], list[typing.Literal['string', 'number', 'integer', 'object', 'array', 'boolean', 'null']]]] = None, description: str | None = None, enum: list[str | int | float | bool] | None = None, items: mlflow.types.chat.ParamType | None = None)[source]

OpenAI 使用 JSON Schema (https://schema.json.js.cn/) 来定义函数参数。请参阅 OpenAI 函数调用参考: https://platform.openai.com/docs/guides/function-calling?&api-mode=responses#defining-functions

JSON Schema 的 enum 支持任何 JSON 类型(str、int、float、bool、null、数组、对象),但出于实际用例和 API 安全性的考虑,我们将其限制为基本标量类型。

class mlflow.types.chat.ParamType(*, type: Optional[Union[Literal['string', 'number', 'integer', 'object', 'array', 'boolean', 'null'], list[typing.Literal['string', 'number', 'integer', 'object', 'array', 'boolean', 'null']]]] = None)[source]
class mlflow.types.chat.TextContentPart(*, type: Literal['text'], text: str)[source]
class mlflow.types.chat.ToolCall(**, id: str, type: str = 'function', function: mlflow.types.chat.Function)[源]
class mlflow.types.chat.ToolCallDelta(**, index: int, id: str | None = None, type: str | None = None, function: mlflow.types.chat.Function)[源]
class mlflow.types.schema.AnyType[源]
to_dict()[源]

对象的可字典表示。

class mlflow.types.schema.Array(dtype: Union[mlflow.types.schema.Array, mlflow.types.schema.DataType, mlflow.types.schema.Map, mlflow.types.schema.Object, mlflow.types.schema.AnyType, str])[源]

用于表示可 JSON 转换的数组的规范。

property dtype: Array' | DataType | Object | 'Map' | 'AnyType

数组数据类型。

classmethod from_json_dict(**kwargs)[源]

从 JSON 加载的字典进行反序列化。字典应包含 typeitems 键。示例:{“type”: “array”, “items”: “string”}

to_dict()[源]

对象的可字典表示。

class mlflow.types.schema.Map(value_type: Union[mlflow.types.schema.Array, mlflow.types.schema.DataType, mlflow.types.schema.Map, mlflow.types.schema.Object, mlflow.types.schema.AnyType, str])[源]

用于表示可 JSON 转换的、键为字符串类型的字典的规范。

classmethod from_json_dict(**kwargs)[源]

从 JSON 加载的字典进行反序列化。字典应包含 typevalues 键。示例:{“type”: “map”, “values”: “string”}

to_dict()[源]

对象的可字典表示。

property value_type
class mlflow.types.schema.Object(properties: list[mlflow.types.schema.Property])[源]

用于表示可 JSON 转换的对象(object)的规范。

classmethod from_json_dict(**kwargs)[源]

从 JSON 加载的字典进行反序列化。字典应包含 typeproperties 键。示例:{“type”: “object”, “properties”: {“property_name”: {“type”: “string”}}}

property properties: list[mlflow.types.schema.Property]

对象属性列表。

to_dict()[源]

对象的可字典表示。

class mlflow.types.schema.Property(name: str, dtype: Union[mlflow.types.schema.Array, mlflow.types.schema.DataType, mlflow.types.schema.Map, mlflow.types.schema.Object, mlflow.types.schema.AnyType, str], required: bool = True)[源]

用于表示可 JSON 转换的对象属性的规范。

property dtype: DataType | 'Array' | 'Object' | 'Map'

属性数据类型。

classmethod from_json_dict(**kwargs)[源]

从 JSON 加载的字典进行反序列化。字典应仅包含一个键 name,其值应为一个包含 type 和可选的 required 键的字典。示例:{“property_name”: {“type”: “string”, “required”: True}}

property name: str

属性名称。

property required: bool

此属性是否必需。

to_dict()[源]

对象的可字典表示。