io.cequence.openaiscala.task.binary.CountOnesTask.scala Maven / Gradle / Ivy
The newest version!
package io.cequence.openaiscala.task.binary
import io.cequence.openaiscala.task.domain.BinaryTaskCoreSettings
class CountOnesTask(withAssistantSeedPrompt: Boolean = false) extends BinaryStringTask[BinaryTaskCoreSettings] {
override def seedAssistantPrompt = if (withAssistantSeedPrompt) Some(onesPrefix) else None
private val onesCountAlternativePattern = "there are [\\d]* ones".r
private val noOnesCountAlternativePattern = "there are no ones".r
override def generatePrompt(
input: String,
settings: BinaryTaskCoreSettings
) =
s"""In this task you must count the number of ones (1s) in a given binary string.${generateExamples(settings)}
|
|Now, count the number of ones (1s) in the following string:
|
|${addSpaces(settings.withSpaces)(input)}""".stripMargin
override def evalResult(
input: String,
output: String
): Option[Int] = {
val expectedOnes = input.count(_ == '1')
val actualOnes = extractWoSpacesAux(onesCountPattern, onesPrefix, output).map(_.toInt)
val actualOnesFinal = actualOnes.orElse(
// fallback to alternative patterns
extractAux(onesCountAlternativePattern, Seq("there are "), output).map(
_.stripSuffix("ones").trim.toInt
).orElse(
extractAux(noOnesCountAlternativePattern, Nil, output).map(_ => 0)
)
)
actualOnesFinal.map { ones =>
ones == expectedOnes match {
case true => 1 // give one point if match
case false => 0 // otherwise give zero points
}
}
}
override def expectedOutput(
input: String,
settings: BinaryTaskCoreSettings
): Option[String] = {
val ones = input.count(_ == '1')
Some(s"$onesPrefix $ones")
}
// aux functions
override protected def addExample(
withSpace: Boolean)(
example: String
) = {
val ones = example.count(_ == '1')
s"${addSpaces(withSpace)(example)} -> $onesPrefix ${ones}"
}
}