prompt
Utilities for prompt generation.
create_chat_completion_messages(messages, strict=True)
Create an array of chat completion message structures from a list of column specifications.
This function generates a Spark SQL Column containing an array of structured messages
suitable for chat completion tasks. It handles all possible fields of a chat message,
including role, content, image URLs, name, tool calls, and tool call IDs. Note that image_urls
are NOT included in the content
due to spark serialization requiring a static schema.
Parameters:
Returns:
Name | Type | Description |
---|---|---|
Column |
Column
|
A Spark SQL Column containing an array of structured messages. Each message |
Column
|
is cast to the SparkChatCompletionMessage schema. |
Notes
- The function uses the SparkChatCompletionMessage schema to ensure type consistency.
- Fields not specified in the input will be set to None in the output.
- This function is particularly useful for creating complex, multi-message prompts for chat-based language models in a Spark environment.
Example
>>> from pyspark.sql import functions as f
>>> from databricks.connect import DatabricksSession
>>> spark = DatabricksSession.builder.serverless().getOrCreate()
>>> df = spark.createDataFrame([("Hello", "Be helpful")], ["user_msg", "sys_msg"])
>>> messages = [
... {"role": f.lit("system"), "content": "sys_msg"},
... {"role": f.lit("user"), "content": "user_msg"}
... ]
>>> chat_messages = create_chat_completion_messages(messages)
>>> df.select(chat_messages.alias("messages")).show(truncate=False)
+-------------------------------------------------------------------------------------+
|messages |
+-------------------------------------------------------------------------------------+
|[{system, Be helpful, NULL, NULL, NULL, NULL}, {user, Hello, NULL, NULL, NULL, NULL}]|
+-------------------------------------------------------------------------------------+
<BLANKLINE>
Raises: ValueError: If a required field (e.g., 'role') is missing from any message specification.
Source code in spark_instructor/utils/prompt.py
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 |
|
get_column_or_null(column=None)
zero_shot_prompt(user_message_column, system_message_column=None)
Generate a zero-shot prompt for language models in Spark DataFrames.
This function creates a structured array of messages suitable for zero-shot prompting in language models. It always includes a user message and optionally a system message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_message_column |
Union[Column, str]
|
The column containing user messages. Can be either a Column object or a string column name. |
required |
system_message_column |
Optional[Union[Column, str]]
|
The column containing system messages. Can be either a Column object or a string column name. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Column |
Column
|
A Spark SQL Column containing an array of message structures. Each structure is a map with 'role' and 'content' keys. |
Notes
- If system_message_column is None, only the user message is included.
- If system_message_column is provided, the system message and user message are included.
Example
>>> from databricks.connect import DatabricksSession
>>> spark = DatabricksSession.builder.serverless().getOrCreate()
>>> df = spark.createDataFrame([("Hello", "Be helpful")], ["user_msg", "sys_msg"])
>>> prompt_col = zero_shot_prompt("user_msg", system_message_column="sys_msg")
>>> df.select(prompt_col.alias("prompt")).show(truncate=False)
+---------------------------------------------------------------------------+
|prompt |
+---------------------------------------------------------------------------+
|[{role -> system, content -> Be helpful}, {role -> user, content -> Hello}]|
+---------------------------------------------------------------------------+
<BLANKLINE>