message_router
Module for MessageRouter
.
MessageRouter
dataclass
Bases: Generic[ResponseModel]
A wrapper for serializing instructor
calls and managing model interactions.
This class provides methods to create Pydantic objects and completions from chat messages, and to generate Spark UDFs for these operations.
Attributes:
Name | Type | Description |
---|---|---|
model |
str
|
The name of the model to use. |
response_model_type |
Type[ResponseModel]
|
The Pydantic model type for the response. |
model_class |
Optional[ModelClass]
|
The class of the model (e.g., |
mode |
Optional[Mode]
|
The mode for the instructor client. |
base_url |
Optional[str]
|
The base URL for API calls. |
api_key |
Optional[str]
|
The API key for authentication. |
Notes
WARNING: MessageRouter
is now deprecated. Use instruct
instead.
Source code in spark_instructor/udf/message_router.py
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 188 189 190 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 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 295 296 297 298 299 |
|
completion_type: Union[Type[AnthropicCompletion], Type[DatabricksCompletion], Type[OpenAICompletion]]
property
Get the appropriate completion type based on the model_class
attribute.
Returns:
Type | Description |
---|---|
Union[Type[AnthropicCompletion], Type[DatabricksCompletion], Type[OpenAICompletion]]
|
Union[Type[AnthropicCompletion], Type[DatabricksCompletion], Type[OpenAICompletion]]: The completion type corresponding to the model class. |
model_serializer: ModelSerializer
property
Get the model serializer for the response model type and completion type.
Returns:
Name | Type | Description |
---|---|---|
ModelSerializer |
ModelSerializer
|
An instance of ModelSerializer. |
spark_schema: StructType
property
Get the Spark schema for the model.
Returns:
Name | Type | Description |
---|---|---|
StructType |
StructType
|
The Spark schema corresponding to the model. |
__post_init__()
Initialize the model_class
if not provided.
The model_class
will be inferred based on the model
attribute.
Source code in spark_instructor/udf/message_router.py
create_object_and_completion_from_messages(messages, **kwargs)
Create a Pydantic object response and completion using the instructor
client.
The completion will be of the type corresponding to the model_class
attribute.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
messages |
list[ChatCompletionMessageParam]
|
The list of chat messages. |
required |
**kwargs |
Any
|
Additional keyword arguments for the chat completion. |
{}
|
Returns:
Type | Description |
---|---|
Tuple[ResponseModel, Union[AnthropicCompletion, DatabricksCompletion, OpenAICompletion]]
|
Tuple[ResponseModel, Union[AnthropicCompletion, DatabricksCompletion, OpenAICompletion]]: A tuple containing the Pydantic object and the completion. |
Source code in spark_instructor/udf/message_router.py
create_object_and_completion_from_messages_udf(**kwargs)
Create a Spark UDF that returns a StructType
.
The response will be based on the response_model_type
and model_class
attributes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs |
Any
|
Additional keyword arguments for the chat completion. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
Callable |
Callable
|
A Spark UDF that takes messages and returns a dictionary with serialized object and completion. |
Source code in spark_instructor/udf/message_router.py
create_object_from_messages(messages, **kwargs)
Create a Pydantic object response from chat messages.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
messages |
list[ChatCompletionMessageParam]
|
The list of chat messages. |
required |
**kwargs |
Any
|
Additional keyword arguments for the chat completion. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
ResponseModel |
ResponseModel
|
A Pydantic object representing the response. |
Source code in spark_instructor/udf/message_router.py
create_object_from_messages_udf(**kwargs)
Create a Spark UDF that returns a StructType
response based on the response_model_type
attribute.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs |
Any
|
Additional keyword arguments for the chat completion. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
Callable |
Callable
|
A Spark UDF that takes messages and returns a serialized object. |
Source code in spark_instructor/udf/message_router.py
get_instructor()
Get an instance of the instructor client.
Returns:
Type | Description |
---|---|
Instructor
|
instructor.Instructor: An initialized instructor client. |
Source code in spark_instructor/udf/message_router.py
ModelSerializer
dataclass
A class for serializing Pydantic models to Spark schemas.
This class provides functionality to convert Pydantic models to Spark StructType schemas, with fields named according to the snake case version of the model class names.
Attributes:
Name | Type | Description |
---|---|---|
response_model_type |
Type[BaseModel]
|
The Pydantic model type for the main data. |
completion_model_type |
Type[BaseModel]
|
The Pydantic model type for the completion data. |
Source code in spark_instructor/udf/message_router.py
completion_model_name: str
property
Pydantic model field name in snake-case.
completion_model_spark_schema: StructType
property
Response model spark schema.
response_model_name: str | None
property
Pydantic model field name in snake-case.
response_model_spark_schema: StructType | None
property
Response model spark schema.
spark_schema: StructType
property
Generate a Spark StructType schema for the Pydantic models.
This property creates a Spark schema that includes two fields: one for the main Pydantic model and one for the completion model. The field names are derived from the snake case versions of the model class names.
Returns:
Name | Type | Description |
---|---|---|
StructType |
StructType
|
A Spark StructType containing two StructFields, one for each model. Each field is named after the snake case version of its model class name and contains the corresponding Spark schema. |
to_snake_case(name)
staticmethod
Convert a string from camel case to snake case.
This method takes a camel case string and converts it to snake case. For example, 'CamelCase' becomes 'camel_case'.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The camel case string to convert. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The snake case version of the input string. |