io.cequence.openaiscala.task.binary.CollectOnesTask.scala Maven / Gradle / Ivy
The newest version!
package io.cequence.openaiscala.task.binary
import io.cequence.openaiscala.task.domain.BinaryTaskCoreSettings
class CollectOnesTask(withAssistantSeedPrompt: Boolean = false) extends BinaryStringTask[BinaryTaskCoreSettings] {
private val onesCollectPatternAlternative = s"""(are|will be|would be):[\\n\\s]*[01\\s]*""".r
private val noOnesCollectPatternAlternative = s"there are no ones".r
override def seedAssistantPrompt = if (withAssistantSeedPrompt) Some(onesPrefix) else None
override def generatePrompt(
input: String,
settings: BinaryTaskCoreSettings
) =
s"""In this task you must collect ones (1s) in a given binary string.${generateExamples(settings)}
|
|Now, collect ones (1s) in the following string:
|
|${addSpaces(settings.withSpaces)(input)}""".stripMargin
override def evalResult(
input: String,
output: String
): Option[Int] = {
val expectedOnes = input.filter(_ == '1')
val actualOnes = extractWoSpacesAux(onesCollectPattern, onesPrefix, output).orElse(
extractWoSpacesAux(onesCollectPatternAlternative, Seq("are:", "will be:", "would be:"), output)
).orElse(
extractAux(noOnesCollectPatternAlternative, Nil, output).map(_ => "")
)
actualOnes.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.filter(_ == '1')
Some(s"$onesPrefix ${addSpaces(settings.withSpaces)(ones)}")
}
// aux functions
override protected def addExample(
withSpace: Boolean)(
example: String
) = {
val ones = example.count(_ == '1')
val onesString = Seq.fill(ones)("1").mkString("")
val addSpacesAux = addSpaces(withSpace) _
s"${addSpacesAux(example)} -> $onesPrefix ${addSpacesAux(onesString)}"
}
}