|
15 | 15 | ai_filter, |
16 | 16 | ai_agg, |
17 | 17 | ai_classify, |
| 18 | + ai_extract, |
18 | 19 | ai_similarity, |
19 | 20 | ai_sentiment, |
20 | 21 | ai_embed, |
@@ -1095,3 +1096,162 @@ def parse_document( |
1095 | 1096 | "DataFrame.ai.parse_document", |
1096 | 1097 | ) |
1097 | 1098 | return df |
| 1099 | + |
| 1100 | + @experimental(version="1.37.0") |
| 1101 | + def extract( |
| 1102 | + self, |
| 1103 | + input_column: ColumnOrName, |
| 1104 | + *, |
| 1105 | + response_format: Optional[Union[Dict[str, str], List]] = None, |
| 1106 | + output_column: Optional[str] = None, |
| 1107 | + _emit_ast: bool = True, |
| 1108 | + ) -> "snowflake.snowpark.DataFrame": |
| 1109 | + """Extract structured information from text or files using a response schema. |
| 1110 | +
|
| 1111 | + Args: |
| 1112 | + input_column: The column (Column object or column name as string) containing the text |
| 1113 | + or FILE data to extract information from. Use ``to_file`` for staged file paths. |
| 1114 | + response_format: The schema describing information to extract. Supports: |
| 1115 | +
|
| 1116 | + - Simple object schema (dict) mapping feature names to extraction prompts: |
| 1117 | + ``{'name': 'What is the last name of the employee?', 'address': 'What is the address of the employee?'}`` |
| 1118 | + - Array of strings containing the information to be extracted: |
| 1119 | + ``['What is the last name of the employee?', 'What is the address of the employee?']`` |
| 1120 | + - Array of arrays containing two strings (feature name and extraction prompt): |
| 1121 | + ``[['name', 'What is the last name of the employee?'], ['address', 'What is the address of the employee?']]`` |
| 1122 | + - Array of strings with colon-separated feature names and extraction prompts: |
| 1123 | + ``['name: What is the last name of the employee?', 'address: What is the address of the employee?']`` |
| 1124 | +
|
| 1125 | + output_column: The name of the output column to be appended. |
| 1126 | + If not provided, a column named ``AI_EXTRACT_OUTPUT`` is appended. |
| 1127 | +
|
| 1128 | + Returns: |
| 1129 | + A new DataFrame with an appended JSON object containing the extracted fields |
| 1130 | + under ``response``. |
| 1131 | +
|
| 1132 | + Examples:: |
| 1133 | +
|
| 1134 | + >>> # Extract from text string |
| 1135 | + >>> from snowflake.snowpark.functions import col |
| 1136 | + >>> df = session.create_dataframe([ |
| 1137 | + ... ["John Smith lives in San Francisco and works for Snowflake"], |
| 1138 | + ... ], schema=["text"]) |
| 1139 | + >>> result_df = df.ai.extract( |
| 1140 | + ... input_column="text", |
| 1141 | + ... response_format={'name': 'What is the first name of the employee?', 'city': 'What is the address of the employee?'}, |
| 1142 | + ... output_column="extracted", |
| 1143 | + ... ) |
| 1144 | + >>> result_df.select("EXTRACTED").show() |
| 1145 | + -------------------------------- |
| 1146 | + |"EXTRACTED" | |
| 1147 | + -------------------------------- |
| 1148 | + |{ | |
| 1149 | + | "response": { | |
| 1150 | + | "city": "San Francisco", | |
| 1151 | + | "name": "John" | |
| 1152 | + | } | |
| 1153 | + |} | |
| 1154 | + -------------------------------- |
| 1155 | + <BLANKLINE> |
| 1156 | +
|
| 1157 | + >>> # Extract using array format |
| 1158 | + >>> df = session.create_dataframe( |
| 1159 | + ... [ |
| 1160 | + ... ["Alice Johnson works in Seattle"], |
| 1161 | + ... ["Bob Williams works in Portland"], |
| 1162 | + ... ], |
| 1163 | + ... schema=["text"] |
| 1164 | + ... ) |
| 1165 | + >>> result_df = df.ai.extract( |
| 1166 | + ... input_column=col("text"), |
| 1167 | + ... response_format=[["name", "What is the first name?"], ["city", "What city do they work in?"]], |
| 1168 | + ... output_column="info", |
| 1169 | + ... ) |
| 1170 | + >>> result_df.show() |
| 1171 | + ------------------------------------------------------------ |
| 1172 | + |"TEXT" |"INFO" | |
| 1173 | + ------------------------------------------------------------ |
| 1174 | + |Alice Johnson works in Seattle |{ | |
| 1175 | + | | "response": { | |
| 1176 | + | | "city": "Seattle", | |
| 1177 | + | | "name": "Alice" | |
| 1178 | + | | } | |
| 1179 | + | |} | |
| 1180 | + |Bob Williams works in Portland |{ | |
| 1181 | + | | "response": { | |
| 1182 | + | | "city": "Portland", | |
| 1183 | + | | "name": "Bob" | |
| 1184 | + | | } | |
| 1185 | + | |} | |
| 1186 | + ------------------------------------------------------------ |
| 1187 | + <BLANKLINE> |
| 1188 | +
|
| 1189 | + >>> # Extract lists using List: prefix |
| 1190 | + >>> df = session.create_dataframe( |
| 1191 | + ... [["Python, Java, and JavaScript are popular programming languages"]], |
| 1192 | + ... schema=["text"] |
| 1193 | + ... ) |
| 1194 | + >>> result_df = df.ai.extract( |
| 1195 | + ... input_column="text", |
| 1196 | + ... response_format=[["languages", "List: What programming languages are mentioned?"]], |
| 1197 | + ... output_column="extracted", |
| 1198 | + ... ) |
| 1199 | + >>> result_df.select("EXTRACTED").show() |
| 1200 | + ---------------------- |
| 1201 | + |"EXTRACTED" | |
| 1202 | + ---------------------- |
| 1203 | + |{ | |
| 1204 | + | "response": { | |
| 1205 | + | "languages": [ | |
| 1206 | + | "Python", | |
| 1207 | + | "Java", | |
| 1208 | + | "JavaScript" | |
| 1209 | + | ] | |
| 1210 | + | } | |
| 1211 | + |} | |
| 1212 | + ---------------------- |
| 1213 | + <BLANKLINE> |
| 1214 | +
|
| 1215 | + >>> # Extract from file |
| 1216 | + >>> from snowflake.snowpark.functions import to_file |
| 1217 | + >>> _ = session.sql("CREATE OR REPLACE TEMP STAGE mystage ENCRYPTION = (TYPE = 'SNOWFLAKE_SSE')").collect() |
| 1218 | + >>> _ = session.file.put("tests/resources/invoice.pdf", "@mystage", auto_compress=False) |
| 1219 | + >>> df = session.create_dataframe([["@mystage/invoice.pdf"]], schema=["file_path"]) |
| 1220 | + >>> result_df = df.ai.extract( |
| 1221 | + ... input_column=to_file(col("file_path")), |
| 1222 | + ... response_format=[["date", "What is the invoice date?"], ["amount", "What is the amount?"]], |
| 1223 | + ... output_column="info", |
| 1224 | + ... ) |
| 1225 | + >>> result_df.select("INFO").show() |
| 1226 | + -------------------------------- |
| 1227 | + |"INFO" | |
| 1228 | + -------------------------------- |
| 1229 | + |{ | |
| 1230 | + | "response": { | |
| 1231 | + | "amount": "USD $950.00", | |
| 1232 | + | "date": "Nov 26, 2016" | |
| 1233 | + | } | |
| 1234 | + |} | |
| 1235 | + -------------------------------- |
| 1236 | + <BLANKLINE> |
| 1237 | +
|
| 1238 | + """ |
| 1239 | + |
| 1240 | + input_col = _to_col_if_str(input_column, "DataFrame.ai.extract") |
| 1241 | + |
| 1242 | + result_col = ai_extract( |
| 1243 | + input=input_col, |
| 1244 | + response_format=response_format, |
| 1245 | + _emit_ast=False, |
| 1246 | + ) |
| 1247 | + |
| 1248 | + output_column_name = output_column or "AI_EXTRACT_OUTPUT" |
| 1249 | + df = self._dataframe.with_column( |
| 1250 | + output_column_name, result_col, _emit_ast=False |
| 1251 | + ) |
| 1252 | + |
| 1253 | + add_api_call( |
| 1254 | + df, |
| 1255 | + "DataFrame.ai.extract", |
| 1256 | + ) |
| 1257 | + return df |
0 commit comments