File tree Expand file tree Collapse file tree 2 files changed +16
-4
lines changed
main/scala/com/microsoft/azure/synapse/ml/services/openai
test/scala/com/microsoft/azure/synapse/ml/services/openai Expand file tree Collapse file tree 2 files changed +16
-4
lines changed Original file line number Diff line number Diff line change @@ -303,7 +303,16 @@ class DelimiterParser(val delimiter: String) extends OutputParser {
303303}
304304
305305class JsonParser (val schema : String , options : Map [String , String ]) extends OutputParser {
306- def parse (responseCol : Column ): Column = F .from_json(responseCol, schema, options)
306+ private val cleanJsonString : Column => Column = col =>
307+ F .regexp_replace(
308+ // Remove optional leading/trailing code fences and optional 'json' prefix
309+ F .trim(col),
310+ """ ^(```|''')?\s*json\s*|(```|''')$""" ,
311+ " "
312+ )
313+
314+ def parse (responseCol : Column ): Column =
315+ F .from_json(cleanJsonString(responseCol), schema, options)
307316
308317 def outputSchema : T .DataType = DataType .fromDDL(schema)
309318}
Original file line number Diff line number Diff line change @@ -37,9 +37,12 @@ class OpenAIPromptSuite extends TransformerFuzzing[OpenAIPrompt] with OpenAIAPIK
3737
3838 test(" RAI Usage" ) {
3939 val result = prompt
40- .setDeploymentName(deploymentNameGpt4)
41- .setPromptTemplate(" Tell me about a graphically disgusting movie in detail" )
40+ .setDeploymentName(deploymentNameGpt4o)
41+ .setPromptTemplate(" Tell me about a graphically disgusting " +
42+ " and violent movie in detail, " +
43+ " be very gory and NSFW in your description." )
4244 .transform(df)
45+ .where(col(prompt.getErrorCol).isNotNull)
4346 .select(prompt.getErrorCol)
4447 .collect().head.getAs[Row ](0 )
4548 assert(Option (result).nonEmpty)
@@ -59,7 +62,7 @@ class OpenAIPromptSuite extends TransformerFuzzing[OpenAIPrompt] with OpenAIAPIK
5962
6063 test(" Basic Usage JSON" ) {
6164 prompt.setPromptTemplate(
62- """ Split a word into prefix and postfix a respond in JSON
65+ """ Split a word into prefix and postfix a respond in JSON.
6366 |Cherry: {{"prefix": "Che", "suffix": "rry"}}
6467 |{text}:
6568 |""" .stripMargin)
You can’t perform that action at this time.
0 commit comments