io.cequence.openaiscala.task.binary.CountOnesAndZerosTask.scala Maven / Gradle / Ivy
The newest version!
package io.cequence.openaiscala.task.binary
import io.cequence.openaiscala.task.domain.BinaryTaskCoreSettings
class CountOnesAndZerosTask(withAssistantSeedPrompt: Boolean = false) extends BinaryStringTask[BinaryTaskCoreSettings] {
override def seedAssistantPrompt = if (withAssistantSeedPrompt) Some(onesPrefix) else None
private val onesCountAlternativePattern = "there are [\\d]* one".r
private val zerosCountAlternativePattern = "and [\\d]* zero".r
override def generatePrompt(
input: String,
settings: BinaryTaskCoreSettings
) =
s"""In this task you must count the number of ones (1s) and zeros (0s) in a given binary string.${generateExamples(settings)}
|
|Now, count the number of ones (1s) and zeros (0s) in the following string:
|
|${addSpaces(settings.withSpaces)(input)}""".stripMargin
override def evalResult(
input: String,
output: String
): Option[Int] = {
val expectedOnes = input.count(_ == '1')
val expectedZeros = input.count(_ == '0')
val actualOnes = extractWoSpacesAux(onesCountPattern, onesPrefix, output).map(_.toInt)
val actualZeros = extractWoSpacesAux(zerosCountPattern, zerosPrefix, output).map(_.toInt)
val actualOnesFinal = actualOnes.orElse(
// fallback to alternative patterns
extractAux(onesCountAlternativePattern, Seq("there are "), output).map(
_.stripSuffix("ones").trim.toInt
)
)
val actualZerosFinal = actualZeros.orElse(
// fallback to alternative patterns
extractAux(zerosCountAlternativePattern, Seq("and "), output).map(
_.stripSuffix("zero").trim.toInt
)
)
(actualOnesFinal, actualZerosFinal).zipped.headOption.map { case (ones, zeros) =>
ones == expectedOnes && zeros == expectedZeros 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')
val zeroes = input.count(_ == '0')
Some(s"$onesPrefix $ones, $zerosPrefix $zeroes")
}
// aux functions
override protected def addExample(
withSpace: Boolean)(
example: String
) = {
val ones = example.count(_ == '1')
val zeroes = example.count(_ == '0')
val addSpacesAux = addSpaces(withSpace) _
s"${addSpacesAux(example)} -> $onesPrefix ${ones}, $zerosPrefix ${zeroes}"
}
}