Skip to content

Commit 5bbbbc7

Browse files
authored
chore: fix failing openai tests (#2385)
1 parent 9af855e commit 5bbbbc7

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

cognitive/src/main/scala/com/microsoft/azure/synapse/ml/services/openai/OpenAIPrompt.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,16 @@ class DelimiterParser(val delimiter: String) extends OutputParser {
303303
}
304304

305305
class 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
}

cognitive/src/test/scala/com/microsoft/azure/synapse/ml/services/openai/OpenAIPromptSuite.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff 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)

0 commit comments

Comments
 (0)