base
A module for defining base classes for each completion type.
SparkChatCompletionMessages = RootModel[List[SparkChatCompletionMessage]]
module-attribute
A root model representing a list of SparkChatCompletionMessage objects.
This model is used to represent an entire conversation or a series of messages in a chat completion context. It provides a convenient way to handle multiple messages as a single entity while maintaining Pydantic's validation and serialization capabilities.
SparkChatCompletionMessage
Bases: BaseModel
A Spark-serializable chat completion message that can be used for any role in a conversation.
This class provides a flexible structure for representing messages in a chat completion context, supporting various roles (user, assistant, system, tool) and additional attributes like image URLs and tool calls.
Attributes:
Name | Type | Description |
---|---|---|
role |
Literal['user', 'assistant', 'system', 'tool']
|
The role of the message sender. |
content |
Optional[str]
|
The text content of the message. |
image_urls |
Optional[List[ImageURLPD]]
|
List of image URLs associated with the message. |
name |
Optional[str]
|
The name of the entity associated with the message. |
tool_calls |
Optional[List[ChatCompletionMessageToolCallParamPD]]
|
Tool calls made in the message (for 'assistant' role). |
tool_call_id |
Optional[str]
|
The ID of the tool call (for 'tool' role). |
cache_control |
Optional[bool]
|
Whether to use Anthropic's prompt caching feature (beta). |
The class provides methods to format the message for different roles and serialize it to OpenAI-compatible types.
Source code in spark_instructor/types/base.py
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 |
|
__call__(*args, string_only=False, **kwargs)
Serialize the message to an OpenAI-compatible type.
This method allows the object to be called directly, returning the appropriate message type based on the role.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string_only |
bool
|
If True, return only the text content. The resulting user message will only have string content. |
False
|
*args |
Variable length argument list. |
()
|
|
**kwargs |
Arbitrary keyword arguments. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
ChatCompletionMessageParam |
ChatCompletionMessageParam
|
The serialized message in OpenAI-compatible format. |
Source code in spark_instructor/types/base.py
as_assistant()
Format the message as an assistant message.
This method prepares the message for use as an assistant response in a chat completion, including any tool calls if present.
Returns:
Name | Type | Description |
---|---|---|
ChatCompletionAssistantMessageParam |
ChatCompletionAssistantMessageParam
|
The formatted assistant message. |
Source code in spark_instructor/types/base.py
as_system()
Format the message as a system message.
This method prepares the message for use as a system instruction in a chat completion.
Returns:
Name | Type | Description |
---|---|---|
ChatCompletionSystemMessageParam |
ChatCompletionSystemMessageParam
|
The formatted system message. |
Raises:
Type | Description |
---|---|
AssertionError
|
If the content is empty. |
Source code in spark_instructor/types/base.py
as_tool()
Format the message as a tool message.
This method prepares the message for use as a tool response in a chat completion.
Returns:
Name | Type | Description |
---|---|---|
ChatCompletionToolMessageParam |
ChatCompletionToolMessageParam
|
The formatted tool message. |
Raises:
Type | Description |
---|---|
AssertionError
|
If either content or tool_call_id is empty. |
Source code in spark_instructor/types/base.py
as_user(string_only=False)
Format the message as a user message.
This method prepares the message for use as a user input in a chat completion.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string_only |
bool
|
If True, return only the text content for messages with images. |
False
|
Returns:
Name | Type | Description |
---|---|---|
ChatCompletionUserMessageParam |
ChatCompletionUserMessageParam
|
The formatted user message. |
Source code in spark_instructor/types/base.py
content_formatted()
Format the message content, including any images, into a list of content parts.
This method prepares the message content for use in API calls, handling both text and image content.
Returns:
Type | Description |
---|---|
List[Union[ChatCompletionContentPartTextParam, ChatCompletionContentPartImageParam]]
|
List[Union[ChatCompletionContentPartTextParam, ChatCompletionContentPartImageParam]]: A list of formatted content parts, including text and images. |
Source code in spark_instructor/types/base.py
format_image_url_pd(image_url_pd)
Format the ImageURLPD object into an ImageURL object.