Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: OpenAIChatCompletion class method shouldSkip method to skip row if no user message #2320

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,22 @@ class OpenAIChatCompletion(override val uid: String) extends OpenAIServicesBase(

override val subscriptionKeyHeaderName: String = "api-key"

override def shouldSkip(row: Row): Boolean =
super.shouldSkip(row) || Option(row.getAs[Row](getMessagesCol)).isEmpty
/**
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like something we should likely have in the prompt API as opposed to the core API just because im not sure if its possible to send just system messages and have it reply

* Check if the row should be skipped. A row should be skipped if the messages column is empty or if any of the
* messages are empty or if there not exists a single message with "role" as "user" or "assistant" and
* "content" as non-empty.
*/
override def shouldSkip(row: Row): Boolean ={
val acceptableRoles = Set("user", "assistant")
super.shouldSkip(row) || {
val messages = Option(row.getAs[Seq[Row]](getMessagesCol)).getOrElse(Seq.empty)
messages.isEmpty || !messages
// filter out messages system messages
.filter(m => acceptableRoles.contains(Option(m.getAs[String]("role")).getOrElse("").toLowerCase))
// check if there exists a user message with non-empty content
.exists(m => Option(m.getAs[String]("content")).getOrElse("").nonEmpty)
}
}

override protected def getVectorParamMap: Map[String, String] = super.getVectorParamMap
.updated("messages", getMessagesCol)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class OpenAIChatCompletionSuite extends TransformerFuzzing[OpenAIChatCompletion]
OpenAIMessage("system", "You are very excited"),
OpenAIMessage("user", null) //scalastyle:ignore null
),
Seq(
OpenAIMessage("system", "You are very excited"),
OpenAIMessage("user", "")
),
Seq(OpenAIMessage("system", "You are very excited")),
null //scalastyle:ignore null
).toDF("messages")

Expand Down Expand Up @@ -144,11 +149,23 @@ class OpenAIChatCompletionSuite extends TransformerFuzzing[OpenAIChatCompletion]
}

test("Robustness to bad inputs") {
val results = completion.transform(badDf).collect()
assert(Option(results.head.getAs[Row](completion.getErrorCol)).isDefined)
assert(Option(results.apply(1).getAs[Row](completion.getErrorCol)).isDefined)
assert(Option(results.apply(2).getAs[Row](completion.getErrorCol)).isEmpty)
assert(Option(results.apply(2).getAs[Row]("out")).isEmpty)
val completion: OpenAIChatCompletion = new OpenAIChatCompletion()
// by having an invalid deployment name, we should get error for all rows
.setDeploymentName("invalid_deployment")
.setCustomServiceName(openAIServiceName)
.setApiVersion("2023-05-15")
.setMaxTokens(5000)
.setOutputCol("out")
.setMessagesCol("messages")
.setTemperature(0)
.setSubscriptionKey(openAIAPIKey)
val rows = completion.transform(goodDf).collect()
assert(rows.length == goodDf.count())
rows.foreach { row =>
assert(Option(row.getAs[Row]("out")).isEmpty)
assert(Option(row.getAs[Row](completion.getErrorCol)).isDefined)
assert(Option(row.getAs[Row](completion.getErrorCol)).nonEmpty)
}
}

ignore("Custom EndPoint") {
Expand All @@ -174,6 +191,51 @@ class OpenAIChatCompletionSuite extends TransformerFuzzing[OpenAIChatCompletion]
testCompletion(customEndpointCompletion, goodDf)
}

test("shouldSkip method for Good and Bad Data") {
shouldSkipRowHelper(goodDf.collect(), expectedToBeSkipped = false)
shouldSkipRowHelper(slowDf.collect(), expectedToBeSkipped = false)
shouldSkipRowHelper(badDf.collect(), expectedToBeSkipped = true)

val dfWithSomeEmptyAndNonEmptyUserMessage = Seq(
Seq(
OpenAIMessage("system", "You are an AI chatbot with red as your favorite color"),
OpenAIMessage("user", ""),
OpenAIMessage("user", "Whats your favorite color")
)
).toDF("messages")
shouldSkipRowHelper(dfWithSomeEmptyAndNonEmptyUserMessage.collect(), expectedToBeSkipped = false)

val dfWithEmptySystemMessage = Seq(
Seq(
OpenAIMessage("system", ""),
OpenAIMessage("user", "Whats your favorite color")
),
Seq(
OpenAIMessage("system", null), //scalastyle:ignore null
OpenAIMessage("user", "Whats your favorite color")
),
Seq(OpenAIMessage("user", "Whats your favorite color"))
).toDF("messages")
shouldSkipRowHelper(dfWithEmptySystemMessage.collect(), expectedToBeSkipped = false)

val dfWithAssistantValidMessages = Seq(
Seq(
OpenAIMessage("system", "You are an AI chatbot with red as your favorite color"),
OpenAIMessage("assistant", "Whats your favorite color")
)
).toDF("messages")
shouldSkipRowHelper(dfWithAssistantValidMessages.collect(), expectedToBeSkipped = false)
}

def shouldSkipRowHelper(rows: Seq[Row], expectedToBeSkipped: Boolean): Unit = {
val completion = new OpenAIChatCompletion()
.setMessagesCol("messages")
for (r <- rows) {
assert(completion.shouldSkip(r) == expectedToBeSkipped)
}
}


def testCompletion(completion: OpenAIChatCompletion, df: DataFrame, requiredLength: Int = 10): Unit = {
val fromRow = ChatCompletionResponse.makeFromRowConverter
completion.transform(df).collect().foreach(r =>
Expand Down